@dynamatix/cat-shared 0.0.10 → 0.0.12
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/index.js +2 -1
- package/middlewares/audit.middleware.js +38 -15
- package/package.json +3 -2
- package/services/index.js +1 -0
- package/services/request-context.service.js +8 -0
package/index.js
CHANGED
|
@@ -1,35 +1,59 @@
|
|
|
1
1
|
import AuditConfigModel from '../models/audit-config.model.js';
|
|
2
|
-
|
|
2
|
+
import AuditLog from '../models/audit.model.js';
|
|
3
|
+
import { getContext } from '../services/request-context.service.js';
|
|
3
4
|
|
|
4
5
|
function applyAuditMiddleware(schema, collectionName) {
|
|
5
|
-
|
|
6
|
+
// Handle create and save
|
|
7
|
+
schema.pre('save', async function (next) {
|
|
6
8
|
const doc = this;
|
|
7
9
|
const isNewDoc = doc.isNew;
|
|
8
10
|
const auditConfig = await AuditConfigModel.findOne({ collectionName });
|
|
9
11
|
if (!auditConfig) return next();
|
|
10
12
|
|
|
11
13
|
const logs = [];
|
|
14
|
+
const context = getContext();
|
|
15
|
+
const userId = context?.userId || 'anonymous';
|
|
12
16
|
|
|
13
17
|
if (isNewDoc && auditConfig.trackCreation) {
|
|
14
18
|
logs.push({
|
|
15
|
-
name: collectionName
|
|
19
|
+
name: `${collectionName} created`,
|
|
16
20
|
documentId: doc._id,
|
|
17
21
|
oldValue: null,
|
|
18
22
|
newValue: 'Document created',
|
|
19
|
-
createdBy:
|
|
23
|
+
createdBy: userId
|
|
20
24
|
});
|
|
21
|
-
}
|
|
22
|
-
const original = await doc.constructor.findById(doc._id).lean();
|
|
23
|
-
if (!original) return next();
|
|
25
|
+
}
|
|
24
26
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
+
if (logs.length) await AuditLog.insertMany(logs);
|
|
28
|
+
next();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// Handle update queries like findByIdAndUpdate
|
|
32
|
+
schema.pre(['findOneAndUpdate', 'findByIdAndUpdate'], async function (next) {
|
|
33
|
+
const query = this;
|
|
34
|
+
const update = query.getUpdate();
|
|
35
|
+
const auditConfig = await AuditConfigModel.findOne({ collectionName });
|
|
36
|
+
if (!auditConfig || !auditConfig.fields?.length) return next();
|
|
37
|
+
|
|
38
|
+
const original = await query.model.findOne(query.getQuery()).lean();
|
|
39
|
+
if (!original) return next();
|
|
40
|
+
|
|
41
|
+
const logs = [];
|
|
42
|
+
const context = getContext();
|
|
43
|
+
const userId = context?.userId || 'anonymous';
|
|
44
|
+
|
|
45
|
+
for (const field of auditConfig.fields) {
|
|
46
|
+
if (update?.$set?.hasOwnProperty(field) || update?.hasOwnProperty(field)) {
|
|
47
|
+
const newValue = update?.$set?.[field] ?? update?.[field];
|
|
48
|
+
const oldValue = original[field];
|
|
49
|
+
|
|
50
|
+
if (oldValue !== newValue) {
|
|
27
51
|
logs.push({
|
|
28
|
-
name: collectionName
|
|
29
|
-
documentId:
|
|
30
|
-
oldValue
|
|
31
|
-
newValue
|
|
32
|
-
createdBy:
|
|
52
|
+
name: `${collectionName}.${field}`,
|
|
53
|
+
documentId: original._id,
|
|
54
|
+
oldValue,
|
|
55
|
+
newValue,
|
|
56
|
+
createdBy: userId
|
|
33
57
|
});
|
|
34
58
|
}
|
|
35
59
|
}
|
|
@@ -40,5 +64,4 @@ function applyAuditMiddleware(schema, collectionName) {
|
|
|
40
64
|
});
|
|
41
65
|
}
|
|
42
66
|
|
|
43
|
-
|
|
44
67
|
export default applyAuditMiddleware;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dynamatix/cat-shared",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.12",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"description": "",
|
|
12
12
|
"exports": {
|
|
13
13
|
"./models": "./models/index.js",
|
|
14
|
-
"./middlewares": "./middlewares/index.js"
|
|
14
|
+
"./middlewares": "./middlewares/index.js",
|
|
15
|
+
"./services": "./services/index.js"
|
|
15
16
|
}
|
|
16
17
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { getContext, setContext } from './request-context.service.js';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// utils/request-context.js
|
|
2
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
3
|
+
|
|
4
|
+
const asyncLocalStorage = new AsyncLocalStorage();
|
|
5
|
+
|
|
6
|
+
export const setContext = (data) => asyncLocalStorage.enterWith(data);
|
|
7
|
+
|
|
8
|
+
export const getContext = () => asyncLocalStorage.getStore();
|