feat(notification): add Resend quota gate and waiting-for-reset state
This commit is contained in:
1 parent
3b917befa9
commit
b856d4ff54
40 files changed
+4116
-48
No files matched your search
@@ -0,0 +1,79 @@
|
||||
package notification
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
// UserDeliveryStatus is what an account may see about its own latest mail
|
||||
// of a kind: the status and, while it waits for a quota reset, until when.
|
||||
// It carries no address, subject, body, provider, or delivery ID.
|
||||
//
|
||||
// A user interface maps it as follows:
|
||||
//
|
||||
// - waiting_quota with WaitingUntil: "your email is waiting until
|
||||
// <WaitingUntil>"; without WaitingUntil: "your email is waiting; the
|
||||
// time is not known yet";
|
||||
// - pending and unknown: "your email is being sent". unknown is not shown
|
||||
// as a failure: the message may have been accepted;
|
||||
// - accepted: "your email was sent". It means that the provider accepted
|
||||
// it, not that it reached the inbox;
|
||||
// - rejected: "your email could not be sent".
|
||||
type UserDeliveryStatus struct {
|
||||
Kind domain.MailKind
|
||||
Status domain.DeliveryStatus
|
||||
// WaitingUntil is the expected quota reset for waiting_quota; zero for
|
||||
// other statuses or when the reset is unknown.
|
||||
WaitingUntil time.Time
|
||||
// UpdatedAt is when the status last changed.
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
// WaitingForQuota reports a message that waits for a quota reset.
|
||||
func (status UserDeliveryStatus) WaitingForQuota() bool {
|
||||
return status.Status == domain.DeliveryStatusWaitingQuota
|
||||
}
|
||||
|
||||
// DeliveryStatusReader shows an account the status of its own mail. It reads
|
||||
// outside a transaction; the identity flows and a later API expose it.
|
||||
type DeliveryStatusReader struct {
|
||||
deliveries outbound.DeliveryRepository
|
||||
}
|
||||
|
||||
func NewDeliveryStatusReader(deliveries outbound.DeliveryRepository) *DeliveryStatusReader {
|
||||
return &DeliveryStatusReader{deliveries: deliveries}
|
||||
}
|
||||
|
||||
// LatestForUser returns the status of the newest delivery of kind addressed
|
||||
// to userID, and false when there is none. userID must be the authenticated
|
||||
// account itself: the query is scoped to deliveries addressed to it, so it
|
||||
// cannot read another account's mail, and callers must never take the ID
|
||||
// from request input.
|
||||
func (reader *DeliveryStatusReader) LatestForUser(ctx context.Context, userID uuid.UUID, kind domain.MailKind) (UserDeliveryStatus, bool, error) {
|
||||
const location = "DeliveryStatusReader.LatestForUser"
|
||||
if userID == uuid.Nil {
|
||||
return UserDeliveryStatus{}, false, fmt.Errorf("%s - user ID must not be nil: %w", location, domain.ErrInvalidDelivery)
|
||||
}
|
||||
if _, err := domain.ParseMailKind(string(kind)); err != nil {
|
||||
return UserDeliveryStatus{}, false, fmt.Errorf("%s - %w", location, err)
|
||||
}
|
||||
snapshot, err := reader.deliveries.LatestForRecipient(ctx, userID, kind)
|
||||
if errors.Is(err, outbound.ErrDeliveryNotFound) {
|
||||
return UserDeliveryStatus{}, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return UserDeliveryStatus{}, false, fmt.Errorf("%s - read latest delivery: %w", location, err)
|
||||
}
|
||||
status := UserDeliveryStatus{Kind: snapshot.Kind, Status: snapshot.Status, UpdatedAt: snapshot.UpdatedAt}
|
||||
if snapshot.Status == domain.DeliveryStatusWaitingQuota {
|
||||
status.WaitingUntil = snapshot.NextAttemptAt
|
||||
}
|
||||
return status, true, nil
|
||||
}
|
||||
Reference in new issue
Block a user