43 lines
1.7 KiB
JavaScript
43 lines
1.7 KiB
JavaScript
|
|
/// <reference path="../pb_data/types.d.ts" />
|
||
|
|
/**
|
||
|
|
* Audit trail for quotes, orders and delivery notes.
|
||
|
|
*
|
||
|
|
* Every create / update / delete made through the PB API on the
|
||
|
|
* collections below writes one `audit_log` row: what changed (per-field
|
||
|
|
* from → to), who did it, and which quote / order / delivery note it
|
||
|
|
* belongs to. Covers every writer — the dashboard's direct PB calls,
|
||
|
|
* the WFDASH server, n8n and the MCP — because they all go through the
|
||
|
|
* API.
|
||
|
|
*
|
||
|
|
* Deploy: copy BOTH this file and audit_log_utils.js into the PB
|
||
|
|
* server's pb_hooks/ directory, after running
|
||
|
|
* scripts/migrations/2026-09-25-create-audit-log.ts. PB picks up new
|
||
|
|
* hook files on restart.
|
||
|
|
*/
|
||
|
|
|
||
|
|
onRecordCreateRequest((e) => {
|
||
|
|
const audit = require(`${__hooks}/audit_log_utils.js`);
|
||
|
|
e.next();
|
||
|
|
audit.log(e, "create", null, audit.toPlain(e.record));
|
||
|
|
}, "quotes", "quote_items", "quote_attachments", "orders", "order_items", "delivery_notes");
|
||
|
|
|
||
|
|
onRecordUpdateRequest((e) => {
|
||
|
|
const audit = require(`${__hooks}/audit_log_utils.js`);
|
||
|
|
// Snapshot the stored state before the save replaces it.
|
||
|
|
let before = null;
|
||
|
|
try {
|
||
|
|
before = audit.toPlain(e.record.original());
|
||
|
|
} catch (err) {
|
||
|
|
console.log("[audit_log] could not snapshot original: " + err);
|
||
|
|
}
|
||
|
|
e.next();
|
||
|
|
if (before) audit.log(e, "update", before, audit.toPlain(e.record));
|
||
|
|
}, "quotes", "quote_items", "orders", "order_items", "delivery_notes");
|
||
|
|
|
||
|
|
onRecordDeleteRequest((e) => {
|
||
|
|
const audit = require(`${__hooks}/audit_log_utils.js`);
|
||
|
|
const before = audit.toPlain(e.record);
|
||
|
|
e.next();
|
||
|
|
audit.log(e, "delete", before, null);
|
||
|
|
}, "quotes", "quote_items", "quote_attachments", "orders", "order_items", "delivery_notes");
|