feat(api): POST /api/sites/[id]/fixed-costs
This commit is contained in:
parent
9f2c10851c
commit
11658bce2d
1 changed files with 66 additions and 0 deletions
66
src/app/api/sites/[id]/fixed-costs/route.ts
Normal file
66
src/app/api/sites/[id]/fixed-costs/route.ts
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
import { NextResponse } from "next/server";
|
||||
import { pbAdmin } from "@/lib/pb";
|
||||
|
||||
const ISO_DATE = /^\d{4}-\d{2}-\d{2}$/;
|
||||
|
||||
function badRequest(field: string, message: string) {
|
||||
return NextResponse.json({ error: "validation", field, message }, { status: 400 });
|
||||
}
|
||||
|
||||
export async function POST(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
const { id } = await params;
|
||||
|
||||
let body: Record<string, unknown>;
|
||||
try {
|
||||
body = await request.json();
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Body must be JSON" }, { status: 400 });
|
||||
}
|
||||
|
||||
const description = typeof body.description === "string" ? body.description.trim() : "";
|
||||
if (!description) return badRequest("description", "Description is required.");
|
||||
if (description.length > 200) return badRequest("description", "Description must be 200 characters or fewer.");
|
||||
|
||||
const costNum = typeof body.cost === "number" ? body.cost : Number(body.cost);
|
||||
if (!Number.isFinite(costNum) || costNum <= 0) {
|
||||
return badRequest("cost", "Cost must be greater than zero.");
|
||||
}
|
||||
|
||||
const dateLogged = typeof body.date_logged === "string" ? body.date_logged : "";
|
||||
if (!ISO_DATE.test(dateLogged)) {
|
||||
return badRequest("date_logged", "Date must be in YYYY-MM-DD format.");
|
||||
}
|
||||
|
||||
if (typeof body.is_recurring !== "boolean") {
|
||||
return badRequest("is_recurring", "is_recurring is required and must be a boolean.");
|
||||
}
|
||||
|
||||
const notes = typeof body.notes === "string" ? body.notes.slice(0, 500) : "";
|
||||
|
||||
const pb = await pbAdmin();
|
||||
let site;
|
||||
try {
|
||||
site = await pb.collection("sites").getOne(id);
|
||||
} catch {
|
||||
return NextResponse.json({ error: "site_not_found" }, { status: 404 });
|
||||
}
|
||||
|
||||
try {
|
||||
const created = await pb.collection("fixed_costs").create({
|
||||
site: id,
|
||||
project_code: site.project_code,
|
||||
description,
|
||||
cost: costNum,
|
||||
date_logged: dateLogged,
|
||||
notes,
|
||||
is_recurring: body.is_recurring,
|
||||
});
|
||||
return NextResponse.json({ fixed_cost: created }, { status: 201 });
|
||||
} catch (err) {
|
||||
console.error("[POST /api/sites/[id]/fixed-costs] PB write failed", err);
|
||||
return NextResponse.json({ error: "write_failed" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue