Files
adminandClaude Sonnet 4.6 f591c0be18 feat(db): phase 4 group 6 — server component pages to Drizzle
Converts all 18 server component page files from Supabase client queries
to Drizzle ORM using asAdmin. Adds getSession() + redirect to the three
pages (hse/incidents, hse/incidents/[id], hse/dashboard) that lacked it.
Maps snake_case component prop shapes explicitly where required.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-23 17:29:22 +08:00

107 lines
4.1 KiB
TypeScript

export const dynamic = 'force-dynamic'
import { redirect } from 'next/navigation'
import { asAdmin } from '@/lib/db/with-user'
import { getSession } from '@/lib/auth/get-session'
import { incidents, sites, zones, trucks, users } from '@/lib/db/schema'
import { eq, and, or, ilike, desc, asc, sql } from 'drizzle-orm'
import { aliasedTable } from 'drizzle-orm'
import { IncidentList, type Incident } from '@/components/incidents/incident-list'
import { Pagination } from '@/components/incidents/pagination'
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; truck_id?: string }>
}) {
const session = await getSession()
if (!session) redirect('/login')
const params = await searchParams
const page = Math.max(1, Number.parseInt(params.page ?? '1', 10) || 1)
const from = (page - 1) * PAGE_SIZE
const reporterAlias = aliasedTable(users, 'reporter')
const conditions = []
if (params.q) conditions.push(or(ilike(incidents.referenceNo, `%${params.q}%`), ilike(incidents.description, `%${params.q}%`)))
if (params.status) conditions.push(eq(incidents.status, params.status as never))
if (params.type) conditions.push(eq(incidents.incidentType, params.type as never))
if (params.site_id) conditions.push(eq(incidents.siteId, params.site_id))
if (params.truck_id) conditions.push(eq(incidents.truckId, params.truck_id))
const where = conditions.length ? and(...conditions) : undefined
const [[countRow], incidentRows, siteRows, truckRows] = await Promise.all([
asAdmin(db =>
db.select({ count: sql<number>`count(*)` })
.from(incidents)
.where(where)
),
asAdmin(db =>
db.select({
id: incidents.id,
referenceNo: incidents.referenceNo,
incidentType: incidents.incidentType,
status: incidents.status,
severity: incidents.severity,
reportedAt: incidents.reportedAt,
siteName: sites.name,
zoneName: zones.name,
reporterName: reporterAlias.name,
truckNo: trucks.truckNo,
})
.from(incidents)
.leftJoin(sites, eq(incidents.siteId, sites.id))
.leftJoin(zones, eq(incidents.zoneId, zones.id))
.leftJoin(reporterAlias, eq(incidents.reportedBy, reporterAlias.id))
.leftJoin(trucks, eq(incidents.truckId, trucks.id))
.where(where)
.orderBy(desc(incidents.reportedAt))
.limit(PAGE_SIZE)
.offset(from)
),
asAdmin(db =>
db.select({ id: sites.id, name: sites.name }).from(sites).orderBy(asc(sites.name))
),
asAdmin(db =>
db.select({ id: trucks.id, truckNo: trucks.truckNo })
.from(trucks)
.where(eq(trucks.active, true))
.orderBy(asc(trucks.truckNo))
),
])
const total = Number(countRow?.count ?? 0)
const incidentList: Incident[] = incidentRows.map(r => ({
id: r.id,
reference_no: r.referenceNo,
incident_type: r.incidentType,
status: r.status,
severity: r.severity,
reported_at: r.reportedAt.toISOString(),
sites: r.siteName ? { name: r.siteName } : null,
zones: r.zoneName ? { name: r.zoneName } : null,
trucks: r.truckNo ? { truck_no: r.truckNo } : null,
reporter: r.reporterName ? { name: r.reporterName } : null,
}))
const siteOptions: SiteOption[] = siteRows.map(s => ({ id: s.id, name: s.name }))
const truckOptions: TruckOption[] = truckRows.map(t => ({ id: t.id, truck_no: t.truckNo }))
return (
<main className="max-w-4xl mx-auto px-4 py-6">
<div className="flex items-center justify-between mb-4">
<h1 className="text-2xl font-bold text-gray-900">Incident Inbox</h1>
<span className="text-sm text-gray-500">{total} incidents</span>
</div>
<IncidentFilters sites={siteOptions} trucks={truckOptions} />
<IncidentList incidents={incidentList} basePath="/supervisor" />
<Pagination page={page} pageSize={PAGE_SIZE} total={total} href="/supervisor/incidents" />
</main>
)
}