Convert lib/settings.ts, lib/notifications/{in-app,email,capa-escalation,
effectiveness-recheck}.ts from Supabase PostgREST to Drizzle asAdmin queries.
Drop supabase arg from all call sites in app/api/ and cron routes. Rewrite
notification unit tests to mock @/lib/db/with-user instead of SupabaseClient.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
86 lines
3.0 KiB
TypeScript
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([{
|
|
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 })
|
|
}
|