package store import ( "context" "time" ) // AccountError is one concrete error recorded during an account's run — // folder-level, message-level, or account-level (connect/login). type AccountError struct { ID int64 `json:"id"` AccountID int64 `json:"account_id"` RunID int64 `json:"run_id"` Kind string `json:"kind"` // folder | message | account Folder string `json:"folder"` MessageRef string `json:"message_ref"` Error string `json:"error"` CreatedAt time.Time `json:"created_at"` } // AddAccountError appends one error row for an account's current run. func (s *Store) AddAccountError(ctx context.Context, accountID, runID int64, kind, folder, ref, msg string) error { _, err := s.Pool.Exec(ctx, `INSERT INTO account_errors (account_id, run_id, kind, folder, message_ref, error) VALUES ($1,$2,$3,$4,$5,$6)`, accountID, runID, kind, folder, ref, msg) return err } // ClearAccountErrors removes an account's errors at the start of a run, so the // list reflects only the current run (mirrors ResetAccountCounters). func (s *Store) ClearAccountErrors(ctx context.Context, accountID int64) error { _, err := s.Pool.Exec(ctx, `DELETE FROM account_errors WHERE account_id=$1`, accountID) return err } // ListAccountErrors returns an account's errors in insertion order, for the // per-account error modal. func (s *Store) ListAccountErrors(ctx context.Context, accountID int64) ([]AccountError, error) { rows, err := s.Pool.Query(ctx, `SELECT id, account_id, run_id, kind, folder, message_ref, error, created_at FROM account_errors WHERE account_id=$1 ORDER BY id`, accountID) if err != nil { return nil, err } defer rows.Close() out := []AccountError{} for rows.Next() { var e AccountError var runID *int64 if err := rows.Scan(&e.ID, &e.AccountID, &runID, &e.Kind, &e.Folder, &e.MessageRef, &e.Error, &e.CreatedAt); err != nil { return nil, err } if runID != nil { e.RunID = *runID } out = append(out, e) } return out, rows.Err() }