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