@autofleet/shtinker 0.0.1 → 0.0.2
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/dist/index.d.ts +4 -3
- package/dist/index.js +29 -23
- package/package.json +1 -1
- package/src/index.ts +29 -22
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { AuditLoggerOptions } from './types';
|
|
1
|
+
import { AuditLoggerOptions, AuditLogContext } from './types';
|
|
2
2
|
declare class AuditLogger {
|
|
3
3
|
private rabbit;
|
|
4
4
|
private sequelize;
|
|
5
5
|
constructor(options: AuditLoggerOptions);
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
addModelHooks(): void;
|
|
7
|
+
sendAuditLog(context: AuditLogContext): Promise<void>;
|
|
8
8
|
}
|
|
9
|
+
export declare const setAuditContext: (entityType: string, entityId: string, action: string) => (req: any, res: any, next: any) => Promise<any>;
|
|
9
10
|
export default AuditLogger;
|
package/dist/index.js
CHANGED
|
@@ -9,41 +9,25 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.setAuditContext = void 0;
|
|
12
13
|
const zehut_1 = require("@autofleet/zehut");
|
|
13
14
|
const const_1 = require("./const");
|
|
14
|
-
const
|
|
15
|
+
const getAuditContext = () => {
|
|
15
16
|
var _a;
|
|
16
17
|
const currentTrace = (0, zehut_1.getCurrentPayload)();
|
|
17
|
-
const
|
|
18
|
-
return
|
|
18
|
+
const auditContext = (_a = currentTrace === null || currentTrace === void 0 ? void 0 : currentTrace.context) === null || _a === void 0 ? void 0 : _a.get(const_1.AUDIT_LOG_CONTEXT_KEY);
|
|
19
|
+
return auditContext;
|
|
19
20
|
};
|
|
20
21
|
class AuditLogger {
|
|
21
22
|
constructor(options) {
|
|
22
|
-
this.setAuditingContext = (entityType, entityId, action) => (req, res, next) => __awaiter(this, void 0, void 0, function* () {
|
|
23
|
-
const currentTrace = (0, zehut_1.getCurrentPayload)();
|
|
24
|
-
if (currentTrace) {
|
|
25
|
-
const user = currentTrace.context.get('userObject');
|
|
26
|
-
const auditLogContext = {
|
|
27
|
-
entityType,
|
|
28
|
-
entityId,
|
|
29
|
-
action,
|
|
30
|
-
endpoint: req.url,
|
|
31
|
-
method: req.method,
|
|
32
|
-
performedBy: user.id,
|
|
33
|
-
};
|
|
34
|
-
currentTrace.context.set(const_1.AUDIT_LOG_CONTEXT_KEY, auditLogContext);
|
|
35
|
-
yield this.rabbit.sendToQueue(const_1.AUDIT_LOG_CONTEXT_QUEUE, auditLogContext);
|
|
36
|
-
next();
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
23
|
this.rabbit = options.rabbit;
|
|
40
24
|
this.sequelize = options.sequelize;
|
|
41
25
|
}
|
|
42
|
-
|
|
26
|
+
addModelHooks() {
|
|
43
27
|
Object.entries(this.sequelize.models).forEach(([modelName, modelType]) => {
|
|
44
28
|
modelType.addHook('afterSave', (instance) => __awaiter(this, void 0, void 0, function* () {
|
|
45
|
-
const
|
|
46
|
-
if (
|
|
29
|
+
const auditContext = getAuditContext();
|
|
30
|
+
if (auditContext) {
|
|
47
31
|
const changedProperties = instance.changed();
|
|
48
32
|
const payload = {
|
|
49
33
|
entityType: modelName,
|
|
@@ -59,5 +43,27 @@ class AuditLogger {
|
|
|
59
43
|
}));
|
|
60
44
|
});
|
|
61
45
|
}
|
|
46
|
+
sendAuditLog(context) {
|
|
47
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
48
|
+
yield this.rabbit.sendToQueue(const_1.AUDIT_LOG_CONTEXT_QUEUE, context);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
62
51
|
}
|
|
52
|
+
const setAuditContext = (entityType, entityId, action) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
53
|
+
const currentTrace = (0, zehut_1.getCurrentPayload)();
|
|
54
|
+
if (currentTrace) {
|
|
55
|
+
const user = currentTrace.context.get('userObject');
|
|
56
|
+
const auditLogContext = {
|
|
57
|
+
entityType,
|
|
58
|
+
entityId,
|
|
59
|
+
action,
|
|
60
|
+
endpoint: req.url,
|
|
61
|
+
method: req.method,
|
|
62
|
+
performedBy: user.id,
|
|
63
|
+
};
|
|
64
|
+
currentTrace.context.set(const_1.AUDIT_LOG_CONTEXT_KEY, auditLogContext);
|
|
65
|
+
}
|
|
66
|
+
return next();
|
|
67
|
+
});
|
|
68
|
+
exports.setAuditContext = setAuditContext;
|
|
63
69
|
exports.default = AuditLogger;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -2,10 +2,10 @@ import { getCurrentPayload as getCurrentTrace } from '@autofleet/zehut';
|
|
|
2
2
|
import { AuditLogPayload, AuditLoggerOptions, AuditLogContext } from './types';
|
|
3
3
|
import { AUDIT_LOG_CONTEXT_QUEUE, AUDIT_LOG_ROWS_QUEUE, AUDIT_LOG_CONTEXT_KEY } from './const';
|
|
4
4
|
|
|
5
|
-
const
|
|
5
|
+
const getAuditContext = () => {
|
|
6
6
|
const currentTrace = getCurrentTrace();
|
|
7
|
-
const
|
|
8
|
-
return
|
|
7
|
+
const auditContext = currentTrace?.context?.get(AUDIT_LOG_CONTEXT_KEY);
|
|
8
|
+
return auditContext;
|
|
9
9
|
};
|
|
10
10
|
|
|
11
11
|
class AuditLogger {
|
|
@@ -18,11 +18,11 @@ class AuditLogger {
|
|
|
18
18
|
this.sequelize = options.sequelize;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
addModelHooks(): void {
|
|
22
22
|
Object.entries(this.sequelize.models).forEach(([modelName, modelType]) => {
|
|
23
23
|
modelType.addHook('afterSave', async (instance: any) => {
|
|
24
|
-
const
|
|
25
|
-
if (
|
|
24
|
+
const auditContext = getAuditContext();
|
|
25
|
+
if (auditContext) {
|
|
26
26
|
const changedProperties = instance.changed();
|
|
27
27
|
const payload: AuditLogPayload = {
|
|
28
28
|
entityType: modelName,
|
|
@@ -39,23 +39,30 @@ class AuditLogger {
|
|
|
39
39
|
});
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (currentTrace) {
|
|
45
|
-
const user = currentTrace.context.get('userObject');
|
|
46
|
-
const auditLogContext: AuditLogContext = {
|
|
47
|
-
entityType,
|
|
48
|
-
entityId,
|
|
49
|
-
action,
|
|
50
|
-
endpoint: req.url,
|
|
51
|
-
method: req.method,
|
|
52
|
-
performedBy: user.id,
|
|
53
|
-
};
|
|
54
|
-
currentTrace.context.set(AUDIT_LOG_CONTEXT_KEY, auditLogContext);
|
|
55
|
-
await this.rabbit.sendToQueue(AUDIT_LOG_CONTEXT_QUEUE, auditLogContext);
|
|
56
|
-
next();
|
|
57
|
-
}
|
|
42
|
+
async sendAuditLog(context: AuditLogContext): Promise<void> {
|
|
43
|
+
await this.rabbit.sendToQueue(AUDIT_LOG_CONTEXT_QUEUE, context);
|
|
58
44
|
}
|
|
59
45
|
}
|
|
60
46
|
|
|
47
|
+
export const setAuditContext = (
|
|
48
|
+
entityType: string,
|
|
49
|
+
entityId: string,
|
|
50
|
+
action: string,
|
|
51
|
+
) => async (req: any, res: any, next: any): Promise<any> => {
|
|
52
|
+
const currentTrace = getCurrentTrace();
|
|
53
|
+
if (currentTrace) {
|
|
54
|
+
const user = currentTrace.context.get('userObject');
|
|
55
|
+
const auditLogContext: AuditLogContext = {
|
|
56
|
+
entityType,
|
|
57
|
+
entityId,
|
|
58
|
+
action,
|
|
59
|
+
endpoint: req.url,
|
|
60
|
+
method: req.method,
|
|
61
|
+
performedBy: user.id,
|
|
62
|
+
};
|
|
63
|
+
currentTrace.context.set(AUDIT_LOG_CONTEXT_KEY, auditLogContext);
|
|
64
|
+
}
|
|
65
|
+
return next();
|
|
66
|
+
};
|
|
67
|
+
|
|
61
68
|
export default AuditLogger;
|