70 lines
2.3 KiB
JavaScript
70 lines
2.3 KiB
JavaScript
|
|
#!/usr/bin/env node
|
||
|
|
/**
|
||
|
|
* Add the customer fields the quote PDF needs: address, VAT number,
|
||
|
|
* email and phone.
|
||
|
|
*
|
||
|
|
* The `customers` collection only had `comapny_name` and
|
||
|
|
* `billing_company`, so the quote PDF's "Customer Information" block
|
||
|
|
* could only ever print a name. These four optional fields give it
|
||
|
|
* something to print.
|
||
|
|
*
|
||
|
|
* Existing records are left alone — the fields are optional and can be
|
||
|
|
* filled in over time from the Customers screen.
|
||
|
|
*
|
||
|
|
* Usage:
|
||
|
|
* node scripts/add-customer-fields.mjs # dry run (default)
|
||
|
|
* node scripts/add-customer-fields.mjs --apply # apply to the instance
|
||
|
|
*/
|
||
|
|
|
||
|
|
import PocketBase from "pocketbase";
|
||
|
|
|
||
|
|
const PB_URL = process.env.POCKETBASE_URL || "https://pb.jeistk.apphub.co.za";
|
||
|
|
const PB_EMAIL = process.env.POCKETBASE_ADMIN_EMAIL || "david@wfsales.co.za";
|
||
|
|
const PB_PASSWORD = process.env.POCKETBASE_ADMIN_PASSWORD || "1234567890";
|
||
|
|
|
||
|
|
const APPLY = process.argv.includes("--apply");
|
||
|
|
|
||
|
|
const NEW_FIELDS = [
|
||
|
|
{ id: "text_cust_address", name: "address", type: "text" },
|
||
|
|
{ id: "text_cust_vat", name: "vatNumber", type: "text" },
|
||
|
|
{ id: "text_cust_email", name: "email", type: "email" },
|
||
|
|
{ id: "text_cust_phone", name: "phone", type: "text" },
|
||
|
|
];
|
||
|
|
|
||
|
|
const pb = new PocketBase(PB_URL);
|
||
|
|
pb.autoCancellation(false);
|
||
|
|
|
||
|
|
await pb.admins.authWithPassword(PB_EMAIL, PB_PASSWORD);
|
||
|
|
|
||
|
|
const collection = await pb.collections.getOne("customers");
|
||
|
|
|
||
|
|
const existing = new Set(collection.fields.map((f) => f.name));
|
||
|
|
const toAdd = NEW_FIELDS.filter((f) => !existing.has(f.name));
|
||
|
|
|
||
|
|
console.log("=== BEFORE ===");
|
||
|
|
console.log(` fields: ${collection.fields.map((f) => f.name).join(", ")}`);
|
||
|
|
console.log(` adding: ${toAdd.length ? toAdd.map((f) => f.name).join(", ") : "(none - already applied)"}`);
|
||
|
|
|
||
|
|
if (!APPLY) {
|
||
|
|
console.log("\nDry run. Re-run with --apply to write these fields to the instance.");
|
||
|
|
process.exit(0);
|
||
|
|
}
|
||
|
|
|
||
|
|
for (const field of toAdd) {
|
||
|
|
collection.fields.push({
|
||
|
|
hidden: false,
|
||
|
|
id: field.id,
|
||
|
|
name: field.name,
|
||
|
|
presentable: false,
|
||
|
|
required: false,
|
||
|
|
system: false,
|
||
|
|
type: field.type,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
const updated = await pb.collections.update(collection.id, collection);
|
||
|
|
|
||
|
|
console.log("\n=== AFTER ===");
|
||
|
|
console.log(` fields: ${updated.fields.map((f) => f.name).join(", ")}`);
|
||
|
|
console.log("\nDone. Fill the new fields in from the Customers screen.");
|