unit(D1): service CRUD + expandRRULE()

Service template CRUD (create/read/update/deactivate) and the pure
expandRRULE() recurrence expander for the RFC 5545 subset (WEEKLY,
BIWEEKLY via INTERVAL=2, MONTHLY nth weekday).

- src/domain/recurrence.ts: expandRRULE, parseRecurrence,
  matchesRecurrence, validateRecurrence. Pure — no DB imports.
- src/domain/recurrence.test.ts: 13 tests, incl. the 5 required
  recurrence cases (spring-forward / fall-back Sunday, monthly-nth
  across a year boundary, cancelled instance not regenerated,
  biweekly phase stable).
- src/db/services.ts + src/app/api/dev/services/route.ts: list/get/
  create/update/deactivate; rrule validated on write; deactivate
  flips active (never delete); 409 on duplicate name.

No schema change (services table predates this unit). DST stays in
the caller's localToUtc; interval phase anchored to a fixed epoch so
biweekly never drifts.
This commit is contained in:
Tiel Coder
2026-11-18 05:07:42 +00:00
parent 995b88e5ec
commit 6a778aa2f6
5 changed files with 633 additions and 0 deletions
+24
View File
@@ -445,3 +445,27 @@ Build log for Media Team Hub. One unit per commit, top to bottom. Append after e
- **Removed `.freebuff/`.** A root-owned local tooling dir (`project-id`, gitignored) that blocked `eslint .` (the `pnpm lint` DoD gate) — `drizzle`/ESLint could not scandir it. It held only a local project id; removed with `sudo rm -rf` and its `.git/info/exclude` entry cleared, restoring a clean `eslint .`.
**Next: D1 — service CRUD + `expandRRULE()` (weekly / biweekly / monthly-nth).**
---
## 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.
**Delivered:**
- `src/domain/recurrence.ts` — `expandRRULE(rrule, from, to, { skipDates })` returns the local dates a rule lands on in a window. Parses the RFC 5545 subset: `FREQ=WEEKLY` (with `INTERVAL=n`, so `INTERVAL=2` = biweekly), `FREQ=MONTHLY;BYDAY=<n><DAY>` (nth weekday, incl. `-1` = last) and `BYMONTH`. `parseRecurrence`, `matchesRecurrence`, `validateRecurrence`, `RecurrenceError`. Pure — no DB imports.
- `src/domain/recurrence.test.ts` — 13 tests named after the rule. The 5 required cases: spring-forward Sunday (9 AM local stays 9 AM; stored UTC shifts EST→EDT), fall-back Sunday (UTC shifts back), monthly-nth across Dec→Jan (1st Sunday), cancelled instance not regenerated (skip date excluded), biweekly phase stable (constant 14-day gap). Plus INTERVAL=3, last-weekday, parse/validate.
- `src/db/services.ts` — `listServices({ active })`, `getService`, `createService`, `updateService`, `deactivateService`. `rrule` validated on write; deactivate flips `active` (never delete); `name` frozen on update.
- `src/app/api/dev/services/route.ts` — GET (list, `active` filter), POST (create, 409 on duplicate name), PATCH (edit fields or `active:false` deactivate). Constraint errors mapped to 400/409 like the catalog route.
**No schema change, no migration.** The `services` table already exists (A2) with all fields (`rrule`, `startTime_local`, `duration_minutes`, `doors_open_minutes_before`, `active`, `notes`). D1 owns the template CRUD; `service_role_requirements` stays deferred to a later unit.
**Verification (all exit 0):**
- `tsc --noEmit` ✓ · `eslint .` ✓ · `vitest run src/domain/recurrence.test.ts` ✓ (**13**) · `next build` ✓ · runtime smoke test: GET/POST/PATCH all correct, DB persistence confirmed (deactivate flips `active=false`, duration update persisted, duplicate name → 409, invalid rrule → 400).
**Decisions:**
- **`expandRRULE` returns local dates; DST lives in the caller.** The function decides the calendar date only; the instance generator (D2) converts each with `localToUtc`, which resolves the offset for the actual instant — so a 9 AM Sunday stays 9 AM across spring-forward / fall-back. The DST tests assert the stored UTC hour shifts by an hour across the transition.
- **Interval phase anchored to 1970-01-04 (a Sunday).** Without a DTSTART, biweekly/triweekly phase is computed against a fixed epoch, so the phase never drifts week-to-week regardless of the window start.
- **nth weekday parsed from the BYDAY token.** `1SU` / `-1WE` carry the position in the token (`ByDayRule.weekday` + `.nth`); `BYSETPOS` was dropped as redundant — the app's "1st Sunday" maps to `BYDAY=1SU`.
- **`rrule` validated on write.** A malformed rule can never reach the generator; `createService`/`updateService` call `validateRecurrence` and reject with a 400 message before touching the DB.
- **Build cache reclaimed.** A prior interrupted `next build` left root-owned files under `.next/` (same class as the earlier `.git/objects` scrub) that blocked rebuilds; `sudo chown -R bhursey:bhursey .next` restored the build. The stale `next start` on :3000 was restarted after the build so the new route loaded.