WFDASH-WEBAPP/pocketbase/pb_hooks/audit_log.pb.js
Dave cb0113f9f4 feat: add item supplier payments and approvals queue
Keep payment holds independent of workflow and margin approval. Require proof before collection readiness; deduplicate uploads transactionally in PocketBase and preserve payment history.

Includes schema migration, deployment runbook, browser checks and isolated PocketBase regression coverage. Full suite has 5156 passing tests and retains two pre-existing script-import errors.
2026-11-03 01:27:07 +02:00

42 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", "supplier_payment_requests");
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", "supplier_payment_requests");
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", "supplier_payment_requests");