@dynamatix/cat-shared 0.0.15 → 0.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,11 @@
1
+ import AuditConfigModel from '../models/audit-config.model.js';
2
+ import AuditLog from '../models/audit.model.js';
3
+ import { getContext } from '../services/request-context.service.js';
1
4
 
2
5
  let onAuditLogCreated = null;
3
6
 
4
7
  function applyAuditMiddleware(schema, collectionName) {
5
- // Handle create
8
+ // Handle create (after save)
6
9
  schema.post('save', async function (doc) {
7
10
  const auditConfig = await AuditConfigModel.findOne({ collectionName });
8
11
  if (!auditConfig?.trackCreation) return;
@@ -19,23 +22,28 @@ function applyAuditMiddleware(schema, collectionName) {
19
22
  createdBy: userId,
20
23
  contextId
21
24
  };
25
+
26
+ await AuditLog.create(log);
27
+
22
28
  if (onAuditLogCreated) {
23
- onAuditLogCreated(logs, contextId); // 👈 Call the hook with the logs
29
+ await onAuditLogCreated([log], contextId);
24
30
  }
31
+ });
25
32
 
26
- await AuditLog.create(log);
33
+ // Capture original doc before update
34
+ schema.pre(['findOneAndUpdate', 'findByIdAndUpdate'], async function (next) {
35
+ this._originalDoc = await this.model.findOne(this.getQuery()).lean();
36
+ next();
27
37
  });
28
38
 
29
39
  // Handle updates (after successful update)
30
40
  schema.post(['findOneAndUpdate', 'findByIdAndUpdate'], async function (result) {
31
- if (!result) return; // No doc was updated
41
+ if (!result || !this._originalDoc) return;
32
42
 
33
43
  const auditConfig = await AuditConfigModel.findOne({ collectionName });
34
44
  if (!auditConfig?.fields?.length) return;
35
45
 
36
- const query = this;
37
- const update = query.getUpdate();
38
-
46
+ const update = this.getUpdate();
39
47
  const logs = [];
40
48
  const context = getContext();
41
49
  const userId = context?.userId || 'anonymous';
@@ -44,7 +52,7 @@ function applyAuditMiddleware(schema, collectionName) {
44
52
  for (const field of auditConfig.fields) {
45
53
  if (update?.$set?.hasOwnProperty(field) || update?.hasOwnProperty(field)) {
46
54
  const newValue = update?.$set?.[field] ?? update?.[field];
47
- const oldValue = result[field];
55
+ const oldValue = this._originalDoc[field];
48
56
 
49
57
  if (oldValue !== newValue) {
50
58
  logs.push({
@@ -59,16 +67,17 @@ function applyAuditMiddleware(schema, collectionName) {
59
67
  }
60
68
  }
61
69
 
62
- if (logs.length) await AuditLog.insertMany(logs);
63
- if (onAuditLogCreated) {
64
- onAuditLogCreated(logs, contextId); // 👈 Call the hook with the logs
70
+ if (logs.length) {
71
+ await AuditLog.insertMany(logs);
72
+ if (onAuditLogCreated) {
73
+ await onAuditLogCreated(logs, contextId);
74
+ }
65
75
  }
66
76
  });
67
77
  }
68
78
 
69
-
70
79
  export function registerAuditHook(callback) {
71
80
  onAuditLogCreated = callback;
72
81
  }
73
82
 
74
- export default applyAuditMiddleware;
83
+ export default applyAuditMiddleware;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dynamatix/cat-shared",
3
- "version": "0.0.15",
3
+ "version": "0.0.17",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"