export const dynamic = 'force-dynamic' import { asAdmin } from '@/lib/db/with-user' import { redirect } from 'next/navigation' import { getSession } from '@/lib/auth/get-session' import { incidents, sites, capaActions } from '@/lib/db/schema' import { eq, gte, lt, and, inArray } from 'drizzle-orm' import { StatCard } from '@/components/dashboard/stat-card' import { RiskFlagsPanel } from '@/components/dashboard/risk-flags-panel' export default async function ManagementPage() { const session = await getSession() if (!session) redirect('/login') if (!['management', 'admin'].includes(session.role)) redirect('/') const now = new Date() const today = now.toISOString().slice(0, 10) const thisMonthStart = new Date(now.getFullYear(), now.getMonth(), 1).toISOString() const lastMonthStart = new Date(now.getFullYear(), now.getMonth() - 1, 1).toISOString() const thirtyDaysAgo = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000).toISOString() const [allIncidents, thisMonthIncidents, lastMonthIncidents, recentIncidents, overdueCapas] = await Promise.all([ asAdmin(db => db.select({ id: incidents.id, severity: incidents.severity, status: incidents.status, incidentType: incidents.incidentType, medicalStatus: incidents.medicalStatus, siteName: sites.name, }) .from(incidents) .leftJoin(sites, eq(incidents.siteId, sites.id)) ), asAdmin(db => db.select({ id: incidents.id }) .from(incidents) .where(gte(incidents.reportedAt, new Date(thisMonthStart))) ), asAdmin(db => db.select({ id: incidents.id }) .from(incidents) .where(and(gte(incidents.reportedAt, new Date(lastMonthStart)), lt(incidents.reportedAt, new Date(thisMonthStart)))) ), asAdmin(db => db.select({ incidentType: incidents.incidentType }) .from(incidents) .where(gte(incidents.reportedAt, new Date(thirtyDaysAgo))) ), asAdmin(db => db.select({ id: capaActions.id }) .from(capaActions) .where(and( inArray(capaActions.status, ['open', 'in_progress']), lt(capaActions.dueDate, today) )) ), ]) const rows = allIncidents const totalThisMonth = thisMonthIncidents.length const totalLastMonth = lastMonthIncidents.length const monthDelta = totalThisMonth - totalLastMonth const ltiCount = rows.filter(r => r.medicalStatus === 'lti').length const overdueCount = overdueCapas.length // Severity distribution const severityDist: Record = { 1: 0, 2: 0, 3: 0, 4: 0, 5: 0 } for (const r of rows) { if (r.severity && r.severity >= 1 && r.severity <= 5) { severityDist[r.severity] = (severityDist[r.severity] ?? 0) + 1 } } const severityMax = Math.max(...Object.values(severityDist), 1) // Leading vs lagging (last 30 days) const leadingCount = recentIncidents.filter(r => ['hazard', 'near_miss'].includes(r.incidentType)).length const laggingCount = recentIncidents.filter(r => r.incidentType === 'injury').length // Site comparison const siteMap: Record = {} for (const r of rows) { const name = r.siteName ?? 'Unknown' siteMap[name] = (siteMap[name] ?? 0) + 1 } const by_site = Object.entries(siteMap).map(([name, count]) => ({ name, count })).sort((a, b) => b.count - a.count) const SEVERITY_LABELS: Record = { 1: 'Minor', 2: 'Low', 3: 'Moderate', 4: 'Serious', 5: 'Critical' } const SEVERITY_COLORS: Record = { 1: 'bg-green-400', 2: 'bg-yellow-400', 3: 'bg-orange-400', 4: 'bg-red-500', 5: 'bg-red-700', } return (

Management Dashboard

Export CSV
{/* Summary stats */}
= 0 ? `+${monthDelta} vs last month` : `${monthDelta} vs last month`} accent={monthDelta > 0 ? 'yellow' : 'green'} /> 0 ? 'red' : 'green'} sub="lost-time injuries" /> 0 ? 'red' : 'green'} sub="past due date" />
{/* Leading vs lagging */}

Leading vs Lagging — Last 30 Days

Leading: hazard + near-miss · Lagging: injuries

{leadingCount}

Leading (Hazards + Near Misses)

{laggingCount}

Lagging (Injuries)

{/* Severity distribution */}

Severity Distribution (All Time)

{([5, 4, 3, 2, 1] as const).map(sev => (
{SEVERITY_LABELS[sev]}
0 ? `${(severityDist[sev] / severityMax) * 100}%` : '0%' }} />
{severityDist[sev]}
))}
{/* Site comparison */}

Incidents by Site

{by_site.length === 0 ? (

No data

) : (
{by_site.map(({ name, count }) => (
{name} {count}
))}
)}
) }