// Package notification turns mail requests into durable deliveries. // // A product usecase requests a notification mail inside its own transaction // with MailRequester, which stores the request text in mail_requests and // appends a notification.mail_requested event that names only the request. // DeliveryHandler consumes the event and, in the transaction of the // consumer's dedup marker, reads the request, resolves the recipient address // from the account, renders the current template version once, stores the // Delivery, and deletes the request. Sending notifications is planned // ; no worker claims them yet. // // Service mail of the account flows (email verification and password reset) // takes a shorter path: ServiceMailer stores its delivery directly in the // transaction that issues the token, bound to that token, and // ServiceMailSender (service_sender.go) claims, checks, and sends it. package notification import ( "context" "encoding/json" "fmt" "time" "github.com/google/uuid" "lincodex/backend/internal/domain" "lincodex/backend/internal/port/outbound" "lincodex/backend/internal/usecase/notification/mailtemplate" ) const ( // MailRequestedEventType announces a stored mail request. MailRequestedEventType = "notification.mail_requested" // mailRequestedSchemaVersion 1 is mailRequestedPayload. mailRequestedSchemaVersion = 1 // mailRequestAggregateType names the request. Its aggregate ID is the // request ID and the ID of the delivery it becomes; a request is never // changed, so its version is 1. mailRequestAggregateType = "mail_request" mailRequestAggregateVersion = 1 ) // mailRequestedPayload is version 1 of the event payload. It names the // request and its kind only. The text and the recipient stay in // mail_requests, which the account's deletion removes, so neither the // outbox table nor the event bus keeps personal data after the account is // gone. type mailRequestedPayload struct { MailRequestID uuid.UUID `json:"mail_request_id"` Kind string `json:"kind"` } // NotificationRenderer renders notification mail as the content a delivery // stores. type NotificationRenderer interface { RenderNotification(data mailtemplate.NotificationData) (domain.MailContent, error) } // ServiceMailRenderer renders the service mail of the account flows, which // carry a one-time token in their link, as the content a delivery stores. type ServiceMailRenderer interface { RenderVerification(data mailtemplate.VerificationData) (domain.MailContent, error) RenderPasswordReset(data mailtemplate.PasswordResetData) (domain.MailContent, error) } // TemplateRenderer renders with the current versions of the embedded mail // templates. type TemplateRenderer struct { renderer *mailtemplate.Renderer } func NewTemplateRenderer(renderer *mailtemplate.Renderer) TemplateRenderer { return TemplateRenderer{renderer: renderer} } func (renderer TemplateRenderer) RenderNotification(data mailtemplate.NotificationData) (domain.MailContent, error) { rendered, err := renderer.renderer.RenderNotification(data) if err != nil { return domain.MailContent{}, fmt.Errorf("TemplateRenderer.RenderNotification - render: %w", err) } content, err := rendered.Content() if err != nil { return domain.MailContent{}, fmt.Errorf("TemplateRenderer.RenderNotification - convert rendering: %w", err) } return content, nil } func (renderer TemplateRenderer) RenderVerification(data mailtemplate.VerificationData) (domain.MailContent, error) { rendered, err := renderer.renderer.RenderVerification(data) if err != nil { return domain.MailContent{}, fmt.Errorf("TemplateRenderer.RenderVerification - render: %w", err) } content, err := rendered.Content() if err != nil { return domain.MailContent{}, fmt.Errorf("TemplateRenderer.RenderVerification - convert rendering: %w", err) } return content, nil } func (renderer TemplateRenderer) RenderPasswordReset(data mailtemplate.PasswordResetData) (domain.MailContent, error) { rendered, err := renderer.renderer.RenderPasswordReset(data) if err != nil { return domain.MailContent{}, fmt.Errorf("TemplateRenderer.RenderPasswordReset - render: %w", err) } content, err := rendered.Content() if err != nil { return domain.MailContent{}, fmt.Errorf("TemplateRenderer.RenderPasswordReset - convert rendering: %w", err) } return content, nil } // NotificationRequest asks for one notification mail to one account. type NotificationRequest struct { RecipientUserID uuid.UUID Notification mailtemplate.NotificationData } // MailRequester records mail requests in the caller's transaction. type MailRequester struct { renderer NotificationRenderer clock func() time.Time } // NewMailRequester takes the renderer only to refuse data that the template // would refuse at handling time, so a bad request fails its own transaction // instead of becoming a dead letter later. clock is the application clock // for the request time. func NewMailRequester(renderer NotificationRenderer, clock func() time.Time) *MailRequester { return &MailRequester{renderer: renderer, clock: clock} } // RequestNotification stores the request and appends a // notification.mail_requested event through unit, which must be the caller's // transaction, and returns the ID that the delivery will have. The mail is // created only if that transaction commits. Invalid input returns an error // wrapping domain.ErrInvalidMailRequest and writes nothing; an unknown // recipient account returns an error wrapping outbound.ErrUserNotFound and // aborts the transaction. func (requester *MailRequester) RequestNotification(ctx context.Context, unit outbound.UnitOfWork, request NotificationRequest) (uuid.UUID, error) { const location = "MailRequester.RequestNotification" if _, err := requester.renderer.RenderNotification(request.Notification); err != nil { return uuid.Nil, fmt.Errorf("%s - validate notification: %w: %w", location, domain.ErrInvalidMailRequest, err) } requestID, err := uuid.NewV7() if err != nil { return uuid.Nil, fmt.Errorf("%s - generate request ID: %w", location, err) } stored, err := domain.NewMailRequest(domain.MailRequest{ ID: requestID, RecipientUserID: request.RecipientUserID, Kind: domain.MailKindNotification, Notification: domain.NotificationText{ Subject: request.Notification.Subject, Body: request.Notification.Body, LinkPath: request.Notification.LinkPath, }, CreatedAt: requester.clock(), }) if err != nil { return uuid.Nil, fmt.Errorf("%s - build request: %w", location, err) } payload, err := json.Marshal(mailRequestedPayload{MailRequestID: requestID, Kind: string(domain.MailKindNotification)}) if err != nil { return uuid.Nil, fmt.Errorf("%s - encode payload: %w", location, err) } event, err := domain.NewDomainEvent(domain.DomainEventInput{ EventType: MailRequestedEventType, SchemaVersion: mailRequestedSchemaVersion, AggregateType: mailRequestAggregateType, AggregateID: requestID, AggregateVersion: mailRequestAggregateVersion, Payload: payload, }) if err != nil { return uuid.Nil, fmt.Errorf("%s - build event: %w", location, err) } if err := unit.MailRequests().Create(ctx, stored); err != nil { return uuid.Nil, fmt.Errorf("%s - store request: %w", location, err) } if err := unit.Outbox().Append(ctx, event); err != nil { return uuid.Nil, fmt.Errorf("%s - append event: %w", location, err) } return requestID, nil }