171 lines
6.7 KiB
TypeScript
171 lines
6.7 KiB
TypeScript
export const dynamic = 'force-dynamic'
|
|
|
|
import { notFound, redirect } from 'next/navigation'
|
|
import Link from 'next/link'
|
|
import { ArrowLeft } from 'lucide-react'
|
|
import { asAdmin } from '@/lib/db/with-user'
|
|
import { getSession } from '@/lib/auth/get-session'
|
|
import { capaActions, evidenceFiles, incidents, users } from '@/lib/db/schema'
|
|
import { eq, and, asc } from 'drizzle-orm'
|
|
import { aliasedTable } from 'drizzle-orm'
|
|
import { VerifyForm } from '@/components/capa/verify-form'
|
|
import { CloseCapaButton } from '@/components/capa/close-capa-button'
|
|
import { CapaOwnerActions } from '@/components/capa/capa-owner-actions'
|
|
import { Avatar } from '@/components/ui/avatar'
|
|
import { PriorityTag, StatusTag } from '@/components/ui/status-tag'
|
|
import { daysLate } from '@/lib/capa/list'
|
|
|
|
interface Props {
|
|
params: Promise<{ id: string }>
|
|
}
|
|
|
|
function Row({ label, children }: { label: string; children: React.ReactNode }) {
|
|
return (
|
|
<div className="flex items-center justify-between gap-3 border-b border-rule py-2.5 last:border-0">
|
|
<dt className="text-xs text-muted">{label}</dt>
|
|
<dd className="text-right text-[13px]">{children}</dd>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default async function CapaDetailPage({ params }: Props) {
|
|
const { id } = await params
|
|
const session = await getSession()
|
|
if (!session) redirect(`/login?redirect=/hse/capa/${id}`)
|
|
|
|
const ownerAlias = aliasedTable(users, 'owner')
|
|
|
|
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,
|
|
ownerUserId: capaActions.ownerUserId,
|
|
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 photos = await asAdmin(db =>
|
|
db.select({ id: evidenceFiles.id, fileUrl: evidenceFiles.fileUrl })
|
|
.from(evidenceFiles)
|
|
.where(and(eq(evidenceFiles.capaId, id), eq(evidenceFiles.deleted, false)))
|
|
.orderBy(asc(evidenceFiles.uploadedAt))
|
|
)
|
|
|
|
const isHse = session && ['hse', 'admin'].includes(session.role)
|
|
const status = capa.status
|
|
const today = new Date().toISOString().slice(0, 10)
|
|
const pastDue = (status === 'open' || status === 'in_progress') && Boolean(capa.dueDate) && capa.dueDate < today
|
|
const shownStatus = pastDue ? 'overdue' : status
|
|
const late = shownStatus === 'overdue' ? daysLate(capa.dueDate, today) : 0
|
|
const canAct = ['open', 'in_progress', 'overdue', 'reopened'].includes(status) &&
|
|
(isHse || session?.sub === capa.ownerUserId)
|
|
|
|
return (
|
|
<div className="space-y-4 px-4 py-5 md:px-8 md:py-6">
|
|
<Link href="/hse/capa" className="inline-flex items-center gap-1 text-[13px] text-accent hover:underline">
|
|
<ArrowLeft size={14} strokeWidth={2} aria-hidden />
|
|
CAPA
|
|
</Link>
|
|
|
|
<div className="space-y-2">
|
|
<h1 className="text-xl font-[650]">{capa.description}</h1>
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
<StatusTag status={shownStatus} />
|
|
<PriorityTag priority={capa.priority} />
|
|
<span className="text-[13px] text-muted">
|
|
From{' '}
|
|
<Link href={`/hse/incidents/${capa.incidentId}`} className="font-mono text-[12.5px] text-accent hover:underline">
|
|
{capa.incidentRefNo ?? '—'}
|
|
</Link>
|
|
</span>
|
|
</div>
|
|
{capa.rootCauseRef && <p className="text-[13px] text-muted">Root cause: {capa.rootCauseRef}</p>}
|
|
</div>
|
|
|
|
<div className="grid gap-4 lg:grid-cols-[1fr_320px]">
|
|
<div className="space-y-4">
|
|
{canAct && (
|
|
<div>
|
|
<CapaOwnerActions capaId={id} currentStatus={status} />
|
|
</div>
|
|
)}
|
|
|
|
{isHse && status === 'pending_verification' && <VerifyForm capaId={id} />}
|
|
{isHse && status === 'verified' && <CloseCapaButton capaId={id} />}
|
|
|
|
{(capa.ownerNotes || capa.completedAt || photos.length > 0) && (
|
|
<section className="space-y-2 rounded-xl bg-surface p-5 shadow-card">
|
|
<h2 className="text-sm font-semibold">Completion report</h2>
|
|
{capa.completedAt && (
|
|
<p className="text-xs text-muted">
|
|
Completed {new Date(capa.completedAt).toLocaleString('en-MY', { timeZone: 'Asia/Kuala_Lumpur' })}
|
|
</p>
|
|
)}
|
|
{capa.ownerNotes && (
|
|
<p className="whitespace-pre-wrap text-[13px] text-ink-3">{capa.ownerNotes}</p>
|
|
)}
|
|
{photos.length > 0 && (
|
|
<ul className="grid grid-cols-2 gap-2 sm:grid-cols-3">
|
|
{photos.map((p, i) => (
|
|
<li key={p.id}>
|
|
<a href={p.fileUrl} target="_blank" rel="noreferrer" className="block overflow-hidden rounded-md shadow-card">
|
|
{/* eslint-disable-next-line @next/next/no-img-element */}
|
|
<img src={p.fileUrl} alt={`CAPA completion evidence ${i + 1} of ${photos.length}`} className="aspect-square w-full object-cover" />
|
|
</a>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
)}
|
|
</section>
|
|
)}
|
|
</div>
|
|
|
|
<aside>
|
|
<section className="rounded-xl bg-surface p-5 shadow-card">
|
|
<h2 className="mb-1 text-sm font-semibold">Details</h2>
|
|
<dl>
|
|
<Row label="Owner">
|
|
{capa.ownerName ? (
|
|
<span className="inline-flex items-center gap-2">
|
|
<Avatar name={capa.ownerName} />
|
|
{capa.ownerName}
|
|
</span>
|
|
) : (
|
|
'—'
|
|
)}
|
|
</Row>
|
|
<Row label="Department">{capa.department}</Row>
|
|
<Row label="Priority"><PriorityTag priority={capa.priority} /></Row>
|
|
<Row label="Due">
|
|
{capa.dueDate}
|
|
{late > 0 && <span className="ml-1.5 text-xs text-danger">{`${late} day${late === 1 ? '' : 's'} late`}</span>}
|
|
</Row>
|
|
{capa.completedAt && (
|
|
<Row label="Completed">
|
|
{new Date(capa.completedAt).toLocaleDateString('en-MY', { timeZone: 'Asia/Kuala_Lumpur' })}
|
|
</Row>
|
|
)}
|
|
</dl>
|
|
</section>
|
|
</aside>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|