feat: transport incidents with truck number support

- DB: trucks table + incidents.truck_id FK + transport enum value
- Validation: transport type requires truck_id
- Create API: validates truck exists/active, persists truck_id
- Report form: truck dropdown shown when type=transport (required)
- Admin: TruckManager CRUD + /api/admin/trucks route
- Detail: trucks join surfaced in incident-detail + detail page query
- Inbox (HSE + supervisor): truck filter, transport in TYPE_OPTIONS,
  fixed stale enum values (dropped dangerous_occurrence/mhe_asset/occupational_disease)
- List: transport label + truck number badge in rows
- i18n: transport + truckLabel/truckPlaceholder in en/ms/zh

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CPf5Rc8QPx6V8KLEEgfKEQ
This commit is contained in:
2026-07-13 10:56:07 +08:00
co-authored by Claude Sonnet 4.6
parent 6c1ad71643
commit f90bf4ed03
16 changed files with 266 additions and 26 deletions
+7 -1
View File
@@ -5,6 +5,7 @@ import { redirect } from 'next/navigation'
import { createClient } from '@/lib/supabase/server'
import { UserManager, type AdminUser, type SiteOption } from '@/components/admin/user-manager'
import { SiteZoneManager, type SiteWithZones } from '@/components/admin/site-zone-manager'
import { TruckManager, type Truck } from '@/components/admin/truck-manager'
export default async function AdminHome() {
const supabase = await createClient()
@@ -15,7 +16,7 @@ export default async function AdminHome() {
.from('users').select('role').eq('id', user.id).single()
if (!profile || profile.role !== 'admin') redirect('/login')
const [{ data: users }, { data: sites }] = await Promise.all([
const [{ data: users }, { data: sites }, { data: trucks }] = await Promise.all([
supabase
.from('users')
.select('id, name, email, role, department, site_id, active')
@@ -24,6 +25,10 @@ export default async function AdminHome() {
.from('sites')
.select('id, name, address, zones (id, name, qr_code_token)')
.order('name'),
supabase
.from('trucks')
.select('id, truck_no, carrier, active')
.order('truck_no'),
])
const siteOptions: SiteOption[] = (sites ?? []).map(s => ({ id: s.id, name: s.name }))
@@ -41,6 +46,7 @@ export default async function AdminHome() {
</div>
<UserManager users={(users ?? []) as AdminUser[]} sites={siteOptions} />
<SiteZoneManager sites={(sites ?? []) as unknown as SiteWithZones[]} />
<TruckManager trucks={(trucks ?? []) as Truck[]} />
</main>
)
}
@@ -25,6 +25,7 @@ export default async function HseIncidentDetailPage({ params }: Props) {
reported_at, closed_at,
sites (id, name),
zones (id, name),
trucks (id, truck_no, carrier),
reporter:users!reported_by (id, name, email),
evidence_files (id, stage, file_url, file_type, uploaded_at)
`)
+8 -4
View File
@@ -3,14 +3,14 @@ export const dynamic = 'force-dynamic'
import { createClient } from '@/lib/supabase/server'
import { IncidentList, type Incident } from '@/components/incidents/incident-list'
import { Pagination } from '@/components/incidents/pagination'
import { IncidentFilters, type SiteOption } from '@/components/incidents/incident-filters'
import { IncidentFilters, type SiteOption, type TruckOption } from '@/components/incidents/incident-filters'
const PAGE_SIZE = 25
export default async function HseInboxPage({
searchParams,
}: {
searchParams: Promise<{ page?: string; q?: string; status?: string; type?: string; site_id?: string }>
searchParams: Promise<{ page?: string; q?: string; status?: string; type?: string; site_id?: string; truck_id?: string }>
}) {
const supabase = await createClient()
const params = await searchParams
@@ -23,6 +23,7 @@ export default async function HseInboxPage({
id, reference_no, incident_type, status, severity, reported_at,
sites (name),
zones (name),
trucks (truck_no),
reporter:users!reported_by (name)
`, { count: 'exact' })
.order('reported_at', { ascending: false })
@@ -33,13 +34,16 @@ export default async function HseInboxPage({
if (params.status) query = query.eq('status', params.status)
if (params.type) query = query.eq('incident_type', params.type)
if (params.site_id) query = query.eq('site_id', params.site_id)
if (params.truck_id) query = query.eq('truck_id', params.truck_id)
const [{ data: incidents, count }, { data: sites }] = await Promise.all([
const [{ data: incidents, count }, { data: sites }, { data: trucks }] = await Promise.all([
query.range(from, from + PAGE_SIZE - 1),
supabase.from('sites').select('id, name').order('name'),
supabase.from('trucks').select('id, truck_no').eq('active', true).order('truck_no'),
])
const siteOptions: SiteOption[] = (sites ?? []).map(s => ({ id: s.id, name: s.name }))
const truckOptions: TruckOption[] = (trucks ?? []).map(tk => ({ id: tk.id, truck_no: tk.truck_no }))
return (
<main className="max-w-4xl mx-auto px-4 py-6">
@@ -47,7 +51,7 @@ export default async function HseInboxPage({
<h1 className="text-2xl font-bold text-gray-900">Incident Inbox</h1>
<span className="text-sm text-gray-500">{count ?? incidents?.length ?? 0} incidents</span>
</div>
<IncidentFilters sites={siteOptions} />
<IncidentFilters sites={siteOptions} trucks={truckOptions} />
<IncidentList incidents={(incidents ?? []) as unknown as Incident[]} basePath="/hse" />
<Pagination page={page} pageSize={PAGE_SIZE} total={count ?? 0} href="/hse/incidents" />
</main>
@@ -3,14 +3,14 @@ export const dynamic = 'force-dynamic'
import { createClient } from '@/lib/supabase/server'
import { IncidentList, type Incident } from '@/components/incidents/incident-list'
import { Pagination } from '@/components/incidents/pagination'
import { IncidentFilters, type SiteOption } from '@/components/incidents/incident-filters'
import { IncidentFilters, type SiteOption, type TruckOption } from '@/components/incidents/incident-filters'
const PAGE_SIZE = 25
export default async function SupervisorInboxPage({
searchParams,
}: {
searchParams: Promise<{ page?: string; q?: string; status?: string; type?: string; site_id?: string }>
searchParams: Promise<{ page?: string; q?: string; status?: string; type?: string; site_id?: string; truck_id?: string }>
}) {
const supabase = await createClient()
const params = await searchParams
@@ -23,6 +23,7 @@ export default async function SupervisorInboxPage({
id, reference_no, incident_type, status, severity, reported_at,
sites (name),
zones (name),
trucks (truck_no),
reporter:users!reported_by (name)
`, { count: 'exact' })
.order('reported_at', { ascending: false })
@@ -33,13 +34,16 @@ export default async function SupervisorInboxPage({
if (params.status) query = query.eq('status', params.status)
if (params.type) query = query.eq('incident_type', params.type)
if (params.site_id) query = query.eq('site_id', params.site_id)
if (params.truck_id) query = query.eq('truck_id', params.truck_id)
const [{ data: incidents, count }, { data: sites }] = await Promise.all([
const [{ data: incidents, count }, { data: sites }, { data: trucks }] = await Promise.all([
query.range(from, from + PAGE_SIZE - 1),
supabase.from('sites').select('id, name').order('name'),
supabase.from('trucks').select('id, truck_no').eq('active', true).order('truck_no'),
])
const siteOptions: SiteOption[] = (sites ?? []).map(s => ({ id: s.id, name: s.name }))
const truckOptions: TruckOption[] = (trucks ?? []).map(tk => ({ id: tk.id, truck_no: tk.truck_no }))
return (
<main className="max-w-4xl mx-auto px-4 py-6">
@@ -47,7 +51,7 @@ export default async function SupervisorInboxPage({
<h1 className="text-2xl font-bold text-gray-900">Incident Inbox</h1>
<span className="text-sm text-gray-500">{count ?? incidents?.length ?? 0} incidents</span>
</div>
<IncidentFilters sites={siteOptions} />
<IncidentFilters sites={siteOptions} trucks={truckOptions} />
<IncidentList incidents={(incidents ?? []) as unknown as Incident[]} basePath="/supervisor" />
<Pagination page={page} pageSize={PAGE_SIZE} total={count ?? 0} href="/supervisor/incidents" />
</main>
+34
View File
@@ -0,0 +1,34 @@
export const dynamic = 'force-dynamic'
import { NextRequest, NextResponse } from 'next/server'
import { requireAdmin } from '@/lib/auth/require-admin'
export async function POST(request: NextRequest) {
const { supabase, user } = await requireAdmin()
if (!user) return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
const body: { truck_no?: string; carrier?: string } = await request.json().catch(() => ({}))
const truck_no = (body.truck_no ?? '').trim()
if (!truck_no) return NextResponse.json({ error: 'truck_no required' }, { status: 422 })
const { data: truck, error } = await supabase
.from('trucks')
.insert({ truck_no, carrier: (body.carrier ?? '').trim() || null })
.select('id')
.single()
if (error || !truck)
return NextResponse.json(
{ error: error?.code === '23505' ? 'Truck number already exists' : 'Insert failed' },
{ status: 400 },
)
await supabase.rpc('write_audit_log', {
p_table_name: 'trucks',
p_record_id: truck.id,
p_action: 'INSERT',
p_new_value: { truck_no, carrier: (body.carrier ?? '').trim() || null },
})
return NextResponse.json({ id: truck.id }, { status: 201 })
}
+10
View File
@@ -47,6 +47,7 @@ export async function POST(request: Request) {
injury_involved: body.injury_involved === 'true' || body.injury_involved === true,
asset_involved: body.asset_involved === 'true' || body.asset_involved === true,
medical_status: body.medical_status as MedicalStatus | undefined || undefined,
truck_id: (body.truck_id as string) || null,
}
const validation = validateIncidentInput(input)
@@ -54,6 +55,14 @@ export async function POST(request: Request) {
return NextResponse.json({ error: 'Validation failed', details: validation.errors }, { status: 422 })
}
let truckId: string | null = null
if (input.incident_type === 'transport') {
const { data: truck } = await supabase
.from('trucks').select('id').eq('id', input.truck_id).eq('active', true).single()
if (!truck) return NextResponse.json({ error: 'Validation failed', details: ['truck not found or inactive'] }, { status: 422 })
truckId = truck.id
}
let rawDetails: Record<string, unknown> | undefined
if (typeof body.type_details === 'string' && body.type_details) {
try {
@@ -91,6 +100,7 @@ export async function POST(request: Request) {
asset_involved: input.asset_involved,
medical_status: input.injury_involved ? (input.medical_status ?? 'none') : null,
type_details: detailsCheck.sanitized,
truck_id: truckId,
})
.select('id, reference_no')
.single()
+12 -1
View File
@@ -32,6 +32,12 @@ export default async function ReportPage({ searchParams }: Props) {
}
}
const { data: trucks } = await supabase
.from('trucks')
.select('id, truck_no, carrier')
.eq('active', true)
.order('truck_no')
return (
<main className="min-h-screen bg-gray-50 py-6 px-4 max-w-lg mx-auto">
<div className="mb-6">
@@ -47,7 +53,12 @@ export default async function ReportPage({ searchParams }: Props) {
<p className="text-sm text-amber-600 mt-1">No zone detected zone will not be recorded</p>
)}
</div>
<ReportForm zoneToken={zone ?? null} zoneName={zoneName} siteName={siteName} />
<ReportForm
zoneToken={zone ?? null}
zoneName={zoneName}
siteName={siteName}
trucks={(trucks ?? []) as Array<{ id: string; truck_no: string; carrier: string | null }>}
/>
<OfflineSync />
</main>
)