fix(audit): wrap admin + capa mutations with their audit writes in single transactions
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -2,7 +2,7 @@ export const dynamic = 'force-dynamic'
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { requireAdmin } from '@/lib/auth/require-admin'
|
||||
import { asAdmin, withUser } from '@/lib/db/with-user'
|
||||
import { asAdmin } from '@/lib/db/with-user'
|
||||
import { sites, zones, incidents } from '@/lib/db/schema'
|
||||
import { writeAuditLog } from '@/lib/db/audit'
|
||||
import { eq, sql } from 'drizzle-orm'
|
||||
@@ -20,13 +20,17 @@ export async function POST(request: NextRequest) {
|
||||
if (!body.site_id) return NextResponse.json({ error: 'site_id required for zone' }, { status: 422 })
|
||||
const qrCodeToken = crypto.randomUUID()
|
||||
try {
|
||||
const [zone] = await asAdmin(db =>
|
||||
db.insert(zones).values({ siteId: body.site_id!, name, qrCodeToken }).returning({ id: zones.id, qrCodeToken: zones.qrCodeToken })
|
||||
)
|
||||
if (!zone) return NextResponse.json({ error: 'Insert failed' }, { status: 500 })
|
||||
await withUser(session.sub, async tx => {
|
||||
await writeAuditLog(tx, 'zones', zone.id, 'INSERT', { name, site_id: body.site_id })
|
||||
let zone: { id: string; qrCodeToken: string } | undefined
|
||||
await asAdmin(async db => {
|
||||
await db.transaction(async tx => {
|
||||
await tx.execute(sql`SELECT set_config('app.user_id', ${session.sub}, true)`)
|
||||
const [result] = await tx.insert(zones).values({ siteId: body.site_id!, name, qrCodeToken }).returning({ id: zones.id, qrCodeToken: zones.qrCodeToken })
|
||||
if (!result) throw new Error('Insert failed')
|
||||
zone = result
|
||||
await writeAuditLog(tx, 'zones', result.id, 'INSERT', { name, site_id: body.site_id })
|
||||
})
|
||||
})
|
||||
if (!zone) return NextResponse.json({ error: 'Insert failed' }, { status: 500 })
|
||||
return NextResponse.json({ id: zone.id, qr_code_token: zone.qrCodeToken }, { status: 201 })
|
||||
} catch {
|
||||
return NextResponse.json({ error: 'Insert failed' }, { status: 500 })
|
||||
@@ -34,14 +38,18 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
|
||||
try {
|
||||
const [site] = await asAdmin(db =>
|
||||
db.insert(sites).values({ name, address: body.address ?? null }).returning({ id: sites.id })
|
||||
)
|
||||
if (!site) return NextResponse.json({ error: 'Insert failed' }, { status: 500 })
|
||||
await withUser(session.sub, async tx => {
|
||||
await writeAuditLog(tx, 'sites', site.id, 'INSERT', { name, address: body.address ?? null })
|
||||
let siteId: string | undefined
|
||||
await asAdmin(async db => {
|
||||
await db.transaction(async tx => {
|
||||
await tx.execute(sql`SELECT set_config('app.user_id', ${session.sub}, true)`)
|
||||
const [site] = await tx.insert(sites).values({ name, address: body.address ?? null }).returning({ id: sites.id })
|
||||
if (!site) throw new Error('Insert failed')
|
||||
siteId = site.id
|
||||
await writeAuditLog(tx, 'sites', site.id, 'INSERT', { name, address: body.address ?? null })
|
||||
})
|
||||
})
|
||||
return NextResponse.json({ id: site.id }, { status: 201 })
|
||||
if (!siteId) return NextResponse.json({ error: 'Insert failed' }, { status: 500 })
|
||||
return NextResponse.json({ id: siteId }, { status: 201 })
|
||||
} catch {
|
||||
return NextResponse.json({ error: 'Insert failed' }, { status: 500 })
|
||||
}
|
||||
@@ -59,17 +67,18 @@ export async function PATCH(request: NextRequest) {
|
||||
const [before] = await asAdmin(db =>
|
||||
db.select().from(table).where(eq(table.id, body.id!)).limit(1)
|
||||
)
|
||||
const tableName = body.kind === 'zone' ? 'zones' : 'sites'
|
||||
try {
|
||||
await asAdmin(db =>
|
||||
db.update(table).set({ active: body.active }).where(eq(table.id, body.id!))
|
||||
)
|
||||
await asAdmin(async db => {
|
||||
await db.transaction(async tx => {
|
||||
await tx.execute(sql`SELECT set_config('app.user_id', ${session.sub}, true)`)
|
||||
await tx.update(table).set({ active: body.active }).where(eq(table.id, body.id!))
|
||||
await writeAuditLog(tx, tableName, body.id!, 'admin_update', { active: body.active }, before ?? null)
|
||||
})
|
||||
})
|
||||
} catch {
|
||||
return NextResponse.json({ error: 'Update failed' }, { status: 500 })
|
||||
}
|
||||
const tableName = body.kind === 'zone' ? 'zones' : 'sites'
|
||||
await withUser(session.sub, async tx => {
|
||||
await writeAuditLog(tx, tableName, body.id!, 'admin_update', { active: body.active }, before ?? null)
|
||||
})
|
||||
return NextResponse.json({ ok: true })
|
||||
}
|
||||
|
||||
@@ -97,9 +106,13 @@ export async function DELETE(request: NextRequest) {
|
||||
db.select().from(body.kind === 'zone' ? zones : sites).where(eq((body.kind === 'zone' ? zones : sites).id, body.id!)).limit(1)
|
||||
)
|
||||
try {
|
||||
await asAdmin(db =>
|
||||
db.delete(body.kind === 'zone' ? zones : sites).where(eq((body.kind === 'zone' ? zones : sites).id, body.id!))
|
||||
)
|
||||
await asAdmin(async db => {
|
||||
await db.transaction(async tx => {
|
||||
await tx.execute(sql`SELECT set_config('app.user_id', ${session.sub}, true)`)
|
||||
await tx.delete(body.kind === 'zone' ? zones : sites).where(eq((body.kind === 'zone' ? zones : sites).id, body.id!))
|
||||
await writeAuditLog(tx, tableName, body.id!, 'DELETE', null, before ?? null)
|
||||
})
|
||||
})
|
||||
} catch (e) {
|
||||
const code = (e as { code?: string }).code
|
||||
return NextResponse.json(
|
||||
@@ -107,8 +120,5 @@ export async function DELETE(request: NextRequest) {
|
||||
{ status: code === '23503' ? 409 : 500 },
|
||||
)
|
||||
}
|
||||
await withUser(session.sub, async tx => {
|
||||
await writeAuditLog(tx, tableName, body.id!, 'DELETE', null, before ?? null)
|
||||
})
|
||||
return NextResponse.json({ ok: true })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user