@autofleet/shtinker 1.1.5-beta.9 → 1.1.6-beta-1
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 +15 -10
- package/dist/index.js +6 -3
- package/dist/logger.d.ts +1 -1
- package/dist/types.d.ts +5 -0
- package/dist/types.js +6 -1
- package/package.json +5 -4
- package/src/audit-logger.ts +17 -10
- package/src/index.ts +6 -4
- package/src/types.ts +6 -0
package/dist/audit-logger.js
CHANGED
|
@@ -18,17 +18,27 @@ const logger_1 = __importDefault(require("./logger"));
|
|
|
18
18
|
const getAuditContext = () => {
|
|
19
19
|
var _a;
|
|
20
20
|
const currentTrace = (0, zehut_1.getCurrentPayload)();
|
|
21
|
-
logger_1.default.info('MATAN getAuditContext currentTrace: ', { currentTrace });
|
|
22
21
|
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);
|
|
23
22
|
return auditContext;
|
|
24
23
|
};
|
|
24
|
+
const isEmpty = (field) => {
|
|
25
|
+
if ([null, undefined].includes(field)) {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
if (Array.isArray(field)) {
|
|
29
|
+
return field.length === 0;
|
|
30
|
+
}
|
|
31
|
+
if (typeof field === 'object' && !(field instanceof Date)) {
|
|
32
|
+
return Object.keys(field).length === 0;
|
|
33
|
+
}
|
|
34
|
+
return false;
|
|
35
|
+
};
|
|
36
|
+
const filterOutEmptyFields = (fields, instance) => fields.filter((field) => !isEmpty(instance.get(field)) || !isEmpty(instance.previous(field)));
|
|
25
37
|
const getChangedFields = (instance, options) => {
|
|
26
38
|
// When bulk updating in sequelize using the "returning" option, the instance.changed() stops working.
|
|
27
39
|
// It's a known issut with sequelize.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
return instance.changed();
|
|
40
|
+
const changedProperties = options.returning ? options.fields : instance.changed();
|
|
41
|
+
return filterOutEmptyFields(changedProperties, instance);
|
|
32
42
|
};
|
|
33
43
|
class AuditLogger {
|
|
34
44
|
constructor(options) {
|
|
@@ -41,10 +51,8 @@ class AuditLogger {
|
|
|
41
51
|
modelType.addHook('afterSave', (instance, options) => __awaiter(this, void 0, void 0, function* () {
|
|
42
52
|
try {
|
|
43
53
|
const auditContext = getAuditContext();
|
|
44
|
-
logger_1.default.info('MATAN auditContext: ', { auditContext });
|
|
45
54
|
if (auditContext) {
|
|
46
55
|
const changedProperties = getChangedFields(instance, options);
|
|
47
|
-
logger_1.default.info('MATAN changedProperties from shtinker:', { changedProperties });
|
|
48
56
|
const payload = {
|
|
49
57
|
entityType: modelName,
|
|
50
58
|
entityId: instance.id,
|
|
@@ -54,8 +62,6 @@ class AuditLogger {
|
|
|
54
62
|
newValue: instance.get(property),
|
|
55
63
|
})),
|
|
56
64
|
};
|
|
57
|
-
const entityId = instance.id;
|
|
58
|
-
logger_1.default.info('MATAN entityId from shtinker:', { entityId });
|
|
59
65
|
this.sendAuditLogRows(payload);
|
|
60
66
|
}
|
|
61
67
|
}
|
|
@@ -78,7 +84,6 @@ class AuditLogger {
|
|
|
78
84
|
sendAuditLogRows(payload) {
|
|
79
85
|
return __awaiter(this, void 0, void 0, function* () {
|
|
80
86
|
try {
|
|
81
|
-
logger_1.default.info('MATAN sendAuditLogRows from shtinker: ', { payload });
|
|
82
87
|
yield this.rabbit.sendToQueue(const_1.AUDIT_LOG_ROWS_QUEUE, payload);
|
|
83
88
|
}
|
|
84
89
|
catch (err) {
|
package/dist/index.js
CHANGED
|
@@ -29,6 +29,7 @@ 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"));
|
|
@@ -42,15 +43,18 @@ exports.enableAuditing = enableAuditing;
|
|
|
42
43
|
const setAuditContext = (entityType, action) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
43
44
|
try {
|
|
44
45
|
const currentTrace = (0, zehut_1.getCurrentPayload)();
|
|
45
|
-
logger_1.default.info('currentTrace: ', { currentTrace });
|
|
46
46
|
if (currentTrace && currentTrace.context && currentTrace.context.get) {
|
|
47
47
|
const user = currentTrace.context.get(const_1.USER_CONTEXT_KEY);
|
|
48
|
+
const { performedBy, actionOrigin } = (user === null || user === void 0 ? void 0 : user.id)
|
|
49
|
+
? { performedBy: user.id, actionOrigin: types_1.ActionOrigin.USER }
|
|
50
|
+
: { performedBy: req.headers['x-af-automation-id'], actionOrigin: types_1.ActionOrigin.AUTOMATION };
|
|
48
51
|
const auditLogContext = {
|
|
49
52
|
entityType,
|
|
50
53
|
action,
|
|
51
54
|
endpoint: req.url,
|
|
52
55
|
method: req.method,
|
|
53
|
-
performedBy
|
|
56
|
+
performedBy,
|
|
57
|
+
actionOrigin,
|
|
54
58
|
};
|
|
55
59
|
currentTrace.context.set(const_1.AUDIT_LOG_CONTEXT_KEY, auditLogContext);
|
|
56
60
|
yield auditLogger.sendAuditLogContext(auditLogContext);
|
|
@@ -65,7 +69,6 @@ exports.setAuditContext = setAuditContext;
|
|
|
65
69
|
const setRabbitAuditContext = (entityType, action) => (endpoint) => __awaiter(void 0, void 0, void 0, function* () {
|
|
66
70
|
var _a;
|
|
67
71
|
const currentTrace = (0, zehut_1.getCurrentPayload)();
|
|
68
|
-
logger_1.default.info('MATAN currentTrace: ', currentTrace);
|
|
69
72
|
if ((_a = currentTrace === null || currentTrace === void 0 ? void 0 : currentTrace.context) === null || _a === void 0 ? void 0 : _a.get) {
|
|
70
73
|
const user = currentTrace.context.get(const_1.USER_CONTEXT_KEY);
|
|
71
74
|
const auditLogContext = {
|
package/dist/logger.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default: import("
|
|
1
|
+
declare const _default: import("@autofleet/logger").LoggerInstanceManager;
|
|
2
2
|
export default _default;
|
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;
|
|
@@ -46,3 +47,7 @@ export declare enum EntityType {
|
|
|
46
47
|
DRIVER = "Driver",
|
|
47
48
|
PRICE_CALCULATION = "PriceCalculation"
|
|
48
49
|
}
|
|
50
|
+
export declare enum ActionOrigin {
|
|
51
|
+
USER = "user",
|
|
52
|
+
AUTOMATION = "automation"
|
|
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";
|
|
@@ -27,3 +27,8 @@ var EntityType;
|
|
|
27
27
|
EntityType["DRIVER"] = "Driver";
|
|
28
28
|
EntityType["PRICE_CALCULATION"] = "PriceCalculation";
|
|
29
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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/shtinker",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.6-beta-1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -13,10 +13,11 @@
|
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
15
|
"@autofleet/errors": "^1.2.2",
|
|
16
|
-
"@autofleet/logger": "^
|
|
16
|
+
"@autofleet/logger": "^4.0.6",
|
|
17
17
|
"@autofleet/network": "^1.4.7",
|
|
18
|
-
"@autofleet/
|
|
19
|
-
"@autofleet/
|
|
18
|
+
"@autofleet/netwrok": "^1.0.0",
|
|
19
|
+
"@autofleet/rabbit": "^3.2.18",
|
|
20
|
+
"@autofleet/zehut": "^3.0.10",
|
|
20
21
|
"sequelize-typescript": "^2.1.5"
|
|
21
22
|
},
|
|
22
23
|
"devDependencies": {
|
package/src/audit-logger.ts
CHANGED
|
@@ -8,18 +8,30 @@ import logger from './logger';
|
|
|
8
8
|
|
|
9
9
|
const getAuditContext = () => {
|
|
10
10
|
const currentTrace = getCurrentTrace();
|
|
11
|
-
logger.info('MATAN getAuditContext currentTrace: ', { currentTrace });
|
|
12
11
|
const auditContext = currentTrace?.context?.get(AUDIT_LOG_CONTEXT_KEY);
|
|
13
12
|
return auditContext;
|
|
14
13
|
};
|
|
15
14
|
|
|
15
|
+
const isEmpty = (field) => {
|
|
16
|
+
if ([null, undefined].includes(field)) {
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
if (Array.isArray(field)) {
|
|
20
|
+
return field.length === 0;
|
|
21
|
+
}
|
|
22
|
+
if (typeof field === 'object' && !(field instanceof Date)) {
|
|
23
|
+
return Object.keys(field).length === 0;
|
|
24
|
+
}
|
|
25
|
+
return false;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const filterOutEmptyFields = (fields, instance) => fields.filter((field) => !isEmpty(instance.get(field)) || !isEmpty(instance.previous(field)));
|
|
29
|
+
|
|
16
30
|
const getChangedFields = (instance, options) => {
|
|
17
31
|
// When bulk updating in sequelize using the "returning" option, the instance.changed() stops working.
|
|
18
32
|
// It's a known issut with sequelize.
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
return instance.changed();
|
|
33
|
+
const changedProperties = options.returning ? options.fields : instance.changed();
|
|
34
|
+
return filterOutEmptyFields(changedProperties, instance);
|
|
23
35
|
};
|
|
24
36
|
|
|
25
37
|
class AuditLogger {
|
|
@@ -40,10 +52,8 @@ class AuditLogger {
|
|
|
40
52
|
modelType.addHook('afterSave', async (instance: any, options: any) => {
|
|
41
53
|
try {
|
|
42
54
|
const auditContext = getAuditContext();
|
|
43
|
-
logger.info('MATAN auditContext: ', { auditContext });
|
|
44
55
|
if (auditContext) {
|
|
45
56
|
const changedProperties = getChangedFields(instance, options);
|
|
46
|
-
logger.info('MATAN changedProperties from shtinker:', { changedProperties });
|
|
47
57
|
const payload: AuditLogPayload = {
|
|
48
58
|
entityType: modelName,
|
|
49
59
|
entityId: instance.id,
|
|
@@ -53,8 +63,6 @@ class AuditLogger {
|
|
|
53
63
|
newValue: instance.get(property),
|
|
54
64
|
})),
|
|
55
65
|
};
|
|
56
|
-
const entityId = instance.id;
|
|
57
|
-
logger.info('MATAN entityId from shtinker:', { entityId });
|
|
58
66
|
this.sendAuditLogRows(payload);
|
|
59
67
|
}
|
|
60
68
|
} catch (error) {
|
|
@@ -74,7 +82,6 @@ class AuditLogger {
|
|
|
74
82
|
|
|
75
83
|
async sendAuditLogRows(payload: AuditLogPayload): Promise<void> {
|
|
76
84
|
try {
|
|
77
|
-
logger.info('MATAN sendAuditLogRows from shtinker: ', { payload });
|
|
78
85
|
await this.rabbit.sendToQueue(AUDIT_LOG_ROWS_QUEUE, payload);
|
|
79
86
|
} catch (err) {
|
|
80
87
|
logger.error('Failed to send audit log rows', err);
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { getCurrentPayload as getCurrentTrace } from '@autofleet/zehut';
|
|
2
2
|
import AuditLogger from './audit-logger';
|
|
3
|
-
import { AuditLogContext, AuditLoggerOptions } from './types';
|
|
3
|
+
import { ActionOrigin, AuditLogContext, AuditLoggerOptions } from './types';
|
|
4
4
|
import { AUDIT_LOG_CONTEXT_KEY, USER_CONTEXT_KEY } from './const';
|
|
5
5
|
import addAuditApi from './audit-api';
|
|
6
6
|
import logger from './logger';
|
|
@@ -19,15 +19,18 @@ export const setAuditContext = (
|
|
|
19
19
|
) => async (req: any, res: any, next: any): Promise<any> => {
|
|
20
20
|
try {
|
|
21
21
|
const currentTrace = getCurrentTrace();
|
|
22
|
-
logger.info('currentTrace: ', { currentTrace });
|
|
23
22
|
if (currentTrace && currentTrace.context && currentTrace.context.get) {
|
|
24
23
|
const user = currentTrace.context.get(USER_CONTEXT_KEY);
|
|
24
|
+
const { performedBy, actionOrigin } = user?.id
|
|
25
|
+
? { performedBy: user.id, actionOrigin: ActionOrigin.USER }
|
|
26
|
+
: { performedBy: req.headers['x-af-automation-id'], actionOrigin: ActionOrigin.AUTOMATION };
|
|
25
27
|
const auditLogContext: AuditLogContext = {
|
|
26
28
|
entityType,
|
|
27
29
|
action,
|
|
28
30
|
endpoint: req.url,
|
|
29
31
|
method: req.method,
|
|
30
|
-
performedBy
|
|
32
|
+
performedBy,
|
|
33
|
+
actionOrigin,
|
|
31
34
|
};
|
|
32
35
|
currentTrace.context.set(AUDIT_LOG_CONTEXT_KEY, auditLogContext);
|
|
33
36
|
await auditLogger.sendAuditLogContext(auditLogContext);
|
|
@@ -43,7 +46,6 @@ export const setRabbitAuditContext = (
|
|
|
43
46
|
action: string,
|
|
44
47
|
) => async (endpoint: string): Promise<any> => {
|
|
45
48
|
const currentTrace = getCurrentTrace();
|
|
46
|
-
logger.info('MATAN currentTrace: ', currentTrace);
|
|
47
49
|
if (currentTrace?.context?.get) {
|
|
48
50
|
const user = currentTrace.context.get(USER_CONTEXT_KEY);
|
|
49
51
|
const auditLogContext: 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 {
|
|
@@ -53,3 +54,8 @@ export enum EntityType {
|
|
|
53
54
|
DRIVER = 'Driver',
|
|
54
55
|
PRICE_CALCULATION = 'PriceCalculation',
|
|
55
56
|
}
|
|
57
|
+
|
|
58
|
+
export enum ActionOrigin {
|
|
59
|
+
USER = 'user',
|
|
60
|
+
AUTOMATION = 'automation',
|
|
61
|
+
}
|