Skip to content

refactor(events): collapse duplicated encodings across the verification lifecycle - #638

Merged
Ardnived merged 4 commits into
feat/event-feedbackfrom
refactor/verification-simplification
Aug 19, 2026
Merged

Ardnived merged 4 commits into
feat/event-feedbackfrom
refactor/verification-simplification

Conversation

@Ardnived

Copy link
Copy Markdown
Contributor

Follow-up to #624→#626: a simplification pass over the verification lifecycle those PRs touched. No behaviour changes except where noted — the point is to collapse places where one fact was encoded twice and could drift.

Stacked on feat/event-feedback (#626), so it needs the stack to merge first.

What changed

One relationId. There were two implementations; 7 of 10 call sites imported the one that can't parse a string id. The complete one now lives in lib/utilities/relationId and everything points at it. Latent bug: in syncVerificationOnSave a manager id that arrived as a string read as "no manager", i.e. pre-adoption.

One attention level per stage. The sidebar bucket, the admin notice severity and the quality-check skip each kept their own per-stage table. Every stage now declares an attention (ok / due / urgent / unpublished / ended) and the three consumers derive from it. bucketForEvent's default: case — which had silently labelled the new unverified/denied stages "verified" until #624 patched it — is gone, and the tables that remain are exhaustive Records, so a new stage is a compile error rather than a wrong colour.

One HMAC token module. eventVerification/token, registrations/unsubscribeToken and the generic utilities/signedToken from #625 were three copies of the same twenty lines with subtly different validity checks. The two older ones are now thin wrappers supplying their claims, TTL and kind.

This closes a real gap rather than just deduplicating: all four link kinds are signed with the same Payload secret, and only the envelope's kind discriminator — which the two older modules lacked — stops a token issued for one link being replayed against another. Pinned by a test. signToken gains ttlMs: null for the unsubscribe link, which may legitimately be clicked months into a course and authorizes a narrow idempotent action.

⚠️ Verify / unsubscribe links in already-sent emails stop validating, since the envelope shape changed. Accepted: affected events are re-notified on the job's next run.

notificationLog gets a JSON Schema. It was a bare array with a hand-written narrower — Array.isArray(value) ? value as Entry[] : [] — which is a cast, not a check, so a malformed entry landed in the column and every reader had to hope. Payload now generates Event['notificationLog'] from the schema and rejects bad writes with a ValidationError. Stored shape is unchanged (still a bare array), so there is no data migration.

This supersedes #618, now closed — same approach (array root, entry union keyed on kind), plus enums derived from VERIFICATION_STAGES and a completeList helper that makes an incomplete restatement of the two email-template unions a compile error. Those imports are type-only, so no react-email reaches the admin client bundle. Registrations.reminderLog is the same pattern and deliberately has not followed; its comments now say so instead of claiming a mirror that no longer holds.

Smaller things. Deleted the eventVerification barrel (no importers) and the UNMANAGED_STAGES / daysUntilUnpublish exports nothing outside the module read. DAY_MS declared once instead of inline in nine modules. addMonths clamped — 31 Aug + 6 months was rolling into 3 March, so a finished event's retention ran a few days long.

Payload CLI wiring

Separate commit, prompted by "these commands just exit without feedback". db:migrate, db:migrations:create and the payload passthrough are removed — pnpm already resolves payload from node_modules/.bin, so three names for two commands bought nothing and made the wrapper look like the suspect whenever a command failed. Everything is pnpm payload <command>; docs, skills and the permission allowlist swept to match.

The wrappers were never the cause. Three real ones, each traced to library source and written up in .claude/rules/migrations.md:

  1. Dev email calls ethereal.email on every CLI invocation. nodemailerAdapter({}) with no transportOptions means createTestAccount() — a live HTTPS request — runs before any command does work. When it fails the adapter throws, and payload's bin.js runs the CLI under a bare void start() with no .catch(), so the process dies with exit 0 and no output. pnpm payload migrate:status alternates between a full table and zero lines on successive runs. Documented, not fixed — removing the network call costs the ethereal preview URL, so it needs a decision.
  2. generate:types silence is success. It diffs against the file on disk and returns early when identical, before the "Types written" log.
  3. A desynced src/migrations/index.ts breaks every payload command — the config imports it for prodMigrations and it statically imports all 44 files. The ERR_MODULE_NOT_FOUND is at the top of a long trace, so it scrolls away and vanishes entirely when piped through head/tail.

Verification

lint, typecheck, typecheck:tests, 1182 unit tests, and 67 integration tests across event-verification, expire-events, atlas-sidebar and event-submissions — all green.

Four assertions here could have passed vacuously, so each was flipped once to confirm it fails: the addMonths clamp (2027-03-03 vs 2027-02-28), the completeList exhaustiveness guard (dropping urgent is a compile error), the schema/hand-written-type drift guard, and the schema validation itself — an integration case asserts a missing required key, an undeclared key and an out-of-enum value are each rejected.

🤖 Generated with Claude Code

Ardnived and others added 4 commits August 14, 2026 09:02
Five simplifications across the verification lifecycle, all removing a
second encoding of a fact the stage table already knows:

- `relationId` had two implementations; 7 of 10 call sites imported the
  one that can't parse a string id. Consolidated onto the complete one.
- Every stage now declares an `attention` level. The sidebar bucket, the
  admin notice severity and the quality-check skip derive from it instead
  of each keeping their own per-stage table; `bucketForEvent`'s `default:`
  case (which silently labelled unfamiliar stages "verified") is gone,
  and the three tables that remain are exhaustive Records.
- Deleted the `eventVerification` barrel (no importers) and the
  `UNMANAGED_STAGES` / `daysUntilUnpublish` exports nothing outside the
  module read.
- `DAY_MS` is declared once in `lib/utilities/time` rather than inline in
  nine modules.
- `addMonths` clamped: 31 Aug + 6 months was rolling into 3 March, so a
  finished event's retention ran a few days long.

Co-Authored-By: Claude Opus 5 <[email protected]>
There were three near-identical hand-rolled HMAC token modules —
eventVerification/token, registrations/unsubscribeToken, and the generic
utilities/signedToken written for #625 — with the same twenty lines of
crypto and subtly different validity checks. The two older ones are now
thin wrappers that supply their claims, TTL and `kind`.

Beyond the duplication, this closes a real gap: all four link kinds are
signed with the same Payload secret, and only the envelope's `kind`
discriminator (which the two older modules lacked) stops a token issued
for one link from being replayed against another. Pinned by a test.

`signToken` gains `ttlMs: null` for never-expiring tokens, which is what
the unsubscribe link needs — it may legitimately be clicked months into
a course, and the action it authorizes is narrow and idempotent.

Existing verify / unsubscribe links in already-sent emails stop
validating, since the envelope shape changed. Accepted: the events being
reminded about are re-notified on the job's next run.

Co-Authored-By: Claude Opus 5 <[email protected]>
`notificationLog` was a bare JSON array with a hand-written narrower
(`Array.isArray(value) ? value as Entry[] : []`) — a cast, not a check,
so a malformed entry landed in the column and every reader had to hope.

It now carries a JSON Schema, which Payload uses to generate
`Event['notificationLog']` AND to reject a bad write with a
ValidationError. The stored shape is unchanged (still a bare array), so
no data migration is needed. Adopted from the approach in #618, which
this supersedes.

The schema's enums are derived from VERIFICATION_STAGES and, for the two
unions declared in an email template, from a `completeList` helper that
makes an incomplete restatement a compile error — the types are imported
type-only, so no react-email code reaches the admin client bundle. A pair
of assignments in the unit spec fails to compile if the schema and the
hand-written entry types drift apart; both guards were verified by
mutation, as was the validation itself (an int case asserts a missing
key, an undeclared key and an out-of-enum value are all rejected).

Registrations.reminderLog is the same pattern and has NOT followed — its
comments now say so rather than claiming a mirror that no longer holds.

Co-Authored-By: Claude Opus 5 <[email protected]>
… go quiet

`db:migrate` and `db:migrations:create` were aliases for `payload migrate`
and `payload migrate:create`, and `payload` was a passthrough to a binary
pnpm already resolves from node_modules/.bin. Three names for two
commands meant docs used them interchangeably, and whenever a command
failed the wrapper looked like the suspect. All removed; everything is
`pnpm payload <command>`.

The wrappers were never the problem. Three real causes, each traced to
library source and now written up in .claude/rules/migrations.md:

- Dev email calls `nodemailer.createTestAccount()` — a live request to
  ethereal.email — on every config load, so on every CLI invocation. When
  it fails the adapter throws, and payload's `bin.js` runs the CLI under a
  bare `void start()` with no `.catch()`, so the process dies with exit 0
  and NO output. This is the intermittent one: `pnpm payload
  migrate:status` alternates between a full table and zero lines on
  successive runs. Documented, not yet fixed — killing the network call
  costs the ethereal preview URL, so it needs a decision first.

- `generate:types` logs "Compiling TS types…" then returns early and
  silently when the compiled output matches the file on disk. Silence
  means "no schema change", not a hang.

- A `src/migrations/index.ts` referencing a missing migration file breaks
  EVERY payload command, because payload.config.ts imports the index for
  `prodMigrations` and the index statically imports all 44 files. The
  ERR_MODULE_NOT_FOUND sits at the top of a long stack trace, so it
  scrolls away — and vanishes entirely when piped through head/tail.

Co-Authored-By: Claude Opus 5 <[email protected]>
@railway-app

railway-app Bot commented Aug 14, 2026 •

Copy link
Copy Markdown

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

Service Status Web Updated (UTC)
SahajCloud ✅ Success (View Logs) Web Aug 14, 2026 at 10:38 pm

@railway-app
railway-app Bot temporarily deployed to sahajcloud / SahajCloud-pr-638 August 14, 2026 22:36 Destroyed
@Ardnived
Ardnived merged commit 5868bb5 into feat/event-feedback Aug 19, 2026
2 checks passed

This branch was successfully deployed

No deployments
sahajcloud / SahajCloud-pr-638 — c79e187d Deployed Aug 14, 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.

1 participant