@autofleet/shtinker 1.2.0 → 1.2.1-beta
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/audit-logger.js +14 -37
- package/dist/const.d.ts +0 -1
- package/dist/const.js +1 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +13 -21
- package/dist/types.d.ts +2 -3
- package/dist/types.js +6 -2
- package/package.json +1 -1
- package/src/index.ts +1 -0
package/dist/audit-logger.js
CHANGED
|
@@ -13,10 +13,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
const zehut_1 = require("@autofleet/zehut");
|
|
16
|
-
const lodash_1 = require("lodash");
|
|
17
16
|
const const_1 = require("./const");
|
|
18
17
|
const logger_1 = __importDefault(require("./logger"));
|
|
19
|
-
const CUSTOM_FIELDS_PROPERTY = 'customFields';
|
|
20
18
|
const getAuditContext = () => {
|
|
21
19
|
var _a;
|
|
22
20
|
const currentTrace = (0, zehut_1.getCurrentPayload)();
|
|
@@ -36,41 +34,17 @@ const isEmpty = (field) => {
|
|
|
36
34
|
return false;
|
|
37
35
|
};
|
|
38
36
|
const filterOutEmptyFields = (fields, instance) => fields.filter((field) => !isEmpty(instance.get(field)) || !isEmpty(instance.previous(field)));
|
|
39
|
-
const
|
|
37
|
+
const getChangedFields = (instance, options) => {
|
|
40
38
|
// When bulk updating in sequelize using the "returning" option, the instance.changed() stops working.
|
|
41
|
-
// It's a known
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
const filteredChangedFields = filterOutEmptyFields(changedFields.filter((field) => field !== CUSTOM_FIELDS_PROPERTY), instance);
|
|
45
|
-
return filteredChangedFields.map((property) => ({
|
|
46
|
-
property,
|
|
47
|
-
previousValue: instance.previous(property),
|
|
48
|
-
newValue: instance.get(property),
|
|
49
|
-
}));
|
|
50
|
-
};
|
|
51
|
-
const getChangedCustomFieldsRows = (instance) => {
|
|
52
|
-
const customFieldsChanged = instance.changed().includes(CUSTOM_FIELDS_PROPERTY);
|
|
53
|
-
if (!customFieldsChanged) {
|
|
54
|
-
return [];
|
|
55
|
-
}
|
|
56
|
-
const customFields = instance.get(CUSTOM_FIELDS_PROPERTY);
|
|
57
|
-
const previousCustomFields = instance.previous(CUSTOM_FIELDS_PROPERTY);
|
|
58
|
-
const changedCustomFields = Object.keys(customFields).filter((field) => {
|
|
59
|
-
const newValue = customFields === null || customFields === void 0 ? void 0 : customFields[field];
|
|
60
|
-
const oldValue = previousCustomFields === null || previousCustomFields === void 0 ? void 0 : previousCustomFields[field];
|
|
61
|
-
return (!isEmpty(newValue) || !isEmpty(oldValue)) && !(0, lodash_1.isEqual)(newValue, oldValue);
|
|
62
|
-
});
|
|
63
|
-
return changedCustomFields.map((changedCustomField) => ({
|
|
64
|
-
property: `customFields.${changedCustomField}`,
|
|
65
|
-
previousValue: previousCustomFields === null || previousCustomFields === void 0 ? void 0 : previousCustomFields[changedCustomField],
|
|
66
|
-
newValue: customFields === null || customFields === void 0 ? void 0 : customFields[changedCustomField],
|
|
67
|
-
}));
|
|
39
|
+
// It's a known issut with sequelize.
|
|
40
|
+
const changedProperties = options.returning ? options.fields : instance.changed();
|
|
41
|
+
return filterOutEmptyFields(changedProperties, instance);
|
|
68
42
|
};
|
|
69
43
|
class AuditLogger {
|
|
70
44
|
constructor(options) {
|
|
71
45
|
this.rabbit = options.rabbit;
|
|
72
46
|
this.sequelize = options.sequelize;
|
|
73
|
-
this.logger = options.logger
|
|
47
|
+
this.logger = options.logger;
|
|
74
48
|
}
|
|
75
49
|
registerHooks() {
|
|
76
50
|
Object.entries(this.sequelize.models).forEach(([modelName, modelType]) => {
|
|
@@ -78,18 +52,21 @@ class AuditLogger {
|
|
|
78
52
|
try {
|
|
79
53
|
const auditContext = getAuditContext();
|
|
80
54
|
if (auditContext) {
|
|
81
|
-
const
|
|
82
|
-
const changedCustomFieldsRows = getChangedCustomFieldsRows(instance);
|
|
55
|
+
const changedProperties = getChangedFields(instance, options);
|
|
83
56
|
const payload = {
|
|
84
57
|
entityType: modelName,
|
|
85
58
|
entityId: instance.id,
|
|
86
|
-
rows:
|
|
59
|
+
rows: changedProperties.map((property) => ({
|
|
60
|
+
property,
|
|
61
|
+
previousValue: instance.previous(property),
|
|
62
|
+
newValue: instance.get(property),
|
|
63
|
+
})),
|
|
87
64
|
};
|
|
88
65
|
this.sendAuditLogRows(payload);
|
|
89
66
|
}
|
|
90
67
|
}
|
|
91
68
|
catch (error) {
|
|
92
|
-
|
|
69
|
+
logger_1.default.error('Failed to send audit log rows', error);
|
|
93
70
|
}
|
|
94
71
|
}));
|
|
95
72
|
});
|
|
@@ -100,7 +77,7 @@ class AuditLogger {
|
|
|
100
77
|
yield this.rabbit.sendToQueue(const_1.AUDIT_LOG_CONTEXT_QUEUE, context);
|
|
101
78
|
}
|
|
102
79
|
catch (err) {
|
|
103
|
-
|
|
80
|
+
logger_1.default.error('Failed to send audit log context', err);
|
|
104
81
|
}
|
|
105
82
|
});
|
|
106
83
|
}
|
|
@@ -110,7 +87,7 @@ class AuditLogger {
|
|
|
110
87
|
yield this.rabbit.sendToQueue(const_1.AUDIT_LOG_ROWS_QUEUE, payload);
|
|
111
88
|
}
|
|
112
89
|
catch (err) {
|
|
113
|
-
|
|
90
|
+
logger_1.default.error('Failed to send audit log rows', err);
|
|
114
91
|
}
|
|
115
92
|
});
|
|
116
93
|
}
|
package/dist/const.d.ts
CHANGED
|
@@ -2,4 +2,3 @@ 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,8 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
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
|
@@ -3,8 +3,8 @@ 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
5
|
export declare const setRabbitAuditContext: (entityType: string, action: string) => (endpoint: string, { userId, automationId }: {
|
|
6
|
-
userId:
|
|
7
|
-
automationId:
|
|
6
|
+
userId: any;
|
|
7
|
+
automationId: any;
|
|
8
8
|
}) => Promise<any>;
|
|
9
9
|
export * from './types';
|
|
10
10
|
export * from './const';
|
package/dist/index.js
CHANGED
|
@@ -29,30 +29,25 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
29
29
|
exports.setRabbitAuditContext = exports.setAuditContext = exports.enableAuditing = void 0;
|
|
30
30
|
const zehut_1 = require("@autofleet/zehut");
|
|
31
31
|
const audit_logger_1 = __importDefault(require("./audit-logger"));
|
|
32
|
+
const types_1 = require("./types");
|
|
32
33
|
const const_1 = require("./const");
|
|
33
34
|
const audit_api_1 = __importDefault(require("./audit-api"));
|
|
34
35
|
const logger_1 = __importDefault(require("./logger"));
|
|
35
36
|
let auditLogger;
|
|
36
37
|
const enableAuditing = (options) => {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
auditLogger.registerHooks();
|
|
41
|
-
}
|
|
38
|
+
auditLogger = new audit_logger_1.default(options);
|
|
39
|
+
(0, audit_api_1.default)(options);
|
|
40
|
+
auditLogger.registerHooks();
|
|
42
41
|
};
|
|
43
42
|
exports.enableAuditing = enableAuditing;
|
|
44
43
|
const setAuditContext = (entityType, action) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
45
|
-
var _a;
|
|
46
44
|
try {
|
|
47
|
-
if (process.env.DISABLE_AUDIT_LOGS === 'true') {
|
|
48
|
-
return next();
|
|
49
|
-
}
|
|
50
45
|
const currentTrace = (0, zehut_1.getCurrentPayload)();
|
|
51
46
|
if (currentTrace && currentTrace.context && currentTrace.context.get) {
|
|
52
47
|
const user = currentTrace.context.get(const_1.USER_CONTEXT_KEY);
|
|
53
48
|
const { performedBy, actionOrigin } = (user === null || user === void 0 ? void 0 : user.id)
|
|
54
|
-
? { performedBy: user.id, actionOrigin:
|
|
55
|
-
: { performedBy:
|
|
49
|
+
? { performedBy: user.id, actionOrigin: types_1.ActionOrigin.USER }
|
|
50
|
+
: { performedBy: req.headers['x-af-automation-id'], actionOrigin: types_1.ActionOrigin.AUTOMATION };
|
|
56
51
|
const auditLogContext = {
|
|
57
52
|
entityType,
|
|
58
53
|
action,
|
|
@@ -72,22 +67,19 @@ const setAuditContext = (entityType, action) => (req, res, next) => __awaiter(vo
|
|
|
72
67
|
});
|
|
73
68
|
exports.setAuditContext = setAuditContext;
|
|
74
69
|
const setRabbitAuditContext = (entityType, action) => (endpoint, { userId, automationId }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
75
|
-
var
|
|
76
|
-
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
70
|
+
var _a;
|
|
71
|
+
console.log({ automationId });
|
|
79
72
|
const currentTrace = (0, zehut_1.getCurrentPayload)();
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
: { performedBy: automationId !== null && automationId !== void 0 ? automationId : null, actionOrigin: automationId ? "automation" /* ActionOrigin.AUTOMATION */ : null };
|
|
73
|
+
const user = currentTrace.context.get(const_1.USER_CONTEXT_KEY);
|
|
74
|
+
const contextAutomationId = currentTrace.context.get('x-af-automation-id');
|
|
75
|
+
if ((_a = currentTrace === null || currentTrace === void 0 ? void 0 : currentTrace.context) === null || _a === void 0 ? void 0 : _a.get) {
|
|
84
76
|
const auditLogContext = {
|
|
85
77
|
entityType,
|
|
86
78
|
action,
|
|
87
79
|
endpoint,
|
|
88
80
|
method: 'rabbit',
|
|
89
|
-
performedBy,
|
|
90
|
-
actionOrigin,
|
|
81
|
+
performedBy: (user === null || user === void 0 ? void 0 : user.id) || contextAutomationId,
|
|
82
|
+
actionOrigin: userId ? types_1.ActionOrigin.USER : types_1.ActionOrigin.AUTOMATION,
|
|
91
83
|
};
|
|
92
84
|
currentTrace.context.set(const_1.AUDIT_LOG_CONTEXT_KEY, auditLogContext);
|
|
93
85
|
yield auditLogger.sendAuditLogContext(auditLogContext);
|
package/dist/types.d.ts
CHANGED
|
@@ -39,8 +39,7 @@ export declare enum Action {
|
|
|
39
39
|
BULK_UPSERT = "bulk-upsert",
|
|
40
40
|
UPSERT = "upsert",
|
|
41
41
|
JOIN = "join",
|
|
42
|
-
MOVE = "move"
|
|
43
|
-
REOPTIMIZATION = "reoptimization"
|
|
42
|
+
MOVE = "move"
|
|
44
43
|
}
|
|
45
44
|
export declare enum EntityType {
|
|
46
45
|
RIDE = "Ride",
|
|
@@ -48,7 +47,7 @@ export declare enum EntityType {
|
|
|
48
47
|
DRIVER = "Driver",
|
|
49
48
|
PRICE_CALCULATION = "PriceCalculation"
|
|
50
49
|
}
|
|
51
|
-
export declare
|
|
50
|
+
export declare enum ActionOrigin {
|
|
52
51
|
USER = "user",
|
|
53
52
|
AUTOMATION = "automation"
|
|
54
53
|
}
|
package/dist/types.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.EntityType = exports.Action = void 0;
|
|
3
|
+
exports.ActionOrigin = exports.EntityType = exports.Action = void 0;
|
|
4
4
|
var Action;
|
|
5
5
|
(function (Action) {
|
|
6
6
|
Action["CREATE"] = "create";
|
|
@@ -19,7 +19,6 @@ var Action;
|
|
|
19
19
|
Action["UPSERT"] = "upsert";
|
|
20
20
|
Action["JOIN"] = "join";
|
|
21
21
|
Action["MOVE"] = "move";
|
|
22
|
-
Action["REOPTIMIZATION"] = "reoptimization";
|
|
23
22
|
})(Action = exports.Action || (exports.Action = {}));
|
|
24
23
|
var EntityType;
|
|
25
24
|
(function (EntityType) {
|
|
@@ -28,3 +27,8 @@ var EntityType;
|
|
|
28
27
|
EntityType["DRIVER"] = "Driver";
|
|
29
28
|
EntityType["PRICE_CALCULATION"] = "PriceCalculation";
|
|
30
29
|
})(EntityType = exports.EntityType || (exports.EntityType = {}));
|
|
30
|
+
var ActionOrigin;
|
|
31
|
+
(function (ActionOrigin) {
|
|
32
|
+
ActionOrigin["USER"] = "user";
|
|
33
|
+
ActionOrigin["AUTOMATION"] = "automation";
|
|
34
|
+
})(ActionOrigin = exports.ActionOrigin || (exports.ActionOrigin = {}));
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -23,6 +23,7 @@ export const setAuditContext = (
|
|
|
23
23
|
if (process.env.DISABLE_AUDIT_LOGS === 'true') {
|
|
24
24
|
return next();
|
|
25
25
|
}
|
|
26
|
+
logger.info('shtinker automation context', { contextData: req.headers[AUTOMATION_ID_HEADER] });
|
|
26
27
|
const currentTrace = getCurrentTrace();
|
|
27
28
|
if (currentTrace && currentTrace.context && currentTrace.context.get) {
|
|
28
29
|
const user = currentTrace.context.get(USER_CONTEXT_KEY);
|