Files
ims/app/api/capa/[id]/verify/route.ts
T
adminandClaude Sonnet 4.6 d18d29168a feat(auth): phase 3 — replace Supabase GoTrue with bcryptjs+jose
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>
2026-07-23 16:20:04 +08:00

86 lines
3.0 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 { createInAppNotifications } from '@/lib/notifications/in-app'
export async function POST(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const { id } = await params
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 { data: capa } = await supabase
.from('capa_actions').select('status, incident_id, owner_user_id').eq('id', id).single()
if (!capa) return NextResponse.json({ error: 'Not found' }, { status: 404 })
if (capa.status !== 'pending_verification')
return NextResponse.json({ error: 'CAPA must be pending_verification' }, { status: 409 })
const body: { verdict: 'verified' | 'reopened'; reopen_reason?: string } = await request.json()
if (body.verdict !== 'verified' && body.verdict !== 'reopened')
return NextResponse.json({ error: 'verdict must be verified or reopened' }, { status: 422 })
const verifiedAt = new Date()
const recheckDate = new Date(verifiedAt)
recheckDate.setUTCDate(recheckDate.getUTCDate() + 30)
const update: Record<string, unknown> = {
status: body.verdict,
verified_by: session.sub,
verified_at: verifiedAt.toISOString(),
...(body.verdict === 'verified'
? {
effectiveness_recheck_date: recheckDate.toISOString().split('T')[0],
effectiveness_recheck_round: 0,
}
: {}),
}
const { error } = await supabase
.from('capa_actions').update(update).eq('id', id)
if (error) return NextResponse.json({ error: 'Update failed' }, { status: 500 })
await supabase.rpc('write_audit_log', {
p_table_name: 'capa_actions',
p_record_id: id,
p_action: body.verdict === 'verified' ? 'verified' : 'reopened',
p_new_value: { ...update, reopen_reason: body.reopen_reason ?? null },
})
if (capa.owner_user_id) {
await createInAppNotifications(supabase, [{
userId: capa.owner_user_id,
title: body.verdict === 'verified'
? 'Your CAPA action was verified'
: `Your CAPA action was reopened${body.reopen_reason ? `: ${body.reopen_reason}` : ''}`,
link: `/hse/capa/${id}`,
incidentId: capa.incident_id,
capaId: id,
}])
}
// Check if all CAPAs for this incident are verified — if so, transition incident to verification
const { data: openCapas } = await supabase
.from('capa_actions')
.select('id')
.eq('incident_id', capa.incident_id)
.not('status', 'in', '(verified,closed)')
if (!openCapas || openCapas.length === 0) {
await supabase
.from('incidents')
.update({ status: 'verification' })
.eq('id', capa.incident_id)
}
return NextResponse.json({ ok: true })
}