unit(D4): blackout CRUD + isUnavailable(user, date)

Blackout / unavailability (ARCHITECTURE §4.7 / §5.3 rule 5 / §5.4):
- src/domain/availability.ts — pure isUnavailable(records, date); a record
  covers the date if active and its inclusive range [starts_on, ends_on]
  contains it OR its rrule matches. No DB imports.
- src/domain/availability.test.ts — 11 tests named after the rule, incl.
  the inclusive-end case (2026-11-21..2026-11-28 covers 11-28).
- src/db/schema.ts — unavailability table: CHECK ends_on >= starts_on,
  CHECK span <= 365 days, unique (user_id, starts_on, ends_on) WHERE active.
- src/db/unavailability.ts — list/get/create/update/cancel (soft-delete);
  range + rrule validated on write.
- src/app/api/dev/unavailability/route.ts — GET/POST/PATCH; bad range/rrule
  -> 400, cancel flips status=cancelled.
- drizzle/0008_needy_stryfe.sql + migration applied to DB on :5433.

date columns stored as strings (mode: string) to match the YYYY-MM-DD
domain; Postgres date is mode-agnostic so the migration is unchanged.
Constraints enforced twice: validateUnavailabilityRange at the API,
chk_unavailability_* at the DB.

.env added to .gitignore (real secret was not ignored; .env.* missed the
bare .env).
This commit is contained in:
Tiel Coder
2026-11-18 11:25:43 +00:00
parent 8ee339b235
commit deb4f35fb8
10 changed files with 3012 additions and 2 deletions
+25 -1
View File
@@ -498,7 +498,31 @@ Build log for Media Team Hub. One unit per commit, top to bottom. Append after e
- **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.**
## D4 — Blackout CRUD + `isUnavailable(user, date)` — DONE 2026-11-18
**Scope (from BUILD-PLAN.md):** blackout CRUD + `isUnavailable(user, date)` pure function, ranges + rrule. Done when: `pnpm test availability` green incl. inclusive-end case. ARCHITECTURE §4.7 / §5.3 rule 5 / §5.4.
**Delivered:**
- `src/domain/availability.ts` — pure `isUnavailable(records, date)` + `overlapsRange(date, startsOn, endsOn)` + `recordCovers(record, date)`. A record covers the date if it is `active` and either its inclusive range `[starts_on, ends_on]` contains the date (same day = one day) or its `rrule` matches. No DB imports.
- `src/domain/availability.test.ts` — 11 tests named after the rule: inclusive-end case (2026-11-21..2026-11-28 covers 11-28), inclusive-start, day-before/day-after excluded, same-day blackout, active range covers, cancelled never covers, recurring rule matches its weekday, intervening Sunday excluded (2-week interval), malformed rrule neither throws nor matches, empty set false.
- `src/db/schema.ts` — `unavailability` table (ARCHITECTURE §4.7): `id, user_id (FK users, cascade), kind (enum), starts_on, ends_on (date), all_day (default true), reason (nullable), rrule (nullable), status (enum active|cancelled), created_at, created_by`. CHECK `ends_on >= starts_on`, CHECK `(ends_on - starts_on) <= 365`, unique index `(user_id, starts_on, ends_on) WHERE status='active'`, index on `user_id`.
- `src/db/unavailability.ts` — `listUnavailability({ userId })`, `getUnavailability`, `createUnavailability`, `updateUnavailability`, `cancelUnavailability` (soft-delete → `status='cancelled'`). Range validated on write (`validateUnavailabilityRange`); `rrule` validated via `validateRecurrence`.
- `src/app/api/dev/unavailability/route.ts` — GET (list, `userId` filter), POST (create, 400 on bad range/rrule), PATCH (edit or `status:'cancelled'` cancel). Constraint errors mapped to 400.
**Schema change + migration:** `drizzle/0008_needy_stryfe.sql` creates the `unavailability` table. Applied to the DB on :5433.
**Verification (all exit 0):**
- `tsc --noEmit` ✓ · `eslint .` ✓ (0 problems) · `vitest run` ✓ (**140** total; D4 = 11)
- Runtime smoke test (running app on :3000): POST valid blackout → 201; POST span > 365 → 400 "span must not exceed 365 days"; POST ends < starts → 400 "ends_on must not precede starts_on"; GET list; PATCH cancel → 200 with `status='cancelled'` (row persists). CHECK constraints enforced at the DB layer too (span > 365 and ends < starts rejected by `chk_unavailability_*`). Test rows cleaned up.
**Decisions:**
- **Inclusive on both ends, by design.** A blackout 2026-11-21..2026-11-28 covers 11-28 (the end date), matching the ARCHITECTURE ("inclusive; same day = one day"). `overlapsRange` uses `>=` / `<=` on ISO strings (lexicographic order == chronological for YYYY-MM-DD), so no date parsing is needed.
- **`date` columns stored as strings (`mode: "string"`).** The whole recurrence/availability domain works in `YYYY-MM-DD` strings; storing the range as strings avoids Date↔string conversion at the DB boundary. Postgres `date` is mode-agnostic, so the migration is identical to a `mode: "date"` column.
- **Range and rrule are mutually exclusive coverage paths.** A record covers a date if it is `active` AND (range contains the date OR rrule matches it). A recurring blackout uses the rrule; a one-time blackout uses the range. `parseRecurrence` throws on malformed input, so `isUnavailable` guards with `validateRecurrence` and never throws.
- **Cancel, never delete.** `cancelUnavailability` flips `status='cancelled'`; the row persists so historical assignment conflicts stay explainable (rule 5 in §5.3 warns, doesn't hard-block). `isUnavailable` skips non-`active` records, so a cancelled blackout no longer excludes the volunteer.
- **Constraints enforced in two layers.** The pure `validateUnavailabilityRange` rejects bad ranges at the API boundary (400 with a clear message); the DB `chk_unavailability_*` CHECK constraints are the backstop so a bad range can never reach the table.
**Next: D5 — conflict engine (pure function, hard vs soft conflicts).**
---