62 lines
1.8 KiB
TypeScript
62 lines
1.8 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { z } from "zod";
|
|
|
|
import {
|
|
approveUpgradeRequest,
|
|
rejectUpgradeRequest,
|
|
} from "@/lib/upgrade-requests";
|
|
import { ApiError } from "@/lib/errors";
|
|
import { withApiHandler } from "@/lib/api-handler";
|
|
import { logger } from "@/lib/logger";
|
|
import { sendUpgradeDoneEmail } from "@/lib/mail";
|
|
import { formatDate } from "@/lib/utils";
|
|
|
|
const CTX = "upgrade-requests/[id]";
|
|
|
|
const patchBodySchema = z.object({
|
|
action: z.enum(["approve", "reject"]),
|
|
});
|
|
|
|
export const PATCH = withApiHandler<{ id: string }>(
|
|
CTX,
|
|
async (request, { params, t0 }) => {
|
|
const { id } = await params;
|
|
const maestroUpgradeID = Number(id);
|
|
|
|
if (!Number.isInteger(maestroUpgradeID) || maestroUpgradeID <= 0) {
|
|
throw new ApiError("Invalid upgrade request id", 400);
|
|
}
|
|
|
|
const body = patchBodySchema.safeParse(
|
|
await request.json().catch(() => ({}))
|
|
);
|
|
if (!body.success) {
|
|
throw new ApiError("Invalid action", 400);
|
|
}
|
|
|
|
if (body.data.action === "approve") {
|
|
const result = await approveUpgradeRequest(maestroUpgradeID);
|
|
logger.info(CTX, "approved", {
|
|
id: maestroUpgradeID,
|
|
duration: Date.now() - t0,
|
|
});
|
|
const emailSent = await sendUpgradeDoneEmail(
|
|
result.maestroName,
|
|
result.maestroEmail,
|
|
result.registeredActivateStatus,
|
|
result.registeredAccountType,
|
|
result.requestedAccountType,
|
|
formatDate(result.availableActivateDateTime)
|
|
);
|
|
return NextResponse.json({ ok: true, availableActivateDateTime: result.availableActivateDateTime, emailSent });
|
|
}
|
|
|
|
await rejectUpgradeRequest(maestroUpgradeID);
|
|
logger.info(CTX, "rejected", {
|
|
id: maestroUpgradeID,
|
|
duration: Date.now() - t0,
|
|
});
|
|
return NextResponse.json({ ok: true });
|
|
}
|
|
);
|