import { NextResponse } from "next/server"; import { requirePlatformRole } from "@/lib/session"; import { getTenant, deleteTenant } from "@/lib/k8s"; import { markTenantRequestDeletedByTenantName } from "@/lib/db"; /** * POST /api/admin/tenants/[name]/delete * Delete a PiecedTenant CR. The operator handles cleanup * (namespace, vault, litellm team, etc.). * Also marks the associated tenant_request as "deleted" so the * customer can re-submit the onboarding wizard. */ export async function POST( _request: Request, { params }: { params: Promise<{ name: string }> } ) { try { await requirePlatformRole(); } catch { return NextResponse.json({ error: "Forbidden" }, { status: 403 }); } const { name } = await params; const tenant = await getTenant(name); if (!tenant) { return NextResponse.json({ error: "Tenant not found" }, { status: 404 }); } try { await deleteTenant(name); // Mark the associated tenant_request as "deleted" so the customer // sees the wizard again instead of a stale "active" status await markTenantRequestDeletedByTenantName(name).catch((e) => console.error("Failed to update tenant request after delete:", e) ); return NextResponse.json({ message: "Tenant deletion initiated. The operator will clean up all resources.", }); } catch (e: any) { console.error("Failed to delete tenant:", e); return NextResponse.json( { error: `Failed to delete tenant: ${e.message}` }, { status: 500 } ); } }