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,10 @@ 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 { incidents, investigations } from '@/lib/db/schema'
|
||||
import { eq, desc } from 'drizzle-orm'
|
||||
import { InvestigationForm } from '@/components/incidents/investigation-form'
|
||||
|
||||
interface Props {
|
||||
@@ -16,25 +18,28 @@ export default async function InvestigationPage({ params }: Props) {
|
||||
if (!session) redirect(`/login?redirect=/hse/incidents/${id}/investigation`)
|
||||
if (!['hse', 'admin'].includes(session.role)) redirect('/hse/incidents')
|
||||
|
||||
const supabase = await createClient()
|
||||
|
||||
const { data: incident } = await supabase
|
||||
.from('incidents')
|
||||
.select('id, reference_no, status')
|
||||
.eq('id', id)
|
||||
.single()
|
||||
const [incident] = await asAdmin(db =>
|
||||
db.select({
|
||||
id: incidents.id,
|
||||
referenceNo: incidents.referenceNo,
|
||||
status: incidents.status,
|
||||
})
|
||||
.from(incidents)
|
||||
.where(eq(incidents.id, id))
|
||||
.limit(1)
|
||||
)
|
||||
|
||||
if (!incident) notFound()
|
||||
if (!['triaged', 'investigating'].includes((incident as { status: string }).status))
|
||||
if (!['triaged', 'investigating'].includes(incident.status))
|
||||
redirect(`/hse/incidents/${id}`)
|
||||
|
||||
const { data: existing } = await supabase
|
||||
.from('investigations')
|
||||
.select('id')
|
||||
.eq('incident_id', id)
|
||||
.order('created_at', { ascending: false })
|
||||
const [existing] = await asAdmin(db =>
|
||||
db.select({ id: investigations.id })
|
||||
.from(investigations)
|
||||
.where(eq(investigations.incidentId, id))
|
||||
.orderBy(desc(investigations.createdAt))
|
||||
.limit(1)
|
||||
.maybeSingle()
|
||||
)
|
||||
|
||||
return (
|
||||
<main className="max-w-2xl mx-auto px-4 py-6">
|
||||
@@ -42,10 +47,10 @@ export default async function InvestigationPage({ params }: Props) {
|
||||
← Back to incident
|
||||
</Link>
|
||||
<h1 className="text-xl font-bold text-gray-900 mb-1">Investigation Workspace</h1>
|
||||
<p className="text-sm text-gray-500 mb-6">{(incident as { reference_no: string | null }).reference_no ?? id}</p>
|
||||
<p className="text-sm text-gray-500 mb-6">{incident.referenceNo ?? id}</p>
|
||||
<InvestigationForm
|
||||
incidentId={id}
|
||||
existingInvestigationId={(existing as { id: string } | null)?.id ?? null}
|
||||
existingInvestigationId={existing?.id ?? null}
|
||||
/>
|
||||
</main>
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user