@dynamatix/cat-shared 0.0.16 → 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.
- package/middlewares/audit.middleware.js +20 -15
- package/package.json +1 -1
|
@@ -2,13 +2,12 @@ import AuditConfigModel from '../models/audit-config.model.js';
|
|
|
2
2
|
import AuditLog from '../models/audit.model.js';
|
|
3
3
|
import { getContext } from '../services/request-context.service.js';
|
|
4
4
|
|
|
5
|
-
|
|
6
5
|
let onAuditLogCreated = null;
|
|
7
6
|
|
|
8
7
|
function applyAuditMiddleware(schema, collectionName) {
|
|
9
|
-
// Handle create
|
|
8
|
+
// Handle create (after save)
|
|
10
9
|
schema.post('save', async function (doc) {
|
|
11
|
-
const auditConfig = await
|
|
10
|
+
const auditConfig = await AuditConfigModel.findOne({ collectionName });
|
|
12
11
|
if (!auditConfig?.trackCreation) return;
|
|
13
12
|
|
|
14
13
|
const context = getContext();
|
|
@@ -23,23 +22,28 @@ function applyAuditMiddleware(schema, collectionName) {
|
|
|
23
22
|
createdBy: userId,
|
|
24
23
|
contextId
|
|
25
24
|
};
|
|
25
|
+
|
|
26
|
+
await AuditLog.create(log);
|
|
27
|
+
|
|
26
28
|
if (onAuditLogCreated) {
|
|
27
|
-
onAuditLogCreated(
|
|
29
|
+
await onAuditLogCreated([log], contextId);
|
|
28
30
|
}
|
|
31
|
+
});
|
|
29
32
|
|
|
30
|
-
|
|
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();
|
|
31
37
|
});
|
|
32
38
|
|
|
33
39
|
// Handle updates (after successful update)
|
|
34
40
|
schema.post(['findOneAndUpdate', 'findByIdAndUpdate'], async function (result) {
|
|
35
|
-
if (!result) return;
|
|
41
|
+
if (!result || !this._originalDoc) return;
|
|
36
42
|
|
|
37
43
|
const auditConfig = await AuditConfigModel.findOne({ collectionName });
|
|
38
44
|
if (!auditConfig?.fields?.length) return;
|
|
39
45
|
|
|
40
|
-
const
|
|
41
|
-
const update = query.getUpdate();
|
|
42
|
-
|
|
46
|
+
const update = this.getUpdate();
|
|
43
47
|
const logs = [];
|
|
44
48
|
const context = getContext();
|
|
45
49
|
const userId = context?.userId || 'anonymous';
|
|
@@ -48,7 +52,7 @@ function applyAuditMiddleware(schema, collectionName) {
|
|
|
48
52
|
for (const field of auditConfig.fields) {
|
|
49
53
|
if (update?.$set?.hasOwnProperty(field) || update?.hasOwnProperty(field)) {
|
|
50
54
|
const newValue = update?.$set?.[field] ?? update?.[field];
|
|
51
|
-
const oldValue =
|
|
55
|
+
const oldValue = this._originalDoc[field];
|
|
52
56
|
|
|
53
57
|
if (oldValue !== newValue) {
|
|
54
58
|
logs.push({
|
|
@@ -63,16 +67,17 @@ function applyAuditMiddleware(schema, collectionName) {
|
|
|
63
67
|
}
|
|
64
68
|
}
|
|
65
69
|
|
|
66
|
-
if (logs.length)
|
|
67
|
-
|
|
68
|
-
onAuditLogCreated
|
|
70
|
+
if (logs.length) {
|
|
71
|
+
await AuditLog.insertMany(logs);
|
|
72
|
+
if (onAuditLogCreated) {
|
|
73
|
+
await onAuditLogCreated(logs, contextId);
|
|
74
|
+
}
|
|
69
75
|
}
|
|
70
76
|
});
|
|
71
77
|
}
|
|
72
78
|
|
|
73
|
-
|
|
74
79
|
export function registerAuditHook(callback) {
|
|
75
80
|
onAuditLogCreated = callback;
|
|
76
81
|
}
|
|
77
82
|
|
|
78
|
-
export default applyAuditMiddleware;
|
|
83
|
+
export default applyAuditMiddleware;
|