66 lines
2.3 KiB
JavaScript
66 lines
2.3 KiB
JavaScript
|
|
/// <reference path="../pb_data/types.d.ts" />
|
||
|
|
|
||
|
|
// =========================================================================
|
||
|
|
// customers — add address + VAT number
|
||
|
|
// =========================================================================
|
||
|
|
//
|
||
|
|
// A quote is a legal document. It currently carries only the customer's
|
||
|
|
// name, which does not identify the entity being quoted and looks
|
||
|
|
// unfinished on the PDF.
|
||
|
|
//
|
||
|
|
// This adds two optional fields to `customers`:
|
||
|
|
//
|
||
|
|
// address - postal address, shown on the quote PDF
|
||
|
|
// vatNumber - VAT registration number, shown on the quote PDF
|
||
|
|
//
|
||
|
|
// Both are optional so the 20 existing customer records keep working
|
||
|
|
// untouched; they can be filled in over time from the Customers screen.
|
||
|
|
//
|
||
|
|
// `email` and `phone` are added at the same time because the quote PDF
|
||
|
|
// already has a "Customer Information" block that prints a contact line
|
||
|
|
// when present - the fields existed in the UI model but had nowhere to
|
||
|
|
// be stored.
|
||
|
|
//
|
||
|
|
// NOTE: this migration is applied directly to the live PB instance by
|
||
|
|
// scripts/add-customer-fields.mjs.
|
||
|
|
// =========================================================================
|
||
|
|
|
||
|
|
migrate((app) => {
|
||
|
|
const collection = app.findCollectionByNameOrId("pbc_108570809"); // customers
|
||
|
|
|
||
|
|
if (!collection) {
|
||
|
|
throw new Error("customers collection not found — aborting.");
|
||
|
|
}
|
||
|
|
|
||
|
|
const newFields = [
|
||
|
|
{ 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" },
|
||
|
|
];
|
||
|
|
|
||
|
|
for (const field of newFields) {
|
||
|
|
if (collection.fields.some((f) => f.name === field.name)) continue;
|
||
|
|
collection.fields.push({
|
||
|
|
hidden: false,
|
||
|
|
id: field.id,
|
||
|
|
name: field.name,
|
||
|
|
presentable: false,
|
||
|
|
required: false,
|
||
|
|
system: false,
|
||
|
|
type: field.type,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
return app.save(collection);
|
||
|
|
}, (app) => {
|
||
|
|
// Rollback: drop the four fields we added.
|
||
|
|
const collection = app.findCollectionByNameOrId("pbc_108570809"); // customers
|
||
|
|
if (!collection) return;
|
||
|
|
|
||
|
|
const added = ["address", "vatNumber", "email", "phone"];
|
||
|
|
collection.fields = collection.fields.filter((f) => !added.includes(f.name));
|
||
|
|
|
||
|
|
return app.save(collection);
|
||
|
|
});
|