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:
@@ -1,7 +1,11 @@
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
import { notFound } from 'next/navigation'
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { notFound, redirect } from 'next/navigation'
|
||||
import { asAdmin } from '@/lib/db/with-user'
|
||||
import { getSession } from '@/lib/auth/get-session'
|
||||
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 { IncidentHeader } from '@/components/incidents/incident-header'
|
||||
import { SimilarIncidentsPanel } from '@/components/incidents/similar-incidents-panel'
|
||||
@@ -14,52 +18,145 @@ interface Props {
|
||||
|
||||
export default async function HseIncidentDetailPage({ params }: Props) {
|
||||
const { id } = await params
|
||||
const supabase = await createClient()
|
||||
const session = await getSession()
|
||||
if (!session) redirect('/login')
|
||||
|
||||
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, type_details,
|
||||
is_fatality, is_serious_bodily_injury, is_dangerous_occurrence, is_occupational_disease,
|
||||
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)
|
||||
`)
|
||||
.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,
|
||||
typeDetails: incidents.typeDetails,
|
||||
isFatality: incidents.isFatality,
|
||||
isSeriousBodilyInjury: incidents.isSeriousBodilyInjury,
|
||||
isDangerousOccurrence: incidents.isDangerousOccurrence,
|
||||
isOccupationalDisease: incidents.isOccupationalDisease,
|
||||
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 status = (incident as { status: string }).status
|
||||
if (!incident) notFound()
|
||||
|
||||
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()
|
||||
const [evidenceRows, invRows] = await Promise.all([
|
||||
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)))
|
||||
),
|
||||
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 status = incident.status
|
||||
|
||||
const needsJkkp = Boolean(
|
||||
(incident as { is_fatality?: boolean }).is_fatality ||
|
||||
(incident as { is_serious_bodily_injury?: boolean }).is_serious_bodily_injury ||
|
||||
(incident as { is_dangerous_occurrence?: boolean }).is_dangerous_occurrence ||
|
||||
((incident as { lost_days?: number | null }).lost_days ?? 0) >= 4
|
||||
incident.isFatality ||
|
||||
incident.isSeriousBodilyInjury ||
|
||||
incident.isDangerousOccurrence ||
|
||||
(incident.lostDays ?? 0) >= 4
|
||||
)
|
||||
|
||||
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,
|
||||
type_details: incident.typeDetails as Record<string, string | boolean> | null,
|
||||
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 inv = invRows[0]
|
||||
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 (
|
||||
<>
|
||||
<IncidentHeader
|
||||
incidentId={id}
|
||||
referenceNo={(incident as { reference_no: string | null }).reference_no}
|
||||
referenceNo={incident.referenceNo}
|
||||
status={status}
|
||||
backHref="/hse/incidents"
|
||||
canTriage={status === 'reported'}
|
||||
@@ -67,10 +164,10 @@ export default async function HseIncidentDetailPage({ params }: Props) {
|
||||
canAddCapa={(['investigating', 'capa_pending'] as string[]).includes(status)}
|
||||
/>
|
||||
<main className="max-w-3xl mx-auto px-4 py-6">
|
||||
<IncidentDetail incident={incident as unknown as Incident} />
|
||||
<IncidentDetail incident={incidentProp} />
|
||||
|
||||
{investigation && (
|
||||
<InvestigationPanel investigation={investigation as never} />
|
||||
{investigationProp && (
|
||||
<InvestigationPanel investigation={investigationProp} />
|
||||
)}
|
||||
|
||||
{needsJkkp && (
|
||||
|
||||
Reference in New Issue
Block a user