Files
lincodex/backend/internal/usecase/notification/service_sender.go
T

51 lines
1.8 KiB
Go

package notification
import (
"context"
"fmt"
"log/slog"
"time"
"lincodex/backend/internal/domain"
"lincodex/backend/internal/port/outbound"
)
// ServiceMailSenderSettings are the named limits of the service mail sender.
type ServiceMailSenderSettings = DeliverySenderSettings
// ServiceMailSender sends the service identity mail of the account flows
// (email verification and password reset) that ServiceMailer stored. It is a
// DeliverySender of the service lane: it claims only service mail and checks
// each delivery's token and account before the send.
type ServiceMailSender = DeliverySender
// serviceEligibility applies the token and account rules of service mail.
// Publishing consent (C2) and the other product consents play no part: a
// service mail is part of the account flow, not a product notification.
type serviceEligibility struct{}
func (serviceEligibility) Check(ctx context.Context, unit outbound.UnitOfWork, delivery domain.Delivery, at time.Time) (domain.DeliveryCancelReason, error) {
state, err := unit.Deliveries().ServiceMailState(ctx, delivery.ID())
if err != nil {
return "", fmt.Errorf("serviceEligibility.Check - read token and account: %w", err)
}
return delivery.CheckServiceMail(state, at), nil
}
// NewServiceMailSender builds the sender of the service lane. gate must be
// built for settings.Account. clock is the application clock.
func NewServiceMailSender(
transactor outbound.Transactor,
sender outbound.MailSender,
gate *QuotaGate,
settings ServiceMailSenderSettings,
clock func() time.Time,
logger *slog.Logger,
) (*ServiceMailSender, error) {
return newDeliverySender(deliveryLane{
scope: outbound.ClaimScopeService,
kinds: domain.ServiceMailKinds(),
eligibility: serviceEligibility{},
}, transactor, sender, gate, settings, clock, logger)
}