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 }