fix(capa): atomic in_progress guard on submit, add behavioural tests
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { eq } from 'drizzle-orm'
|
||||
import { and, eq } from 'drizzle-orm'
|
||||
import { getSession } from '@/lib/auth/get-session'
|
||||
import { withUser } from '@/lib/db/with-user'
|
||||
import { writeAuditLog } from '@/lib/db/audit'
|
||||
@@ -9,6 +9,8 @@ import { capaActions, evidenceFiles } from '@/lib/db/schema'
|
||||
import { uploadEvidenceFile } from '@/lib/storage/evidence'
|
||||
import { validateCapaSubmission } from '@/lib/capa/submit-validation'
|
||||
|
||||
class StatusConflict extends Error {}
|
||||
|
||||
export async function POST(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
@@ -53,36 +55,44 @@ export async function POST(
|
||||
}
|
||||
|
||||
const completedAt = new Date()
|
||||
await withUser(session.sub, async tx => {
|
||||
const rows = await tx.insert(evidenceFiles).values(
|
||||
uploaded.map(u => ({
|
||||
incidentId: capa.incidentId,
|
||||
capaId: id,
|
||||
stage: 'capa' as const,
|
||||
fileUrl: u.publicUrl,
|
||||
fileType: u.type,
|
||||
fileHash: u.hash,
|
||||
uploadedBy: session.sub,
|
||||
}))
|
||||
).returning({ id: evidenceFiles.id })
|
||||
|
||||
await tx.update(capaActions).set({
|
||||
status: 'pending_verification',
|
||||
ownerNotes: check.note,
|
||||
completedAt,
|
||||
}).where(eq(capaActions.id, id))
|
||||
|
||||
await writeAuditLog(
|
||||
tx, 'capa_actions', id, 'submitted_for_verification',
|
||||
{
|
||||
try {
|
||||
await withUser(session.sub, async tx => {
|
||||
const updated = await tx.update(capaActions).set({
|
||||
status: 'pending_verification',
|
||||
owner_notes: check.note,
|
||||
completed_at: completedAt.toISOString(),
|
||||
evidence_file_ids: rows.map(r => r.id),
|
||||
},
|
||||
{ status: 'in_progress' },
|
||||
)
|
||||
})
|
||||
ownerNotes: check.note,
|
||||
completedAt,
|
||||
}).where(and(eq(capaActions.id, id), eq(capaActions.status, 'in_progress')))
|
||||
.returning({ id: capaActions.id })
|
||||
if (updated.length === 0) throw new StatusConflict()
|
||||
|
||||
const rows = await tx.insert(evidenceFiles).values(
|
||||
uploaded.map(u => ({
|
||||
incidentId: capa.incidentId,
|
||||
capaId: id,
|
||||
stage: 'capa' as const,
|
||||
fileUrl: u.publicUrl,
|
||||
fileType: u.type,
|
||||
fileHash: u.hash,
|
||||
uploadedBy: session.sub,
|
||||
}))
|
||||
).returning({ id: evidenceFiles.id })
|
||||
|
||||
await writeAuditLog(
|
||||
tx, 'capa_actions', id, 'submitted_for_verification',
|
||||
{
|
||||
status: 'pending_verification',
|
||||
owner_notes: check.note,
|
||||
completed_at: completedAt.toISOString(),
|
||||
evidence_file_ids: rows.map(r => r.id),
|
||||
},
|
||||
{ status: 'in_progress' },
|
||||
)
|
||||
})
|
||||
} catch (err) {
|
||||
if (err instanceof StatusConflict)
|
||||
return NextResponse.json({ error: 'CAPA must be in_progress' }, { status: 409 })
|
||||
throw err
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user