import { getCloudViewer } from "@/app/cloud-auth"; import { createExemption, revokeExemption } from "@/db/repository"; import { apiError, readJsonMutation } from "../../respond"; export async function POST(request: Request) { try { const viewer = await getCloudViewer(); if (!viewer) return Response.json({ error: "authentication_required", message: "请先登录" }, { status: 401 }); if (!viewer.isAdmin) return Response.json({ error: "forbidden", message: "没有商业后台权限" }, { status: 403 }); const payload = await readJsonMutation<{ action?: string; grantId?: string; accountId?: string; capacitySlots?: number | null; endsAt?: string; reason?: string; idempotencyKey?: string }>(request); if (payload.action === "revoke") { const grant = await revokeExemption({ actorId: viewer.userId, grantId: payload.grantId ?? "", reason: payload.reason ?? "", idempotencyKey: payload.idempotencyKey ?? "" }); return Response.json({ grant }); } const grant = await createExemption({ actorId: viewer.userId, accountId: payload.accountId ?? "", capacitySlots: payload.capacitySlots ?? null, endsAt: payload.endsAt ?? "", reason: payload.reason ?? "", idempotencyKey: payload.idempotencyKey ?? "" }); return Response.json({ grant }, { status: 201 }); } catch (error) { return apiError(error); } }