- /api/incidents/[id]/similar: embedding backfill on a closed incident hit the closure-lock trigger and turned the whole request into a 503; now skips persistence for closed incidents (vector still used for the query) - addenda: cap body at 5000 chars; include body text in audit_log entry - admin users PATCH: 404 when target user does not exist (was silent ok) - extract shared requireAdmin to lib/auth/require-admin.ts (was duplicated in admin users + sites routes) - extract escapeCsv/rowsToCsv to lib/csv.ts (was duplicated in dashboard export route and lib/reports/jkkp8.ts) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CPf5Rc8QPx6V8KLEEgfKEQ
48 lines
1.7 KiB
TypeScript
48 lines
1.7 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: { kind?: 'site' | 'zone'; name?: string; address?: string; site_id?: string } =
|
|
await request.json().catch(() => ({}))
|
|
const name = (body.name ?? '').trim()
|
|
if (!name) return NextResponse.json({ error: 'name required' }, { status: 422 })
|
|
|
|
if (body.kind === 'zone') {
|
|
if (!body.site_id) return NextResponse.json({ error: 'site_id required for zone' }, { status: 422 })
|
|
const { data: zone, error } = await supabase
|
|
.from('zones')
|
|
.insert({ site_id: body.site_id, name })
|
|
.select('id, qr_code_token')
|
|
.single()
|
|
if (error || !zone) return NextResponse.json({ error: 'Insert failed' }, { status: 500 })
|
|
|
|
await supabase.rpc('write_audit_log', {
|
|
p_table_name: 'zones',
|
|
p_record_id: zone.id,
|
|
p_action: 'INSERT',
|
|
p_new_value: { name, site_id: body.site_id },
|
|
})
|
|
return NextResponse.json({ id: zone.id, qr_code_token: zone.qr_code_token }, { status: 201 })
|
|
}
|
|
|
|
const { data: site, error } = await supabase
|
|
.from('sites')
|
|
.insert({ name, address: body.address ?? null })
|
|
.select('id')
|
|
.single()
|
|
if (error || !site) return NextResponse.json({ error: 'Insert failed' }, { status: 500 })
|
|
|
|
await supabase.rpc('write_audit_log', {
|
|
p_table_name: 'sites',
|
|
p_record_id: site.id,
|
|
p_action: 'INSERT',
|
|
p_new_value: { name, address: body.address ?? null },
|
|
})
|
|
return NextResponse.json({ id: site.id }, { status: 201 })
|
|
}
|