112 lines
5.0 KiB
Go
112 lines
5.0 KiB
Go
package outbound
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
)
|
|
|
|
var (
|
|
// ErrDeliveryNotFound reports that no delivery has the ID.
|
|
ErrDeliveryNotFound = errors.New("delivery not found")
|
|
|
|
// ErrDeliveryVersionConflict reports that RecordAttempt matched no row:
|
|
// another worker recorded an attempt first, or the delivery no longer
|
|
// exists. Nothing was written; read the delivery again.
|
|
ErrDeliveryVersionConflict = errors.New("delivery changed concurrently")
|
|
|
|
// ErrDeliveryContentImmutable reports a statement that tried to change
|
|
// the recipient, content, provider account, or idempotency key of a
|
|
// stored delivery, or a recorded attempt. The database refuses it; it
|
|
// signals a code defect.
|
|
ErrDeliveryContentImmutable = errors.New("delivery content is immutable")
|
|
|
|
// ErrDeliveryProviderPinned reports a statement that tried to change the
|
|
// provider account of a delivery after its first attempt. The database
|
|
// refuses it; domain.Delivery.Rebind refuses it first.
|
|
ErrDeliveryProviderPinned = errors.New("delivery provider account is pinned")
|
|
|
|
// ErrMailRequestNotFound reports that no mail request has the ID: it was
|
|
// already turned into a delivery, or its account was deleted.
|
|
ErrMailRequestNotFound = errors.New("mail request not found")
|
|
)
|
|
|
|
// DeliveryRepository stores mail deliveries and their attempts. UnitOfWork
|
|
// binds it to one transaction; an adapter may also provide it outside a
|
|
// transaction, where every call is its own statement. Errors never quote the
|
|
// recipient or the content.
|
|
type DeliveryRepository interface {
|
|
// Create stores a delivery built by domain.NewDelivery and reports
|
|
// created=true. When a delivery with the same ID exists, it stores
|
|
// nothing, keeps the stored content, and reports created=false. It
|
|
// returns an error wrapping ErrUserNotFound when the recipient account
|
|
// does not exist; the surrounding transaction is then aborted.
|
|
Create(ctx context.Context, delivery domain.Delivery) (created bool, err error)
|
|
|
|
// Get returns ErrDeliveryNotFound when no delivery has the ID, and an
|
|
// error wrapping domain.ErrMailContentIntegrity when the stored content
|
|
// no longer matches its hash.
|
|
Get(ctx context.Context, id uuid.UUID) (domain.Delivery, error)
|
|
|
|
// ListAttempts returns the recorded attempts in attempt order; an empty
|
|
// list for a delivery without attempts or an unknown ID.
|
|
ListAttempts(ctx context.Context, id uuid.UUID) ([]domain.DeliveryAttempt, error)
|
|
|
|
// RecordAttempt stores next and attempt as returned by
|
|
// domain.Delivery.RecordAttempt, atomically, when the stored version
|
|
// still equals the version before the attempt (next.Version() - 1).
|
|
// Otherwise it writes nothing and returns ErrDeliveryVersionConflict.
|
|
RecordAttempt(ctx context.Context, next domain.Delivery, attempt domain.DeliveryAttempt) error
|
|
|
|
// WaitForQuota stores next as returned by domain.Delivery.WaitForQuota,
|
|
// without an attempt record, when the stored version still equals
|
|
// next.Version() - 1. Otherwise it returns ErrDeliveryVersionConflict.
|
|
WaitForQuota(ctx context.Context, next domain.Delivery) error
|
|
|
|
// LatestForRecipient returns the status of the newest delivery of kind
|
|
// addressed to the account, by creation time, or ErrDeliveryNotFound
|
|
// when there is none. It reads no address and no content.
|
|
LatestForRecipient(ctx context.Context, recipientUserID uuid.UUID, kind domain.MailKind) (DeliveryStatusSnapshot, error)
|
|
|
|
// Rebind stores the provider account of next as returned by
|
|
// domain.Delivery.Rebind under the same version rule as WaitForQuota. A
|
|
// delivery that has an attempt meanwhile fails with
|
|
// ErrDeliveryVersionConflict; the database refuses a provider change
|
|
// after an attempt with ErrDeliveryProviderPinned.
|
|
Rebind(ctx context.Context, next domain.Delivery) error
|
|
}
|
|
|
|
// DeliveryStatusSnapshot is the state of a delivery without its recipient
|
|
// address or content. A zero NextAttemptAt means none is scheduled.
|
|
type DeliveryStatusSnapshot struct {
|
|
DeliveryID uuid.UUID
|
|
Kind domain.MailKind
|
|
Status domain.DeliveryStatus
|
|
NextAttemptAt time.Time
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
}
|
|
|
|
// MailRequestRepository stores mail requests between the producer
|
|
// transaction and the consumer transaction that turns them into deliveries.
|
|
// Errors never quote the request text.
|
|
type MailRequestRepository interface {
|
|
// Create stores a request built by domain.NewMailRequest. It returns an
|
|
// error wrapping ErrUserNotFound when the recipient account does not
|
|
// exist; the surrounding transaction is then aborted.
|
|
Create(ctx context.Context, request domain.MailRequest) error
|
|
|
|
// Lock reads the request and locks it until the transaction ends. A
|
|
// concurrent caller waits and then gets ErrMailRequestNotFound when the
|
|
// first transaction deleted it.
|
|
Lock(ctx context.Context, id uuid.UUID) (domain.MailRequest, error)
|
|
|
|
// Delete removes the request; deleting a missing request is not an
|
|
// error.
|
|
Delete(ctx context.Context, id uuid.UUID) error
|
|
}
|