Files
tenant-registry/internal/store/postgres_mail.go
T
sharangandClaude Opus 5.5 e0bff8c793
ci / shared (push) Successful in 13s
ci / test (push) Successful in 23m16s
ci / image (push) Successful in 36s
feat(notify): branded mails for published documents and new findings (#35)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
2026-09-23 21:10:06 +00:00

100 lines
2.7 KiB
Go

package store
import (
"context"
"encoding/json"
"time"
)
func (p *Postgres) EnqueueMail(ctx context.Context, it MailItem) (bool, error) {
if it.IdempotencyKey == "" || it.Recipient == "" || it.Kind == "" {
return false, ErrInvalidInput
}
if it.Locale == "" {
it.Locale = "de"
}
if it.Data == nil {
it.Data = map[string]interface{}{}
}
data, err := json.Marshal(it.Data)
if err != nil {
return false, err
}
var tenant any
if it.TenantID != "" {
tenant = it.TenantID
}
next := it.NextAttemptAt
if next.IsZero() {
next = time.Now().UTC()
}
tag, err := p.pool.Exec(ctx, `
INSERT INTO mail_outbox (tenant_id, kind, recipient, locale, data, idempotency_key, next_attempt_at)
VALUES ($1::uuid, $2, $3, $4, $5, $6, $7)
ON CONFLICT (idempotency_key) DO NOTHING`,
tenant, it.Kind, it.Recipient, it.Locale, data, it.IdempotencyKey, next)
if err != nil {
if isFKViolation(err) || isInvalidUUID(err) {
return false, ErrNotFound
}
return false, err
}
return tag.RowsAffected() == 1, nil
}
func (p *Postgres) ClaimMail(ctx context.Context, limit int, now time.Time, lease time.Duration) ([]MailItem, error) {
rows, err := p.pool.Query(ctx, `
UPDATE mail_outbox SET next_attempt_at = $2
WHERE id IN (
SELECT id FROM mail_outbox
WHERE sent_at IS NULL AND next_attempt_at <= $1
ORDER BY next_attempt_at, id
LIMIT $3
FOR UPDATE SKIP LOCKED)
RETURNING id, COALESCE(tenant_id::text, ''), kind, recipient, locale, data, idempotency_key,
attempts, next_attempt_at, sent_at, COALESCE(last_error, ''), created_at`,
now.UTC(), now.UTC().Add(lease), limit)
if err != nil {
return nil, err
}
defer rows.Close()
out := []MailItem{}
for rows.Next() {
var it MailItem
var raw []byte
if err := rows.Scan(&it.ID, &it.TenantID, &it.Kind, &it.Recipient, &it.Locale, &raw, &it.IdempotencyKey,
&it.Attempts, &it.NextAttemptAt, &it.SentAt, &it.LastError, &it.CreatedAt); err != nil {
return nil, err
}
if err := json.Unmarshal(raw, &it.Data); err != nil {
it.Data = map[string]interface{}{}
}
out = append(out, it)
}
return out, rows.Err()
}
func (p *Postgres) CompleteMail(ctx context.Context, id int64, at time.Time) error {
tag, err := p.pool.Exec(ctx, `UPDATE mail_outbox SET sent_at = $2, last_error = NULL WHERE id = $1`, id, at.UTC())
if err != nil {
return err
}
if tag.RowsAffected() == 0 {
return ErrNotFound
}
return nil
}
func (p *Postgres) FailMail(ctx context.Context, id int64, errMsg string, next time.Time) error {
tag, err := p.pool.Exec(ctx, `
UPDATE mail_outbox SET attempts = attempts + 1, last_error = $2, next_attempt_at = $3 WHERE id = $1`,
id, errMsg, next.UTC())
if err != nil {
return err
}
if tag.RowsAffected() == 0 {
return ErrNotFound
}
return nil
}