Publishing a document now mails the workspace members who may read documents, and products can POST /v1/notifications (route group "notify") to report new findings, which reach IT_ADMIN, OWNER, CXO and LEGAL. Demo tenants and tenants without an organization get nothing. Every mail is a mail_outbox row (migration 0009), one per recipient, idempotent per key, sent by a worker with backoff and parked with its error after twelve attempts. Messages are rendered when sent from embedded German and English templates in the BreakPilot layout, as multipart text + HTML, through the SMTP relay (SMTP_*). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
100 lines
2.7 KiB
Go
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
|
|
}
|