Files
tenant-registry/internal/store/memory_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

89 lines
1.8 KiB
Go

package store
import (
"context"
"time"
)
func (m *Memory) EnqueueMail(_ context.Context, it MailItem) (bool, error) {
m.mu.Lock()
defer m.mu.Unlock()
if it.IdempotencyKey == "" || it.Recipient == "" || it.Kind == "" {
return false, ErrInvalidInput
}
for _, x := range m.mail {
if x.IdempotencyKey == it.IdempotencyKey {
return false, nil
}
}
m.mailID++
now := time.Now().UTC()
it.ID, it.CreatedAt, it.Attempts, it.SentAt = m.mailID, now, 0, nil
if it.NextAttemptAt.IsZero() {
it.NextAttemptAt = now
}
if it.Locale == "" {
it.Locale = "de"
}
if it.Data == nil {
it.Data = map[string]interface{}{}
}
row := it
m.mail = append(m.mail, &row)
return true, nil
}
func (m *Memory) ClaimMail(_ context.Context, limit int, now time.Time, lease time.Duration) ([]MailItem, error) {
m.mu.Lock()
defer m.mu.Unlock()
out := []MailItem{}
for _, x := range m.mail {
if len(out) >= limit {
break
}
if x.SentAt != nil || x.NextAttemptAt.After(now) {
continue
}
x.NextAttemptAt = now.Add(lease)
out = append(out, *x)
}
return out, nil
}
func (m *Memory) CompleteMail(_ context.Context, id int64, at time.Time) error {
m.mu.Lock()
defer m.mu.Unlock()
for _, x := range m.mail {
if x.ID == id {
at = at.UTC()
x.SentAt, x.LastError = &at, ""
return nil
}
}
return ErrNotFound
}
func (m *Memory) FailMail(_ context.Context, id int64, errMsg string, next time.Time) error {
m.mu.Lock()
defer m.mu.Unlock()
for _, x := range m.mail {
if x.ID == id {
x.Attempts++
x.LastError, x.NextAttemptAt = errMsg, next.UTC()
return nil
}
}
return ErrNotFound
}
// MailItems is a test helper: a copy of the outbox.
func (m *Memory) MailItems() []MailItem {
m.mu.RLock()
defer m.mu.RUnlock()
out := make([]MailItem, len(m.mail))
for i, x := range m.mail {
out[i] = *x
}
return out
}