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:
@@ -1,9 +1,12 @@
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
import Link from 'next/link'
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { asAdmin } from '@/lib/db/with-user'
|
||||
import { redirect } from 'next/navigation'
|
||||
import { getSession } from '@/lib/auth/get-session'
|
||||
import { incidents, sites, capaActions, users } from '@/lib/db/schema'
|
||||
import { eq, not, and, inArray, sql } from 'drizzle-orm'
|
||||
import { aliasedTable } from 'drizzle-orm'
|
||||
import { StatCard } from '@/components/dashboard/stat-card'
|
||||
|
||||
const STATUS_LABELS: Record<string, string> = {
|
||||
@@ -29,8 +32,6 @@ export default async function SupervisorPage() {
|
||||
if (!session) redirect('/login')
|
||||
if (!['supervisor', 'admin'].includes(session.role)) redirect('/')
|
||||
|
||||
const supabase = await createClient()
|
||||
|
||||
if (!session.siteId) {
|
||||
return (
|
||||
<main className="max-w-4xl mx-auto px-4 py-6">
|
||||
@@ -40,56 +41,69 @@ export default async function SupervisorPage() {
|
||||
)
|
||||
}
|
||||
|
||||
const { data: siteRow } = await supabase.from('sites').select('name').eq('id', session.siteId).single()
|
||||
const siteName = (siteRow as unknown as { name: string } | null)?.name ?? 'Your Site'
|
||||
const [siteRow] = await asAdmin(db =>
|
||||
db.select({ name: sites.name }).from(sites).where(eq(sites.id, session.siteId!)).limit(1)
|
||||
)
|
||||
const siteName = siteRow?.name ?? 'Your Site'
|
||||
|
||||
// Resolve incident IDs once to avoid duplicate queries inside Promise.all
|
||||
const { data: siteIncidents } = await supabase
|
||||
.from('incidents')
|
||||
.select('id')
|
||||
.eq('site_id', session.siteId)
|
||||
const incidentIds = siteIncidents?.map(r => r.id) ?? []
|
||||
// Get all incident IDs for this site
|
||||
const siteIncidents = await asAdmin(db =>
|
||||
db.select({ id: incidents.id }).from(incidents).where(eq(incidents.siteId, session.siteId!))
|
||||
)
|
||||
const incidentIds = siteIncidents.map(r => r.id)
|
||||
|
||||
const [
|
||||
{ data: openIncidents },
|
||||
{ count: closedCount },
|
||||
{ count: openCount },
|
||||
{ data: overdueCapas },
|
||||
{ data: allCapas },
|
||||
] = await Promise.all([
|
||||
supabase
|
||||
.from('incidents')
|
||||
.select('id, reference_no, incident_type, status, reported_at')
|
||||
.eq('site_id', session.siteId)
|
||||
.neq('status', 'closed')
|
||||
.order('reported_at', { ascending: false })
|
||||
.limit(10),
|
||||
supabase
|
||||
.from('incidents')
|
||||
.select('*', { count: 'exact', head: true })
|
||||
.eq('site_id', session.siteId)
|
||||
.eq('status', 'closed'),
|
||||
supabase
|
||||
.from('incidents')
|
||||
.select('*', { count: 'exact', head: true })
|
||||
.eq('site_id', session.siteId)
|
||||
.neq('status', 'closed'),
|
||||
supabase
|
||||
.from('capa_actions')
|
||||
.select('id, description, due_date, users (name)')
|
||||
.eq('status', 'overdue')
|
||||
.in('incident_id', incidentIds),
|
||||
supabase
|
||||
.from('capa_actions')
|
||||
.select('id, status')
|
||||
.in('incident_id', incidentIds),
|
||||
const ownerAlias = aliasedTable(users, 'owner')
|
||||
|
||||
const [openIncidents, closedCountRow, openCountRow, overdueCapas, allCapas] = await Promise.all([
|
||||
asAdmin(db =>
|
||||
db.select({
|
||||
id: incidents.id,
|
||||
referenceNo: incidents.referenceNo,
|
||||
incidentType: incidents.incidentType,
|
||||
status: incidents.status,
|
||||
reportedAt: incidents.reportedAt,
|
||||
})
|
||||
.from(incidents)
|
||||
.where(and(eq(incidents.siteId, session.siteId!), not(eq(incidents.status, 'closed'))))
|
||||
.orderBy(sql`${incidents.reportedAt} desc`)
|
||||
.limit(10)
|
||||
),
|
||||
asAdmin(db =>
|
||||
db.select({ count: sql<number>`count(*)` })
|
||||
.from(incidents)
|
||||
.where(and(eq(incidents.siteId, session.siteId!), eq(incidents.status, 'closed')))
|
||||
),
|
||||
asAdmin(db =>
|
||||
db.select({ count: sql<number>`count(*)` })
|
||||
.from(incidents)
|
||||
.where(and(eq(incidents.siteId, session.siteId!), not(eq(incidents.status, 'closed'))))
|
||||
),
|
||||
incidentIds.length > 0
|
||||
? asAdmin(db =>
|
||||
db.select({
|
||||
id: capaActions.id,
|
||||
description: capaActions.description,
|
||||
dueDate: capaActions.dueDate,
|
||||
ownerName: ownerAlias.name,
|
||||
})
|
||||
.from(capaActions)
|
||||
.leftJoin(ownerAlias, eq(capaActions.ownerUserId, ownerAlias.id))
|
||||
.where(and(eq(capaActions.status, 'overdue'), inArray(capaActions.incidentId, incidentIds)))
|
||||
)
|
||||
: Promise.resolve([]),
|
||||
incidentIds.length > 0
|
||||
? asAdmin(db =>
|
||||
db.select({ id: capaActions.id, status: capaActions.status })
|
||||
.from(capaActions)
|
||||
.where(inArray(capaActions.incidentId, incidentIds))
|
||||
)
|
||||
: Promise.resolve([]),
|
||||
])
|
||||
|
||||
const resolvedOpenCount = openCount ?? 0
|
||||
const resolvedClosedCount = closedCount ?? 0
|
||||
const overdueCount = overdueCapas?.length ?? 0
|
||||
|
||||
const capaRows = allCapas ?? []
|
||||
const resolvedOpenCount = Number(openCountRow[0]?.count ?? 0)
|
||||
const resolvedClosedCount = Number(closedCountRow[0]?.count ?? 0)
|
||||
const overdueCount = overdueCapas.length
|
||||
const capaRows = allCapas
|
||||
const capaOpenCount = capaRows.filter(c => ['open', 'in_progress'].includes(c.status)).length
|
||||
const capaVerifiedCount = capaRows.filter(c => c.status === 'verified').length
|
||||
|
||||
@@ -121,19 +135,19 @@ export default async function SupervisorPage() {
|
||||
{/* Open incidents */}
|
||||
<div className="bg-white rounded-xl shadow-sm p-5 mb-4">
|
||||
<h2 className="text-sm font-semibold text-gray-700 uppercase tracking-wide mb-4">Open Incidents</h2>
|
||||
{(openIncidents ?? []).length === 0 ? (
|
||||
{openIncidents.length === 0 ? (
|
||||
<p className="text-sm text-gray-400">No open incidents.</p>
|
||||
) : (
|
||||
<div className="space-y-2">
|
||||
{(openIncidents ?? []).map(inc => (
|
||||
{openIncidents.map(inc => (
|
||||
<Link
|
||||
key={inc.id}
|
||||
href={`/supervisor/incidents/${inc.id}`}
|
||||
className="flex items-center justify-between py-2 border-b border-gray-50 last:border-0 hover:bg-gray-50 -mx-2 px-2 rounded"
|
||||
>
|
||||
<div>
|
||||
<p className="text-sm font-medium text-gray-900">{inc.reference_no ?? inc.id.slice(0, 8)}</p>
|
||||
<p className="text-xs text-gray-500">{TYPE_LABELS[inc.incident_type] ?? inc.incident_type}</p>
|
||||
<p className="text-sm font-medium text-gray-900">{inc.referenceNo ?? inc.id.slice(0, 8)}</p>
|
||||
<p className="text-xs text-gray-500">{TYPE_LABELS[inc.incidentType] ?? inc.incidentType}</p>
|
||||
</div>
|
||||
<span className={`text-xs rounded-full px-2 py-0.5 font-medium ${STATUS_COLORS[inc.status] ?? 'bg-gray-100 text-gray-700'}`}>
|
||||
{STATUS_LABELS[inc.status] ?? inc.status}
|
||||
@@ -149,16 +163,16 @@ export default async function SupervisorPage() {
|
||||
<div className="bg-white rounded-xl shadow-sm p-5">
|
||||
<h2 className="text-sm font-semibold text-red-600 uppercase tracking-wide mb-4">Overdue CAPAs</h2>
|
||||
<div className="space-y-2">
|
||||
{(overdueCapas ?? []).map(capa => (
|
||||
{overdueCapas.map(capa => (
|
||||
<div key={capa.id} className="flex items-start justify-between py-2 border-b border-gray-50 last:border-0">
|
||||
<div className="flex-1 min-w-0 mr-4">
|
||||
<p className="text-sm text-gray-800 line-clamp-2">{capa.description}</p>
|
||||
<p className="text-xs text-gray-500 mt-0.5">
|
||||
Owner: {(capa.users as unknown as { name: string } | null)?.name ?? 'Unassigned'}
|
||||
Owner: {capa.ownerName ?? 'Unassigned'}
|
||||
</p>
|
||||
</div>
|
||||
<span className="text-xs text-red-600 font-semibold shrink-0">
|
||||
Due {capa.due_date ? new Date(capa.due_date as string).toLocaleDateString('en-MY') : '—'}
|
||||
Due {capa.dueDate ? new Date(capa.dueDate).toLocaleDateString('en-MY') : '—'}
|
||||
</span>
|
||||
</div>
|
||||
))}
|
||||
|
||||
Reference in New Issue
Block a user