feat: recognition job context, diary linkage, worker improvements

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
dbastrikin
2026-03-19 22:22:44 +02:00
parent cf69a4a3d9
commit 9357c194eb
5 changed files with 130 additions and 5 deletions

View File

@@ -17,6 +17,7 @@ type JobRepository interface {
QueuePosition(ctx context.Context, userPlan string, createdAt time.Time) (int, error)
NotifyJobUpdate(ctx context.Context, jobID string) error
ListTodayUnlinked(ctx context.Context, userID string) ([]*JobSummary, error)
ListAll(ctx context.Context, userID string) ([]*JobSummary, error)
}
// PostgresJobRepository implements JobRepository using a pgxpool.
@@ -127,6 +128,46 @@ func (repository *PostgresJobRepository) NotifyJobUpdate(queryContext context.Co
return notifyError
}
// ListAll returns all recognition jobs for the given user, newest first.
func (repository *PostgresJobRepository) ListAll(queryContext context.Context, userID string) ([]*JobSummary, error) {
rows, queryError := repository.pool.Query(queryContext,
`SELECT id, status, target_date::text, target_meal_type,
result, error, created_at
FROM dish_recognition_jobs
WHERE user_id = $1
ORDER BY created_at DESC`,
userID,
)
if queryError != nil {
return nil, queryError
}
defer rows.Close()
var summaries []*JobSummary
for rows.Next() {
var summary JobSummary
var resultJSON []byte
scanError := rows.Scan(
&summary.ID, &summary.Status, &summary.TargetDate, &summary.TargetMealType,
&resultJSON, &summary.Error, &summary.CreatedAt,
)
if scanError != nil {
return nil, scanError
}
if resultJSON != nil {
var dishResult ai.DishResult
if unmarshalError := json.Unmarshal(resultJSON, &dishResult); unmarshalError == nil {
summary.Result = &dishResult
}
}
summaries = append(summaries, &summary)
}
if rowsError := rows.Err(); rowsError != nil {
return nil, rowsError
}
return summaries, nil
}
// ListTodayUnlinked returns today's jobs for the given user that have not yet been
// linked to any meal_diary entry.
func (repository *PostgresJobRepository) ListTodayUnlinked(queryContext context.Context, userID string) ([]*JobSummary, error) {