@dynamatix/cat-shared 0.0.13 → 0.0.15

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,67 +1,74 @@
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
+
2
+ let onAuditLogCreated = null;
4
3
 
5
4
  function applyAuditMiddleware(schema, collectionName) {
6
- // Handle create and save
7
- schema.pre('save', async function (next) {
8
- const doc = this;
9
- const isNewDoc = doc.isNew;
5
+ // Handle create
6
+ schema.post('save', async function (doc) {
10
7
  const auditConfig = await AuditConfigModel.findOne({ collectionName });
11
- if (!auditConfig) return next();
8
+ if (!auditConfig?.trackCreation) return;
12
9
 
13
- const logs = [];
14
10
  const context = getContext();
15
11
  const userId = context?.userId || 'anonymous';
12
+ const contextId = context?.contextId;
16
13
 
17
- if (isNewDoc && auditConfig.trackCreation) {
18
- logs.push({
19
- name: `${collectionName} created`,
20
- recordId: doc._id,
21
- oldValue: null,
22
- newValue: 'Document created',
23
- createdBy: userId
24
- });
14
+ const log = {
15
+ name: `${collectionName} created`,
16
+ recordId: doc._id,
17
+ oldValue: null,
18
+ newValue: 'Document created',
19
+ createdBy: userId,
20
+ contextId
21
+ };
22
+ if (onAuditLogCreated) {
23
+ onAuditLogCreated(logs, contextId); // 👈 Call the hook with the logs
25
24
  }
26
25
 
27
- if (logs.length) await AuditLog.insertMany(logs);
28
- next();
26
+ await AuditLog.create(log);
29
27
  });
30
28
 
31
- // Handle update queries like findByIdAndUpdate
32
- schema.pre(['findOneAndUpdate', 'findByIdAndUpdate'], async function (next) {
33
- const query = this;
34
- const update = query.getUpdate();
29
+ // Handle updates (after successful update)
30
+ schema.post(['findOneAndUpdate', 'findByIdAndUpdate'], async function (result) {
31
+ if (!result) return; // No doc was updated
32
+
35
33
  const auditConfig = await AuditConfigModel.findOne({ collectionName });
36
- if (!auditConfig || !auditConfig.fields?.length) return next();
34
+ if (!auditConfig?.fields?.length) return;
37
35
 
38
- const original = await query.model.findOne(query.getQuery()).lean();
39
- if (!original) return next();
36
+ const query = this;
37
+ const update = query.getUpdate();
40
38
 
41
39
  const logs = [];
42
40
  const context = getContext();
43
41
  const userId = context?.userId || 'anonymous';
42
+ const contextId = context?.contextId;
44
43
 
45
44
  for (const field of auditConfig.fields) {
46
45
  if (update?.$set?.hasOwnProperty(field) || update?.hasOwnProperty(field)) {
47
46
  const newValue = update?.$set?.[field] ?? update?.[field];
48
- const oldValue = original[field];
47
+ const oldValue = result[field];
49
48
 
50
49
  if (oldValue !== newValue) {
51
50
  logs.push({
52
51
  name: `${collectionName}.${field}`,
53
- recordId: original._id,
52
+ recordId: result._id,
54
53
  oldValue,
55
54
  newValue,
56
- createdBy: userId
55
+ createdBy: userId,
56
+ contextId
57
57
  });
58
58
  }
59
59
  }
60
60
  }
61
61
 
62
62
  if (logs.length) await AuditLog.insertMany(logs);
63
- next();
63
+ if (onAuditLogCreated) {
64
+ onAuditLogCreated(logs, contextId); // 👈 Call the hook with the logs
65
+ }
64
66
  });
65
67
  }
66
68
 
67
- export default applyAuditMiddleware;
69
+
70
+ export function registerAuditHook(callback) {
71
+ onAuditLogCreated = callback;
72
+ }
73
+
74
+ export default applyAuditMiddleware;
@@ -1 +1,2 @@
1
1
  export {default as applyAuditMiddleware} from './audit.middleware.js';
2
+ export { registerAuditHook } from './audit.middleware.js';
@@ -3,6 +3,7 @@ import mongoose from 'mongoose';
3
3
  const auditSchema = new mongoose.Schema({
4
4
  name: String,
5
5
  recordId: mongoose.Schema.Types.ObjectId,
6
+ contextId: mongoose.Schema.Types.ObjectId,
6
7
  oldValue: mongoose.Schema.Types.Mixed,
7
8
  newValue: mongoose.Schema.Types.Mixed,
8
9
  timestamp: { type: Date, default: Date.now },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dynamatix/cat-shared",
3
- "version": "0.0.13",
3
+ "version": "0.0.15",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -0,0 +1,2 @@
1
+
2
+