61 lines
1.6 KiB
TypeScript
61 lines
1.6 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { revalidatePath } from "next/cache";
|
|
import { getPb } from "../../../../lib/pb";
|
|
|
|
const BATCHES = "JEI_payment_batches";
|
|
const ALLOWED_MIME = new Set(["application/pdf", "image/jpeg", "image/png"]);
|
|
const MAX_POP_BYTES = 8 * 1024 * 1024;
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
export async function POST(
|
|
req: NextRequest,
|
|
{ params }: { params: { id: string } },
|
|
) {
|
|
const pb = getPb();
|
|
const id = params.id;
|
|
|
|
// Confirm batch exists
|
|
try {
|
|
await pb.collection(BATCHES).getOne(id);
|
|
} catch (e: any) {
|
|
return NextResponse.json({ error: `Batch ${id} not found` }, { status: 404 });
|
|
}
|
|
|
|
let form: FormData;
|
|
try {
|
|
form = await req.formData();
|
|
} catch (e: any) {
|
|
return NextResponse.json({ error: `Invalid form-data: ${e?.message || e}` }, { status: 400 });
|
|
}
|
|
|
|
const popFile = form.get("pop_file");
|
|
if (!(popFile instanceof File)) {
|
|
return NextResponse.json({ error: "pop_file is required" }, { status: 400 });
|
|
}
|
|
if (!ALLOWED_MIME.has(popFile.type)) {
|
|
return NextResponse.json(
|
|
{ error: `pop_file type must be PDF, JPG or PNG (got ${popFile.type})` },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
if (popFile.size > MAX_POP_BYTES) {
|
|
return NextResponse.json(
|
|
{ error: `pop_file exceeds 8 MB (got ${popFile.size} bytes)` },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
|
|
try {
|
|
await pb.collection(BATCHES).update(id, { pop_file: popFile });
|
|
} catch (e: any) {
|
|
return NextResponse.json(
|
|
{ error: `Failed to replace POP: ${e?.message || e}` },
|
|
{ status: 500 },
|
|
);
|
|
}
|
|
|
|
revalidatePath("/remittance");
|
|
|
|
return NextResponse.json({ ok: true });
|
|
}
|