feat(trucks): site_id column and site/truck helpers

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-15 11:44:37 +08:00
co-authored by Claude Sonnet 5.5
parent fd7a55337f
commit 867b725262
9 changed files with 105 additions and 1 deletions
+5
View File
@@ -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;
+3
View File
@@ -101,6 +101,9 @@ CREATE TABLE trucks (
created_at TIMESTAMPTZ NOT NULL DEFAULT now() 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 -- incidents: final column set includes all columns added in later migrations
CREATE TABLE incidents ( CREATE TABLE incidents (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
+11
View File
@@ -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 }
}
+1
View File
@@ -103,6 +103,7 @@ export const trucks = pgTable('trucks', {
id: uuid('id').primaryKey().defaultRandom(), id: uuid('id').primaryKey().defaultRandom(),
truckNo: text('truck_no').notNull().unique(), truckNo: text('truck_no').notNull().unique(),
carrier: text('carrier'), carrier: text('carrier'),
siteId: uuid('site_id').references(() => sites.id),
active: boolean('active').notNull().default(true), active: boolean('active').notNull().default(true),
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(), createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow(),
}) })
+12
View File
@@ -0,0 +1,12 @@
export function trucksForSite<T extends { site_id: string | null }>(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
}
+5
View File
@@ -31,3 +31,8 @@ export function resolveZoneToken(args: {
} }
return null 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 ?? ''
}
+21
View File
@@ -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 })
})
})
+32
View File
@@ -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()
})
})
+15 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest' 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[] = [ const rows: ZoneRow[] = [
{ siteId: 's1', siteName: 'SC1', zoneId: 'z1', zoneName: 'Dock A', token: 'tok-a' }, { 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() 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('')
})
})