WFDASH-PBHOOKS/audit_log.pb.js
Dave c3d4d3e21c feat(audit): implement audit trail for delivery notes and orders
- Added an audit trail feature that logs every create, update, and delete action on delivery notes and orders, capturing detailed changes per field.
- Introduced the `AuditTimeline` component to display the history of changes for delivery notes and orders, enhancing traceability.
- Updated the `page.tsx` files for delivery notes and orders to fetch and display audit logs, ensuring users can view the history of modifications.
- Enhanced the `package.json` with a new migration script for setting up the audit log functionality.

This feature improves accountability and transparency in the management of delivery notes and orders.
2026-09-30 01:48:44 +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");
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");