Files
ims/docs/superpowers/plans/2026-07-28-capa-status-bugs.md
admin 1e33ef2483 fix(evidence): disable auth on evidence route, filesystem storage already protected
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.
2026-08-16 09:44:57 +08:00

142 lines
5.0 KiB
Markdown

# 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 `.env` or 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: `incidents` Drizzle table from `@/lib/db/schema`, `and`, `eq` from `drizzle-orm`, `writeAuditLog` already imported
- Produces: incident `status` updated 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 `and` to drizzle-orm import and `incidents` already imported — verify**
Open `app/api/capa/route.ts` line 8. Current:
```typescript
import { aliasedTable, eq, asc } from 'drizzle-orm'
```
Change to:
```typescript
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:
```typescript
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:
```typescript
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**
```bash
cd /Users/yapweeihan/Desktop/Projects/IMS.nosync && npm run build 2>&1 | tail -20
```
Expected: `✓ Compiled successfully` with zero errors.
- [ ] **Step 4: Commit**
```bash
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**
```bash
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):
```bash
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**
1. Open an incident currently in `investigating` status.
2. Add a new CAPA via the CAPA new form.
3. Reload the incident detail page.
4. Verify incident status badge shows `capa_pending`.
- [ ] **Step 4: Smoke test Bug 2 — overdue column**
1. Open the CAPA board (`/hse/capa`).
2. Any CAPA with `status = open` or `in_progress` and `due_date` in the past should appear in the **Overdue** column.
3. Verify the count badge on the Overdue column is non-zero if such CAPAs exist.