2.6 KiB
2.6 KiB
Link trucks to a site
Date: 2026-10-15. Status: draft for review.
Problem
Trucks are a flat list. The Report an Incident form shows every active truck regardless of site. Admin cannot say which site a truck belongs to.
Decisions
- A truck links to one site (optional).
- The report form filters the Truck dropdown by the selected site.
- A truck with no site (all existing trucks) shows for every site until an admin assigns one.
1. Schema (manual, needs owner go-ahead)
ALTER TABLE trucks ADD COLUMN IF NOT EXISTS site_id UUID REFERENCES sites(id);
CREATE INDEX IF NOT EXISTS trucks_site_id_idx ON trucks(site_id);
- Add
db/ops/2026-10-15-trucks-site-id.sql(wrapped in BEGIN/COMMIT), updatedb/schema.sqlandlib/db/schema.ts(siteId: uuid('site_id').references(() => sites.id)). - No RLS change:
trucks_read(all) andtrucks_admin_allalready cover the column.
2. Admin, Trucks tab
app/(protected)/admin/page.tsxselectstrucks.siteId;TruckManagerreceivessite_idper truck and the list of active sites.- Add form: optional Site select. Each row shows the site name, or "Unassigned". Edit modal: Site select with a "No site" option.
POST /api/admin/trucksandPATCH /api/admin/trucksacceptsite_id(uuid or null). Non-null must reference an existing active site, otherwise 422. Audit log entry stays in the same transaction and records old and newsite_id.
3. Report form
app/report/page.tsxpassessite_idwith each truck.- New pure helper
trucksForSite(trucks, siteId)inlib/report/trucks.ts: returns trucks wheresite_idis null or equalssiteId; with nositeId, returns all. - The site comes from the QR zone when present, otherwise from the Site select. When the site changes and the selected truck no longer fits, the selection clears.
?truck_id=still pre-selects the truck. If no zone is given, the truck'ssite_id(when set) becomes the default site.POST /api/incidentsis unchanged and does not reject a truck/site mismatch (UI filter only).
Testing
trucksForSiteunit tests (null site, matching, other site, no site selected).- API tests for
site_idon create and edit: invalid site 422, valid site saved with audit row, null clears. - Component test: truck dropdown filters on site change and clears an invalid selection.
npm run check:designmust printdesign gates: clean.
Out of scope
Several sites per truck, bulk assignment, filtering the Incidents list by truck site.
Deploy
Apply the SQL by hand first (additive, nullable, safe for the old build), then deploy per CLAUDE.md.