Skip to content

feat(emails): scheduled registration digests + session reminders - #596

Merged
Ardnived merged 20 commits into
mainfrom
feat/registration-notifications-scheduled
Jul 22, 2026
Merged

Ardnived merged 20 commits into
mainfrom
feat/registration-notifications-scheduled

Conversation

@Ardnived

@Ardnived Ardnived commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Two scheduled jobs deliver every registration email the CMS sends on a cadence rather than in response to a request: manager digests (batched summaries for Daily/Weekly Summary recipients) and registrant session reminders (~24h before each session, with an unsubscribe link).
  • Both live in one job folder with one exactly-once pattern each — a per-registration reminderLog ledger for reminders, a per-manager lastRegistrationDigestSentAt watermark for digests.
  • Adds a logged-out, token-gated unsubscribe page for registrants that stops reminders without deleting the registration.

Design decisions (delegated by the ticket)

  • Cadence: reminders run hourly (0 * * * *), so the notice always lands 23–24h before a session — worst-case deviation < 1h. Digests ride a coarser anchor: daily 07:00 UTC, weekly on Monday 07:00 UTC (deterministic). Documented in each job's module comment.
  • One task or two: two tasks, one folder — independent locks + retries so a digest failure never blocks the time-critical reminder promise.
  • Digest marker: per-manager watermark (the ticket's "smallest thing that works"); digest recipients are always managers (event-override emails only offer Immediate/Never).
  • Unsubscribe TTL: unbounded — the HMAC token is scoped to one registrationId and the action is idempotent/non-destructive, so a link stays valid across a 12-week course.

The digest email

Grouped by event, one email per manager per period. Each registration is a card — an inline identity line (name · email · "Attending ") plus the registrant's answers to the event's registration questions — with a compact date (formatShortDate → "5 Aug 2025"). No per-event count line; a grand total sits in the intro. Manager-facing, so it's Atlas-branded and English (not client-branded/localized).

Changes

  • src/jobs/RegistrationNotifications/ — SendSessionReminders (hourly) + SendRegistrationDigests (daily/weekly) + reminderLedger + shared loadUsers; registered via src/jobs/index.ts.
  • src/emails/ — SessionReminderEmail (client-branded, single occurrence, no ICS) + RegistrationDigestEmail (Atlas brand, grouped cards + answers); lifts the confirmation's StackedDetailRow into EmailLayout; adds formatShortDate.
  • src/lib/notifications/ — sendSessionReminder + sendRegistrationDigest + occurrenceLine/buildReminderEmailDetails.
  • src/lib/registrations/ — unsubscribeToken (HMAC, no expiry) + unsubscribeUrl.
  • src/app/(frontend)/registrations/unsubscribe/ — public page + server action + card (GET confirms, POST mutates; localized).
  • src/collections/{Managers,Registrations} — watermark, reminder ledger, unsubscribe state.
  • scripts/preview-reminder-digest-emails.ts — Ethereal preview for both emails (incl. a high-volume digest fixture).

Migration

  • New migration: src/migrations/20260720_195203_registration_notifications.ts
  • Impact: additive — managers.last_registration_digest_sent_at, registrations.reminders_unsubscribed_at + reminder_log, and two payload_jobs task-slug enum values. Existing rows unaffected (nullable columns).
  • Reversible: yes — down() drops the columns and reverts the enums.

Test Results

  • Unit: 831 passed (token forgery/replay, reminder ledger, formatShortDate, email renders incl. digest answers/identity, occurrence formatting)
  • Integration: 11 passed — reminder send + dedup + startingAt + unsubscribe/unpublished skips; digest grouping + answers + watermark + Immediate-unaffected + no-empty-digest + Monday-only weekly anchor + unsubscribe-separation
  • Lint: ✓ · Typecheck: ✓ · Full suite + smoke run in CI

Notes for reviewer

  • Security review: clean (token forgery/tamper-safe, unsubscribe GET/POST-safe + IDOR-safe, digests scoped per manager, email-header injection defended). The digest renders registrant-authored answers — React escapes them as text nodes (no injection).
  • Exactly-once uses send-then-persist (identical to ExpireEvents): a normal re-run/retry sends nothing (dedup-proven by int specs); the only duplicate is the inherent window where a send succeeds but the mark write fails — persist-then-send would instead risk missing a send.
  • The manager digest deliberately does not filter on the registrant's remindersUnsubscribedAt — per the ticket, the unsubscribe link covers registrant reminders only, not manager notifications (pinned by a dedicated int test).

Manual verification

  1. Trigger the queue (pnpm payload jobs:run --queue nightly) with a published sahaj-atlas event whose next session is <24h away and a subscribed registration → registrant gets one reminder; the unsubscribe link opens the localized page and stops further reminders.
  2. A manager with Daily Summary and new registrations → one grouped digest at the 07:00 UTC run. Preview both emails locally with pnpm tsx scripts/preview-reminder-digest-emails.ts.

Closes #589

🤖 Generated with Claude Code

Ardnived and others added 12 commits July 20, 2026 12:21
…atermark

Schema for the scheduled notification job (#589):
- Registrations.remindersUnsubscribedAt — set by the unsubscribe link; the
  reminder job skips a registration whose value is set (leaves the record).
- Registrations.reminderLog — per-(registration, occurrence) exactly-once
  ledger, mirroring the Events notificationLog pattern.
- Managers.lastRegistrationDigestSentAt — per-manager digest watermark.

Refs #589

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Session-reminder chrome (subject/heading/intro/footer, unsubscribe CTA) and the
unsubscribe landing-page copy, added to EMAIL_STRING_DEFAULTS and the emails
translations group so both render in the registration's stored locale. Reuses
the existing when/where/contact/CTA labels. The manager digest email is Atlas
project brand + English (like EventRegistrationEmail), so it needs no keys.

Refs #589

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
HMAC-SHA256 token over { registrationId }, signed with the Payload secret,
mirroring eventVerification/token.ts. No expiry: a reminder link may be clicked
weeks into a course, and unsubscribing is narrow + idempotent, so a replayed or
aged link can't harm — only the named registration is ever affected, and a
forged/altered id fails the signature check. buildUnsubscribeEmailLink points at
the /registrations/unsubscribe page (mutation runs on POST, not on GET).

Refs #589

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Registrant-facing reminder, a sibling of the confirmation sharing EmailLayout +
the lifted StackedDetailRow + BrandButton. States the single upcoming occurrence
(occurrenceLine / buildReminderEmailDetails), no session count, no ICS, and a
footer unsubscribe link. sendSessionReminder mirrors sendRegistrationConfirmation
(client brand, localized copy, Reply-To) and throws on failure so the job owns
the retry policy.

Lifts the confirmation's local label-above-value DetailRow into EmailLayout as
StackedDetailRow (its own comment invited this once a second template needed it);
rendered output is unchanged (email-templates unit suite still green).

Refs #589

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Manager-facing digest listing new registrations grouped by event — one email per
recipient per period, not one per registration. Reuses EmailLayout + SectionHeading
and the Sahaj Atlas project brand (internal notice, never client-branded), the
manager-notice counterpart to EventRegistrationEmail. sendRegistrationDigest stubs
non-email channels like the immediate notification and throws on failure so the
digest job keeps the watermark on retry.

Refs #589

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Two tasks in one folder, both on the hourly nightly queue via exclusive
concurrency:

- SendSessionReminders (hourly): reminds each subscribed registration ~24h before
  a session. Enumerates occurrences from buildRRuleTemporal (exclusions applied),
  reminds for the single startingAt session or the whole series, dedups via the
  per-registration reminderLog ledger, skips unsubscribed/unpublished/trashed.
  Worst-case < 1h off "24h before" (documented in the module comment).
- SendRegistrationDigests (daily 07:00 UTC; weekly on Monday): batches new
  registrations per manager, grouped by event, using the lastRegistrationDigestSentAt
  watermark for exactly-once. No empty digests; deterministic anchors.

Registrations are read at depth 0 with users batch-loaded to avoid the #541
virtual-field N+1. Registers both slugs in the jobs barrel (picked up by
payload.config.ts + testHelpers) and regenerates the task types.

Refs #589

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Logged-out /registrations/unsubscribe page mirroring the event-verify flow: the
signed token is the sole gate (invalid → 404), a GET shows a confirm button, and
the mutation runs only on the POST server action — so an email link-scanner can't
auto-unsubscribe. Sets remindersUnsubscribedAt (stops the reminder job) without
touching the registration; idempotent, so a replayed link is safe. All copy
renders in the registration's stored locale.

Refs #589

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Additive: managers.last_registration_digest_sent_at, registrations.
reminders_unsubscribed_at + reminder_log, and the two new job-slug enum values.
DROP COLUMNs appear only in down() (rollback). Applies in-process on deploy.

Refs #589

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Unit: unsubscribe-token forgery/replay/no-expiry, reminder-ledger coercion, and
the reminder/digest email renders + occurrenceLine formatting.

Int (via emailTestAdapter, deterministic — no wall-clock dependence): the reminder
job's send + dedup-on-rerun + startingAt-single-session + unsubscribe/unpublished
skips, and the digest job's grouped-per-event send + watermark dedup + Immediate
unaffected + no-empty-digest + Monday-only weekly anchor.

Adds a `req.context.now` clock seam to both handlers so the 24h window and the
weekly anchor test deterministically.

Refs #589

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
- Reuse the shared @/lib/utilities/relationId in both jobs (dropped two local copies).
- Extract formatTimeSpan in registrationDetails, shared by timeSpan + occurrenceLine
  (removes the duplicated DST-safe span logic).
- SendSessionReminders now reads registrations at depth 0 and batch-loads users,
  matching the digest job — avoids populating user/client for registrations with no
  reminder due and eliminates the double client load.

Skipped: sharing PAGINATION_LIMIT across the two jobs (over-coupling a one-liner);
lifting the unsubscribe CardShell to share with events/verify (outside this diff).

Refs #589

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
- email.md: SessionReminderEmail + RegistrationDigestEmail rows, StackedDetailRow
  in the EmailLayout exports + guest-email note.
- architecture.md: RegistrationNotifications scheduled-jobs section.
- project-structure.md: unsubscribe token/url in the src/lib/registrations domain.

Refs #589

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
- Extract the duplicated loadUsers into a shared job helper (introduced during
  the /simplify efficiency pass, so the simplify agents didn't see it).
- Add a digest int test pinning the intended spec separation: a registrant's
  reminder-unsubscribe does NOT hide their registration from the manager digest
  (unsubscribe covers reminders only, per #589).

Dismissed review findings (verified against source/spec):
- send-then-persist ordering (reminder ledger / digest watermark) is the same
  accepted at-least-once trade-off as ExpireEvents; persist-then-send would risk
  missing a send. Normal re-runs/retries are dedup-proven by the int specs.
- digest correctly omits _status / remindersUnsubscribedAt filters: it's the
  batched manager notification (unsubscribe is registrant-reminder-only), and
  filtering _status would miss real registrations for later-unpublished events.
- recipient-null watermark stall is unreachable (managers are auth users with a
  required email; pickChannel falls back to it).

Refs #589

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@railway-app
railway-app Bot temporarily deployed to sahajcloud / SahajCloud-pr-596 July 20, 2026 21:01 Destroyed
@railway-app

railway-app Bot commented Jul 20, 2026 •

Copy link
Copy Markdown

🚅 Deployed to the SahajCloud-pr-596 environment in sahajcloud

Service Status Web Updated (UTC)
SahajCloud ✅ Success (View Logs) Web Jul 21, 2026 at 6:02 pm

@railway-app
railway-app Bot temporarily deployed to sahajcloud / SahajCloud-pr-596 July 20, 2026 23:28 Destroyed
Ardnived and others added 7 commits July 20, 2026 16:33
preview-reminder-digest-emails.ts drives the real sendSessionReminder /
sendRegistrationDigest paths through a stub payload + Ethereal transport, so the
subject/From/Reply-To/HTML/text match what the jobs send. Covers online/offline,
locale fallback, branded/unbranded reminders, and daily/weekly digests.

Refs #589

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
- Include each registrant's registration-question answers (the digest job now
  loads `questions` and resolves them via buildRegistrationAnswers).
- Give each registration its own white card (name, contact, chosen session,
  answers) so they're visually distinct in a busy digest.
- Drop the redundant per-event count — the cards are the count; keep the grand
  total in the intro. Branded, ruled per-event headings as scan anchors.
- Preview script: answers on every scenario + a high-volume weekly stress case
  (~35 registrations across 7 events) to gauge readability at scale.

Refs #589

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Collapse each registration's name + email + chosen session onto one row
(bold name · email · session) instead of stacking name and contact on
separate lines — saves a row per registration, tightening high-volume digests.
Answers unchanged; plain-text was already single-line.

Refs #589

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The bare per-registration date (the registrant's startingAt session) read
ambiguously — it could be mistaken for a signup date. Prefix it with
"Attending" in both the HTML and plain-text parts so it's self-explanatory,
and its absence reads correctly as "no specific session chosen".

Refs #589

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The digest session date now uses a short form ("5 Aug 2025" via new
formatShortDate) instead of the long "Tuesday, 5 August 2025", tightening each
registration's identity line. formatShortDate wraps the existing private short
formatter and is exported alongside formatLongDate.

Refs #589

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Preview stress generator: fold the parallel event-title + count arrays into one
array of { title, count } objects (removes the index-sync risk).

Refs #589

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@railway-app
railway-app Bot temporarily deployed to sahajcloud / SahajCloud-pr-596 July 21, 2026 18:00 Destroyed
@Ardnived
Ardnived merged commit 01dfe8e into main Jul 22, 2026
2 checks passed
@Ardnived
Ardnived deleted the feat/registration-notifications-scheduled branch July 22, 2026 18:00

This branch was successfully deployed

No deployments
sahajcloud / SahajCloud-pr-596 — 74089df8 Deployed Jul 21, 2026 by railway-app[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(emails): scheduled registration digests + session reminders

1 participant