59 lines
1.5 KiB
TypeScript
59 lines
1.5 KiB
TypeScript
import { z } from "zod";
|
|
import { NextResponse } from "next/server";
|
|
|
|
import { createUpgradeRequest } from "@/lib/upgrade-requests";
|
|
import { ApiError } from "@/lib/errors";
|
|
import { withApiHandler } from "@/lib/api-handler";
|
|
import { logger } from "@/lib/logger";
|
|
import { ACCOUNT_TYPES } from "@/lib/constants";
|
|
|
|
const CTX = "maestros/[id]/upgrade-requests";
|
|
|
|
const paidAccountTypeValues = [
|
|
ACCOUNT_TYPES.BASIC_20,
|
|
ACCOUNT_TYPES.STANDARD_50,
|
|
ACCOUNT_TYPES.PRO_100,
|
|
ACCOUNT_TYPES.SCHOOL_500,
|
|
ACCOUNT_TYPES.SCHOOL_1000,
|
|
] as const;
|
|
|
|
const postBodySchema = z.object({
|
|
requestedAccountType: z.coerce
|
|
.number()
|
|
.int()
|
|
.refine((v) => (paidAccountTypeValues as readonly number[]).includes(v)),
|
|
});
|
|
|
|
export const POST = withApiHandler<{ id: string }>(
|
|
CTX,
|
|
async (request, { params, t0 }) => {
|
|
const { id } = await params;
|
|
const maestroID = Number(id);
|
|
|
|
if (!Number.isInteger(maestroID) || maestroID <= 0) {
|
|
throw new ApiError("Invalid maestro id", 400);
|
|
}
|
|
|
|
const body = postBodySchema.safeParse(
|
|
await request.json().catch(() => ({}))
|
|
);
|
|
if (!body.success) {
|
|
throw new ApiError("Invalid request body", 400);
|
|
}
|
|
|
|
const result = await createUpgradeRequest(
|
|
maestroID,
|
|
body.data.requestedAccountType
|
|
);
|
|
|
|
logger.info(CTX, "created", {
|
|
maestroID,
|
|
maestroUpgradeID: result.maestroUpgradeID,
|
|
requestedAccountType: body.data.requestedAccountType,
|
|
duration: Date.now() - t0,
|
|
});
|
|
|
|
return NextResponse.json(result, { status: 201 });
|
|
}
|
|
);
|