diff --git a/db/ops/2026-10-15-trucks-site-id.sql b/db/ops/2026-10-15-trucks-site-id.sql new file mode 100644 index 0000000..b0b5938 --- /dev/null +++ b/db/ops/2026-10-15-trucks-site-id.sql @@ -0,0 +1,5 @@ +-- Apply by hand on the server DB with the owner's go-ahead. Not run by deploy. +BEGIN; +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); +COMMIT; diff --git a/db/schema.sql b/db/schema.sql index eb02d0f..8391254 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -101,6 +101,9 @@ CREATE TABLE trucks ( created_at TIMESTAMPTZ NOT NULL DEFAULT now() ); +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); + -- incidents: final column set includes all columns added in later migrations CREATE TABLE incidents ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), diff --git a/lib/admin/truck-input.ts b/lib/admin/truck-input.ts new file mode 100644 index 0000000..e7d0a62 --- /dev/null +++ b/lib/admin/truck-input.ts @@ -0,0 +1,11 @@ +const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i + +export type SiteIdInput = { ok: true; value: string | null | undefined } | { ok: false } + +// undefined = field not sent, null/'' = clear the link, uuid string = set it. +export function parseSiteId(raw: unknown): SiteIdInput { + if (raw === undefined) return { ok: true, value: undefined } + if (raw === null || raw === '') return { ok: true, value: null } + if (typeof raw === 'string' && UUID_RE.test(raw)) return { ok: true, value: raw } + return { ok: false } +} diff --git a/lib/db/schema.ts b/lib/db/schema.ts index 319800a..445437a 100644 --- a/lib/db/schema.ts +++ b/lib/db/schema.ts @@ -103,6 +103,7 @@ export const trucks = pgTable('trucks', { id: uuid('id').primaryKey().defaultRandom(), truckNo: text('truck_no').notNull().unique(), carrier: text('carrier'), + siteId: uuid('site_id').references(() => sites.id), active: boolean('active').notNull().default(true), createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(), }) diff --git a/lib/report/trucks.ts b/lib/report/trucks.ts new file mode 100644 index 0000000..caffcae --- /dev/null +++ b/lib/report/trucks.ts @@ -0,0 +1,12 @@ +export function trucksForSite(trucks: T[], siteId: string): T[] { + if (!siteId) return trucks + return trucks.filter(t => t.site_id === null || t.site_id === siteId) +} + +export function siteForTruck( + trucks: { id: string; site_id: string | null }[], + truckId: string | null | undefined, +): string | null { + if (!truckId) return null + return trucks.find(t => t.id === truckId)?.site_id ?? null +} diff --git a/lib/report/zones.ts b/lib/report/zones.ts index ec9ac51..e6f2e40 100644 --- a/lib/report/zones.ts +++ b/lib/report/zones.ts @@ -31,3 +31,8 @@ export function resolveZoneToken(args: { } return null } + +export function siteIdForToken(sites: SiteOption[], token: string | null): string { + if (!token) return '' + return sites.find(s => s.zones.some(z => z.qr_code_token === token))?.id ?? '' +} diff --git a/tests/lib/admin/truck-input.test.ts b/tests/lib/admin/truck-input.test.ts new file mode 100644 index 0000000..6bf8505 --- /dev/null +++ b/tests/lib/admin/truck-input.test.ts @@ -0,0 +1,21 @@ +import { describe, it, expect } from 'vitest' +import { parseSiteId } from '@/lib/admin/truck-input' + +const UUID = '3f2b8c1e-9d4a-4e6f-8a1b-2c3d4e5f6a7b' + +describe('parseSiteId', () => { + it('treats undefined as not provided', () => { + expect(parseSiteId(undefined)).toEqual({ ok: true, value: undefined }) + }) + it('treats null and empty string as clear', () => { + expect(parseSiteId(null)).toEqual({ ok: true, value: null }) + expect(parseSiteId('')).toEqual({ ok: true, value: null }) + }) + it('accepts a uuid', () => { + expect(parseSiteId(UUID)).toEqual({ ok: true, value: UUID }) + }) + it('rejects anything else', () => { + expect(parseSiteId('abc')).toEqual({ ok: false }) + expect(parseSiteId(12)).toEqual({ ok: false }) + }) +}) diff --git a/tests/lib/report/trucks.test.ts b/tests/lib/report/trucks.test.ts new file mode 100644 index 0000000..7b9e609 --- /dev/null +++ b/tests/lib/report/trucks.test.ts @@ -0,0 +1,32 @@ +import { describe, it, expect } from 'vitest' +import { trucksForSite, siteForTruck } from '@/lib/report/trucks' + +const trucks = [ + { id: 't1', site_id: 'A' }, + { id: 't2', site_id: 'B' }, + { id: 't3', site_id: null }, +] + +describe('trucksForSite', () => { + it('returns matching and unassigned trucks', () => { + expect(trucksForSite(trucks, 'A').map(t => t.id)).toEqual(['t1', 't3']) + expect(trucksForSite(trucks, 'B').map(t => t.id)).toEqual(['t2', 't3']) + }) + it('returns all trucks when no site is selected', () => { + expect(trucksForSite(trucks, '').map(t => t.id)).toEqual(['t1', 't2', 't3']) + }) + it('returns only unassigned trucks for a site with none linked', () => { + expect(trucksForSite(trucks, 'C').map(t => t.id)).toEqual(['t3']) + }) +}) + +describe('siteForTruck', () => { + it('returns the truck site', () => { + expect(siteForTruck(trucks, 't2')).toBe('B') + }) + it('returns null for unassigned, unknown or missing truck', () => { + expect(siteForTruck(trucks, 't3')).toBeNull() + expect(siteForTruck(trucks, 'nope')).toBeNull() + expect(siteForTruck(trucks, null)).toBeNull() + }) +}) diff --git a/tests/lib/report/zones.test.ts b/tests/lib/report/zones.test.ts index 5d43e09..173aa40 100644 --- a/tests/lib/report/zones.test.ts +++ b/tests/lib/report/zones.test.ts @@ -1,5 +1,5 @@ import { describe, it, expect } from 'vitest' -import { groupSites, zonesForSite, resolveZoneToken, type ZoneRow } from '@/lib/report/zones' +import { groupSites, zonesForSite, resolveZoneToken, siteIdForToken, type ZoneRow } from '@/lib/report/zones' const rows: ZoneRow[] = [ { siteId: 's1', siteName: 'SC1', zoneId: 'z1', zoneName: 'Dock A', token: 'tok-a' }, @@ -44,3 +44,17 @@ describe('resolveZoneToken', () => { expect(resolveZoneToken({ zoneToken: null, sites, zoneId: 'zz' })).toBeNull() }) }) + +describe('siteIdForToken', () => { + const sites = [ + { id: 'A', name: 'Site A', zones: [{ id: 'z1', name: 'Dock', qr_code_token: 'tok-1' }] }, + { id: 'B', name: 'Site B', zones: [{ id: 'z2', name: 'Yard', qr_code_token: 'tok-2' }] }, + ] + it('finds the site that owns the token', () => { + expect(siteIdForToken(sites, 'tok-2')).toBe('B') + }) + it('returns empty string for null or unknown token', () => { + expect(siteIdForToken(sites, null)).toBe('') + expect(siteIdForToken(sites, 'nope')).toBe('') + }) +})