package notification import ( "context" "errors" "fmt" "time" "github.com/google/uuid" "lincodex/backend/internal/domain" "lincodex/backend/internal/port/outbound" ) var ErrDuplicateRiskAcknowledgementRequired = errors.New("duplicate-risk acknowledgement is required") const DuplicateRiskWarning = "The earlier send may already have been accepted. After the provider's deduplication window, the old key does not guarantee deduplication; retrying can send a duplicate." // DeliveryRetryInspection contains the metadata an operator needs to decide // whether an unknown delivery is eligible for a manual retry. It has no // recipient address, token, subject, or body. type DeliveryRetryInspection struct { DeliveryID uuid.UUID Provider string ProviderAccount string Status domain.DeliveryStatus AttemptCount int32 NextAttemptAt time.Time FirstSendStartedAt time.Time ManualCycleStarted time.Time } // DeliveryRetryOperator inspects and authorizes retries of terminal unknown // deliveries. Authorization only schedules work; the normal sender uses the // delivery's pinned provider account, key, and stored body for the call. type DeliveryRetryOperator struct { transactor outbound.Transactor deliveries outbound.DeliveryRepository account domain.MailProviderAccount providerWindow time.Duration clock func() time.Time } func NewDeliveryRetryOperator( transactor outbound.Transactor, deliveries outbound.DeliveryRepository, account domain.MailProviderAccount, providerWindow time.Duration, clock func() time.Time, ) (*DeliveryRetryOperator, error) { switch { case transactor == nil: return nil, errors.New("notification.NewDeliveryRetryOperator - transactor must be set") case deliveries == nil: return nil, errors.New("notification.NewDeliveryRetryOperator - delivery repository must be set") case account.IsZero(): return nil, errors.New("notification.NewDeliveryRetryOperator - provider account must be set") case providerWindow < 0: return nil, errors.New("notification.NewDeliveryRetryOperator - provider window must not be negative") case clock == nil: return nil, errors.New("notification.NewDeliveryRetryOperator - clock must be set") } return &DeliveryRetryOperator{ transactor: transactor, deliveries: deliveries, account: account, providerWindow: providerWindow, clock: clock, }, nil } func (operator *DeliveryRetryOperator) Inspect(ctx context.Context, id uuid.UUID) (DeliveryRetryInspection, error) { if id == uuid.Nil { return DeliveryRetryInspection{}, fmt.Errorf("DeliveryRetryOperator.Inspect - delivery ID is required: %w", domain.ErrInvalidDelivery) } snapshot, err := operator.deliveries.GetRetrySnapshot(ctx, id) if err != nil { return DeliveryRetryInspection{}, fmt.Errorf("DeliveryRetryOperator.Inspect - read delivery: %w", err) } return DeliveryRetryInspection{ DeliveryID: snapshot.ID, Provider: snapshot.Provider, ProviderAccount: snapshot.ProviderAccount, Status: snapshot.Status, AttemptCount: snapshot.AttemptCount, NextAttemptAt: snapshot.NextAttemptAt, FirstSendStartedAt: snapshot.FirstSendStartedAt, ManualCycleStarted: snapshot.ManualRetryStartedAt, }, nil } // RetryUnknown requires the operator identity and explicit duplicate-risk // acknowledgement, then atomically schedules the current terminal unknown // attempt and appends its audit record. func (operator *DeliveryRetryOperator) RetryUnknown( ctx context.Context, id uuid.UUID, actor string, acknowledgeDuplicateRisk bool, ) (string, error) { if id == uuid.Nil { return "", fmt.Errorf("DeliveryRetryOperator.RetryUnknown - delivery ID is required: %w", domain.ErrInvalidDelivery) } if !acknowledgeDuplicateRisk { return "", ErrDuplicateRiskAcknowledgementRequired } parsedActor, err := domain.ParseMailRetryActor(actor) if err != nil { return "", err } minimumAge := time.Duration(0) if operator.providerWindow > 0 { minimumAge = max(operator.providerWindow-dedupSafetyMargin, 0) } err = operator.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error { return unit.Deliveries().AuthorizeUnknownRetry( ctx, id, operator.account, parsedActor, acknowledgeDuplicateRisk, operator.clock(), minimumAge, ) }) if err != nil { return "", fmt.Errorf("DeliveryRetryOperator.RetryUnknown - authorize retry: %w", err) } return DuplicateRiskWarning, nil }