unit(D2): idempotent instance generator (16-week window) on cron route

Service instance generation (ARCHITECTURE §5.1): for each active
service, plan instances in the rolling window and upsert on
(service_id, starts_at). onConflictDoNothing makes the whole thing
idempotent — a re-run creates 0 rows, cancelled/published instances
are never touched, past instances are never deleted.

- src/domain/instances.ts: pure planInstances() + windowTarget();
  DST-safe instants via localToUtc, no DB imports.
- src/domain/instances.test.ts: 7 tests (window boundary, DST-safe
  instants, determinism, biweekly phase, monthly-nth, windowTarget).
- src/db/instances.ts: generateInstances() idempotent upsert;
  occurs_on stored as zoned midnight (no UTC-midnight shift).
- src/app/api/dev/cron/instances/route.ts: GET drives the daily job.

No schema change (service_instances unique index predates this unit;
slot materialization stays deferred to D3). Verified: run twice ->
0 created, count unchanged; published instances byte-identical;
generated instances stored as DST-safe UTC instants.
This commit is contained in:
Tiel Coder
2026-11-18 05:17:11 +00:00
parent 6a778aa2f6
commit 8fb6459b0d
5 changed files with 306 additions and 0 deletions
+15
View File
@@ -0,0 +1,15 @@
import { generateInstances } from "@/db/instances";
/**
* Daily instance generator (ARCHITECTURE §5.1). Cron `0 6 * * *` (church-local
* 06:00, expressed in UTC) calls this. Idempotent — a re-run creates 0 rows.
*
* GET /api/dev/cron/instances
*/
export const GET = async () => {
const result = await generateInstances();
return new Response(JSON.stringify({ result }), {
status: 200,
headers: { "content-type": "application/json" },
});
};