Skip to content

Delivery policy ​

A delivery policy decides, before anything is delivered, whether each recipient of a send receives it. GrydNotifications provides the hook. The rules belong to the product: user preferences per notice type and channel, "do not repeat this reminder within 24 hours", or any other reason not to deliver. See ADR 0016 for why the module holds the hook but not the rules.

What the module does and does not do ​

The module:

  • asks every registered INotificationDeliveryPolicy on every send path: POST /send, /schedule, /email, /push, and SendInAppNotificationCommand. Product code that calls the commands through MediatR goes through the same gate;
  • records each suppression on the recipient, with its code and time, so you can later answer "why did this person not receive it?";
  • counts suppressions on the gryd.notifications.suppressed metric, tagged by channel and code.

The module does not store preferences, repeat windows, a catalog of notice types, audience resolvers or digests. A product keeps those in its own database and reads them from its policy.

Writing a policy ​

csharp
public sealed class PreferencePolicy(IPreferenceReader preferences) : INotificationDeliveryPolicy
{
    public async ValueTask<IReadOnlyList<DeliveryDecision>> EvaluateAsync(
        DeliveryRequest request,
        IReadOnlyList<DeliveryCandidate> candidates,
        CancellationToken cancellationToken)
    {
        // One read for the whole send: the candidates come in a single batch.
        var optedOut = await preferences.OptedOutAsync(
            request.NotificationType, request.Channel,
            candidates.Where(c => c.UserId is not null).Select(c => c.UserId!.Value),
            cancellationToken);

        return [.. candidates.Select(c =>
            c.UserId is { } user && optedOut.Contains(user)
                ? DeliveryDecision.Suppress("preference")
                : DeliveryDecision.Deliver)];
    }
}

services.AddGrydNotificationDeliveryPolicy<PreferencePolicy>();
  • The policy returns one decision per candidate, in the same order. A different count fails the send.
  • A candidate without a UserId is someone the caller did not identify as a user, such as a supplier contact. Your policy decides what to do with it. The module never resolves an address to a user.
  • DeliveryDecision.Suppress(code) takes a product code of 1 to 64 characters: lower-case letters, digits, . and -, starting with a letter or digit. The code is stored and used as a metric tag, so it must not carry personal data.
  • Policies are resolved per scope and run in registration order. A recipient suppressed by one policy is not shown to the next ones. Registering the same type twice has no effect.
  • With no policy registered, everyone is delivered to, as before.

What the sender provides ​

FieldWhereMeaning
recipients[].userIdPOST /send, /scheduleUser behind the address. Optional.
notificationTypeevery send commandProduct class of the notice, for example sla.reminder. Up to 128 characters. It is not the template slug.
entityKeyevery send commandKey of the entity the notice is about, for example requisition:42. Up to 256 characters.

DeliveryRequest also carries the Channel, the TenantId of the send, and the OperationId when one was given. Direct push uses the command's UserId. Direct in-app always has its UserId. POST /email takes plain addresses, so its candidates have no UserId. To tie an e-mail recipient to a user, send through POST /send with recipients[].userId.

The new fields are omitted from the operation hash when they are absent. A send that does not use them keeps the hash it had before, so replaying an OperationId accepted by an older version still works.

When the policy is asked ​

The policy is asked once, when the send is accepted: after the template is rendered and before anything is persisted, queued, scheduled or sent.

  • In durable mode the frozen payload already carries only the recipients that were kept. A replay of an accepted OperationId returns the stored outcome and does not ask the policy again.
  • A scheduled send is decided when it is scheduled, not when it fires. A preference changed in between does not apply to that send.

Outcomes per send path ​

PathSome recipients suppressedAll recipients suppressed
POST /send, /scheduleDelivered to the others; the suppressed ones stay on record with suppressionCode and suppressedAt200 OK with status Suppressed; nothing is queued, scheduled or sent
POST /emailTo keeps the others; Cc and Bcc are not asked about200 OK with status Suppressed; the copies are not sent either
POST /push with deviceTokens and no userIdThe suppressed tokens are dropped400 with NOTIFICATION_SUPPRESSED
POST /push with userId—400 with NOTIFICATION_SUPPRESSED; no device lookup
SendInAppNotificationCommand—Failure with NOTIFICATION_SUPPRESSED

Direct push and direct in-app return a failure because they keep no recipient rows to record the suppression, and the in-app result has no "nothing sent" shape. The suppression still shows on the metric.

A Suppressed notification is terminal. In durable mode it is stored, so its operation can be replayed, but it gets no outbox intent.

Failures ​

If a policy throws or answers with the wrong number of decisions, the send fails with NOTIFICATION_POLICY_UNAVAILABLE (503). Nothing is delivered and nothing is recorded. The caller retries with the same OperationId. Only the policy type and the exception type are logged, never the exception message. Cancellation is not turned into this failure: it propagates.

Reading suppressions ​

GET /api/v1/notifications/{id} lists every recipient. A suppressed one carries its code and time:

json
"recipients": [
  { "address": "buyer@example.com", "userId": "6f1c...", "suppressionCode": "preference", "suppressedAt": "2026-11-10T12:00:00Z" },
  { "address": "approver@example.com", "userId": "0b9e..." }
]

suppressionCode and suppressedAt are read only. A send that sets them is refused by validation.

Released under the MIT License.