- zones: add active column (migration 20260713000005) - sites/zones API: PATCH (toggle active) + DELETE (blocked when incidents reference it) - trucks API: PATCH + DELETE with same pattern - admin page: select active for sites + zones - site-zone-manager + truck-manager: deactivate toggle + delete button per row with busyId - incidents API: reject reports on deactivated zone/site; wrap handler in top-level try-catch so unhandled errors return JSON (not HTML) - report-form: parse JSON separately so HTTP status code surfaces instead of generic "Something went wrong" Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CPf5Rc8QPx6V8KLEEgfKEQ
95 lines
3.3 KiB
TypeScript
95 lines
3.3 KiB
TypeScript
export const dynamic = 'force-dynamic'
|
|
|
|
import { NextRequest, NextResponse } from 'next/server'
|
|
import { requireAdmin } from '@/lib/auth/require-admin'
|
|
|
|
export async function POST(request: NextRequest) {
|
|
const { supabase, user } = await requireAdmin()
|
|
if (!user) return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
|
|
|
|
const body: { truck_no?: string; carrier?: string } = await request.json().catch(() => ({}))
|
|
const truck_no = (body.truck_no ?? '').trim()
|
|
if (!truck_no) return NextResponse.json({ error: 'truck_no required' }, { status: 422 })
|
|
|
|
const { data: truck, error } = await supabase
|
|
.from('trucks')
|
|
.insert({ truck_no, carrier: (body.carrier ?? '').trim() || null })
|
|
.select('id')
|
|
.single()
|
|
|
|
if (error || !truck)
|
|
return NextResponse.json(
|
|
{ error: error?.code === '23505' ? 'Truck number already exists' : 'Insert failed' },
|
|
{ status: 400 },
|
|
)
|
|
|
|
await supabase.rpc('write_audit_log', {
|
|
p_table_name: 'trucks',
|
|
p_record_id: truck.id,
|
|
p_action: 'INSERT',
|
|
p_new_value: { truck_no, carrier: (body.carrier ?? '').trim() || null },
|
|
})
|
|
|
|
return NextResponse.json({ id: truck.id }, { status: 201 })
|
|
}
|
|
|
|
export async function PATCH(request: NextRequest) {
|
|
const { supabase, user } = await requireAdmin()
|
|
if (!user) return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
|
|
|
|
const body: { id?: string; active?: boolean } = await request.json().catch(() => ({}))
|
|
if (!body.id) return NextResponse.json({ error: 'id required' }, { status: 422 })
|
|
|
|
const { data: before } = await supabase.from('trucks').select('*').eq('id', body.id).single()
|
|
const { error } = await supabase.from('trucks').update({ active: body.active }).eq('id', body.id)
|
|
if (error) return NextResponse.json({ error: 'Update failed' }, { status: 500 })
|
|
|
|
await supabase.rpc('write_audit_log', {
|
|
p_table_name: 'trucks',
|
|
p_record_id: body.id,
|
|
p_action: 'admin_update',
|
|
p_old_value: before,
|
|
p_new_value: { active: body.active },
|
|
})
|
|
return NextResponse.json({ ok: true })
|
|
}
|
|
|
|
export async function DELETE(request: NextRequest) {
|
|
const { supabase, user } = await requireAdmin()
|
|
if (!user) return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
|
|
|
|
const body: { id?: string } = await request.json().catch(() => ({}))
|
|
if (!body.id) return NextResponse.json({ error: 'id required' }, { status: 422 })
|
|
|
|
const { count } = await supabase
|
|
.from('incidents')
|
|
.select('id', { count: 'exact', head: true })
|
|
.eq('truck_id', body.id)
|
|
if ((count ?? 0) > 0)
|
|
return NextResponse.json(
|
|
{ error: `Cannot delete — ${count} incident(s) reference this truck. Deactivate it instead.` },
|
|
{ status: 409 },
|
|
)
|
|
|
|
const { data: before } = await supabase.from('trucks').select('*').eq('id', body.id).single()
|
|
const { error } = await supabase.from('trucks').delete().eq('id', body.id)
|
|
if (error)
|
|
return NextResponse.json(
|
|
{
|
|
error:
|
|
error.code === '23503'
|
|
? 'Cannot delete — this truck is still referenced elsewhere. Deactivate it instead.'
|
|
: 'Delete failed',
|
|
},
|
|
{ status: error.code === '23503' ? 409 : 500 },
|
|
)
|
|
|
|
await supabase.rpc('write_audit_log', {
|
|
p_table_name: 'trucks',
|
|
p_record_id: body.id,
|
|
p_action: 'DELETE',
|
|
p_old_value: before,
|
|
})
|
|
return NextResponse.json({ ok: true })
|
|
}
|