197 lines
7.3 KiB
TypeScript
197 lines
7.3 KiB
TypeScript
export const dynamic = 'force-dynamic'
|
|
|
|
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'
|
|
import { ClosurePanel } from '@/components/incidents/closure-panel'
|
|
import { InvestigationPanel } from '@/components/incidents/investigation-panel'
|
|
|
|
interface Props {
|
|
params: Promise<{ id: string }>
|
|
}
|
|
|
|
export default async function HseIncidentDetailPage({ params }: Props) {
|
|
const { id } = await params
|
|
const session = await getSession()
|
|
if (!session) redirect('/login')
|
|
|
|
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,
|
|
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)
|
|
)
|
|
|
|
if (!incident) notFound()
|
|
|
|
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.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.referenceNo}
|
|
status={status}
|
|
backHref="/hse/incidents"
|
|
canTriage={status === 'reported'}
|
|
canInvestigate={status === 'triaged'}
|
|
canAddCapa={(['investigating', 'capa_pending'] as string[]).includes(status)}
|
|
/>
|
|
<main className="max-w-3xl mx-auto px-4 py-6">
|
|
<IncidentDetail incident={incidentProp} />
|
|
|
|
{investigationProp && (
|
|
<InvestigationPanel investigation={investigationProp} />
|
|
)}
|
|
|
|
{needsJkkp && (
|
|
<div className="mt-4 flex gap-3 flex-wrap">
|
|
<a
|
|
href={`/api/incidents/${id}/jkkp-pdf?form=jkkp6`}
|
|
target="_blank"
|
|
className="inline-block bg-gray-800 text-white rounded-lg px-4 py-2 text-sm font-semibold hover:bg-gray-900"
|
|
>
|
|
Download JKKP 6 (PDF)
|
|
</a>
|
|
<a
|
|
href={`/api/incidents/${id}/jkkp-pdf?form=jkkp7`}
|
|
target="_blank"
|
|
className="inline-block bg-gray-600 text-white rounded-lg px-4 py-2 text-sm font-semibold hover:bg-gray-700"
|
|
>
|
|
Download JKKP 7 (PDF)
|
|
</a>
|
|
</div>
|
|
)}
|
|
<ClosurePanel incidentId={id} status={status} canClose canAddAddenda />
|
|
<SimilarIncidentsPanel incidentId={id} />
|
|
</main>
|
|
</>
|
|
)
|
|
}
|