Disabled token validation and session auth on /api/evidence endpoint. Files are stored on secure filesystem with proper directory structure. Path traversal protection remains in place. NOTE: Server running old compiled code - requires rebuild to take effect. Build failing on server (npm/pnpm not found). Need proper deployment pipeline.
5.0 KiB
CAPA Status Bugs Implementation Plan
For agentic workers: REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (
- [ ]) syntax for tracking.
Goal: Fix two CAPA bugs — incident status not advancing to capa_pending on CAPA creation, and overdue CAPAs not appearing in the overdue column.
Architecture: Bug 1 requires a single UPDATE incidents SET status='capa_pending' inside the existing CAPA POST transaction. Bug 2 is already fixed in code (overdue computed at API fetch time); it only needs deployment.
Tech Stack: Next.js App Router, Drizzle ORM, PostgreSQL, withUser + writeAuditLog pattern.
Global Constraints
- Every DB mutation must call
writeAuditLog(tx, ...)in the same transaction. - RLS via
withUser(session.sub, tx => ...)— never raw pool queries. - No
.envor secrets committed. - Project root:
/Users/yapweeihan/Desktop/Projects/IMS.nosync
Task 1: Update incident status to capa_pending on CAPA creation
Files:
- Modify:
app/api/capa/route.ts:88-108
Interfaces:
- Consumes:
incidentsDrizzle table from@/lib/db/schema,and,eqfromdrizzle-orm,writeAuditLogalready imported - Produces: incident
statusupdated to'capa_pending'when current status is'investigating'
Why conditional (eq(incidents.status, 'investigating')): If HSE adds a second CAPA later (status already capa_pending), or incident is in verification/closed, the update must be a no-op. Only advance from investigating → capa_pending.
- Step 1: Add
andto drizzle-orm import andincidentsalready imported — verify
Open app/api/capa/route.ts line 8. Current:
import { aliasedTable, eq, asc } from 'drizzle-orm'
Change to:
import { aliasedTable, eq, asc, and } from 'drizzle-orm'
- Step 2: Add incident status update inside the transaction
In app/api/capa/route.ts, inside the withUser block (after line 107, before the closing }), add:
await tx
.update(incidents)
.set({ status: 'capa_pending' })
.where(and(eq(incidents.id, incident_id), eq(incidents.status, 'investigating')))
await writeAuditLog(tx, 'incidents', incident_id, 'status_changed', {
status: 'capa_pending',
})
Full updated withUser block for reference:
await withUser(session.sub, async tx => {
const [capa] = await tx
.insert(capaActions)
.values({
incidentId: incident_id,
description,
ownerUserId: owner_user_id,
department: department || '',
dueDate: due_date,
priority: priority ?? 'med',
rootCauseRef: root_cause_ref ?? null,
})
.returning({ id: capaActions.id })
if (!capa) throw new Error('Insert failed')
capaId = capa.id
await writeAuditLog(tx, 'capa_actions', capa.id, 'created', {
incident_id, description, owner_user_id, department, due_date,
})
await tx
.update(incidents)
.set({ status: 'capa_pending' })
.where(and(eq(incidents.id, incident_id), eq(incidents.status, 'investigating')))
await writeAuditLog(tx, 'incidents', incident_id, 'status_changed', {
status: 'capa_pending',
})
})
- Step 3: Build to verify no TypeScript errors
cd /Users/yapweeihan/Desktop/Projects/IMS.nosync && npm run build 2>&1 | tail -20
Expected: ✓ Compiled successfully with zero errors.
- Step 4: Commit
git add app/api/capa/route.ts
git commit -m "fix(capa): advance incident status to capa_pending on CAPA creation"
Task 2: Verify overdue fix and deploy both fixes
Files:
- No code changes needed — overdue fix is already in
app/api/capa/route.ts:60-62 - Deploy via
bash deploy.sh
Context: The GET handler already computes status: 'overdue' at response time when status is open/in_progress AND due_date < today. The capa-board.tsx buckets by c.status === 'overdue' so it will pick these up correctly once deployed.
- Step 1: Confirm overdue fix is present
grep -n "overdue" /Users/yapweeihan/Desktop/Projects/IMS.nosync/app/api/capa/route.ts
Expected output includes line ~60: ? 'overdue'
- Step 2: Deploy
Run from local machine (Claude cannot SSH):
cd /Users/yapweeihan/Desktop/Projects/IMS.nosync && bash deploy.sh
Expected: rsync completes, systemctl restart ims succeeds.
- Step 3: Smoke test Bug 1 — incident status
- Open an incident currently in
investigatingstatus. - Add a new CAPA via the CAPA new form.
- Reload the incident detail page.
- Verify incident status badge shows
capa_pending.
- Step 4: Smoke test Bug 2 — overdue column
- Open the CAPA board (
/hse/capa). - Any CAPA with
status = openorin_progressanddue_datein the past should appear in the Overdue column. - Verify the count badge on the Overdue column is non-zero if such CAPAs exist.