82 lines
3.2 KiB
Go
82 lines
3.2 KiB
Go
package notification
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"time"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
// ErrNoSubjectRights reports a notification whose subject kind has no rights
|
|
// checker in this build. Such a notice is neither sent nor cancelled: its
|
|
// basis cannot be checked, so it waits for a build that can check it.
|
|
var ErrNoSubjectRights = errors.New("no rights check is registered for the notification subject")
|
|
|
|
// SubjectRights is the rights check of one subject kind of product
|
|
// notifications. A notice names a subject, and its check runs in the claim
|
|
// transaction and again before the transport is called, under the delivery
|
|
// lock. A future notice that carries a candidate field or a contact adds its
|
|
// consent check here (C4 for a contact, C2 for a publication-dependent field)
|
|
// and never puts a contact into the mail: contacts are read on request, and
|
|
// every read re-checks its grant.
|
|
type SubjectRights interface {
|
|
// Subject is the subject kind this check decides.
|
|
Subject() domain.MailSubjectKind
|
|
// Check reads the current standing and returns the cancel reason, or an
|
|
// empty reason when the notice may be sent.
|
|
Check(ctx context.Context, unit outbound.UnitOfWork, delivery domain.Delivery, at time.Time) (domain.DeliveryCancelReason, error)
|
|
}
|
|
|
|
// subjectEligibility dispatches the rights check by the subject of the
|
|
// delivery. A notification that names no subject has no basis to check and is
|
|
// cancelled with source_closed.
|
|
type subjectEligibility struct {
|
|
rights map[domain.MailSubjectKind]SubjectRights
|
|
}
|
|
|
|
func (eligibility subjectEligibility) Check(ctx context.Context, unit outbound.UnitOfWork, delivery domain.Delivery, at time.Time) (domain.DeliveryCancelReason, error) {
|
|
subject := delivery.Subject()
|
|
if subject.IsZero() {
|
|
return domain.DeliveryCancelSourceClosed, nil
|
|
}
|
|
checker, ok := eligibility.rights[subject.Kind()]
|
|
if !ok {
|
|
return "", fmt.Errorf("subjectEligibility.Check - subject kind %q: %w", subject.Kind(), ErrNoSubjectRights)
|
|
}
|
|
return checker.Check(ctx, unit, delivery, at)
|
|
}
|
|
|
|
// NewNotificationSender builds the sender of the product notification lane. It
|
|
// claims only notifications and sends one only when the rights check of its
|
|
// subject kind passes. rights lists one check per subject kind; a kind listed
|
|
// twice is refused. gate must be built for settings.Account.
|
|
func NewNotificationSender(
|
|
transactor outbound.Transactor,
|
|
sender outbound.MailSender,
|
|
gate *QuotaGate,
|
|
settings DeliverySenderSettings,
|
|
clock func() time.Time,
|
|
logger *slog.Logger,
|
|
rights []SubjectRights,
|
|
) (*DeliverySender, error) {
|
|
byKind := make(map[domain.MailSubjectKind]SubjectRights, len(rights))
|
|
for _, check := range rights {
|
|
if check == nil {
|
|
return nil, errors.New("notification.NewNotificationSender - rights check must not be nil")
|
|
}
|
|
if _, dup := byKind[check.Subject()]; dup {
|
|
return nil, fmt.Errorf("notification.NewNotificationSender - subject kind %q has two rights checks", check.Subject())
|
|
}
|
|
byKind[check.Subject()] = check
|
|
}
|
|
return newDeliverySender(deliveryLane{
|
|
scope: outbound.ClaimScopeNotification,
|
|
kinds: domain.ProductMailKinds(),
|
|
eligibility: subjectEligibility{rights: byKind},
|
|
}, transactor, sender, gate, settings, clock, logger)
|
|
}
|