Files
glenloch-media/docs/superpowers/specs/2026-11-18-d8-assign-mutations-design.md
T
Tiel Coder d0a696167f fix(D8): grid slots render, per-cell optimistic revert, shared E2E helpers, admin reset endpoint
- src/db/grid.ts: key role map by ID (was slug, causing all cells to show '—')
- src/components/app/schedule-grid-editor.tsx: revert only failed cell, not entire grid
- src/db/audit.ts: fix import path ./db → @/db
- src/db/seed.ts: add resetAndSeed(), assign Audio only (leave Livestream/Slides open)
- src/auth/config.ts: carry id/email on JWT for audit actor
- e2e/dev.ts: shared signInAs + resetDb helpers
- e2e/schedule-grid.spec.ts: use shared helpers, reset DB beforeAll
- e2e/assign-mutations.spec.ts: E2E for assign/clear with optimistic update + rollback
- src/app/api/dev/reset/route.ts: admin-only DB reset endpoint
- playwright.config.ts: workers: 1 (serial, suites share one Postgres)
2026-11-18 21:51:04 +00:00

57 lines
4.5 KiB
Markdown

# D8 — Assign / unassign / clear with optimistic update + rollback toast
**Date:** 2026-11-18
**Phase:** D (Scheduling). Depends on D6 (ranking) + D7 (grid). Feeds D9 (auto-fill) + D10 (publish).
**Size:** M. **Done-when (BUILD-PLAN):** E2E — assign appears immediately; forced failure rolls back with a message; no console errors.
## Intent and why
D7 made the schedule grid **read-only**. D8 turns it into a working scheduling surface: coordinators assign, unassign, and clear who fills each slot, with instant optimistic feedback and a clear rollback when the write is rejected. The data layer already provides the pieces — D6 ranks who can fill a slot, D7 renders the week. D8 writes the `assignments`.
## Goals
- **Assign** a volunteer to a slot: the cell updates instantly (optimistic), the write goes through a REST route, an audit row is written.
- **Unassign / clear**: remove an assignment; the slot returns to `open`.
- **Rollback**: if the write is rejected (a conflict), the cell reverts and a toast shows the reason.
- **Done-when E2E**: assign appears without a full reload; a forced failure (a duplicate-instance assignment) rolls back with a message; no console/page errors.
## Non-goals
- No ranking/picker changes — D6 owns who ranks high.
- No notifications — D10/E1 own that. D8 assigns; it does not tell anyone.
- No bulk-assign UI polish beyond what the done-when needs (the route supports it, the UI can stay single-cell for D8).
## Anti-goals (what would make this a failure even if it "works")
- A **silent delete** when an assignment conflicts (BUILD-SPEC rule 4: never silently drop).
- A full page reload on assign (defeats "optimistic").
- A hard-rule violation slipping through because the toast hid it.
## Constraints
- **Rule 12** — every mutation writes an `audit_log` row. Build the audit table first.
- **Rule 2** — one active assignment per slot is DB-enforced (`uq_active_assignment` on `slot_id` where status IN proposed/confirmed/completed). Already present.
- **Rule 1** — a volunteer cannot hold two active assignments in the same service instance. App-layer check before insert.
- Domain logic lives in `src/domain/**` and never touches the DB from routes/components (ARCHITECTURE §5).
- REST convention: `POST /api/...` with zod validation; routes return JSON.
- **Container**: the API route ships in the Next build; the `audit_log` migration is created with `pnpm db:generate`, committed to `drizzle/`, and applied by `pnpm db:migrate` in the `migrate` container. No extra infra.
## Approaches
- **Client component + REST route + optimistic state** — *chosen*. A thin client wrapper calls `POST /api/assignments`, updates the cell locally before the response, and rolls back on error with a toast. Matches the project's REST convention and makes the done-when directly testable in Playwright.
- Server actions + `useOptimistic` — rejected: diverges from the `/api/...` convention the E2E targets.
- Pure server re-render on mutation — rejected: not truly optimistic (full round-trip flash).
## Decided (their calls)
- REST API approach for the optimistic layer.
## Calls made by the builder (override if you disagree)
- **`audit_log` first**: new table + migration + a small `writeAudit(db, {actor, action, entityType, entityId, before, after})` helper. Append-only; no app update/delete.
- **One route, one action field**: `POST /api/assignments` with `{ action: "assign" | "clear", slotId, userId? }`. Assign creates `status="proposed", source="manual"` (awaiting the volunteer confirm flow); clear deletes the active row (history preserved).
- **API enforces rule 1 + rule 2 + audit.** Skill gating (rules 6/13 — trainee can't fill a `qualified` slot, core-skill-below-required blocks solo) stays on the picker/ranking surface, not the assign API — the coordinator picks from the D6-ranked list. *(See open question below.)*
- **Toast**: a minimal inline toast (no new library).
- **Actor**: the authenticated user's id (coordinator/admin) is the audit `actor_id`; the route guards to coordinator+ roles.
## What's left to the builder
- Optimistic state wiring on the grid cell + rollback on non-2xx.
- The toast.
- The E2E (assign appears; forced duplicate rolls back; no console errors).
- `audit_log` table + migration + helper.
## Open question (needs a yes/no)
Should the **assign API itself** enforce skill gating (rules 6/13), or only the duplicate rules (1/2)? Keeping it to 1/2 keeps D8 focused and puts gating in the picker; enforcing 6/13 in the API makes a manual mis-assign hard-fail. This changes how much domain logic the route carries.