@autofleet/shtinker 1.2.3-beta.5 → 1.2.3
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.d.ts +1 -0
- package/dist/audit-logger.js +19 -5
- package/dist/index.d.ts +1 -1
- package/dist/index.js +19 -7
- package/package.json +2 -2
- package/src/audit-logger.ts +21 -7
- package/src/index.ts +4 -4
package/dist/audit-logger.d.ts
CHANGED
package/dist/audit-logger.js
CHANGED
|
@@ -12,11 +12,26 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.isEntityIdRequired = void 0;
|
|
15
16
|
const zehut_1 = require("@autofleet/zehut");
|
|
16
17
|
const lodash_1 = require("lodash");
|
|
18
|
+
const types_1 = require("./types");
|
|
17
19
|
const const_1 = require("./const");
|
|
18
20
|
const logger_1 = __importDefault(require("./logger"));
|
|
19
21
|
const CUSTOM_FIELDS_PROPERTY = 'customFields';
|
|
22
|
+
const getAuditContext = () => {
|
|
23
|
+
var _a;
|
|
24
|
+
const currentTrace = (0, zehut_1.getCurrentPayload)();
|
|
25
|
+
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);
|
|
26
|
+
return auditContext;
|
|
27
|
+
};
|
|
28
|
+
const ACTIONS_TO_OMIT_ENTITY_ID_FROM_CONTEXT = {
|
|
29
|
+
[types_1.Action.BULK_CREATE]: types_1.Action.BULK_CREATE,
|
|
30
|
+
[types_1.Action.BULK_EDIT]: types_1.Action.BULK_EDIT,
|
|
31
|
+
[types_1.Action.BULK_ASSIGN]: types_1.Action.BULK_ASSIGN,
|
|
32
|
+
[types_1.Action.BULK_DISPATCH]: types_1.Action.BULK_DISPATCH,
|
|
33
|
+
[types_1.Action.BULK_UPSERT]: types_1.Action.BULK_UPSERT,
|
|
34
|
+
};
|
|
20
35
|
const isEmpty = (field) => {
|
|
21
36
|
if ([null, undefined].includes(field)) {
|
|
22
37
|
return true;
|
|
@@ -29,6 +44,8 @@ const isEmpty = (field) => {
|
|
|
29
44
|
}
|
|
30
45
|
return false;
|
|
31
46
|
};
|
|
47
|
+
const isEntityIdRequired = (action) => !ACTIONS_TO_OMIT_ENTITY_ID_FROM_CONTEXT[action];
|
|
48
|
+
exports.isEntityIdRequired = isEntityIdRequired;
|
|
32
49
|
const filterOutEmptyFields = (fields, instance) => fields.filter((field) => !isEmpty(instance.get(field)) || !isEmpty(instance.previous(field)));
|
|
33
50
|
const getChangedFieldsRows = (instance, options) => {
|
|
34
51
|
// When bulk updating in sequelize using the "returning" option, the instance.changed() stops working.
|
|
@@ -69,14 +86,11 @@ class AuditLogger {
|
|
|
69
86
|
registerHooks() {
|
|
70
87
|
Object.entries(this.sequelize.models).forEach(([modelName, modelType]) => {
|
|
71
88
|
modelType.addHook('afterSave', (instance, options) => __awaiter(this, void 0, void 0, function* () {
|
|
72
|
-
var _a;
|
|
73
89
|
try {
|
|
74
|
-
const
|
|
75
|
-
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);
|
|
90
|
+
const auditContext = getAuditContext();
|
|
76
91
|
if (auditContext) {
|
|
77
|
-
if (
|
|
92
|
+
if ((0, exports.isEntityIdRequired)(auditContext.action)) {
|
|
78
93
|
auditContext.entityId = instance.id;
|
|
79
|
-
currentTrace.context.set(const_1.AUDIT_LOG_CONTEXT_KEY, auditContext);
|
|
80
94
|
}
|
|
81
95
|
const changedFieldsRows = getChangedFieldsRows(instance, options);
|
|
82
96
|
const changedCustomFieldsRows = getChangedCustomFieldsRows(instance);
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Request, Response, NextFunction } from 'express';
|
|
1
|
+
import type { Request, Response, NextFunction } from 'express';
|
|
2
2
|
import AuditLogger from './audit-logger';
|
|
3
3
|
import { AuditLoggerOptions } from './types';
|
|
4
4
|
export declare const enableAuditing: (options: AuditLoggerOptions) => void;
|
package/dist/index.js
CHANGED
|
@@ -10,6 +10,18 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
10
10
|
if (k2 === undefined) k2 = k;
|
|
11
11
|
o[k2] = m[k];
|
|
12
12
|
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
13
25
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
26
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
27
|
};
|
|
@@ -28,7 +40,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
28
40
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
41
|
exports.setRabbitAuditContext = exports.setAuditContext = exports.enableAuditing = void 0;
|
|
30
42
|
const zehut_1 = require("@autofleet/zehut");
|
|
31
|
-
const audit_logger_1 =
|
|
43
|
+
const audit_logger_1 = __importStar(require("./audit-logger"));
|
|
32
44
|
const const_1 = require("./const");
|
|
33
45
|
const audit_api_1 = __importDefault(require("./audit-api"));
|
|
34
46
|
const logger_1 = __importDefault(require("./logger"));
|
|
@@ -42,16 +54,16 @@ const enableAuditing = (options) => {
|
|
|
42
54
|
};
|
|
43
55
|
exports.enableAuditing = enableAuditing;
|
|
44
56
|
const setAuditContext = (entityType, action) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
45
|
-
var _a, _b
|
|
57
|
+
var _a, _b;
|
|
46
58
|
try {
|
|
47
59
|
const currentTrace = (0, zehut_1.getCurrentPayload)();
|
|
48
60
|
if (process.env.DISABLE_AUDIT_LOGS === 'true' || !((_a = currentTrace === null || currentTrace === void 0 ? void 0 : currentTrace.context) === null || _a === void 0 ? void 0 : _a.get)) {
|
|
49
61
|
return next();
|
|
50
62
|
}
|
|
51
|
-
const user =
|
|
63
|
+
const user = currentTrace.context.get(const_1.USER_CONTEXT_KEY);
|
|
52
64
|
const { performedBy, actionOrigin } = (user === null || user === void 0 ? void 0 : user.id)
|
|
53
65
|
? { performedBy: user.id, actionOrigin: "user" /* ActionOrigin.USER */ }
|
|
54
|
-
: { performedBy: (
|
|
66
|
+
: { performedBy: (_b = req.headers[const_1.AUTOMATION_ID_HEADER]) !== null && _b !== void 0 ? _b : null, actionOrigin: req.headers[const_1.AUTOMATION_ID_HEADER] ? "automation" /* ActionOrigin.AUTOMATION */ : null };
|
|
55
67
|
const auditLogContext = {
|
|
56
68
|
entityType,
|
|
57
69
|
action,
|
|
@@ -67,7 +79,7 @@ const setAuditContext = (entityType, action) => (req, res, next) => __awaiter(vo
|
|
|
67
79
|
res.off('error', sendAuditLogContextEvent);
|
|
68
80
|
yield auditLogger.sendAuditLogContext(auditLogContext);
|
|
69
81
|
});
|
|
70
|
-
if (
|
|
82
|
+
if (!(0, audit_logger_1.isEntityIdRequired)(action)) { // if it's a bulk action, we don't want to wait for the response to add the entity id
|
|
71
83
|
yield auditLogger.sendAuditLogContext(auditLogContext);
|
|
72
84
|
}
|
|
73
85
|
else {
|
|
@@ -83,12 +95,12 @@ const setAuditContext = (entityType, action) => (req, res, next) => __awaiter(vo
|
|
|
83
95
|
});
|
|
84
96
|
exports.setAuditContext = setAuditContext;
|
|
85
97
|
const setRabbitAuditContext = (entityType, action) => (endpoint, { userId, automationId }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
86
|
-
var
|
|
98
|
+
var _c;
|
|
87
99
|
if (process.env.DISABLE_AUDIT_LOGS === 'true') {
|
|
88
100
|
return;
|
|
89
101
|
}
|
|
90
102
|
const currentTrace = (0, zehut_1.getCurrentPayload)();
|
|
91
|
-
if ((
|
|
103
|
+
if ((_c = currentTrace === null || currentTrace === void 0 ? void 0 : currentTrace.context) === null || _c === void 0 ? void 0 : _c.get) {
|
|
92
104
|
const { performedBy, actionOrigin } = userId
|
|
93
105
|
? { performedBy: userId, actionOrigin: "user" /* ActionOrigin.USER */ }
|
|
94
106
|
: { performedBy: automationId !== null && automationId !== void 0 ? automationId : null, actionOrigin: automationId ? "automation" /* ActionOrigin.AUTOMATION */ : null };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/shtinker",
|
|
3
|
-
"version": "1.2.3
|
|
3
|
+
"version": "1.2.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"typescript": "^4.9.5"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"@autofleet/zehut": "3.
|
|
38
|
+
"@autofleet/zehut": "^3.x"
|
|
39
39
|
},
|
|
40
40
|
"author": "Autofleet",
|
|
41
41
|
"license": "ISC"
|
package/src/audit-logger.ts
CHANGED
|
@@ -1,14 +1,28 @@
|
|
|
1
1
|
import { getCurrentPayload as getCurrentTrace } from '@autofleet/zehut';
|
|
2
2
|
import { Sequelize } from 'sequelize';
|
|
3
3
|
import { isEqual } from 'lodash';
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
import {
|
|
5
|
+
AuditLogPayload, AuditLoggerOptions, AuditLogContext, Action,
|
|
6
|
+
} from './types';
|
|
6
7
|
import { AUDIT_LOG_CONTEXT_QUEUE, AUDIT_LOG_ROWS_QUEUE, AUDIT_LOG_CONTEXT_KEY } from './const';
|
|
7
|
-
|
|
8
8
|
import logger from './logger';
|
|
9
9
|
|
|
10
10
|
const CUSTOM_FIELDS_PROPERTY = 'customFields';
|
|
11
11
|
|
|
12
|
+
const getAuditContext = () => {
|
|
13
|
+
const currentTrace = getCurrentTrace();
|
|
14
|
+
const auditContext = currentTrace?.context?.get(AUDIT_LOG_CONTEXT_KEY);
|
|
15
|
+
return auditContext;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const ACTIONS_TO_OMIT_ENTITY_ID_FROM_CONTEXT = {
|
|
19
|
+
[Action.BULK_CREATE]: Action.BULK_CREATE,
|
|
20
|
+
[Action.BULK_EDIT]: Action.BULK_EDIT,
|
|
21
|
+
[Action.BULK_ASSIGN]: Action.BULK_ASSIGN,
|
|
22
|
+
[Action.BULK_DISPATCH]: Action.BULK_DISPATCH,
|
|
23
|
+
[Action.BULK_UPSERT]: Action.BULK_UPSERT,
|
|
24
|
+
};
|
|
25
|
+
|
|
12
26
|
const isEmpty = (field) => {
|
|
13
27
|
if ([null, undefined].includes(field)) {
|
|
14
28
|
return true;
|
|
@@ -22,6 +36,8 @@ const isEmpty = (field) => {
|
|
|
22
36
|
return false;
|
|
23
37
|
};
|
|
24
38
|
|
|
39
|
+
export const isEntityIdRequired = (action: string): boolean => !ACTIONS_TO_OMIT_ENTITY_ID_FROM_CONTEXT[action];
|
|
40
|
+
|
|
25
41
|
const filterOutEmptyFields = (fields: string[], instance) => fields.filter((field) => !isEmpty(instance.get(field)) || !isEmpty(instance.previous(field)));
|
|
26
42
|
|
|
27
43
|
const getChangedFieldsRows = (instance, options) => {
|
|
@@ -74,12 +90,10 @@ class AuditLogger {
|
|
|
74
90
|
Object.entries(this.sequelize.models).forEach(([modelName, modelType]) => {
|
|
75
91
|
modelType.addHook('afterSave', async (instance: any, options: any) => {
|
|
76
92
|
try {
|
|
77
|
-
const
|
|
78
|
-
const auditContext = currentTrace?.context?.get(AUDIT_LOG_CONTEXT_KEY);
|
|
93
|
+
const auditContext = getAuditContext();
|
|
79
94
|
if (auditContext) {
|
|
80
|
-
if (
|
|
95
|
+
if (isEntityIdRequired(auditContext.action)) {
|
|
81
96
|
auditContext.entityId = instance.id;
|
|
82
|
-
currentTrace.context.set(AUDIT_LOG_CONTEXT_KEY, auditContext);
|
|
83
97
|
}
|
|
84
98
|
const changedFieldsRows = getChangedFieldsRows(instance, options);
|
|
85
99
|
const changedCustomFieldsRows = getChangedCustomFieldsRows(instance);
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { getCurrentPayload as getCurrentTrace } from '@autofleet/zehut';
|
|
2
|
-
import { Request, Response, NextFunction } from 'express';
|
|
3
|
-
import AuditLogger from './audit-logger';
|
|
2
|
+
import type { Request, Response, NextFunction } from 'express';
|
|
3
|
+
import AuditLogger, { isEntityIdRequired } from './audit-logger';
|
|
4
4
|
import { ActionOrigin, AuditLogContext, AuditLoggerOptions } from './types';
|
|
5
5
|
import { AUDIT_LOG_CONTEXT_KEY, AUTOMATION_ID_HEADER, USER_CONTEXT_KEY } from './const';
|
|
6
6
|
import addAuditApi from './audit-api';
|
|
@@ -25,7 +25,7 @@ export const setAuditContext = (
|
|
|
25
25
|
if (process.env.DISABLE_AUDIT_LOGS === 'true' || !currentTrace?.context?.get) {
|
|
26
26
|
return next();
|
|
27
27
|
}
|
|
28
|
-
const user = currentTrace.context.get
|
|
28
|
+
const user = currentTrace.context.get(USER_CONTEXT_KEY);
|
|
29
29
|
const { performedBy, actionOrigin } = user?.id
|
|
30
30
|
? { performedBy: user.id, actionOrigin: ActionOrigin.USER }
|
|
31
31
|
: { performedBy: req.headers[AUTOMATION_ID_HEADER] ?? null, actionOrigin: req.headers[AUTOMATION_ID_HEADER] ? ActionOrigin.AUTOMATION : null };
|
|
@@ -46,7 +46,7 @@ export const setAuditContext = (
|
|
|
46
46
|
res.off('error', sendAuditLogContextEvent);
|
|
47
47
|
await auditLogger.sendAuditLogContext(auditLogContext);
|
|
48
48
|
};
|
|
49
|
-
if (action
|
|
49
|
+
if (!isEntityIdRequired(action)) { // if it's a bulk action, we don't want to wait for the response to add the entity id
|
|
50
50
|
await auditLogger.sendAuditLogContext(auditLogContext);
|
|
51
51
|
} else {
|
|
52
52
|
res.once('finish', sendAuditLogContextEvent);
|