@dynamatix/cat-shared 0.0.12 → 0.0.14
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
|
-
|
|
2
|
-
|
|
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
|
|
7
|
-
schema.
|
|
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
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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); // 👈 Call the hook with the logs
|
|
25
24
|
}
|
|
26
25
|
|
|
27
|
-
|
|
28
|
-
next();
|
|
26
|
+
await AuditLog.create(log);
|
|
29
27
|
});
|
|
30
28
|
|
|
31
|
-
// Handle
|
|
32
|
-
schema.
|
|
33
|
-
|
|
34
|
-
|
|
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
|
|
34
|
+
if (!auditConfig?.fields?.length) return;
|
|
37
35
|
|
|
38
|
-
const
|
|
39
|
-
|
|
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 =
|
|
47
|
+
const oldValue = result[field];
|
|
49
48
|
|
|
50
49
|
if (oldValue !== newValue) {
|
|
51
50
|
logs.push({
|
|
52
51
|
name: `${collectionName}.${field}`,
|
|
53
|
-
|
|
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
|
-
|
|
63
|
+
if (onAuditLogCreated) {
|
|
64
|
+
onAuditLogCreated(logs); // 👈 Call the hook with the logs
|
|
65
|
+
}
|
|
64
66
|
});
|
|
65
67
|
}
|
|
66
68
|
|
|
67
|
-
|
|
69
|
+
|
|
70
|
+
export function registerAuditHook(callback) {
|
|
71
|
+
onAuditLogCreated = callback;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export default applyAuditMiddleware;
|
package/middlewares/index.js
CHANGED
package/models/audit.model.js
CHANGED
|
@@ -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