Files
ims/lib/reports/jkkp8.ts
T
adminandClaude Fable 5 4242af029c fix: code-review findings — closed-incident embedding 503, hardening, dedup
- /api/incidents/[id]/similar: embedding backfill on a closed incident hit
  the closure-lock trigger and turned the whole request into a 503; now
  skips persistence for closed incidents (vector still used for the query)
- addenda: cap body at 5000 chars; include body text in audit_log entry
- admin users PATCH: 404 when target user does not exist (was silent ok)
- extract shared requireAdmin to lib/auth/require-admin.ts (was duplicated
  in admin users + sites routes)
- extract escapeCsv/rowsToCsv to lib/csv.ts (was duplicated in dashboard
  export route and lib/reports/jkkp8.ts)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CPf5Rc8QPx6V8KLEEgfKEQ
2026-07-12 12:45:01 +08:00

89 lines
2.7 KiB
TypeScript

import { computeDoshObligation } from '@/lib/incidents/dosh'
import { escapeCsv } from '@/lib/csv'
export interface Jkkp8Incident {
reference_no: string | null
incident_type: string
description: string
reported_at: string
medical_status: string | null
lost_days: number | null
is_fatality: boolean
is_serious_bodily_injury: boolean
is_dangerous_occurrence: boolean
is_occupational_disease: boolean
sites: { name: string } | null
zones: { name: string } | null
reporter: { name: string } | null
dosh_reports: Array<{ form_type: string; status: string; submitted_at: string | null }>
}
export interface Jkkp8Row {
reference: string
date: string
site: string
zone: string
incident_type: string
reported_by: string
description: string
medical_status: string
lost_days: string
obligation: string
filing_status: string
}
// JKKP 8 annual register: every incident with any NADOPOD obligation for the
// year (PRD §9 — "Any of the above → also logged in the JKKP 8 annual register").
export function buildJkkp8Rows(incidents: Jkkp8Incident[]): Jkkp8Row[] {
const rows: Jkkp8Row[] = []
for (const inc of incidents) {
const obligation = computeDoshObligation(inc)
const reportable =
obligation.requires_jkkp6 || obligation.requires_jkkp7 || obligation.requires_jkkp8
if (!reportable) continue
const filings = inc.dosh_reports ?? []
const filingStatus =
filings.length === 0
? 'pending'
: filings
.map(f => `${f.form_type.toUpperCase()}: ${f.status}${f.submitted_at ? ` (${f.submitted_at.split('T')[0]})` : ''}`)
.join('; ')
rows.push({
reference: inc.reference_no ?? '',
date: inc.reported_at.split('T')[0],
site: inc.sites?.name ?? '',
zone: inc.zones?.name ?? '',
incident_type: inc.incident_type,
reported_by: inc.reporter?.name ?? '',
description: inc.description,
medical_status: inc.medical_status ?? '',
lost_days: String(inc.lost_days ?? ''),
obligation: obligation.reasons.join('; '),
filing_status: filingStatus,
})
}
return rows
}
export const JKKP8_HEADERS = [
'Reference', 'Date', 'Site', 'Zone', 'Incident Type', 'Reported By',
'Description', 'Medical Status', 'Lost Days', 'NADOPOD Obligation', 'DOSH Filing Status',
]
export function jkkp8Csv(rows: Jkkp8Row[]): string {
const lines = [JKKP8_HEADERS.map(escapeCsv).join(',')]
for (const r of rows) {
lines.push(
[
r.reference, r.date, r.site, r.zone, r.incident_type, r.reported_by,
r.description, r.medical_status, r.lost_days, r.obligation, r.filing_status,
].map(escapeCsv).join(','),
)
}
return lines.join('\r\n')
}