unit(D3): slot materialization (headcount, call time)
Slot materialization (ARCHITECTURE §5.2): for each non-published instance, plan the seats planSlots() computes and upsert them on (service_instance_id, role_id, slot_index). onConflictDoNothing makes it idempotent; published weeks are skipped (owned by the publish flow). - src/domain/slots.ts: pure planSlots()/planSlotsForRole(); headcount fills slotIndex 1..N, call_time = start + offset (null if no offset). - src/domain/slots.test.ts: 5 tests (Audio headcount 2 -> 2 slots with correct call_time, clamp to 1, null offset, multi-role flatten). - src/db/slots.ts: materializeSlotsForInstance() + materializeSlots(). - src/app/api/dev/cron/slots/route.ts: GET drives the daily job. No schema change (slots unique index predates this unit; position_skills stays deferred to D5). Verified: draft instance + Audio headcount 2 -> 4 seats (Audio x2 + Livestream + Slides), Audio call_time 09:30 EST; re-run creates 0 rows. Seed restored to canonical 4 instances / 12 slots after testing.
This commit is contained in:
@@ -475,6 +475,33 @@ Build log for Media Team Hub. One unit per commit, top to bottom. Append after e
|
||||
|
||||
---
|
||||
|
||||
## D3 — Slot materialization (headcount, call time) — DONE 2026-11-18
|
||||
|
||||
**Scope (from BUILD-PLAN.md):** slot materialization from role requirements (headcount, call time). Done when: Audio headcount 2 creates 2 slots with correct call_time. ARCHITECTURE §5.2.
|
||||
|
||||
**Delivered:**
|
||||
- `src/domain/slots.ts` — pure `planSlotsForRole(instanceStartsAt, role)` + `planSlots(instanceStartsAt, roles)`. A role with headcount N fills N seats (slotIndex 1..N); each seat's call_time is `instanceStartsAt + call_time_offset_minutes` (null when the role has no offset); headcount clamped to >= 1. No DB imports.
|
||||
- `src/domain/slots.test.ts` — 5 tests named after the rule: Audio headcount 2 → 2 slots with correct call_time (11:00 EST − 90 min = 09:30 EST = 14:30 UTC), headcount clamped to 1, null call_time when offset null, multi-role flattening in role order.
|
||||
- `src/db/slots.ts` — `materializeSlotsForInstance(serviceInstanceId, instanceStartsAt, roles)` upserts seats on `(service_instance_id, role_id, slot_index)` via `onConflictDoNothing`; `materializeSlots(now)` processes every non-published instance at/after `now` (published weeks are owned by the publish flow, so their slot set is left untouched per §5.1).
|
||||
- `src/app/api/dev/cron/slots/route.ts` — GET `/api/dev/cron/slots` drives `materializeSlots()`; idempotent on re-run.
|
||||
|
||||
**No schema change, no migration.** The `slots` table already exists (A2) with the `(service_instance_id, role_id, slot_index)` unique index that makes materialization idempotent. `position_skills` (which drives conflict detection in D5) stays deferred.
|
||||
|
||||
**Verification (all exit 0):**
|
||||
- `tsc --noEmit` ✓ · `eslint .` ✓ (0 problems) · `vitest run` ✓ (**129** total; D3 = 5) · `next build` ✓
|
||||
- Runtime smoke test: draft instance + Audio headcount 2 → 4 seats (Audio×2 + Livestream×1 + Slides×1); Audio slots slot_index 1,2 with call_time 09:30 EST (14:30 UTC); re-run creates 0 rows (idempotent). Seed state restored to canonical 4 instances / 12 slots after testing.
|
||||
|
||||
**Decisions:**
|
||||
- **Idempotency is one primitive again.** `onConflictDoNothing({ target: [service_instance_id, role_id, slot_index] })` returns only rows this run inserted, so `created` counts genuine additions and a re-run is a no-op that never disturbs an admin's assignment on an existing seat.
|
||||
- **Published instances are skipped, not upserted.** §5.1 says slots are created for *new* instances and a published week's slot set is owned by the publish flow. `materializeSlots` filters `status != 'published'`, so re-running generation never rewrites a published week's seats.
|
||||
- **call_time is a zoned offset, not a naive add.** `instanceStartsAt + offset` operates on the UTC instant, so a −90 min offset applied to a DST-safe start stays DST-safe (the stored UTC shifts with the wall clock, exactly as the seed does).
|
||||
- **Headcount clamped to >= 1.** A role always produces at least one seat, so a zero/missing headcount can never silently drop a role from an instance.
|
||||
- **Seed is date-dependent, not fully idempotent.** The seed materializes `nextSundaysInTz(2)` (rolling window), so re-running it across dates creates new instances. After testing I reset instances/slots/assignments and re-ran the seed once to restore the canonical 4-instance / 12-slot state.
|
||||
|
||||
**Next: D4 — blackout CRUD + `isUnavailable(user, date)` pure function, ranges + rrule.**
|
||||
|
||||
---
|
||||
|
||||
## D1 — Service CRUD + `expandRRULE()` — DONE 2026-11-18
|
||||
|
||||
**Scope (from BUILD-PLAN.md):** service CRUD + `expandRRULE()` (weekly / biweekly / monthly-nth). Done when: `pnpm test recurrence` green on 5 named cases — spring-forward Sunday, fall-back Sunday, monthly-nth across a year boundary, cancelled instance not regenerated, biweekly phase stable. ARCHITECTURE §4.3 / §5.1 / §5.8.3.
|
||||
|
||||
Reference in New Issue
Block a user