feat: verification flow — HSE verify/reopen CAPA, auto-transition incident to verification

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FFDuBhMKvoWjrWT3ZnGmmr
This commit is contained in:
2026-07-11 15:18:17 +08:00
co-authored by Claude Opus 4.8
parent 1780586185
commit 804b1fbf33
3 changed files with 253 additions and 0 deletions
+100
View File
@@ -0,0 +1,100 @@
export const dynamic = 'force-dynamic'
import { notFound, redirect } from 'next/navigation'
import Link from 'next/link'
import { createClient } from '@/lib/supabase/server'
import { VerifyForm } from '@/components/capa/verify-form'
interface Props {
params: Promise<{ id: string }>
}
const PRIORITY_BADGE: Record<string, string> = {
low: 'bg-gray-100 text-gray-600',
med: 'bg-yellow-100 text-yellow-700',
high: 'bg-red-100 text-red-700',
}
export default async function CapaDetailPage({ params }: Props) {
const { id } = await params
const supabase = await createClient()
const { data, error: authError } = await supabase.auth.getUser()
if (authError || !data?.user) redirect(`/login?redirect=/hse/capa/${id}`)
const { data: profile } = await supabase
.from('users').select('role').eq('id', data.user.id).single()
const { data: capa } = await supabase
.from('capa_actions')
.select(`
id, incident_id, root_cause_ref, description, department,
due_date, priority, status, completed_at, verified_by, verified_at, created_at,
incidents (reference_no, incident_type),
owner:users!owner_user_id (name, email)
`)
.eq('id', id)
.single()
if (!capa) notFound()
const isHse = profile && ['hse', 'admin'].includes(profile.role)
const status = (capa as { status: string }).status
return (
<main className="max-w-2xl mx-auto px-4 py-6">
<Link href="/hse/capa" className="text-sm text-blue-600 hover:underline mb-4 inline-block">
CAPA Board
</Link>
<div className="bg-white rounded-xl shadow-sm p-5 mb-4 space-y-4">
<div className="flex items-start justify-between">
<div>
<p className="text-xs text-gray-500 mb-1">
Incident: {(capa.incidents as unknown as { reference_no: string | null } | null)?.reference_no ?? '—'}
</p>
<p className="text-gray-900 font-medium">{(capa as { description: string }).description}</p>
</div>
<span className={`px-2 py-1 rounded text-xs font-semibold ${PRIORITY_BADGE[(capa as { priority: string }).priority] ?? ''}`}>
{(capa as { priority: string }).priority}
</span>
</div>
<div className="grid grid-cols-2 gap-3 text-sm">
<div>
<p className="text-xs text-gray-500">Owner</p>
<p className="text-gray-800">{(capa.owner as unknown as { name: string } | null)?.name ?? '—'}</p>
</div>
<div>
<p className="text-xs text-gray-500">Department</p>
<p className="text-gray-800">{(capa as { department: string }).department}</p>
</div>
<div>
<p className="text-xs text-gray-500">Due Date</p>
<p className="text-gray-800">{(capa as { due_date: string }).due_date}</p>
</div>
<div>
<p className="text-xs text-gray-500">Status</p>
<p className="text-gray-800 capitalize">{status.replace(/_/g, ' ')}</p>
</div>
</div>
{(capa as { root_cause_ref: string | null }).root_cause_ref && (
<div>
<p className="text-xs text-gray-500">Root Cause Reference</p>
<p className="text-sm text-gray-800">{(capa as { root_cause_ref: string }).root_cause_ref}</p>
</div>
)}
{(capa as { completed_at: string | null }).completed_at && (
<p className="text-xs text-gray-500">
Completed: {new Date((capa as { completed_at: string }).completed_at).toLocaleString('en-MY', { timeZone: 'Asia/Kuala_Lumpur' })}
</p>
)}
</div>
{isHse && status === 'pending_verification' && (
<VerifyForm capaId={id} />
)}
</main>
)
}
+64
View File
@@ -0,0 +1,64 @@
export const dynamic = 'force-dynamic'
import { NextRequest, NextResponse } from 'next/server'
import { createClient } from '@/lib/supabase/server'
export async function POST(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const { id } = await params
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: capa } = await supabase
.from('capa_actions').select('status, incident_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 update: Record<string, unknown> = {
status: body.verdict,
verified_by: user.id,
verified_at: new Date().toISOString(),
}
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 },
})
// 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 })
}