feat: JKKP 6/7 PDF generation via pdf-lib, download from incident detail
Also excludes node_modules.nosync from vitest test discovery to fix pre-existing bleed. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FFDuBhMKvoWjrWT3ZnGmmr
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
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 supabase = await createClient()
|
||||
const { data: { user }, error: authError } = await supabase.auth.getUser()
|
||||
if (authError || !user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
|
||||
const { data: profile } = await supabase
|
||||
.from('users').select('role').eq('id', user.id).single()
|
||||
if (!profile || !['hse', 'admin'].includes(profile.role))
|
||||
return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
|
||||
|
||||
const { data: incident } = await supabase
|
||||
.from('incidents')
|
||||
.select(`
|
||||
reference_no, incident_type, description, reported_at, severity, lost_days,
|
||||
is_fatality, is_serious_bodily_injury, is_dangerous_occurrence, is_occupational_disease,
|
||||
sites (name),
|
||||
reporter:users!reported_by (name)
|
||||
`)
|
||||
.eq('id', id)
|
||||
.single()
|
||||
|
||||
if (!incident) return NextResponse.json({ error: 'Not found' }, { status: 404 })
|
||||
|
||||
const dosh = computeDoshObligation({
|
||||
is_fatality: (incident as { is_fatality: boolean }).is_fatality,
|
||||
is_serious_bodily_injury: (incident as { is_serious_bodily_injury: boolean }).is_serious_bodily_injury,
|
||||
is_dangerous_occurrence: (incident as { is_dangerous_occurrence: boolean }).is_dangerous_occurrence,
|
||||
is_occupational_disease: (incident as { is_occupational_disease: boolean }).is_occupational_disease,
|
||||
lost_days: (incident as { lost_days: number | null }).lost_days,
|
||||
})
|
||||
|
||||
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 as { reference_no: string | null }).reference_no,
|
||||
incident_type: (incident as { incident_type: string }).incident_type,
|
||||
description: (incident as { description: string }).description,
|
||||
reported_at: (incident as { reported_at: string }).reported_at,
|
||||
severity: (incident as { severity: number | null }).severity,
|
||||
is_fatality: (incident as { is_fatality: boolean }).is_fatality,
|
||||
is_serious_bodily_injury: (incident as { is_serious_bodily_injury: boolean }).is_serious_bodily_injury,
|
||||
is_dangerous_occurrence: (incident as { is_dangerous_occurrence: boolean }).is_dangerous_occurrence,
|
||||
lost_days: (incident as { lost_days: number | null }).lost_days,
|
||||
site_name: (incident.sites as unknown as { name: string } | null)?.name ?? 'Unknown',
|
||||
reporter_name: (incident.reporter as unknown as { name: string } | null)?.name ?? '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"`,
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user