Files
ims/app/(protected)/supervisor/incidents/[id]/page.tsx
T
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

145 lines
5.3 KiB
TypeScript

export const dynamic = 'force-dynamic'
import { notFound } from 'next/navigation'
import Link from 'next/link'
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'
interface Props {
params: Promise<{ id: string }>
}
export default async function SupervisorIncidentDetailPage({ params }: Props) {
const { id } = await params
const reporterAlias = aliasedTable(users, 'reporter')
const investigatorAlias = aliasedTable(users, 'investigator')
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)
)
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={incidentProp} />
{investigationProp && (
<InvestigationPanel investigation={investigationProp} />
)}
</main>
)
}