Custom auth stack: bcryptjs password hashing (cost 10, GoTrue-compatible), jose JWT session cookies (edge-safe, 8hr TTL), new API routes for login/logout/reset/change-password, middleware rewritten to JWT-only verification with no DB access. All 38 protected pages and API routes migrated from supabase.auth.getUser() to getSession(). Supabase .from() queries retained for Phase 4. lib/db/index.ts refactored to lazy Proxy singleton to avoid module-level throw during Next.js build. tsc: clean, build: clean, tests: 4/4 passed Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
54 lines
1.9 KiB
TypeScript
54 lines
1.9 KiB
TypeScript
export const dynamic = 'force-dynamic'
|
|
|
|
import { NextRequest, NextResponse } from 'next/server'
|
|
import { createClient } from '@/lib/supabase/server'
|
|
import { getSession } from '@/lib/auth/get-session'
|
|
import { buildJkkp8Rows, jkkp8Csv, type Jkkp8Incident } from '@/lib/reports/jkkp8'
|
|
|
|
export async function GET(request: NextRequest) {
|
|
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 supabase = await createClient()
|
|
|
|
const yearParam = request.nextUrl.searchParams.get('year')
|
|
const year = Number.parseInt(yearParam ?? '', 10) || new Date().getFullYear()
|
|
|
|
const { data: incidents, error } = await supabase
|
|
.from('incidents')
|
|
.select(`
|
|
reference_no, incident_type, description, reported_at,
|
|
medical_status, lost_days,
|
|
is_fatality, is_serious_bodily_injury, is_dangerous_occurrence, is_occupational_disease,
|
|
sites (name),
|
|
zones (name),
|
|
reporter:users!reported_by (name),
|
|
dosh_reports (form_type, status, submitted_at)
|
|
`)
|
|
.gte('reported_at', `${year}-01-01T00:00:00Z`)
|
|
.lt('reported_at', `${year + 1}-01-01T00:00:00Z`)
|
|
.order('reported_at', { ascending: true })
|
|
|
|
if (error) return NextResponse.json({ error: 'Fetch failed' }, { status: 500 })
|
|
|
|
const rows = buildJkkp8Rows((incidents ?? []) as unknown as Jkkp8Incident[])
|
|
const csv = jkkp8Csv(rows)
|
|
|
|
await supabase.rpc('write_audit_log', {
|
|
p_table_name: 'incidents',
|
|
p_record_id: session.sub,
|
|
p_action: 'jkkp8_register_export',
|
|
p_new_value: { year, row_count: rows.length },
|
|
})
|
|
|
|
return new NextResponse(csv, {
|
|
status: 200,
|
|
headers: {
|
|
'Content-Type': 'text/csv; charset=utf-8',
|
|
'Content-Disposition': `attachment; filename="jkkp8-register-${year}.csv"`,
|
|
},
|
|
})
|
|
}
|