package models import ( "context" "database/sql" "encoding/json" "fmt" "time" ) type GenerationHistory struct { ID string `json:"id"` SummaryID *string `json:"summary_id"` Outcome json.RawMessage `json:"outcome"` CreatedAt time.Time `json:"created_at"` } // SaveGeneration deliberately accepts no article data and commits both rows together. func (r *Repository) SaveGeneration(ctx context.Context, userID, id string, summary *Summary, outcome json.RawMessage, createdAt time.Time) error { return r.saveGeneration(ctx, userID, id, summary, outcome, createdAt, nil, 0, false) } func (r *Repository) SaveGenerationWithQASources(ctx context.Context, userID, id string, summary *Summary, outcome json.RawMessage, createdAt time.Time, sources []SummarySourceArticle, days int) error { return r.saveGeneration(ctx, userID, id, summary, outcome, createdAt, sources, days, true) } func (r *Repository) saveGeneration(ctx context.Context, userID, id string, summary *Summary, outcome json.RawMessage, createdAt time.Time, sources []SummarySourceArticle, days int, withQA bool) error { tx, err := r.db.BeginTx(ctx, nil) if err != nil { return err } defer tx.Rollback() var summaryID *string if summary != nil { if _, err = tx.ExecContext(ctx, `INSERT INTO summaries (id,user_id,content,ai_provider_id,generated_at,summary_model_id,provider_name,model_name) VALUES ($1,$2,$3,$4,$5,$6,$7,$8)`, summary.ID, userID, summary.Content, summary.AIProviderID, summary.GeneratedAt, summary.SummaryModelID, summary.ProviderName, summary.ModelName); err != nil { return err } if withQA { if err = r.saveQASources(ctx, tx, userID, summary, sources, days); err != nil { return err } } summaryID = &summary.ID } if _, err = tx.ExecContext(ctx, `INSERT INTO summary_generations (id,user_id,summary_id,outcome,created_at) VALUES ($1,$2,$3,$4,$5)`, id, userID, summaryID, string(outcome), createdAt); err != nil { return err } // HTTP jobs complete atomically with their summary/outcome. Scheduled runs // retain the existing persistence path and do not require a job row. if GenerationJobID(ctx) != "" { var terminal struct { Status string `json:"status"` FailureCode string `json:"failure_code"` } if err = json.Unmarshal(outcome, &terminal); err != nil { return err } result, updateErr := tx.ExecContext(ctx, `UPDATE summary_generation_jobs SET status=$3,failure_code=$4,finished_at=NOW() WHERE id=$1 AND user_id=$2 AND status='running'`, id, userID, terminal.Status, terminal.FailureCode) if updateErr != nil { return updateErr } n, updateErr := result.RowsAffected() if updateErr != nil { return updateErr } if n != 1 { return fmt.Errorf("generation job is no longer running") } } return tx.Commit() } func (r *Repository) ListGenerations(ctx context.Context, userID string, limit int) ([]GenerationHistory, error) { rows, err := r.db.QueryContext(ctx, `SELECT id,summary_id,outcome,created_at FROM summary_generations WHERE user_id=$1 ORDER BY created_at DESC,id DESC LIMIT $2`, userID, limit) if err != nil { return nil, err } defer rows.Close() result := []GenerationHistory{} for rows.Next() { var g GenerationHistory if err = rows.Scan(&g.ID, &g.SummaryID, &g.Outcome, &g.CreatedAt); err != nil { return nil, err } result = append(result, g) } return result, rows.Err() } func (r *Repository) GetOwnedSummary(id, userID string) (*Summary, error) { s := &Summary{} err := r.db.QueryRow(`SELECT id,user_id,content,ai_provider_id,generated_at,summary_model_id,provider_name,model_name FROM summaries WHERE id=$1 AND user_id=$2`, id, userID).Scan(&s.ID, &s.UserID, &s.Content, &s.AIProviderID, &s.GeneratedAt, &s.SummaryModelID, &s.ProviderName, &s.ModelName) if err == sql.ErrNoRows { return nil, nil } return s, err }