@autofleet/shtinker 1.2.0-beta.4 → 1.2.0
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/const.d.ts +1 -0
- package/dist/const.js +2 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +14 -6
- package/dist/types.d.ts +7 -1
- package/dist/types.js +1 -0
- package/package.json +3 -3
- package/src/const.ts +1 -0
- package/src/index.ts +13 -6
- package/src/types.ts +7 -0
package/dist/const.d.ts
CHANGED
|
@@ -2,3 +2,4 @@ export declare const AUDIT_LOG_CONTEXT_QUEUE = "audit-log-context";
|
|
|
2
2
|
export declare const AUDIT_LOG_ROWS_QUEUE = "audit-log-rows";
|
|
3
3
|
export declare const AUDIT_LOG_CONTEXT_KEY = "auditLogContext";
|
|
4
4
|
export declare const USER_CONTEXT_KEY = "userObject";
|
|
5
|
+
export declare const AUTOMATION_ID_HEADER = "x-af-automation-id";
|
package/dist/const.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.USER_CONTEXT_KEY = exports.AUDIT_LOG_CONTEXT_KEY = exports.AUDIT_LOG_ROWS_QUEUE = exports.AUDIT_LOG_CONTEXT_QUEUE = void 0;
|
|
3
|
+
exports.AUTOMATION_ID_HEADER = exports.USER_CONTEXT_KEY = exports.AUDIT_LOG_CONTEXT_KEY = exports.AUDIT_LOG_ROWS_QUEUE = exports.AUDIT_LOG_CONTEXT_QUEUE = void 0;
|
|
4
4
|
exports.AUDIT_LOG_CONTEXT_QUEUE = 'audit-log-context';
|
|
5
5
|
exports.AUDIT_LOG_ROWS_QUEUE = 'audit-log-rows';
|
|
6
6
|
exports.AUDIT_LOG_CONTEXT_KEY = 'auditLogContext';
|
|
7
7
|
exports.USER_CONTEXT_KEY = 'userObject';
|
|
8
|
+
exports.AUTOMATION_ID_HEADER = 'x-af-automation-id';
|
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,10 @@ import AuditLogger from './audit-logger';
|
|
|
2
2
|
import { AuditLoggerOptions } from './types';
|
|
3
3
|
export declare const enableAuditing: (options: AuditLoggerOptions) => void;
|
|
4
4
|
export declare const setAuditContext: (entityType: string, action: string) => (req: any, res: any, next: any) => Promise<any>;
|
|
5
|
-
export declare const setRabbitAuditContext: (entityType: string, action: string) => (endpoint: string
|
|
5
|
+
export declare const setRabbitAuditContext: (entityType: string, action: string) => (endpoint: string, { userId, automationId }: {
|
|
6
|
+
userId: string;
|
|
7
|
+
automationId: string;
|
|
8
|
+
}) => Promise<any>;
|
|
6
9
|
export * from './types';
|
|
7
10
|
export * from './const';
|
|
8
11
|
export default AuditLogger;
|
package/dist/index.js
CHANGED
|
@@ -42,6 +42,7 @@ const enableAuditing = (options) => {
|
|
|
42
42
|
};
|
|
43
43
|
exports.enableAuditing = enableAuditing;
|
|
44
44
|
const setAuditContext = (entityType, action) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
45
|
+
var _a;
|
|
45
46
|
try {
|
|
46
47
|
if (process.env.DISABLE_AUDIT_LOGS === 'true') {
|
|
47
48
|
return next();
|
|
@@ -49,12 +50,16 @@ const setAuditContext = (entityType, action) => (req, res, next) => __awaiter(vo
|
|
|
49
50
|
const currentTrace = (0, zehut_1.getCurrentPayload)();
|
|
50
51
|
if (currentTrace && currentTrace.context && currentTrace.context.get) {
|
|
51
52
|
const user = currentTrace.context.get(const_1.USER_CONTEXT_KEY);
|
|
53
|
+
const { performedBy, actionOrigin } = (user === null || user === void 0 ? void 0 : user.id)
|
|
54
|
+
? { performedBy: user.id, actionOrigin: "user" /* ActionOrigin.USER */ }
|
|
55
|
+
: { performedBy: (_a = req.headers[const_1.AUTOMATION_ID_HEADER]) !== null && _a !== void 0 ? _a : null, actionOrigin: req.headers[const_1.AUTOMATION_ID_HEADER] ? "automation" /* ActionOrigin.AUTOMATION */ : null };
|
|
52
56
|
const auditLogContext = {
|
|
53
57
|
entityType,
|
|
54
58
|
action,
|
|
55
59
|
endpoint: req.url,
|
|
56
60
|
method: req.method,
|
|
57
|
-
performedBy
|
|
61
|
+
performedBy,
|
|
62
|
+
actionOrigin,
|
|
58
63
|
};
|
|
59
64
|
currentTrace.context.set(const_1.AUDIT_LOG_CONTEXT_KEY, auditLogContext);
|
|
60
65
|
yield auditLogger.sendAuditLogContext(auditLogContext);
|
|
@@ -66,20 +71,23 @@ const setAuditContext = (entityType, action) => (req, res, next) => __awaiter(vo
|
|
|
66
71
|
return next();
|
|
67
72
|
});
|
|
68
73
|
exports.setAuditContext = setAuditContext;
|
|
69
|
-
const setRabbitAuditContext = (entityType, action) => (endpoint) => __awaiter(void 0, void 0, void 0, function* () {
|
|
70
|
-
var
|
|
74
|
+
const setRabbitAuditContext = (entityType, action) => (endpoint, { userId, automationId }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
75
|
+
var _b;
|
|
71
76
|
if (process.env.DISABLE_AUDIT_LOGS === 'true') {
|
|
72
77
|
return;
|
|
73
78
|
}
|
|
74
79
|
const currentTrace = (0, zehut_1.getCurrentPayload)();
|
|
75
|
-
if ((
|
|
76
|
-
const
|
|
80
|
+
if ((_b = currentTrace === null || currentTrace === void 0 ? void 0 : currentTrace.context) === null || _b === void 0 ? void 0 : _b.get) {
|
|
81
|
+
const { performedBy, actionOrigin } = userId
|
|
82
|
+
? { performedBy: userId, actionOrigin: "user" /* ActionOrigin.USER */ }
|
|
83
|
+
: { performedBy: automationId !== null && automationId !== void 0 ? automationId : null, actionOrigin: automationId ? "automation" /* ActionOrigin.AUTOMATION */ : null };
|
|
77
84
|
const auditLogContext = {
|
|
78
85
|
entityType,
|
|
79
86
|
action,
|
|
80
87
|
endpoint,
|
|
81
88
|
method: 'rabbit',
|
|
82
|
-
performedBy
|
|
89
|
+
performedBy,
|
|
90
|
+
actionOrigin,
|
|
83
91
|
};
|
|
84
92
|
currentTrace.context.set(const_1.AUDIT_LOG_CONTEXT_KEY, auditLogContext);
|
|
85
93
|
yield auditLogger.sendAuditLogContext(auditLogContext);
|
package/dist/types.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ export interface AuditLogContext {
|
|
|
11
11
|
performedBy: string;
|
|
12
12
|
endpoint: string;
|
|
13
13
|
method: string;
|
|
14
|
+
actionOrigin?: string;
|
|
14
15
|
}
|
|
15
16
|
export interface AuditLogRow {
|
|
16
17
|
property: string;
|
|
@@ -38,7 +39,8 @@ export declare enum Action {
|
|
|
38
39
|
BULK_UPSERT = "bulk-upsert",
|
|
39
40
|
UPSERT = "upsert",
|
|
40
41
|
JOIN = "join",
|
|
41
|
-
MOVE = "move"
|
|
42
|
+
MOVE = "move",
|
|
43
|
+
REOPTIMIZATION = "reoptimization"
|
|
42
44
|
}
|
|
43
45
|
export declare enum EntityType {
|
|
44
46
|
RIDE = "Ride",
|
|
@@ -46,3 +48,7 @@ export declare enum EntityType {
|
|
|
46
48
|
DRIVER = "Driver",
|
|
47
49
|
PRICE_CALCULATION = "PriceCalculation"
|
|
48
50
|
}
|
|
51
|
+
export declare const enum ActionOrigin {
|
|
52
|
+
USER = "user",
|
|
53
|
+
AUTOMATION = "automation"
|
|
54
|
+
}
|
package/dist/types.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/shtinker",
|
|
3
|
-
"version": "1.2.0
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -15,12 +15,12 @@
|
|
|
15
15
|
"@autofleet/errors": "^1.2.2",
|
|
16
16
|
"@autofleet/logger": "^4.0.6",
|
|
17
17
|
"@autofleet/network": "^1.4.7",
|
|
18
|
-
"@autofleet/rabbit": "^3.2.
|
|
18
|
+
"@autofleet/rabbit": "^3.2.22",
|
|
19
19
|
"lodash": "^4.17.21",
|
|
20
20
|
"sequelize-typescript": "^2.1.5"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"@autofleet/zehut": "^3.
|
|
23
|
+
"@autofleet/zehut": "^3.0.10",
|
|
24
24
|
"@types/express": "^4.17.17",
|
|
25
25
|
"@types/jest": "^27.0.9",
|
|
26
26
|
"@types/node": "^18.11.19",
|
package/src/const.ts
CHANGED
|
@@ -2,3 +2,4 @@ export const AUDIT_LOG_CONTEXT_QUEUE = 'audit-log-context';
|
|
|
2
2
|
export const AUDIT_LOG_ROWS_QUEUE = 'audit-log-rows';
|
|
3
3
|
export const AUDIT_LOG_CONTEXT_KEY = 'auditLogContext';
|
|
4
4
|
export const USER_CONTEXT_KEY = 'userObject';
|
|
5
|
+
export const AUTOMATION_ID_HEADER = 'x-af-automation-id';
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { getCurrentPayload as getCurrentTrace } from '@autofleet/zehut';
|
|
2
2
|
import AuditLogger from './audit-logger';
|
|
3
|
-
import { AuditLogContext, AuditLoggerOptions } from './types';
|
|
4
|
-
import { AUDIT_LOG_CONTEXT_KEY, USER_CONTEXT_KEY } from './const';
|
|
3
|
+
import { ActionOrigin, AuditLogContext, AuditLoggerOptions } from './types';
|
|
4
|
+
import { AUDIT_LOG_CONTEXT_KEY, AUTOMATION_ID_HEADER, USER_CONTEXT_KEY } from './const';
|
|
5
5
|
import addAuditApi from './audit-api';
|
|
6
6
|
import logger from './logger';
|
|
7
7
|
|
|
@@ -26,12 +26,16 @@ export const setAuditContext = (
|
|
|
26
26
|
const currentTrace = getCurrentTrace();
|
|
27
27
|
if (currentTrace && currentTrace.context && currentTrace.context.get) {
|
|
28
28
|
const user = currentTrace.context.get(USER_CONTEXT_KEY);
|
|
29
|
+
const { performedBy, actionOrigin } = user?.id
|
|
30
|
+
? { performedBy: user.id, actionOrigin: ActionOrigin.USER }
|
|
31
|
+
: { performedBy: req.headers[AUTOMATION_ID_HEADER] ?? null, actionOrigin: req.headers[AUTOMATION_ID_HEADER] ? ActionOrigin.AUTOMATION : null };
|
|
29
32
|
const auditLogContext: AuditLogContext = {
|
|
30
33
|
entityType,
|
|
31
34
|
action,
|
|
32
35
|
endpoint: req.url,
|
|
33
36
|
method: req.method,
|
|
34
|
-
performedBy
|
|
37
|
+
performedBy,
|
|
38
|
+
actionOrigin,
|
|
35
39
|
};
|
|
36
40
|
currentTrace.context.set(AUDIT_LOG_CONTEXT_KEY, auditLogContext);
|
|
37
41
|
await auditLogger.sendAuditLogContext(auditLogContext);
|
|
@@ -45,19 +49,22 @@ export const setAuditContext = (
|
|
|
45
49
|
export const setRabbitAuditContext = (
|
|
46
50
|
entityType: string,
|
|
47
51
|
action: string,
|
|
48
|
-
) => async (endpoint: string): Promise<any> => {
|
|
52
|
+
) => async (endpoint: string, { userId, automationId }:{ userId:string; automationId:string; }): Promise<any> => {
|
|
49
53
|
if (process.env.DISABLE_AUDIT_LOGS === 'true') {
|
|
50
54
|
return;
|
|
51
55
|
}
|
|
52
56
|
const currentTrace = getCurrentTrace();
|
|
53
57
|
if (currentTrace?.context?.get) {
|
|
54
|
-
const
|
|
58
|
+
const { performedBy, actionOrigin } = userId
|
|
59
|
+
? { performedBy: userId, actionOrigin: ActionOrigin.USER }
|
|
60
|
+
: { performedBy: automationId ?? null, actionOrigin: automationId ? ActionOrigin.AUTOMATION : null };
|
|
55
61
|
const auditLogContext: AuditLogContext = {
|
|
56
62
|
entityType,
|
|
57
63
|
action,
|
|
58
64
|
endpoint,
|
|
59
65
|
method: 'rabbit',
|
|
60
|
-
performedBy
|
|
66
|
+
performedBy,
|
|
67
|
+
actionOrigin,
|
|
61
68
|
};
|
|
62
69
|
currentTrace.context.set(AUDIT_LOG_CONTEXT_KEY, auditLogContext);
|
|
63
70
|
await auditLogger.sendAuditLogContext(auditLogContext);
|
package/src/types.ts
CHANGED
|
@@ -14,6 +14,7 @@ export interface AuditLogContext {
|
|
|
14
14
|
performedBy: string;
|
|
15
15
|
endpoint: string;
|
|
16
16
|
method: string;
|
|
17
|
+
actionOrigin?: string;
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
export interface AuditLogRow {
|
|
@@ -45,6 +46,7 @@ export enum Action {
|
|
|
45
46
|
UPSERT = 'upsert',
|
|
46
47
|
JOIN = 'join',
|
|
47
48
|
MOVE = 'move',
|
|
49
|
+
REOPTIMIZATION = 'reoptimization',
|
|
48
50
|
}
|
|
49
51
|
|
|
50
52
|
export enum EntityType {
|
|
@@ -53,3 +55,8 @@ export enum EntityType {
|
|
|
53
55
|
DRIVER = 'Driver',
|
|
54
56
|
PRICE_CALCULATION = 'PriceCalculation',
|
|
55
57
|
}
|
|
58
|
+
|
|
59
|
+
export const enum ActionOrigin {
|
|
60
|
+
USER = 'user',
|
|
61
|
+
AUTOMATION = 'automation',
|
|
62
|
+
}
|