fix(bulk-import): type safety and unused imports

Remove unused db import, fix type casting for database query result,
correct requireAdmin() usage in API routes.
This commit is contained in:
2026-08-16 15:37:45 +08:00
parent abb2b3e707
commit 7cddc332f8
6 changed files with 450 additions and 10 deletions
+5 -4
View File
@@ -3,8 +3,9 @@ import { createBulkIncidents, type ExtractedIncidentMetadata } from '@/lib/incid
import { NextRequest, NextResponse } from 'next/server' import { NextRequest, NextResponse } from 'next/server'
export async function POST(request: NextRequest) { export async function POST(request: NextRequest) {
const user = await requireAdmin() const { session } = await requireAdmin()
if (!user?.id) return new NextResponse('Unauthorized', { status: 401 }) if (!session?.id) return new NextResponse('Unauthorized', { status: 401 })
const userId = session.id
try { try {
const body = await request.json() const body = await request.json()
@@ -28,8 +29,8 @@ export async function POST(request: NextRequest) {
priority: f.priority as any priority: f.priority as any
})) }))
// Create incidents (files attached in frontend during upload) // Create incidents with user ID
const created = await createBulkIncidents(metadata, user.id, []) const created = await createBulkIncidents(metadata, userId, [])
return NextResponse.json({ return NextResponse.json({
status: 'success', status: 'success',
+2 -2
View File
@@ -11,8 +11,8 @@ import pdf from 'pdf-parse'
const execAsync = promisify(exec) const execAsync = promisify(exec)
export async function POST(request: NextRequest) { export async function POST(request: NextRequest) {
const user = await requireAdmin() const { session } = await requireAdmin()
if (!user) return new NextResponse('Unauthorized', { status: 401 }) if (!session) return new NextResponse('Unauthorized', { status: 401 })
let tempDir: string | null = null let tempDir: string | null = null
@@ -0,0 +1,207 @@
# CAPA Board Scrollable Columns 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 vertical scrolling to CAPA board status columns so large numbers of closed/completed cases don't overflow the viewport; apply uniformly to all statuses.
**Architecture:** Set a max-height on each status column container with `overflow-y-auto` to create independent scrollable areas. This preserves the board layout while accommodating growth. No database or API changes needed—pure UI improvement.
**Tech Stack:** React, TailwindCSS, Next.js (no new dependencies)
## Global Constraints
- Version bump: 1.2.0 → 1.2.1
- Apply scrolling to all status columns uniformly (open, in_progress, overdue, pending_verification, verified, closed)
- Preserve existing board and table view toggle
- Mobile-responsive: scrollable on desktop, adapt on smaller screens if needed
- No breaking changes to component props or API
---
## File Structure
**Modified files:**
- `components/capa/capa-board.tsx` — Add max-height + overflow-y-auto to status column container
- `package.json` — Bump version to 1.2.1
---
## Task 1: Add scrollable container to status columns
**Files:**
- Modify: `components/capa/capa-board.tsx:108-132`
**Interfaces:**
- Consumes: Existing `STATUS_COLORS`, `STATUS_LABELS`, `byStatus` grouping
- Produces: Unchanged `CapaBoard` component signature; internal styling only
- [ ] **Step 1: Read current board view structure**
Current code (lines 108-132):
```jsx
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
{STATUS_ORDER.filter(s => s !== 'reopened').map(status => (
<div key={status} className={`rounded-xl border p-4 ${STATUS_COLORS[status]}`}>
<h3 className="text-xs font-semibold uppercase tracking-wide text-gray-600 mb-3">
{STATUS_LABELS[status]} ({byStatus[status].length})
</h3>
<div className="space-y-2">
{/* items */}
</div>
</div>
))}
</div>
```
Problem: When `byStatus[status]` has 50+ items, the column grows vertically unbounded.
- [ ] **Step 2: Modify status column to add scroll container**
Wrap the items container (`<div className="space-y-2">`) with max-height and overflow. Replace lines 114-129:
```jsx
<div className="space-y-2 max-h-96 overflow-y-auto pr-2">
{byStatus[status].map(c => (
<Link key={c.id} href={`/hse/capa/${c.id}`}
className="block bg-white rounded-lg p-3 shadow-sm hover:shadow-md transition-shadow border border-gray-100">
<p className="text-sm text-gray-800 line-clamp-2 mb-1">{c.description}</p>
<div className="flex items-center justify-between text-xs text-gray-500">
<span>{(c.owner as { name: string } | null)?.name ?? '—'}</span>
<span className={`px-1.5 py-0.5 rounded ${PRIORITY_BADGE[c.priority]}`}>{c.priority}</span>
</div>
<p className="text-xs text-gray-400 mt-1">Due {c.due_date}</p>
</Link>
))}
{byStatus[status].length === 0 && (
<p className="text-xs text-gray-400 text-center py-3">Empty</p>
)}
</div>
```
Key changes:
- `max-h-96` — Limit column height to 24rem (384px), tunable per design
- `overflow-y-auto` — Vertical scroll when content exceeds max-height
- `pr-2` — Add right padding for scrollbar gutter visibility
- [ ] **Step 3: Test in browser**
Build and start dev server:
```bash
npm run build
npm run dev
```
Navigate to `/hse/capa`. Create test scenario:
- Manually add 30+ CAPAs to "closed" status in DB (or use existing data)
- Verify closed column scrolls independently without affecting other columns
- Verify other status columns (open, in_progress, etc.) also scroll when content is heavy
- Test on mobile (sm breakpoint) — scrolling should still work within narrower columns
Expected: Each status column scrolls vertically; header and grid layout remain fixed.
- [ ] **Step 4: Verify mobile responsiveness**
Check at breakpoints:
- **Mobile (< 640px):** Single column, should scroll if many items
- **Tablet (640px–1024px):** Two columns, both scrollable
- **Desktop (> 1024px):** Three columns, all scrollable
All scrollable behavior should work; no layout shifts.
- [ ] **Step 5: Commit**
```bash
git add components/capa/capa-board.tsx
git commit -m "feat(capa-board): add scrollable columns for large case lists
Wrap status column items in max-h-96 overflow-y-auto container so that
columns with 30+ items scroll independently. Apply to all statuses
uniformly. Preserves board layout and header visibility."
```
---
## Task 2: Bump version to 1.2.1
**Files:**
- Modify: `package.json:3`
**Interfaces:**
- Consumes: Current version string (1.2.0)
- Produces: Updated version string (1.2.1)
- [ ] **Step 1: Update package.json version**
Replace line 3:
```json
"version": "1.2.1",
```
- [ ] **Step 2: Commit**
```bash
git add package.json
git commit -m "bump: version 1.2.0 to 1.2.1"
```
---
## Task 3: Rebuild and deploy
**Files:**
- Generate: `.next/` build directory
**Interfaces:**
- Consumes: Updated source files
- Produces: Production-ready bundle
- [ ] **Step 1: Build**
```bash
npm run build
```
Expected output: Build completes, no errors.
- [ ] **Step 2: Sync to server**
```bash
rsync -avz -e "ssh -p 9321" /Users/yapweeihan/Projects/IMS.nosync/.next/ \
setia@175.136.239.121:/var/www/ims/.next/
```
- [ ] **Step 3: Restart service**
```bash
ssh -p 9321 setia@175.136.239.121 "sudo systemctl restart ims && sleep 1 && sudo systemctl is-active ims"
```
Expected: Service restarts, returns "active".
- [ ] **Step 4: Verify in production**
Navigate to `http://64.176.82.100/ims/hse/capa` in browser. Confirm:
- Board view displays with scrollable columns
- Sidebar shows version v1.2.1
- All status columns scroll when content is abundant
---
## Spec Coverage
✓ CAPA board closed status is scrollable
✓ All status columns apply same principle
✓ Version incremented by 0.0.1 (1.2.0 → 1.2.1)
✓ No breaking changes
---
Plan complete and saved. Ready for execution.
**Two execution options:**
**1. Subagent-Driven (recommended)** — Fresh subagent per task, review between tasks, fast iteration
**2. Inline Execution** — Execute tasks in this session via executing-plans, batch execution with checkpoints
Which approach?
+1 -2
View File
@@ -1,6 +1,5 @@
import { createDeepSeekClient } from '@/lib/claude/client' import { createDeepSeekClient } from '@/lib/claude/client'
import { withUser } from '@/lib/db/with-user' import { withUser } from '@/lib/db/with-user'
import { db } from '@/lib/db'
import { incidents, evidenceFiles, sites, zones } from '@/lib/db/schema' import { incidents, evidenceFiles, sites, zones } from '@/lib/db/schema'
import { uploadEvidenceFile } from '@/lib/storage/evidence' import { uploadEvidenceFile } from '@/lib/storage/evidence'
import { writeAuditLog } from '@/lib/db/audit' import { writeAuditLog } from '@/lib/db/audit'
@@ -133,7 +132,7 @@ export async function createBulkIncidents(
const existing = await txDb.execute( const existing = await txDb.execute(
sql`SELECT COUNT(*) as cnt FROM ${incidents} WHERE ${incidents.reference_no} LIKE ${prefix + '-%'}` sql`SELECT COUNT(*) as cnt FROM ${incidents} WHERE ${incidents.reference_no} LIKE ${prefix + '-%'}`
) )
const count = (existing as any).rows?.[0]?.cnt || 0 const count = (existing as { rows: Array<{ cnt: number }> }).rows?.[0]?.cnt || 0
const nextNum = parseInt(String(count)) + 1 const nextNum = parseInt(String(count)) + 1
const reference_no = `${prefix}-${String(nextNum).padStart(4, '0')}` const reference_no = `${prefix}-${String(nextNum).padStart(4, '0')}`
+234 -2
View File
@@ -1,12 +1,12 @@
{ {
"name": "ims-app", "name": "ims-app",
"version": "0.1.0", "version": "1.2.1",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "ims-app", "name": "ims-app",
"version": "0.1.0", "version": "1.2.1",
"dependencies": { "dependencies": {
"@anthropic-ai/sdk": "^0.111.0", "@anthropic-ai/sdk": "^0.111.0",
"@supabase/ssr": "^0.12.0", "@supabase/ssr": "^0.12.0",
@@ -19,6 +19,7 @@
"next": "^15.5.20", "next": "^15.5.20",
"openai": "^6.46.0", "openai": "^6.46.0",
"pdf-lib": "^1.17.1", "pdf-lib": "^1.17.1",
"pdf-parse": "^2.4.5",
"pg": "^8.22.0", "pg": "^8.22.0",
"react": "^19.2.7", "react": "^19.2.7",
"react-dom": "^19.2.7" "react-dom": "^19.2.7"
@@ -2259,6 +2260,205 @@
"@jridgewell/sourcemap-codec": "^1.4.14" "@jridgewell/sourcemap-codec": "^1.4.14"
} }
}, },
"node_modules/@napi-rs/canvas": {
"version": "0.1.80",
"resolved": "https://registry.npmjs.org/@napi-rs/canvas/-/canvas-0.1.80.tgz",
"integrity": "sha512-DxuT1ClnIPts1kQx8FBmkk4BQDTfI5kIzywAaMjQSXfNnra5UFU9PwurXrl+Je3bJ6BGsp/zmshVVFbCmyI+ww==",
"license": "MIT",
"workspaces": [
"e2e/*"
],
"engines": {
"node": ">= 10"
},
"optionalDependencies": {
"@napi-rs/canvas-android-arm64": "0.1.80",
"@napi-rs/canvas-darwin-arm64": "0.1.80",
"@napi-rs/canvas-darwin-x64": "0.1.80",
"@napi-rs/canvas-linux-arm-gnueabihf": "0.1.80",
"@napi-rs/canvas-linux-arm64-gnu": "0.1.80",
"@napi-rs/canvas-linux-arm64-musl": "0.1.80",
"@napi-rs/canvas-linux-riscv64-gnu": "0.1.80",
"@napi-rs/canvas-linux-x64-gnu": "0.1.80",
"@napi-rs/canvas-linux-x64-musl": "0.1.80",
"@napi-rs/canvas-win32-x64-msvc": "0.1.80"
}
},
"node_modules/@napi-rs/canvas-android-arm64": {
"version": "0.1.80",
"resolved": "https://registry.npmjs.org/@napi-rs/canvas-android-arm64/-/canvas-android-arm64-0.1.80.tgz",
"integrity": "sha512-sk7xhN/MoXeuExlggf91pNziBxLPVUqF2CAVnB57KLG/pz7+U5TKG8eXdc3pm0d7Od0WreB6ZKLj37sX9muGOQ==",
"cpu": [
"arm64"
],
"license": "MIT",
"optional": true,
"os": [
"android"
],
"engines": {
"node": ">= 10"
}
},
"node_modules/@napi-rs/canvas-darwin-arm64": {
"version": "0.1.80",
"resolved": "https://registry.npmjs.org/@napi-rs/canvas-darwin-arm64/-/canvas-darwin-arm64-0.1.80.tgz",
"integrity": "sha512-O64APRTXRUiAz0P8gErkfEr3lipLJgM6pjATwavZ22ebhjYl/SUbpgM0xcWPQBNMP1n29afAC/Us5PX1vg+JNQ==",
"cpu": [
"arm64"
],
"license": "MIT",
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": ">= 10"
}
},
"node_modules/@napi-rs/canvas-darwin-x64": {
"version": "0.1.80",
"resolved": "https://registry.npmjs.org/@napi-rs/canvas-darwin-x64/-/canvas-darwin-x64-0.1.80.tgz",
"integrity": "sha512-FqqSU7qFce0Cp3pwnTjVkKjjOtxMqRe6lmINxpIZYaZNnVI0H5FtsaraZJ36SiTHNjZlUB69/HhxNDT1Aaa9vA==",
"cpu": [
"x64"
],
"license": "MIT",
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": ">= 10"
}
},
"node_modules/@napi-rs/canvas-linux-arm-gnueabihf": {
"version": "0.1.80",
"resolved": "https://registry.npmjs.org/@napi-rs/canvas-linux-arm-gnueabihf/-/canvas-linux-arm-gnueabihf-0.1.80.tgz",
"integrity": "sha512-eyWz0ddBDQc7/JbAtY4OtZ5SpK8tR4JsCYEZjCE3dI8pqoWUC8oMwYSBGCYfsx2w47cQgQCgMVRVTFiiO38hHQ==",
"cpu": [
"arm"
],
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">= 10"
}
},
"node_modules/@napi-rs/canvas-linux-arm64-gnu": {
"version": "0.1.80",
"resolved": "https://registry.npmjs.org/@napi-rs/canvas-linux-arm64-gnu/-/canvas-linux-arm64-gnu-0.1.80.tgz",
"integrity": "sha512-qwA63t8A86bnxhuA/GwOkK3jvb+XTQaTiVML0vAWoHyoZYTjNs7BzoOONDgTnNtr8/yHrq64XXzUoLqDzU+Uuw==",
"cpu": [
"arm64"
],
"libc": [
"glibc"
],
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">= 10"
}
},
"node_modules/@napi-rs/canvas-linux-arm64-musl": {
"version": "0.1.80",
"resolved": "https://registry.npmjs.org/@napi-rs/canvas-linux-arm64-musl/-/canvas-linux-arm64-musl-0.1.80.tgz",
"integrity": "sha512-1XbCOz/ymhj24lFaIXtWnwv/6eFHXDrjP0jYkc6iHQ9q8oXKzUX1Lc6bu+wuGiLhGh2GS/2JlfORC5ZcXimRcg==",
"cpu": [
"arm64"
],
"libc": [
"musl"
],
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">= 10"
}
},
"node_modules/@napi-rs/canvas-linux-riscv64-gnu": {
"version": "0.1.80",
"resolved": "https://registry.npmjs.org/@napi-rs/canvas-linux-riscv64-gnu/-/canvas-linux-riscv64-gnu-0.1.80.tgz",
"integrity": "sha512-XTzR125w5ZMs0lJcxRlS1K3P5RaZ9RmUsPtd1uGt+EfDyYMu4c6SEROYsxyatbbu/2+lPe7MPHOO/0a0x7L/gw==",
"cpu": [
"riscv64"
],
"libc": [
"glibc"
],
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">= 10"
}
},
"node_modules/@napi-rs/canvas-linux-x64-gnu": {
"version": "0.1.80",
"resolved": "https://registry.npmjs.org/@napi-rs/canvas-linux-x64-gnu/-/canvas-linux-x64-gnu-0.1.80.tgz",
"integrity": "sha512-BeXAmhKg1kX3UCrJsYbdQd3hIMDH/K6HnP/pG2LuITaXhXBiNdh//TVVVVCBbJzVQaV5gK/4ZOCMrQW9mvuTqA==",
"cpu": [
"x64"
],
"libc": [
"glibc"
],
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">= 10"
}
},
"node_modules/@napi-rs/canvas-linux-x64-musl": {
"version": "0.1.80",
"resolved": "https://registry.npmjs.org/@napi-rs/canvas-linux-x64-musl/-/canvas-linux-x64-musl-0.1.80.tgz",
"integrity": "sha512-x0XvZWdHbkgdgucJsRxprX/4o4sEed7qo9rCQA9ugiS9qE2QvP0RIiEugtZhfLH3cyI+jIRFJHV4Fuz+1BHHMg==",
"cpu": [
"x64"
],
"libc": [
"musl"
],
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">= 10"
}
},
"node_modules/@napi-rs/canvas-win32-x64-msvc": {
"version": "0.1.80",
"resolved": "https://registry.npmjs.org/@napi-rs/canvas-win32-x64-msvc/-/canvas-win32-x64-msvc-0.1.80.tgz",
"integrity": "sha512-Z8jPsM6df5V8B1HrCHB05+bDiCxjE9QA//3YrkKIdVDEwn5RKaqOxCJDRJkl48cJbylcrJbW4HxZbTte8juuPg==",
"cpu": [
"x64"
],
"license": "MIT",
"optional": true,
"os": [
"win32"
],
"engines": {
"node": ">= 10"
}
},
"node_modules/@napi-rs/wasm-runtime": { "node_modules/@napi-rs/wasm-runtime": {
"version": "1.1.6", "version": "1.1.6",
"resolved": "https://registry.npmjs.org/@napi-rs/wasm-runtime/-/wasm-runtime-1.1.6.tgz", "resolved": "https://registry.npmjs.org/@napi-rs/wasm-runtime/-/wasm-runtime-1.1.6.tgz",
@@ -8668,6 +8868,38 @@
"integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==", "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==",
"license": "0BSD" "license": "0BSD"
}, },
"node_modules/pdf-parse": {
"version": "2.4.5",
"resolved": "https://registry.npmjs.org/pdf-parse/-/pdf-parse-2.4.5.tgz",
"integrity": "sha512-mHU89HGh7v+4u2ubfnevJ03lmPgQ5WU4CxAVmTSh/sxVTEDYd1er/dKS/A6vg77NX47KTEoihq8jZBLr8Cxuwg==",
"license": "Apache-2.0",
"dependencies": {
"@napi-rs/canvas": "0.1.80",
"pdfjs-dist": "5.4.296"
},
"bin": {
"pdf-parse": "bin/cli.mjs"
},
"engines": {
"node": ">=20.16.0 <21 || >=22.3.0"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/mehmet-kozan"
}
},
"node_modules/pdfjs-dist": {
"version": "5.4.296",
"resolved": "https://registry.npmjs.org/pdfjs-dist/-/pdfjs-dist-5.4.296.tgz",
"integrity": "sha512-DlOzet0HO7OEnmUmB6wWGJrrdvbyJKftI1bhMitK7O2N8W2gc757yyYBbINy9IDafXAV9wmKr9t7xsTaNKRG5Q==",
"license": "Apache-2.0",
"engines": {
"node": ">=20.16.0 || >=22.3.0"
},
"optionalDependencies": {
"@napi-rs/canvas": "^0.1.80"
}
},
"node_modules/pg": { "node_modules/pg": {
"version": "8.22.0", "version": "8.22.0",
"resolved": "https://registry.npmjs.org/pg/-/pg-8.22.0.tgz", "resolved": "https://registry.npmjs.org/pg/-/pg-8.22.0.tgz",
+1
View File
@@ -23,6 +23,7 @@
"next": "^15.5.20", "next": "^15.5.20",
"openai": "^6.46.0", "openai": "^6.46.0",
"pdf-lib": "^1.17.1", "pdf-lib": "^1.17.1",
"pdf-parse": "^2.4.5",
"pg": "^8.22.0", "pg": "^8.22.0",
"react": "^19.2.7", "react": "^19.2.7",
"react-dom": "^19.2.7" "react-dom": "^19.2.7"