import { NextResponse, type NextRequest } from 'next/server'; import { requireAdminApi } from '@/lib/auth/admin-guard'; import { getSupabaseAdmin } from '@/lib/supabase/admin'; import { logAdminAction } from '@/lib/auth/audit'; import { isUuid, parsePositiveInt } from '@/lib/admin/validators'; export const runtime = 'nodejs'; export const dynamic = 'force-dynamic'; // 100 TiB ceiling — generous but guards against absurd values. const MAX_QUOTA = 100 * 1024 ** 4; export async function POST( req: NextRequest, { params }: { params: { id: string } }, ) { const auth = await requireAdminApi(); if (!auth.ok) return auth.response; const { id } = params; if (!isUuid(id)) { return NextResponse.json({ error: 'invalid tunnel id' }, { status: 400 }); } let body: { quota_bytes?: unknown }; try { body = (await req.json()) as { quota_bytes?: unknown }; } catch { return NextResponse.json({ error: 'invalid json' }, { status: 400 }); } const parsed = parsePositiveInt(body.quota_bytes, MAX_QUOTA); if (!parsed.ok) { return NextResponse.json( { error: `quota_bytes ${parsed.error}` }, { status: 400 }, ); } const admin = getSupabaseAdmin(); const { data, error } = await admin .from('tunnels') .update({ quota_bytes: parsed.value }) .eq('user_id', id) .select('subdomain') .maybeSingle<{ subdomain: string }>(); if (error) { return NextResponse.json({ error: error.message }, { status: 500 }); } if (!data) { return NextResponse.json({ error: 'tunnel not found' }, { status: 404 }); } await logAdminAction(auth.user, { action: 'tunnel.quota', target_type: 'tunnel', target_id: id, details: { subdomain: data.subdomain, quota_bytes: parsed.value }, }); return NextResponse.json({ ok: true, quota_bytes: parsed.value }); }