/** * Immutable log endpoint. * * GET /_emdash/api/immutable-log?limit=&before= — the append-only billing * ledger merged with the admin audit trail, newest first. `before` is an * exclusive ISO-timestamp cursor. Admin-only. */ import type { APIRoute } from "astro"; import { requirePerm } from "#api/authorize.js"; import { apiError, handleError, unwrapResult } from "#api/error.js"; import { handleImmutableLog } from "#api/handlers/billing.js"; export const prerender = false; export const GET: APIRoute = async ({ locals, url }) => { const { emdash, user } = locals; const denied = requirePerm(user, "settings:manage"); if (denied) return denied; if (!emdash?.db) { return apiError("NOT_CONFIGURED", "EmDash is not initialized", 500); } const limitRaw = Number(url.searchParams.get("limit")); const limit = Number.isFinite(limitRaw) && limitRaw > 0 ? Math.floor(limitRaw) : undefined; const before = url.searchParams.get("before") ?? undefined; try { return unwrapResult(await handleImmutableLog(emdash.db, { limit, before })); } catch (error) { return handleError(error, "Failed to load log", "IMMUTABLE_LOG_ERROR"); } };