Convert all 11 incident API routes from Supabase PostgREST to Drizzle ORM with withUser/asAdmin/writeAuditLog patterns and RLS enforcement. Only uploadEvidenceFile retains supabase client (Phase 5 storage work). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
89 lines
3.4 KiB
TypeScript
89 lines
3.4 KiB
TypeScript
export const dynamic = 'force-dynamic'
|
|
|
|
import { NextRequest, NextResponse } from 'next/server'
|
|
import { aliasedTable } from 'drizzle-orm'
|
|
import { getSession } from '@/lib/auth/get-session'
|
|
import { withUser } from '@/lib/db/with-user'
|
|
import { incidents, sites, users } from '@/lib/db/schema'
|
|
import { eq } from 'drizzle-orm'
|
|
import { buildJkkp6Pdf, buildJkkp7Pdf, type JkkpIncident } from '@/lib/pdf/jkkp'
|
|
import { computeDoshObligation } from '@/lib/incidents/dosh'
|
|
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
const { id } = await params
|
|
const form = request.nextUrl.searchParams.get('form')
|
|
if (form !== 'jkkp6' && form !== 'jkkp7')
|
|
return NextResponse.json({ error: 'form must be jkkp6 or jkkp7' }, { status: 422 })
|
|
|
|
const session = await getSession()
|
|
if (!session) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
if (!['hse', 'admin'].includes(session.role))
|
|
return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
|
|
|
|
const reporterAlias = aliasedTable(users, 'reporter')
|
|
|
|
const [incident] = await withUser(session.sub, async tx =>
|
|
tx.select({
|
|
referenceNo: incidents.referenceNo,
|
|
incidentType: incidents.incidentType,
|
|
description: incidents.description,
|
|
reportedAt: incidents.reportedAt,
|
|
severity: incidents.severity,
|
|
lostDays: incidents.lostDays,
|
|
isFatality: incidents.isFatality,
|
|
isSeriousBodilyInjury: incidents.isSeriousBodilyInjury,
|
|
isDangerousOccurrence: incidents.isDangerousOccurrence,
|
|
isOccupationalDisease: incidents.isOccupationalDisease,
|
|
siteName: sites.name,
|
|
reporterName: reporterAlias.name,
|
|
})
|
|
.from(incidents)
|
|
.leftJoin(sites, eq(incidents.siteId, sites.id))
|
|
.leftJoin(reporterAlias, eq(incidents.reportedBy, reporterAlias.id))
|
|
.where(eq(incidents.id, id))
|
|
.limit(1)
|
|
)
|
|
|
|
if (!incident) return NextResponse.json({ error: 'Not found' }, { status: 404 })
|
|
|
|
const dosh = computeDoshObligation({
|
|
is_fatality: incident.isFatality,
|
|
is_serious_bodily_injury: incident.isSeriousBodilyInjury,
|
|
is_dangerous_occurrence: incident.isDangerousOccurrence,
|
|
is_occupational_disease: incident.isOccupationalDisease,
|
|
lost_days: incident.lostDays,
|
|
})
|
|
|
|
const required = form === 'jkkp6' ? dosh.requires_jkkp6 : dosh.requires_jkkp7
|
|
if (!required) return NextResponse.json({ error: 'This form is not required for this incident' }, { status: 400 })
|
|
|
|
const jkkpIncident: JkkpIncident = {
|
|
reference_no: incident.referenceNo,
|
|
incident_type: incident.incidentType,
|
|
description: incident.description,
|
|
reported_at: incident.reportedAt instanceof Date ? incident.reportedAt.toISOString() : (incident.reportedAt as string),
|
|
severity: incident.severity,
|
|
is_fatality: incident.isFatality,
|
|
is_serious_bodily_injury: incident.isSeriousBodilyInjury,
|
|
is_dangerous_occurrence: incident.isDangerousOccurrence,
|
|
lost_days: incident.lostDays,
|
|
site_name: incident.siteName ?? 'Unknown',
|
|
reporter_name: incident.reporterName ?? 'Unknown',
|
|
}
|
|
|
|
const pdfBytes = form === 'jkkp6'
|
|
? await buildJkkp6Pdf(jkkpIncident)
|
|
: await buildJkkp7Pdf(jkkpIncident)
|
|
|
|
const ref = jkkpIncident.reference_no ?? id
|
|
return new NextResponse(Buffer.from(pdfBytes), {
|
|
headers: {
|
|
'Content-Type': 'application/pdf',
|
|
'Content-Disposition': `attachment; filename="${form}-${ref}.pdf"`,
|
|
},
|
|
})
|
|
}
|