Files
MealQuest/internal/db/notifications.sql.go
T
dev_agentandClaude Opus 5.5 fd835829e2
CI / ci (pull_request) Successful in 6m57s
CI / ci (push) Successful in 5m49s
Add notification preferences API and screen (1n)
GET/PUT /api/v1/settings/notifications read and replace the event x
{push, chat} matrix for the five events and the reminder time. The PUT
validates every event exactly once and an HH:MM time, refuses unknown
events and fields, and writes the preferences and the household
settings' reminder_time (the one copy) in one transaction, publishing
the new settings to the scheduler as a household settings change does.

/settings/notifications renders mock 1n: the matrix with Toggle
switches, including the next-day "Keep it in rotation?" Rotation check
(D-021); the reminder time edited in a sheet; the push permission row
using pushStatus/subscribePush/unsubscribePush; the iOS InstallHint;
the Google Chat webhook card with the masked URL, "Connected · {space}"
and Send test showing success or the HTTP status; and the preview
bubble. The desktop 2e Notifications & AI tab shows the matrix and
webhook cards in its left column. The settings home row now reads Web
Push on this device and the Google Chat space, and the Household form
takes a reminder time saved elsewhere without undoing it.

Maestro task 308a460d-b22c-4179-acba-d5ea0968ccd5.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 14:52:02 -04:00

