feat: finalize immutable billing reports
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
a384c4c35e
commit
7f1ecd9322
15 files changed
+1697
-3
No files matched your search
@@ -0,0 +1,93 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { requireAdmin, PrincipalAuthorizationError } from "@/modules/auth/principal";
|
||||
import {
|
||||
finalizeReport,
|
||||
ReportValidationError,
|
||||
ReportConflictError,
|
||||
} from "@/modules/billing/reports";
|
||||
import { monthPeriod, quarterPeriod } from "@/modules/billing/period";
|
||||
|
||||
const finalizePayload = z
|
||||
.object({
|
||||
editorialId: z.string().uuid(),
|
||||
usageIds: z.array(z.string().uuid()).min(1),
|
||||
year: z.number().int().min(2020).max(2100),
|
||||
month: z.number().int().min(1).max(12).optional(),
|
||||
quarter: z.number().int().min(1).max(4).optional(),
|
||||
})
|
||||
.strict()
|
||||
.refine((data) => (data.month !== undefined) !== (data.quarter !== undefined), {
|
||||
message: "Exactly one of month or quarter must be provided",
|
||||
});
|
||||
|
||||
type FinalizeHandlerDependencies = {
|
||||
requireAdmin: typeof requireAdmin;
|
||||
finalizeReport: typeof finalizeReport;
|
||||
};
|
||||
|
||||
export function createFinalizeHandler(dependencies: FinalizeHandlerDependencies) {
|
||||
return async function POST(request: Request): Promise<Response> {
|
||||
try {
|
||||
const principal = await dependencies.requireAdmin(request.headers);
|
||||
const payload = finalizePayload.safeParse(await readJson(request));
|
||||
if (!payload.success) return jsonError(400, "INVALID_REQUEST");
|
||||
|
||||
const { editorialId, usageIds, year, month, quarter } = payload.data;
|
||||
const period = month ? monthPeriod(year, month) : quarterPeriod(year, quarter!);
|
||||
|
||||
const result = await dependencies.finalizeReport({
|
||||
editorialId,
|
||||
usageIds,
|
||||
period,
|
||||
adminUserId: principal.userId,
|
||||
});
|
||||
|
||||
return Response.json(
|
||||
{
|
||||
reportId: result.reportId,
|
||||
reportNumber: result.reportNumber,
|
||||
},
|
||||
{
|
||||
status: 201,
|
||||
headers: { "cache-control": "private, no-store" },
|
||||
},
|
||||
);
|
||||
} catch (error) {
|
||||
return mappedError(error);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
async function readJson(request: Request): Promise<unknown> {
|
||||
try {
|
||||
return await request.json();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function mappedError(error: unknown): Response {
|
||||
if (error instanceof PrincipalAuthorizationError) {
|
||||
return jsonError(error.code === "AUTHENTICATION_REQUIRED" ? 401 : 403, "FORBIDDEN");
|
||||
}
|
||||
if (error instanceof ReportValidationError) {
|
||||
return jsonError(400, error.code);
|
||||
}
|
||||
if (error instanceof ReportConflictError) {
|
||||
return jsonError(409, error.code);
|
||||
}
|
||||
return jsonError(500, "REQUEST_FAILED");
|
||||
}
|
||||
|
||||
function jsonError(status: number, code: string): Response {
|
||||
return Response.json(
|
||||
{ code },
|
||||
{ status, headers: { "cache-control": "private, no-store" } },
|
||||
);
|
||||
}
|
||||
|
||||
export const finalizeHandler = createFinalizeHandler({
|
||||
requireAdmin,
|
||||
finalizeReport,
|
||||
});
|
||||
Reference in new issue
Block a user