Renames the existing /income/invoices page to "Internal Invoices" and adds a sibling /income/external-invoices page, splitting ONE `invoices` collection by the debtor printed on the invoice: a Mega group company is internal, anything else is external. Never two collections. An invoice must not be able to exist on neither page, and the halves must stay reconcilable against /income. Both routes render through the single components/InvoiceRegisterPage.tsx so they cannot drift apart. lib/invoice-party-policy.json holds the group entity list and lib/invoice-party.ts does the matching. Matching is on debtor name OR debtor_no, never name alone: all three external invoices (PHI 178/186/192) carry an empty debtor name and only a third-party number, so a name-only match would misfile every one of them. An invoice naming neither is never internal - it lands on External and is reported as unclassified rather than silently filed. The split is 16 internal / 3 external across the 19 invoices in the register. Also fixes three things the split would otherwise break: - Booking a line, importing a PDF and retrying an intake can all land on either side depending on the debtor, so the five write paths only revalidated /income/invoices and would have left the other page stale. They now call revalidateInvoicePages() from lib/invoice-revalidate.ts. Route paths live in lib/invoice-routes.ts, which imports no next/* so a client component may import it; the revalidate helper imports next/cache and is server-only. - A site's "Go and assign them" link hard-coded the internal path, so a site whose outstanding lines sit on external invoices would dead-end. SiteSummary now carries the outstanding lines split by party. - verify-auth-routes.mjs now covers the new route. The two routes are siblings by design: isGroupActive matches a child by path prefix, so a nested /income/invoices/external would highlight both leaves at once. Verified: typecheck clean, 41 tests pass (7 new), production build green, anonymous access to both routes 307s to /login, and the mobile checklist passes at 375x812 and 768x1024 - no horizontal page scroll, no tap target under 44px, no text under 12px. The nested line-item table still scrolls inside its own container without stretching the outer table. Note: lib/rows.ts, lib/server-data.ts and the running/insurance feature are someone else's in-flight work and were deliberately left uncommitted here.
98 lines
No EOL
3.2 KiB
TypeScript
98 lines
No EOL
3.2 KiB
TypeScript
import { serverAuth } from "@/lib/pb";
|
|
import { revalidateInvoicePages } from "@/lib/invoice-revalidate";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
// POST /api/invoices/upload
|
|
// Body: multipart/form-data with `file` and `invoiceNo`.
|
|
//
|
|
// Stores the original document against the invoice's `source_pdf` file field so the
|
|
// source of every extracted figure stays attached to the row it came from.
|
|
//
|
|
// Accepts PDF only — an invoice in any other format cannot have its line items verified
|
|
// against the PDF totals, and accepting it silently would break that guarantee.
|
|
|
|
const MAX_BYTES = 20 * 1024 * 1024; // matches the PocketBase field's maxSize
|
|
|
|
export async function POST(req: Request) {
|
|
let form: FormData;
|
|
try {
|
|
form = await req.formData();
|
|
} catch {
|
|
return Response.json({ ok: false, error: "Expected multipart/form-data" }, { status: 400 });
|
|
}
|
|
|
|
const file = form.get("file");
|
|
const invoiceNo = String(form.get("invoiceNo") ?? "").trim();
|
|
|
|
if (!(file instanceof File) || file.size === 0) {
|
|
return Response.json({ ok: false, error: "No file received" }, { status: 400 });
|
|
}
|
|
if (file.type !== "application/pdf" && !file.name.toLowerCase().endsWith(".pdf")) {
|
|
return Response.json(
|
|
{ ok: false, error: "Only PDF invoices are accepted" },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
if (file.size > MAX_BYTES) {
|
|
return Response.json(
|
|
{ ok: false, error: `File is ${(file.size / 1048576).toFixed(1)} MB — limit is 20 MB` },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
|
|
const pb = await serverAuth();
|
|
|
|
try {
|
|
// Resolve the invoice by number; fall back to the only one present.
|
|
let target = null;
|
|
if (invoiceNo) {
|
|
const found = await pb.collection("invoices").getFullList({
|
|
filter: `invoice_no = "${invoiceNo.replace(/"/g, "")}"`,
|
|
perPage: 1,
|
|
});
|
|
target = found[0] ?? null;
|
|
if (!target) {
|
|
return Response.json(
|
|
{ ok: false, error: `No invoice found with number "${invoiceNo}"` },
|
|
{ status: 404 },
|
|
);
|
|
}
|
|
} else {
|
|
const all = await pb.collection("invoices").getFullList({ perPage: 2 });
|
|
if (all.length === 0) {
|
|
return Response.json({ ok: false, error: "No invoices exist yet" }, { status: 404 });
|
|
}
|
|
if (all.length > 1) {
|
|
return Response.json(
|
|
{ ok: false, error: "More than one invoice — specify which by number" },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
target = all[0];
|
|
}
|
|
|
|
const safeName = file.name.replace(/[^\w.\- ]+/g, "_").slice(-120);
|
|
const form2 = new FormData();
|
|
form2.append("source_pdf", new File([await file.arrayBuffer()], safeName, { type: "application/pdf" }));
|
|
|
|
const updated = await pb.collection("invoices").update(target.id, form2);
|
|
|
|
revalidateInvoicePages();
|
|
|
|
const stored = updated.source_pdf as string | undefined;
|
|
return Response.json({
|
|
ok: true,
|
|
invoice_no: target.invoice_no,
|
|
file: safeName,
|
|
size: file.size,
|
|
// PocketBase returns "filename" or "filename_abcd1234"; the base name is the file.
|
|
path: stored?.split("/").pop() ?? null,
|
|
});
|
|
} catch (e: any) {
|
|
return Response.json(
|
|
{ ok: false, error: e?.message || "Upload failed" },
|
|
{ status: 500 },
|
|
);
|
|
}
|
|
} |