feat(db): phase 4 group 6 — server component pages to Drizzle
Converts all 18 server component page files from Supabase client queries to Drizzle ORM using asAdmin. Adds getSession() + redirect to the three pages (hse/incidents, hse/incidents/[id], hse/dashboard) that lacked it. Maps snake_case component prop shapes explicitly where required. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -2,8 +2,11 @@ export const dynamic = 'force-dynamic'
|
||||
|
||||
import { notFound, redirect } from 'next/navigation'
|
||||
import Link from 'next/link'
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { asAdmin } from '@/lib/db/with-user'
|
||||
import { getSession } from '@/lib/auth/get-session'
|
||||
import { capaActions, incidents, users } from '@/lib/db/schema'
|
||||
import { eq } from 'drizzle-orm'
|
||||
import { aliasedTable } from 'drizzle-orm'
|
||||
import { VerifyForm } from '@/components/capa/verify-form'
|
||||
import { CloseCapaButton } from '@/components/capa/close-capa-button'
|
||||
|
||||
@@ -22,24 +25,34 @@ export default async function CapaDetailPage({ params }: Props) {
|
||||
const session = await getSession()
|
||||
if (!session) redirect(`/login?redirect=/hse/capa/${id}`)
|
||||
|
||||
const supabase = await createClient()
|
||||
const ownerAlias = aliasedTable(users, 'owner')
|
||||
|
||||
const { data: capa } = await supabase
|
||||
.from('capa_actions')
|
||||
.select(`
|
||||
id, incident_id, root_cause_ref, description, 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()
|
||||
const [capa] = await asAdmin(db =>
|
||||
db.select({
|
||||
id: capaActions.id,
|
||||
incidentId: capaActions.incidentId,
|
||||
rootCauseRef: capaActions.rootCauseRef,
|
||||
description: capaActions.description,
|
||||
department: capaActions.department,
|
||||
dueDate: capaActions.dueDate,
|
||||
priority: capaActions.priority,
|
||||
status: capaActions.status,
|
||||
completedAt: capaActions.completedAt,
|
||||
ownerNotes: capaActions.ownerNotes,
|
||||
incidentRefNo: incidents.referenceNo,
|
||||
ownerName: ownerAlias.name,
|
||||
})
|
||||
.from(capaActions)
|
||||
.leftJoin(incidents, eq(capaActions.incidentId, incidents.id))
|
||||
.leftJoin(ownerAlias, eq(capaActions.ownerUserId, ownerAlias.id))
|
||||
.where(eq(capaActions.id, id))
|
||||
.limit(1)
|
||||
)
|
||||
|
||||
if (!capa) notFound()
|
||||
|
||||
const isHse = session && ['hse', 'admin'].includes(session.role)
|
||||
const status = (capa as { status: string }).status
|
||||
const status = capa.status
|
||||
|
||||
return (
|
||||
<main className="max-w-2xl mx-auto px-4 py-6">
|
||||
@@ -51,52 +64,50 @@ export default async function CapaDetailPage({ params }: Props) {
|
||||
<div className="flex items-start justify-between">
|
||||
<div>
|
||||
<p className="text-xs text-gray-500 mb-1">
|
||||
Incident: {(capa.incidents as unknown as { reference_no: string | null } | null)?.reference_no ?? '—'}
|
||||
Incident: {capa.incidentRefNo ?? '—'}
|
||||
</p>
|
||||
<p className="text-gray-900 font-medium">{(capa as { description: string }).description}</p>
|
||||
<p className="text-gray-900 font-medium">{capa.description}</p>
|
||||
</div>
|
||||
<span className={`px-2 py-1 rounded text-xs font-semibold ${PRIORITY_BADGE[(capa as { priority: string }).priority] ?? ''}`}>
|
||||
{(capa as { priority: string }).priority}
|
||||
<span className={`px-2 py-1 rounded text-xs font-semibold ${PRIORITY_BADGE[capa.priority] ?? ''}`}>
|
||||
{capa.priority}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-2 gap-3 text-sm">
|
||||
<div>
|
||||
<p className="text-xs text-gray-500">Owner</p>
|
||||
<p className="text-gray-800">{(capa.owner as unknown as { name: string } | null)?.name ?? '—'}</p>
|
||||
<p className="text-gray-800">{capa.ownerName ?? '—'}</p>
|
||||
</div>
|
||||
<div>
|
||||
<p className="text-xs text-gray-500">Department</p>
|
||||
<p className="text-gray-800">{(capa as { department: string }).department}</p>
|
||||
<p className="text-gray-800">{capa.department}</p>
|
||||
</div>
|
||||
<div>
|
||||
<p className="text-xs text-gray-500">Due Date</p>
|
||||
<p className="text-gray-800">{(capa as { due_date: string }).due_date}</p>
|
||||
<p className="text-gray-800">{capa.dueDate}</p>
|
||||
</div>
|
||||
<div>
|
||||
<p className="text-xs text-gray-500">Status</p>
|
||||
<p className="text-gray-800 capitalize">{status.replace(/_/g, ' ')}</p>
|
||||
</div>
|
||||
{(capa as { owner_notes: string | null }).owner_notes && (
|
||||
{capa.ownerNotes && (
|
||||
<div className="col-span-2">
|
||||
<p className="text-xs text-gray-500">Owner Notes</p>
|
||||
<p className="text-sm text-gray-800 whitespace-pre-wrap">
|
||||
{(capa as { owner_notes: string }).owner_notes}
|
||||
</p>
|
||||
<p className="text-sm text-gray-800 whitespace-pre-wrap">{capa.ownerNotes}</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{(capa as { root_cause_ref: string | null }).root_cause_ref && (
|
||||
{capa.rootCauseRef && (
|
||||
<div>
|
||||
<p className="text-xs text-gray-500">Root Cause Reference</p>
|
||||
<p className="text-sm text-gray-800">{(capa as { root_cause_ref: string }).root_cause_ref}</p>
|
||||
<p className="text-sm text-gray-800">{capa.rootCauseRef}</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{(capa as { completed_at: string | null }).completed_at && (
|
||||
{capa.completedAt && (
|
||||
<p className="text-xs text-gray-500">
|
||||
Completed: {new Date((capa as { completed_at: string }).completed_at).toLocaleString('en-MY', { timeZone: 'Asia/Kuala_Lumpur' })}
|
||||
Completed: {new Date(capa.completedAt).toLocaleString('en-MY', { timeZone: 'Asia/Kuala_Lumpur' })}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -2,24 +2,48 @@ export const dynamic = 'force-dynamic'
|
||||
|
||||
import { redirect } from 'next/navigation'
|
||||
import Link from 'next/link'
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { asAdmin } from '@/lib/db/with-user'
|
||||
import { getSession } from '@/lib/auth/get-session'
|
||||
import { capaActions, incidents, users } from '@/lib/db/schema'
|
||||
import { eq, asc } from 'drizzle-orm'
|
||||
import { aliasedTable } from 'drizzle-orm'
|
||||
import { CapaBoard } from '@/components/capa/capa-board'
|
||||
|
||||
export default async function CapaListPage() {
|
||||
const session = await getSession()
|
||||
if (!session) redirect('/login?redirect=/hse/capa')
|
||||
|
||||
const supabase = await createClient()
|
||||
const ownerAlias = aliasedTable(users, 'owner')
|
||||
|
||||
const { data: capas } = await supabase
|
||||
.from('capa_actions')
|
||||
.select(`
|
||||
id, incident_id, description, department, due_date, priority, status,
|
||||
incidents (reference_no, incident_type),
|
||||
owner:users!owner_user_id (name, email)
|
||||
`)
|
||||
.order('due_date', { ascending: true })
|
||||
const rows = await asAdmin(db =>
|
||||
db.select({
|
||||
id: capaActions.id,
|
||||
incidentId: capaActions.incidentId,
|
||||
description: capaActions.description,
|
||||
department: capaActions.department,
|
||||
dueDate: capaActions.dueDate,
|
||||
priority: capaActions.priority,
|
||||
status: capaActions.status,
|
||||
incidentRefNo: incidents.referenceNo,
|
||||
ownerName: ownerAlias.name,
|
||||
})
|
||||
.from(capaActions)
|
||||
.leftJoin(incidents, eq(capaActions.incidentId, incidents.id))
|
||||
.leftJoin(ownerAlias, eq(capaActions.ownerUserId, ownerAlias.id))
|
||||
.orderBy(asc(capaActions.dueDate))
|
||||
)
|
||||
|
||||
const capas = rows.map(r => ({
|
||||
id: r.id,
|
||||
incident_id: r.incidentId,
|
||||
description: r.description,
|
||||
department: r.department,
|
||||
due_date: r.dueDate,
|
||||
priority: r.priority,
|
||||
status: r.status,
|
||||
incidents: { reference_no: r.incidentRefNo ?? null },
|
||||
owner: r.ownerName ? { name: r.ownerName } : null,
|
||||
}))
|
||||
|
||||
return (
|
||||
<main className="max-w-6xl mx-auto px-4 py-6">
|
||||
@@ -29,7 +53,7 @@ export default async function CapaListPage() {
|
||||
← Incidents
|
||||
</Link>
|
||||
</div>
|
||||
<CapaBoard capas={(capas ?? []) as unknown as Parameters<typeof CapaBoard>[0]['capas']} />
|
||||
<CapaBoard capas={capas as Parameters<typeof CapaBoard>[0]['capas']} />
|
||||
</main>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user