export const dynamic = 'force-dynamic' import Link from 'next/link' import { asAdmin } from '@/lib/db/with-user' import { redirect } from 'next/navigation' import { getSession } from '@/lib/auth/get-session' import { incidents, sites } from '@/lib/db/schema' import { eq, desc } from 'drizzle-orm' const STATUS_LABELS: Record = { reported: 'Reported', triaged: 'Triaged', investigating: 'Investigating', capa_pending: 'CAPA Pending', verification: 'Verification', closed: 'Closed', } const STATUS_COLORS: Record = { reported: 'bg-gray-100 text-gray-600', triaged: 'bg-yellow-100 text-yellow-800', investigating: 'bg-blue-100 text-blue-700', capa_pending: 'bg-orange-100 text-orange-700', verification: 'bg-purple-100 text-purple-700', closed: 'bg-green-100 text-green-700', } const TYPE_LABELS: Record = { injury: 'Injury', near_miss: 'Near Miss', hazard: 'Hazard', asset_damage: 'Asset Damage', environmental: 'Environmental', security: 'Security', fire: 'Fire', } export default async function ReporterPage() { const session = await getSession() if (!session) redirect('/login') const rows = await asAdmin(db => db.select({ id: incidents.id, referenceNo: incidents.referenceNo, incidentType: incidents.incidentType, status: incidents.status, reportedAt: incidents.reportedAt, severity: incidents.severity, siteName: sites.name, }) .from(incidents) .leftJoin(sites, eq(incidents.siteId, sites.id)) .where(eq(incidents.reportedBy, session.sub)) .orderBy(desc(incidents.reportedAt)) ) const openCount = rows.filter(r => r.status !== 'closed').length const closedCount = rows.filter(r => r.status === 'closed').length return (

My Reports

Welcome, {session.name}

+ New Report

{rows.length}

Total Submitted

{openCount}

In Progress

{closedCount}

Closed

{rows.length === 0 ? (

No incidents reported yet.

Submit your first report →
) : (
{rows.map(inc => (

{inc.referenceNo ?? inc.id.slice(0, 8)}

{TYPE_LABELS[inc.incidentType] ?? inc.incidentType} {inc.siteName ? ` · ${inc.siteName}` : ''} {inc.severity ? ` · Severity ${inc.severity}` : ''}

{inc.reportedAt && (

{new Date(inc.reportedAt).toLocaleDateString('en-MY', { day: 'numeric', month: 'short', year: 'numeric', })}

)}
{STATUS_LABELS[inc.status] ?? inc.status}
))}
)}
) }