mega-plant/app/api/cash-float/topup/route.ts
David cfd54385b3 Add Suppliers list; book purchase orders and petty-cash slips against it
- /expenses/suppliers: terms (cash / 30 / 60 days), PO + petty cash totals per supplier,
  period filters, add/edit/delete, due dates
- POs and slips resolve to a supplier by code then folded name; new PO suppliers start
  with terms unset, new slip suppliers as cash
- Full-user guard on cash-float and job-card write routes

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-22 15:43:57 +02:00

33 lines
1.6 KiB
TypeScript

import { revalidatePath } from "next/cache";
import { serverAuth } from "@/lib/pb";
import { refuseUnlessFull } from "@/lib/require-full";
import { getSession } from "@/lib/auth";
import { parseAmount } from "@/lib/cash-float";
export const dynamic = "force-dynamic";
// POST /api/cash-float/topup { amount, comment? } — money loaded onto the card (main app only).
export async function POST(req: Request) {
const refused = await refuseUnlessFull();
if (refused) return refused;
let body: { amount?: unknown; comment?: unknown } = {};
try { body = await req.json(); } catch { return Response.json({ ok: false, error: "Invalid JSON body" }, { status: 400 }); }
const amount = parseAmount(body.amount);
if (amount === null) return Response.json({ ok: false, error: "Enter the amount loaded, more than 0." }, { status: 400 });
try {
const pb = await serverAuth();
if (!(await pb.collection("cash_float_entries").getList(1, 1, { filter: 'kind = "opening"' })).totalItems) {
return Response.json({ ok: false, error: "Set the opening balance first." }, { status: 409 });
}
const session = await getSession();
await pb.collection("cash_float_entries").create({
kind: "topup", amount, occurred_on: new Date().toISOString(),
comment: String(body.comment || "").trim().slice(0, 1000) || "Card top-up",
recorded_by: String(session?.user.name || session?.user.email || ""),
});
revalidatePath("/expenses/petty-cash");
return Response.json({ ok: true });
} catch (e: any) {
return Response.json({ ok: false, error: e?.message || "Could not save" }, { status: 500 });
}
}