fix: code-review findings — closed-incident embedding 503, hardening, dedup
- /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
This commit is contained in:
@@ -1,19 +1,9 @@
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { createAdminClient } from '@/lib/supabase/admin'
|
||||
import { isValidRole } from '@/lib/auth/roles'
|
||||
|
||||
async function requireAdmin() {
|
||||
const supabase = await createClient()
|
||||
const { data: { user }, error } = await supabase.auth.getUser()
|
||||
if (error || !user) return { supabase, user: null }
|
||||
const { data: profile } = await supabase
|
||||
.from('users').select('role').eq('id', user.id).single()
|
||||
if (!profile || profile.role !== 'admin') return { supabase, user: null }
|
||||
return { supabase, user }
|
||||
}
|
||||
import { requireAdmin } from '@/lib/auth/require-admin'
|
||||
|
||||
export async function GET() {
|
||||
const { supabase, user } = await requireAdmin()
|
||||
@@ -108,6 +98,7 @@ export async function PATCH(request: NextRequest) {
|
||||
|
||||
const { data: before } = await supabase
|
||||
.from('users').select('role, site_id, active, department').eq('id', body.id).single()
|
||||
if (!before) return NextResponse.json({ error: 'User not found' }, { status: 404 })
|
||||
|
||||
const { error } = await supabase.from('users').update(update).eq('id', body.id)
|
||||
if (error) return NextResponse.json({ error: 'Update failed' }, { status: 500 })
|
||||
|
||||
Reference in New Issue
Block a user