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>
This commit is contained in:
2026-07-23 17:29:22 +08:00
co-authored by Claude Sonnet 4.6
parent c2db693d9f
commit f591c0be18
18 changed files with 932 additions and 450 deletions
@@ -2,7 +2,10 @@ export const dynamic = 'force-dynamic'
import { notFound } from 'next/navigation'
import Link from 'next/link'
import { createClient } from '@/lib/supabase/server'
import { asAdmin } from '@/lib/db/with-user'
import { incidents, sites, zones, trucks, users, evidenceFiles, investigations } from '@/lib/db/schema'
import { eq, and } from 'drizzle-orm'
import { aliasedTable } from 'drizzle-orm'
import { IncidentDetail, type Incident } from '@/components/incidents/incident-detail'
import { InvestigationPanel } from '@/components/incidents/investigation-panel'
@@ -12,44 +15,129 @@ interface Props {
export default async function SupervisorIncidentDetailPage({ params }: Props) {
const { id } = await params
const supabase = await createClient()
const { data: incident, error } = await supabase
.from('incidents')
.select(`
id, reference_no, incident_type, description, severity, status,
injury_involved, asset_involved, medical_status, lost_days,
reported_at, closed_at,
sites (id, name),
zones (id, name),
reporter:users!reported_by (id, name, email),
evidence_files (id, stage, file_url, file_type, uploaded_at)
`)
.eq('id', id)
.eq('evidence_files.deleted', false)
.single()
const reporterAlias = aliasedTable(users, 'reporter')
const investigatorAlias = aliasedTable(users, 'investigator')
if (error || !incident) notFound()
const [incident] = await asAdmin(db =>
db.select({
id: incidents.id,
referenceNo: incidents.referenceNo,
incidentType: incidents.incidentType,
description: incidents.description,
severity: incidents.severity,
status: incidents.status,
injuryInvolved: incidents.injuryInvolved,
assetInvolved: incidents.assetInvolved,
medicalStatus: incidents.medicalStatus,
lostDays: incidents.lostDays,
reportedAt: incidents.reportedAt,
closedAt: incidents.closedAt,
siteId: sites.id,
siteName: sites.name,
zoneId: zones.id,
zoneName: zones.name,
truckId: trucks.id,
truckNo: trucks.truckNo,
truckCarrier: trucks.carrier,
reporterId: reporterAlias.id,
reporterName: reporterAlias.name,
reporterEmail: reporterAlias.email,
})
.from(incidents)
.leftJoin(sites, eq(incidents.siteId, sites.id))
.leftJoin(zones, eq(incidents.zoneId, zones.id))
.leftJoin(trucks, eq(incidents.truckId, trucks.id))
.leftJoin(reporterAlias, eq(incidents.reportedBy, reporterAlias.id))
.where(eq(incidents.id, id))
.limit(1)
)
const { data: investigation } = await supabase
.from('investigations')
.select(`
id, method, five_why_steps, fishbone_categories,
findings_text, root_cause_summary, alcohol_test_result, urine_test_result,
witness_statement_refs, completed_at,
investigator:users!investigator_id (name, email)
`)
.eq('incident_id', id)
.maybeSingle()
if (!incident) notFound()
const evidenceRows = await asAdmin(db =>
db.select({
id: evidenceFiles.id,
stage: evidenceFiles.stage,
fileUrl: evidenceFiles.fileUrl,
fileType: evidenceFiles.fileType,
uploadedAt: evidenceFiles.uploadedAt,
})
.from(evidenceFiles)
.where(and(eq(evidenceFiles.incidentId, id), eq(evidenceFiles.deleted, false)))
)
const [inv] = await asAdmin(db =>
db.select({
id: investigations.id,
method: investigations.method,
fiveWhySteps: investigations.fiveWhySteps,
fishboneCategories: investigations.fishboneCategories,
findingsText: investigations.findingsText,
rootCauseSummary: investigations.rootCauseSummary,
alcoholTestResult: investigations.alcoholTestResult,
urineTestResult: investigations.urineTestResult,
witnessStatementRefs: investigations.witnessStatementRefs,
completedAt: investigations.completedAt,
investigatorName: investigatorAlias.name,
investigatorEmail: investigatorAlias.email,
})
.from(investigations)
.leftJoin(investigatorAlias, eq(investigations.investigatorId, investigatorAlias.id))
.where(eq(investigations.incidentId, id))
.limit(1)
)
const incidentProp: Incident = {
id: incident.id,
reference_no: incident.referenceNo,
incident_type: incident.incidentType,
description: incident.description,
status: incident.status,
severity: incident.severity,
injury_involved: incident.injuryInvolved,
asset_involved: incident.assetInvolved,
medical_status: incident.medicalStatus,
lost_days: incident.lostDays,
reported_at: incident.reportedAt.toISOString(),
closed_at: incident.closedAt?.toISOString() ?? null,
sites: incident.siteId ? { id: incident.siteId, name: incident.siteName! } : null,
zones: incident.zoneId ? { id: incident.zoneId, name: incident.zoneName! } : null,
trucks: incident.truckId ? { id: incident.truckId, truck_no: incident.truckNo!, carrier: incident.truckCarrier ?? null } : null,
reporter: incident.reporterId ? { id: incident.reporterId, name: incident.reporterName!, email: incident.reporterEmail! } : null,
evidence_files: evidenceRows.map(e => ({
id: e.id,
stage: e.stage,
file_url: e.fileUrl,
file_type: e.fileType,
uploaded_at: e.uploadedAt.toISOString(),
})),
}
const investigationProp = inv ? {
id: inv.id,
method: inv.method,
five_why_steps: inv.fiveWhySteps as { why: string; answer: string }[] | null,
fishbone_categories: inv.fishboneCategories as Record<string, string> | null,
findings_text: inv.findingsText,
root_cause_summary: inv.rootCauseSummary,
alcohol_test_result: inv.alcoholTestResult,
urine_test_result: inv.urineTestResult,
witness_statement_refs: inv.witnessStatementRefs ?? [],
completed_at: inv.completedAt?.toISOString() ?? null,
investigator: inv.investigatorEmail
? { name: inv.investigatorName ?? null, email: inv.investigatorEmail }
: null,
} : null
return (
<main className="max-w-3xl mx-auto px-4 py-6">
<Link href="/supervisor/incidents" className="text-sm text-blue-600 hover:underline mb-4 inline-block">
Back to inbox
</Link>
<IncidentDetail incident={incident as unknown as Incident} />
{investigation && (
<InvestigationPanel investigation={investigation as never} />
<IncidentDetail incident={incidentProp} />
{investigationProp && (
<InvestigationPanel investigation={investigationProp} />
)}
</main>
)