45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { requirePlatformRole } from "@/lib/session";
|
|
import { listInvoices, syncOverdueInvoices } from "@/lib/db";
|
|
import type { InvoiceStatus } from "@/types";
|
|
|
|
/**
|
|
* GET /api/admin/billing/invoices
|
|
*
|
|
* List invoices for admin. Optional filters:
|
|
* ?status=open|paid|overdue|void|uncollectible
|
|
* ?orgId=...
|
|
* ?month=YYYY-MM
|
|
* ?limit=200
|
|
*
|
|
* Refreshes overdue status on each call (cheap UPDATE), so the
|
|
* admin list always reflects the latest due-date math without
|
|
* needing a cron.
|
|
*/
|
|
export async function GET(request: Request) {
|
|
try {
|
|
await requirePlatformRole();
|
|
} catch {
|
|
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
|
}
|
|
|
|
await syncOverdueInvoices().catch((e) =>
|
|
console.error("syncOverdueInvoices failed:", e)
|
|
);
|
|
|
|
const { searchParams } = new URL(request.url);
|
|
const status = searchParams.get("status") as InvoiceStatus | null;
|
|
const orgId = searchParams.get("orgId");
|
|
const month = searchParams.get("month");
|
|
const limitParam = searchParams.get("limit");
|
|
const limit = limitParam ? Math.max(1, Math.min(1000, parseInt(limitParam, 10))) : 200;
|
|
|
|
const invoices = await listInvoices({
|
|
status: status ?? undefined,
|
|
zitadelOrgId: orgId ?? undefined,
|
|
periodMonth: month ?? undefined,
|
|
limit,
|
|
});
|
|
return NextResponse.json(invoices);
|
|
}
|