import { NextResponse } from "next/server"; import { requirePlatformRole } from "@/lib/session"; import { listTenantRequests, syncProvisioningStatuses } from "@/lib/db"; import { getTenant } from "@/lib/k8s"; /** * GET /api/admin/requests * List all tenant requests. Optionally filter by ?status=pending * Auto-syncs "provisioning" → "active" when the PiecedTenant CR is Ready. */ export async function GET(request: Request) { try { await requirePlatformRole(); } catch { return NextResponse.json({ error: "Forbidden" }, { status: 403 }); } // Sync provisioning statuses before listing await syncProvisioningStatuses(async (tenantName: string) => { const tenant = await getTenant(tenantName); return tenant?.status?.phase ?? null; }); const { searchParams } = new URL(request.url); const status = searchParams.get("status") as any; const requests = await listTenantRequests(status || undefined); return NextResponse.json(requests); }