68 lines
1.5 KiB
TypeScript
68 lines
1.5 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { z } from "zod";
|
|
|
|
import { auth } from "@/auth";
|
|
import {
|
|
approveExtensionRequest,
|
|
cancelExtensionRequest,
|
|
ExtensionRequestActionError,
|
|
} from "@/lib/extension-requests";
|
|
|
|
const patchBodySchema = z.object({
|
|
action: z.enum(["approve", "cancel"]),
|
|
});
|
|
|
|
type ExtensionRequestRouteContext = {
|
|
params: Promise<{
|
|
id: string;
|
|
}>;
|
|
};
|
|
|
|
export async function PATCH(
|
|
request: Request,
|
|
{ params }: ExtensionRequestRouteContext
|
|
) {
|
|
const session = await auth();
|
|
|
|
if (!session) {
|
|
return NextResponse.json({ message: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
const { id } = await params;
|
|
const maestroExtensionID = Number(id);
|
|
|
|
if (!Number.isInteger(maestroExtensionID) || maestroExtensionID <= 0) {
|
|
return NextResponse.json(
|
|
{ message: "Invalid extension request id" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const body = patchBodySchema.safeParse(
|
|
await request.json().catch(() => ({}))
|
|
);
|
|
|
|
if (!body.success) {
|
|
return NextResponse.json({ message: "Invalid action" }, { status: 400 });
|
|
}
|
|
|
|
try {
|
|
if (body.data.action === "approve") {
|
|
const result = await approveExtensionRequest(maestroExtensionID);
|
|
return NextResponse.json({ ok: true, ...result });
|
|
}
|
|
|
|
await cancelExtensionRequest(maestroExtensionID);
|
|
return NextResponse.json({ ok: true });
|
|
} catch (error) {
|
|
if (error instanceof ExtensionRequestActionError) {
|
|
return NextResponse.json(
|
|
{ message: error.message },
|
|
{ status: error.statusCode }
|
|
);
|
|
}
|
|
|
|
throw error;
|
|
}
|
|
}
|