264 lines
6.9 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: notifications.sql
package db
import (
"context"
"database/sql"
)
const claimDueNotification = `-- name: ClaimDueNotification :one
UPDATE notification_outbox
SET next_attempt_at = ?1, updated_at = ?2
WHERE id = (
SELECT id FROM notification_outbox
WHERE status = 'pending' AND next_attempt_at <= ?2
ORDER BY next_attempt_at, id
LIMIT 1
)
RETURNING id, event_key, channel, payload, status, attempts, next_attempt_at, last_error, created_at, updated_at
`
type ClaimDueNotificationParams struct {
LeaseUntil sql.NullString
Now string
}
// Leases the pending notification due longest ago by moving its
// next_attempt_at to lease_until, and returns it. A claimed row is not due
// again until the lease ends, so no other claim takes it while it is being
// delivered. Returns sql.ErrNoRows when none is due.
func (q *Queries) ClaimDueNotification(ctx context.Context, arg ClaimDueNotificationParams) (NotificationOutbox, error) {
row := q.db.QueryRowContext(ctx, claimDueNotification, arg.LeaseUntil, arg.Now)
var i NotificationOutbox
err := row.Scan(
&i.ID,
&i.EventKey,
&i.Channel,
&i.Payload,
&i.Status,
&i.Attempts,
&i.NextAttemptAt,
&i.LastError,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const failNotification = `-- name: FailNotification :execrows
UPDATE notification_outbox
SET status = 'failed', attempts = attempts + ?1, next_attempt_at = NULL, last_error = ?2, updated_at = ?3
WHERE id = ?4 AND status = 'pending' AND next_attempt_at = ?5
`
type FailNotificationParams struct {
Attempted int64
LastError sql.NullString
Now string
ID string
LeaseUntil sql.NullString
}
// Gives up on a notification. attempts counts the delivery attempt that
// failed, if one was made. The lease must still be the claim's.
func (q *Queries) FailNotification(ctx context.Context, arg FailNotificationParams) (int64, error) {
result, err := q.db.ExecContext(ctx, failNotification,
arg.Attempted,
arg.LastError,
arg.Now,
arg.ID,
arg.LeaseUntil,
)
if err != nil {
return 0, err
}
return result.RowsAffected()
}
const getNotificationPref = `-- name: GetNotificationPref :one
SELECT event, push, chat, created_at, updated_at FROM notification_prefs
WHERE event = ?
`
func (q *Queries) GetNotificationPref(ctx context.Context, event string) (NotificationPref, error) {
row := q.db.QueryRowContext(ctx, getNotificationPref, event)
var i NotificationPref
err := row.Scan(
&i.Event,
&i.Push,
&i.Chat,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const insertNotification = `-- name: InsertNotification :execrows
INSERT INTO notification_outbox (
id, event_key, channel, payload, status, attempts, next_attempt_at, created_at, updated_at
) VALUES (
?1, ?2, ?3, ?4, 'pending', 0, ?5, ?6, ?6
)
ON CONFLICT (event_key, channel) DO NOTHING
`
type InsertNotificationParams struct {
ID string
EventKey string
Channel string
Payload string
NextAttemptAt sql.NullString
Now string
}
// Stores a pending notification for one channel, due at once. A row already
// stored for the event key and channel, in any status, is left unchanged and
// no row is inserted.
func (q *Queries) InsertNotification(ctx context.Context, arg InsertNotificationParams) (int64, error) {
result, err := q.db.ExecContext(ctx, insertNotification,
arg.ID,
arg.EventKey,
arg.Channel,
arg.Payload,
arg.NextAttemptAt,
arg.Now,
)
if err != nil {
return 0, err
}
return result.RowsAffected()
}
const listNotificationPrefs = `-- name: ListNotificationPrefs :many
SELECT event, push, chat, created_at, updated_at FROM notification_prefs
`
func (q *Queries) ListNotificationPrefs(ctx context.Context) ([]NotificationPref, error) {
rows, err := q.db.QueryContext(ctx, listNotificationPrefs)
if err != nil {
return nil, err
}
defer rows.Close()
var items []NotificationPref
for rows.Next() {
var i NotificationPref
if err := rows.Scan(
&i.Event,
&i.Push,
&i.Chat,
&i.CreatedAt,
&i.UpdatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const markNotificationSent = `-- name: MarkNotificationSent :execrows
UPDATE notification_outbox
SET status = 'sent', attempts = attempts + 1, next_attempt_at = NULL, updated_at = ?1
WHERE id = ?2 AND status = 'pending' AND next_attempt_at = ?3
`
type MarkNotificationSentParams struct {
Now string
ID string
LeaseUntil sql.NullString
}
// Records a delivered notification; updated_at is when it was sent. The
// lease must still be the claim's.
func (q *Queries) MarkNotificationSent(ctx context.Context, arg MarkNotificationSentParams) (int64, error) {
result, err := q.db.ExecContext(ctx, markNotificationSent, arg.Now, arg.ID, arg.LeaseUntil)
if err != nil {
return 0, err
}
return result.RowsAffected()
}
const nextNotificationDue = `-- name: NextNotificationDue :one
SELECT next_attempt_at FROM notification_outbox
WHERE status = 'pending'
ORDER BY next_attempt_at
LIMIT 1
`
// Returns when the next pending notification is due. Returns sql.ErrNoRows
// when none is pending.
func (q *Queries) NextNotificationDue(ctx context.Context) (sql.NullString, error) {
row := q.db.QueryRowContext(ctx, nextNotificationDue)
var next_attempt_at sql.NullString
err := row.Scan(&next_attempt_at)
return next_attempt_at, err
}
const retryNotification = `-- name: RetryNotification :execrows
UPDATE notification_outbox
SET attempts = attempts + 1, next_attempt_at = ?1, last_error = ?2, updated_at = ?3
WHERE id = ?4 AND status = 'pending' AND next_attempt_at = ?5
`
type RetryNotificationParams struct {
NextAttemptAt sql.NullString
LastError sql.NullString
Now string
ID string
LeaseUntil sql.NullString
}
// Records a failed delivery and when to try again. The lease must still be
// the claim's.
func (q *Queries) RetryNotification(ctx context.Context, arg RetryNotificationParams) (int64, error) {
result, err := q.db.ExecContext(ctx, retryNotification,
arg.NextAttemptAt,
arg.LastError,
arg.Now,
arg.ID,
arg.LeaseUntil,
)
if err != nil {
return 0, err
}
return result.RowsAffected()
}
const updateNotificationPref = `-- name: UpdateNotificationPref :execrows
UPDATE notification_prefs
SET push = ?, chat = ?, updated_at = ?
WHERE event = ?
`
type UpdateNotificationPrefParams struct {
Push int64
Chat int64
UpdatedAt string
Event string
}
// Sets which channels an event goes to; affects no row when the event has
// no preference row.
func (q *Queries) UpdateNotificationPref(ctx context.Context, arg UpdateNotificationPrefParams) (int64, error) {
result, err := q.db.ExecContext(ctx, updateNotificationPref,
arg.Push,
arg.Chat,
arg.UpdatedAt,
arg.Event,
)
if err != nil {
return 0, err
}
return result.RowsAffected()
}