Appearance
ADR 0016 — Notification delivery policy belongs to the product; the module holds the hook
- Status: Accepted
- Date: 2026-11-10
- Baseline inspected:
develop,1b4bcfaf - Related: ADR 0012
- Request answered: Nexio's assessment of a delivery policy layer in GrydNotifications (user preference per notice type and channel, controlled repetition, notice catalog)
- Implementation contract: Delivery policy
Context
GrydNotifications handles delivery: channel, template, durable queue, retry, OperationId idempotency, recorded attempts and the in-app inbox. A product also needs to decide, before delivery, whether and to whom a notice goes out. Examples are a user who turned a notice type off, a reminder that must not repeat within 24 hours, or a notice type whose e-mail is mandatory.
What the module had on develop at 1b4bcfaf:
- five accepting send paths:
SendNotificationCommand,ScheduleNotificationCommand,SendEmailCommand,SendPushNotificationCommandandSendInAppNotificationCommand, all reachable over HTTP or MediatR; the last three call the provider directly; NotificationRecipientwith onlyAddressandDisplayName. On e-mail the module cannot tell which user is behind an address;- status and attempts at the level of the whole
Notification, with nowhere to record "this recipient was skipped, and why"; CollapseKeyon the in-app inbox and on FCM. Both group or replace items on the device or in the inbox, and neither limits repetition;INotificationSchedulerschedules one existing notification. It does not accumulate items.
Decision
- The rules stay in the product. Preference storage and semantics (full state or exceptions only, mandatory channels, external recipients), repeat windows, the notice catalog, audience resolvers and digests are product choices. Two products will want different semantics, and the catalog is coupled to the domain.
- The module holds one hook on every send path.
INotificationDeliveryPolicydecides per recipient, in one batch per send. A single gate (NotificationDeliveryGate) is called by the five handlers. Without it, a product that sends through the module's own API would bypass a rule kept outside the module. - The decision is taken once, when the send is accepted. That is after rendering and before persisting, queueing or sending. In durable mode the payload is frozen at acceptance, and a replay of the operation returns the stored outcome without asking again. A scheduled send is decided when it is scheduled.
- Suppression is recorded, not hidden. Suppression data (user id, code, time) lives on
NotificationRecipient. A send whose recipients are all suppressed ends asNotificationStatus.Suppressed. It is stored for replay, but gets no outbox intent. Thegryd.notifications.suppressedmetric counts suppressions by channel and code. - Paths without recipient rows fail visibly. Direct in-app, and direct push with nobody left, return
NOTIFICATION_SUPPRESSED. In e-mail, onlyTois asked about.CcandBccare explicit copies, and they are not sent when everyTois suppressed. - A failing policy fails the send. The send fails with
NOTIFICATION_POLICY_UNAVAILABLE(503). The recipient is neither silently delivered to nor silently dropped. - The contract is additive. New fields are left out of the operation hash when absent, so operations accepted by an older version still replay.
Consequences
- A product implements its preference and repeat tables as a policy and registers it with
AddGrydNotificationDeliveryPolicy<T>(). It no longer needs to wrap every send call. - Digest is not planned in the module. A product that wants it builds it on its own store and sends the result through the module.
- A preference changed between scheduling and firing does not apply to that scheduled send.
POST /emailtakes plain addresses, so its candidates carry no user. A product that needs per-user rules on e-mail usesPOST /sendwithrecipients[].userId.
Alternatives considered
- Preferences, repetition and catalog inside the module. Rejected: it fixes one product's semantics in the framework.
- Evaluating at the worker, when the notification fires. Rejected for now: the durable payload is frozen at acceptance and would have to be rewritten. It would also make replays depend on time.
- A MediatR pipeline behavior instead of a gate called by the handlers. Rejected: the handler needs the filtered recipients to build the entity and the frozen payload.