# CAPA Owner Notes 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:** Add an `owner_notes` remarks field so CAPA owners can record notes/updates against their assigned CAPA actions. **Architecture:** Add a nullable `TEXT` column to `capa_actions` via a new Supabase migration. Expose it through the existing PATCH and GET API routes. Render a textarea + save button (`CapaOwnerNotesForm`) inline on each card on the CAPA owner list page. Display saved notes read-only on the HSE CAPA detail page. **Tech Stack:** Next.js 15 App Router, Supabase (Postgres + RLS), TypeScript, Tailwind CSS, Vitest ## Global Constraints - Project root: `/Users/yapweeihan/Desktop/Projects/IMS.nosync` - basePath: `/ims` — all hardcoded API fetch paths must start with `/ims/api/...` - Admin client (`createAdminClient`) must be used for DB writes in PATCH handler (RLS blocks user-session updates) - `owner_notes` is editable by CAPA owners and HSE/admin; readable by HSE/admin and the owner - No new dependencies — use existing Supabase client, fetch, React state - DB migration file naming: `20260723000001_capa_owner_notes.sql` - Vitest for tests: run with `npm test` from project root --- ### Task 1: DB Migration — add `owner_notes` column **Files:** - Create: `supabase/migrations/20260723000001_capa_owner_notes.sql` **Interfaces:** - Produces: `capa_actions.owner_notes TEXT` column (nullable, default NULL) - [ ] **Step 1: Create migration file** ```sql -- supabase/migrations/20260723000001_capa_owner_notes.sql ALTER TABLE capa_actions ADD COLUMN owner_notes TEXT; ``` - [ ] **Step 2: Apply migration to Supabase (remote)** ```bash npx supabase db push --db-url "$(grep NEXT_PUBLIC_SUPABASE_URL .env.production | cut -d= -f2)" ``` If `supabase db push` isn't available, apply via the Supabase Dashboard SQL editor: ```sql ALTER TABLE capa_actions ADD COLUMN owner_notes TEXT; ``` Verify by running in the Supabase SQL editor: ```sql SELECT column_name, data_type, is_nullable FROM information_schema.columns WHERE table_name = 'capa_actions' AND column_name = 'owner_notes'; ``` Expected: one row with `data_type = text`, `is_nullable = YES`. - [ ] **Step 3: Commit** ```bash git add supabase/migrations/20260723000001_capa_owner_notes.sql git commit -m "feat(db): add owner_notes column to capa_actions" ``` --- ### Task 2: API — expose `owner_notes` in GET and PATCH **Files:** - Modify: `app/api/capa/[id]/route.ts` **Interfaces:** - Consumes: `owner_notes TEXT | null` from DB (Task 1) - Produces: - GET response includes `owner_notes: string | null` - PATCH accepts `{ owner_notes: string }` in request body and writes it - [ ] **Step 1: Update GET select to include `owner_notes`** In `app/api/capa/[id]/route.ts`, find the `.select(...)` call in the `GET` handler (around line 16). Add `owner_notes` to the field list: ```typescript const { data, error } = await supabase .from('capa_actions') .select(` id, incident_id, root_cause_ref, description, owner_user_id, department, due_date, priority, status, completed_at, verified_by, verified_at, created_at, owner_notes, incidents (reference_no, incident_type), owner:users!owner_user_id (name, email) `) .eq('id', id) .single() ``` - [ ] **Step 2: Add `owner_notes` to the PATCH allowed fields** In the same file, find the `allowed` array (around line 68): ```typescript const allowed = ['description', 'due_date', 'priority', 'status', 'department', 'root_cause_ref', 'owner_notes'] ``` - [ ] **Step 3: Verify build passes** ```bash npm run build 2>&1 | grep -E "error|Error|✓ Compiled" ``` Expected: `✓ Compiled successfully` - [ ] **Step 4: Commit** ```bash git add 'app/api/capa/[id]/route.ts' git commit -m "feat(api): expose owner_notes in capa GET and PATCH" ``` --- ### Task 3: `CapaOwnerNotesForm` component **Files:** - Create: `components/capa/capa-owner-notes-form.tsx` **Interfaces:** - Consumes: `capaId: string`, `initialNotes: string | null` - Produces: exported `CapaOwnerNotesForm` React client component - [ ] **Step 1: Create the component** ```tsx // components/capa/capa-owner-notes-form.tsx 'use client' import { useState } from 'react' interface Props { capaId: string initialNotes: string | null } export function CapaOwnerNotesForm({ capaId, initialNotes }: Props) { const [notes, setNotes] = useState(initialNotes ?? '') const [saving, setSaving] = useState(false) const [saved, setSaved] = useState(false) const [error, setError] = useState(null) async function handleSave() { setSaving(true) setSaved(false) setError(null) const res = await fetch(`/ims/api/capa/${capaId}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ owner_notes: notes }), }) setSaving(false) if (!res.ok) { setError('Failed to save. Please try again.') return } setSaved(true) setTimeout(() => setSaved(false), 2000) } return (