JEISA-WEBAPP/scripts/fix-items-field.mjs
Dave d606345de4 fix(quotes,po): repair item storage, totals and PDF line items
Quotes and purchase orders were silently losing their line items.

Quote items field was a multi-relation to stock_items, but PocketBase
stored only the last id as a plain string no matter how it was sent
(JSON string via FormData, raw array, or repeated FormData entries).
Every quote created through the UI had `items: ""` or `items: "<id>"`.
Recreated the field as JSON so an array of stock_item ids round-trips
(companion fields quantities/unitPrices already line up with it).

PO snapshot_items had the same class of bug from the other direction:
String(obj) coerced the snapshot array to "[object Object]", which
PocketBase stored verbatim, rendering the PO as N rows of
"Unknown / £0.00". The form now sends JSON-stringified arrays, and
the reader only trusts a snapshot whose rows actually carry a name.

Also fixes:
- Quote item lines are hydrated from stock_items; the parallel
  quantities/unitPrices arrays are the source of truth, so quotes
  written before the schema fix still render and total correctly.
- Quote form rejects stock rows where no item was actually picked
  from the dropdown, instead of saving an empty stockItemId.
- PO PDF filename is now the plain order number (PO-20261015-001.pdf).

Adds the quotes.items schema migration plus three dev scripts for
creating a quote, re-applying the field fix and rendering a test PDF.
2026-10-20 20:13:46 +02:00

50 lines
1.6 KiB
JavaScript

#!/usr/bin/env node
import PocketBase from "pocketbase";
const PB_URL = "https://pb.jeistk.apphub.co.za";
const PB_EMAIL = "david@wfsales.co.za";
const PB_PASSWORD = "1234567890";
const pb = new PocketBase(PB_URL);
pb.autoCancellation(false);
await pb.admins.authWithPassword(PB_EMAIL, PB_PASSWORD);
const COL_ID = "pbc_134296766";
const col = await pb.collections.getOne(COL_ID);
console.log("Loaded collection. Field names:", col.fields.map(f => f.name).join(","));
// Remove the broken relation field
col.fields = col.fields.filter(f => f.name !== "items");
// Add new items field as JSON
col.fields.push({
name: "items",
type: "json",
required: false,
options: { maxSize: 2000000 },
});
console.log("Sending update...");
try {
const updated = await pb.collections.update(COL_ID, col);
console.log("Update succeeded, items field now:");
const itemsField = updated.fields.find(f => f.name === "items");
console.log(JSON.stringify(itemsField, null, 2));
// Test
const test = await pb.collection("quotes").create({
quote_number: "QT-SCHEMA-FIX-TEST",
customerName: ["dgv6gxgcj2f2o14"],
quoteDate: "2026-09-14",
status: "Draft",
items: ["oz9zgj5xp72m77s", "gfcfv9mkveq95co", "9lfcwus2w5w9h84"],
quantities: [1, 2, 1],
unitPrices: [1, 2, 3],
});
console.log("Test quote items:", JSON.stringify(test.items));
await pb.collection("quotes").delete(test.id);
console.log("✅ schema fix verified");
} catch (e) {
console.error("Update failed:", e.status, JSON.stringify(e.response || e.data, null, 2));
console.error("URL:", e.url);
}