@dynamatix/cat-shared 0.0.11 → 0.0.13
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
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
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
|
|
@@ -10,14 +11,16 @@ function applyAuditMiddleware(schema, collectionName) {
|
|
|
10
11
|
if (!auditConfig) return next();
|
|
11
12
|
|
|
12
13
|
const logs = [];
|
|
14
|
+
const context = getContext();
|
|
15
|
+
const userId = context?.userId || 'anonymous';
|
|
13
16
|
|
|
14
17
|
if (isNewDoc && auditConfig.trackCreation) {
|
|
15
18
|
logs.push({
|
|
16
19
|
name: `${collectionName} created`,
|
|
17
|
-
|
|
20
|
+
recordId: doc._id,
|
|
18
21
|
oldValue: null,
|
|
19
22
|
newValue: 'Document created',
|
|
20
|
-
createdBy:
|
|
23
|
+
createdBy: userId
|
|
21
24
|
});
|
|
22
25
|
}
|
|
23
26
|
|
|
@@ -36,6 +39,8 @@ function applyAuditMiddleware(schema, collectionName) {
|
|
|
36
39
|
if (!original) return next();
|
|
37
40
|
|
|
38
41
|
const logs = [];
|
|
42
|
+
const context = getContext();
|
|
43
|
+
const userId = context?.userId || 'anonymous';
|
|
39
44
|
|
|
40
45
|
for (const field of auditConfig.fields) {
|
|
41
46
|
if (update?.$set?.hasOwnProperty(field) || update?.hasOwnProperty(field)) {
|
|
@@ -45,10 +50,10 @@ function applyAuditMiddleware(schema, collectionName) {
|
|
|
45
50
|
if (oldValue !== newValue) {
|
|
46
51
|
logs.push({
|
|
47
52
|
name: `${collectionName}.${field}`,
|
|
48
|
-
|
|
53
|
+
recordId: original._id,
|
|
49
54
|
oldValue,
|
|
50
55
|
newValue,
|
|
51
|
-
createdBy:
|
|
56
|
+
createdBy: userId
|
|
52
57
|
});
|
|
53
58
|
}
|
|
54
59
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dynamatix/cat-shared",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.13",
|
|
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();
|