package repositories import ( "context" "encoding/json" "github.com/jackc/pgx/v5/pgxpool" "time" ) type Attachment struct { ID string `json:"id"` ChatID string `json:"chat_id"` OwnerID string `json:"owner_id"` Filename string `json:"filename"` ContentType string `json:"content_type"` SizeBytes int64 `json:"size_bytes"` SHA256 string `json:"sha256"` Status string `json:"status"` Variants []any `json:"variants"` ErrorCode *string `json:"error_code"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` ObjectKey string `json:"-"` PipelineVersion int `json:"-"` } type AttachmentStore struct{ db *pgxpool.Pool } func NewAttachmentStore(db *pgxpool.Pool) *AttachmentStore { return &AttachmentStore{db: db} } func (s *AttachmentStore) Create(ctx context.Context, a Attachment) error { ctx, cancel := boundedQuery(ctx) defer cancel() _, err := s.db.Exec(ctx, `INSERT INTO attachments(id,chat_id,owner_id,filename,object_key,content_type,size_bytes,sha256,created_at,updated_at) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)`, a.ID, a.ChatID, a.OwnerID, a.Filename, a.ObjectKey, a.ContentType, a.SizeBytes, a.SHA256, a.CreatedAt, a.UpdatedAt) return err } func (s *AttachmentStore) GetForMember(ctx context.Context, id, userID string) (Attachment, error) { ctx, cancel := boundedQuery(ctx) defer cancel() a := Attachment{Variants: []any{}} var variants []byte err := s.db.QueryRow(ctx, `SELECT a.id,a.chat_id,a.owner_id,a.filename,a.object_key,a.content_type,a.size_bytes,a.sha256,a.created_at,a.updated_at,a.status,a.variants,a.error_code,a.published_pipeline_version FROM attachments a JOIN chat_members cm ON cm.chat_id=a.chat_id WHERE a.id=$1 AND cm.user_id=$2`, id, userID).Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ObjectKey, &a.ContentType, &a.SizeBytes, &a.SHA256, &a.CreatedAt, &a.UpdatedAt, &a.Status, &variants, &a.ErrorCode, &a.PipelineVersion) if err == nil { err = json.Unmarshal(variants, &a.Variants) } a.CreatedAt = a.CreatedAt.UTC() a.UpdatedAt = a.UpdatedAt.UTC() return a, err }