@autofleet/shtinker 1.2.4-beta.17 → 1.2.4-beta.2
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/package.json +3 -6
- package/src/audit-logger.ts +0 -2
- package/src/index.ts +0 -14
- package/dist/audit-api.d.ts +0 -6
- package/dist/audit-api.js +0 -36
- package/dist/audit-logger.d.ts +0 -12
- package/dist/audit-logger.js +0 -134
- package/dist/audit-ms.d.ts +0 -4
- package/dist/audit-ms.js +0 -14
- package/dist/const.d.ts +0 -5
- package/dist/const.js +0 -8
- package/dist/index.d.ts +0 -12
- package/dist/index.js +0 -136
- package/dist/logger.d.ts +0 -2
- package/dist/logger.js +0 -7
- package/dist/types.d.ts +0 -55
- package/dist/types.js +0 -30
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/shtinker",
|
|
3
|
-
"version": "1.2.4-beta.
|
|
3
|
+
"version": "1.2.4-beta.2",
|
|
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.1.2",
|
|
23
|
+
"@autofleet/zehut": "^3.1.2-beta.8",
|
|
24
24
|
"@types/express": "^4.17.17",
|
|
25
25
|
"@types/jest": "^27.0.9",
|
|
26
26
|
"@types/node": "^18.11.19",
|
|
@@ -34,9 +34,6 @@
|
|
|
34
34
|
"ts-node": "^10.9.1",
|
|
35
35
|
"typescript": "^4.9.5"
|
|
36
36
|
},
|
|
37
|
-
"peerDependencies": {
|
|
38
|
-
"@autofleet/zehut": "^3.x"
|
|
39
|
-
},
|
|
40
37
|
"author": "Autofleet",
|
|
41
38
|
"license": "ISC"
|
|
42
39
|
}
|
package/src/audit-logger.ts
CHANGED
|
@@ -112,7 +112,6 @@ class AuditLogger {
|
|
|
112
112
|
}
|
|
113
113
|
|
|
114
114
|
async sendAuditLogContext(context: AuditLogContext): Promise<void> {
|
|
115
|
-
logger.error('OMRYYY - sendAuditLogContext', { context });
|
|
116
115
|
try {
|
|
117
116
|
await this.rabbit.sendToQueue(AUDIT_LOG_CONTEXT_QUEUE, context);
|
|
118
117
|
} catch (err) {
|
|
@@ -121,7 +120,6 @@ class AuditLogger {
|
|
|
121
120
|
}
|
|
122
121
|
|
|
123
122
|
async sendAuditLogRows(payload: AuditLogPayload): Promise<void> {
|
|
124
|
-
logger.error('OMRYYY - sendAuditLogRows', { payload });
|
|
125
123
|
try {
|
|
126
124
|
await this.rabbit.sendToQueue(AUDIT_LOG_ROWS_QUEUE, payload);
|
|
127
125
|
} catch (err) {
|
package/src/index.ts
CHANGED
|
@@ -21,24 +21,11 @@ export const setAuditContext = (
|
|
|
21
21
|
action: string,
|
|
22
22
|
) => async (req: Request, res: Response, next: NextFunction): Promise<any> => {
|
|
23
23
|
try {
|
|
24
|
-
logger.error('OMRYYY - setAuditContext - Start', { entityType, action });
|
|
25
24
|
const currentTrace = getCurrentTrace();
|
|
26
25
|
if (process.env.DISABLE_AUDIT_LOGS === 'true' || !currentTrace?.context?.get) {
|
|
27
|
-
logger.error('OMRYYY - setAuditContext - in first if: returning', {
|
|
28
|
-
entityType,
|
|
29
|
-
action,
|
|
30
|
-
DISABLE_AUDIT_LOGS: process.env.DISABLE_AUDIT_LOGS,
|
|
31
|
-
});
|
|
32
26
|
return next();
|
|
33
27
|
}
|
|
34
|
-
logger.error('OMRYYY - setAuditContext - after first if', {
|
|
35
|
-
context: currentTrace.context,
|
|
36
|
-
});
|
|
37
28
|
const user = currentTrace.context.get(USER_CONTEXT_KEY);
|
|
38
|
-
logger.error('OMRYYY - setAuditContext - user = ', {
|
|
39
|
-
user,
|
|
40
|
-
});
|
|
41
|
-
// This is where we can fix the System, user/driver/client, and API-empty-user bug
|
|
42
29
|
const { performedBy, actionOrigin } = user?.id
|
|
43
30
|
? { performedBy: user.id, actionOrigin: ActionOrigin.USER }
|
|
44
31
|
: { performedBy: req.headers[AUTOMATION_ID_HEADER] ?? null, actionOrigin: req.headers[AUTOMATION_ID_HEADER] ? ActionOrigin.AUTOMATION : null };
|
|
@@ -54,7 +41,6 @@ export const setAuditContext = (
|
|
|
54
41
|
currentTrace.context.set(AUDIT_LOG_CONTEXT_KEY, auditLogContext);
|
|
55
42
|
|
|
56
43
|
const sendAuditLogContextEvent = async () => {
|
|
57
|
-
logger.error('OMRYYY - sendAuditLogContextEvent - Start', { entityType, action, auditLogContext });
|
|
58
44
|
res.off('finish', sendAuditLogContextEvent);
|
|
59
45
|
res.off('close', sendAuditLogContextEvent);
|
|
60
46
|
res.off('error', sendAuditLogContextEvent);
|
package/dist/audit-api.d.ts
DELETED
package/dist/audit-api.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
const errors_1 = require("@autofleet/errors");
|
|
16
|
-
const audit_ms_1 = __importDefault(require("./audit-ms"));
|
|
17
|
-
exports.default = ({ router, entiyScopedModelMap, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
18
|
-
if (entiyScopedModelMap) {
|
|
19
|
-
Object.entries(entiyScopedModelMap).forEach(([entity, ScopedModel]) => {
|
|
20
|
-
router.get(`${entity}/:id/audit`, (req, res) => __awaiter(void 0, void 0, void 0, function* () {
|
|
21
|
-
try {
|
|
22
|
-
const { id } = req.params;
|
|
23
|
-
const entityData = yield ScopedModel.findByPk(id);
|
|
24
|
-
if (!entityData) {
|
|
25
|
-
return (0, errors_1.handleError)(new errors_1.ResourceNotFoundError(), res);
|
|
26
|
-
}
|
|
27
|
-
const auditData = audit_ms_1.default.getByEntityId(id);
|
|
28
|
-
return res.json(auditData);
|
|
29
|
-
}
|
|
30
|
-
catch (err) {
|
|
31
|
-
return (0, errors_1.handleError)(new errors_1.UnexpectedError(err), res);
|
|
32
|
-
}
|
|
33
|
-
}));
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
});
|
package/dist/audit-logger.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { AuditLogPayload, AuditLoggerOptions, AuditLogContext } from './types';
|
|
2
|
-
export declare const isEntityIdRequired: (action: string) => boolean;
|
|
3
|
-
declare class AuditLogger {
|
|
4
|
-
private rabbit;
|
|
5
|
-
private sequelize;
|
|
6
|
-
private logger;
|
|
7
|
-
constructor(options: AuditLoggerOptions);
|
|
8
|
-
registerHooks(): void;
|
|
9
|
-
sendAuditLogContext(context: AuditLogContext): Promise<void>;
|
|
10
|
-
sendAuditLogRows(payload: AuditLogPayload): Promise<void>;
|
|
11
|
-
}
|
|
12
|
-
export default AuditLogger;
|
package/dist/audit-logger.js
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.isEntityIdRequired = void 0;
|
|
16
|
-
const zehut_1 = require("@autofleet/zehut");
|
|
17
|
-
const lodash_1 = require("lodash");
|
|
18
|
-
const types_1 = require("./types");
|
|
19
|
-
const const_1 = require("./const");
|
|
20
|
-
const logger_1 = __importDefault(require("./logger"));
|
|
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
|
-
};
|
|
35
|
-
const isEmpty = (field) => {
|
|
36
|
-
if ([null, undefined].includes(field)) {
|
|
37
|
-
return true;
|
|
38
|
-
}
|
|
39
|
-
if (Array.isArray(field)) {
|
|
40
|
-
return field.length === 0;
|
|
41
|
-
}
|
|
42
|
-
if (typeof field === 'object' && !(field instanceof Date)) {
|
|
43
|
-
return Object.keys(field).length === 0;
|
|
44
|
-
}
|
|
45
|
-
return false;
|
|
46
|
-
};
|
|
47
|
-
const isEntityIdRequired = (action) => !ACTIONS_TO_OMIT_ENTITY_ID_FROM_CONTEXT[action];
|
|
48
|
-
exports.isEntityIdRequired = isEntityIdRequired;
|
|
49
|
-
const filterOutEmptyFields = (fields, instance) => fields.filter((field) => !isEmpty(instance.get(field)) || !isEmpty(instance.previous(field)));
|
|
50
|
-
const getChangedFieldsRows = (instance, options) => {
|
|
51
|
-
// When bulk updating in sequelize using the "returning" option, the instance.changed() stops working.
|
|
52
|
-
// It's a known issue with sequelize.
|
|
53
|
-
const changedFields = options.returning ? options.fields : instance.changed();
|
|
54
|
-
// Filter customFields - we'll handle them later
|
|
55
|
-
const filteredChangedFields = filterOutEmptyFields(changedFields.filter((field) => field !== CUSTOM_FIELDS_PROPERTY), instance);
|
|
56
|
-
return filteredChangedFields.map((property) => ({
|
|
57
|
-
property,
|
|
58
|
-
previousValue: instance.previous(property),
|
|
59
|
-
newValue: instance.get(property),
|
|
60
|
-
}));
|
|
61
|
-
};
|
|
62
|
-
const getChangedCustomFieldsRows = (instance) => {
|
|
63
|
-
const customFieldsChanged = instance.changed().includes(CUSTOM_FIELDS_PROPERTY);
|
|
64
|
-
if (!customFieldsChanged) {
|
|
65
|
-
return [];
|
|
66
|
-
}
|
|
67
|
-
const customFields = instance.get(CUSTOM_FIELDS_PROPERTY);
|
|
68
|
-
const previousCustomFields = instance.previous(CUSTOM_FIELDS_PROPERTY);
|
|
69
|
-
const changedCustomFields = Object.keys(customFields).filter((field) => {
|
|
70
|
-
const newValue = customFields === null || customFields === void 0 ? void 0 : customFields[field];
|
|
71
|
-
const oldValue = previousCustomFields === null || previousCustomFields === void 0 ? void 0 : previousCustomFields[field];
|
|
72
|
-
return (!isEmpty(newValue) || !isEmpty(oldValue)) && !(0, lodash_1.isEqual)(newValue, oldValue);
|
|
73
|
-
});
|
|
74
|
-
return changedCustomFields.map((changedCustomField) => ({
|
|
75
|
-
property: `customFields.${changedCustomField}`,
|
|
76
|
-
previousValue: previousCustomFields === null || previousCustomFields === void 0 ? void 0 : previousCustomFields[changedCustomField],
|
|
77
|
-
newValue: customFields === null || customFields === void 0 ? void 0 : customFields[changedCustomField],
|
|
78
|
-
}));
|
|
79
|
-
};
|
|
80
|
-
class AuditLogger {
|
|
81
|
-
constructor(options) {
|
|
82
|
-
this.rabbit = options.rabbit;
|
|
83
|
-
this.sequelize = options.sequelize;
|
|
84
|
-
this.logger = options.logger || logger_1.default;
|
|
85
|
-
}
|
|
86
|
-
registerHooks() {
|
|
87
|
-
Object.entries(this.sequelize.models).forEach(([modelName, modelType]) => {
|
|
88
|
-
modelType.addHook('afterSave', (instance, options) => __awaiter(this, void 0, void 0, function* () {
|
|
89
|
-
try {
|
|
90
|
-
const auditContext = getAuditContext();
|
|
91
|
-
if (auditContext) {
|
|
92
|
-
if ((0, exports.isEntityIdRequired)(auditContext.action)) {
|
|
93
|
-
auditContext.entityId = instance.id;
|
|
94
|
-
}
|
|
95
|
-
const changedFieldsRows = getChangedFieldsRows(instance, options);
|
|
96
|
-
const changedCustomFieldsRows = getChangedCustomFieldsRows(instance);
|
|
97
|
-
const payload = {
|
|
98
|
-
entityType: modelName,
|
|
99
|
-
entityId: instance.id,
|
|
100
|
-
rows: [...changedFieldsRows, ...changedCustomFieldsRows],
|
|
101
|
-
};
|
|
102
|
-
this.sendAuditLogRows(payload);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
catch (error) {
|
|
106
|
-
this.logger.error('Failed to send audit log rows', error);
|
|
107
|
-
}
|
|
108
|
-
}));
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
sendAuditLogContext(context) {
|
|
112
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
113
|
-
logger_1.default.error('OMRYYY - sendAuditLogContext', { context });
|
|
114
|
-
try {
|
|
115
|
-
yield this.rabbit.sendToQueue(const_1.AUDIT_LOG_CONTEXT_QUEUE, context);
|
|
116
|
-
}
|
|
117
|
-
catch (err) {
|
|
118
|
-
this.logger.error('Failed to send audit log context', err);
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
sendAuditLogRows(payload) {
|
|
123
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
124
|
-
logger_1.default.error('OMRYYY - sendAuditLogRows', { payload });
|
|
125
|
-
try {
|
|
126
|
-
yield this.rabbit.sendToQueue(const_1.AUDIT_LOG_ROWS_QUEUE, payload);
|
|
127
|
-
}
|
|
128
|
-
catch (err) {
|
|
129
|
-
this.logger.error('Failed to send audit log rows', err);
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
exports.default = AuditLogger;
|
package/dist/audit-ms.d.ts
DELETED
package/dist/audit-ms.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const network_1 = __importDefault(require("@autofleet/network"));
|
|
7
|
-
const auditMs = new network_1.default({ serviceName: 'AUDIT_MS', timeout: 1000 * 60 });
|
|
8
|
-
const getByEntityId = (entityId) => {
|
|
9
|
-
const { data } = auditMs.get(`api/v1/audit-logs/${entityId}`);
|
|
10
|
-
return data;
|
|
11
|
-
};
|
|
12
|
-
exports.default = {
|
|
13
|
-
getByEntityId,
|
|
14
|
-
};
|
package/dist/const.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export declare const AUDIT_LOG_CONTEXT_QUEUE = "audit-log-context";
|
|
2
|
-
export declare const AUDIT_LOG_ROWS_QUEUE = "audit-log-rows";
|
|
3
|
-
export declare const AUDIT_LOG_CONTEXT_KEY = "auditLogContext";
|
|
4
|
-
export declare const USER_CONTEXT_KEY = "userObject";
|
|
5
|
-
export declare const AUTOMATION_ID_HEADER = "x-af-automation-id";
|
package/dist/const.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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
|
-
exports.AUDIT_LOG_CONTEXT_QUEUE = 'audit-log-context';
|
|
5
|
-
exports.AUDIT_LOG_ROWS_QUEUE = 'audit-log-rows';
|
|
6
|
-
exports.AUDIT_LOG_CONTEXT_KEY = 'auditLogContext';
|
|
7
|
-
exports.USER_CONTEXT_KEY = 'userObject';
|
|
8
|
-
exports.AUTOMATION_ID_HEADER = 'x-af-automation-id';
|
package/dist/index.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { Request, Response, NextFunction } from 'express';
|
|
2
|
-
import AuditLogger from './audit-logger';
|
|
3
|
-
import { AuditLoggerOptions } from './types';
|
|
4
|
-
export declare const enableAuditing: (options: AuditLoggerOptions) => void;
|
|
5
|
-
export declare const setAuditContext: (entityType: string, action: string) => (req: Request, res: Response, next: NextFunction) => Promise<any>;
|
|
6
|
-
export declare const setRabbitAuditContext: (entityType: string, action: string) => (endpoint: string, { userId, automationId }: {
|
|
7
|
-
userId: string;
|
|
8
|
-
automationId: string;
|
|
9
|
-
}) => Promise<any>;
|
|
10
|
-
export * from './types';
|
|
11
|
-
export * from './const';
|
|
12
|
-
export default AuditLogger;
|
package/dist/index.js
DELETED
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
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
|
-
};
|
|
25
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
26
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
27
|
-
};
|
|
28
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
29
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
30
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
31
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
32
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
33
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
34
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
35
|
-
});
|
|
36
|
-
};
|
|
37
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
38
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
39
|
-
};
|
|
40
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
-
exports.setRabbitAuditContext = exports.setAuditContext = exports.enableAuditing = void 0;
|
|
42
|
-
const zehut_1 = require("@autofleet/zehut");
|
|
43
|
-
const audit_logger_1 = __importStar(require("./audit-logger"));
|
|
44
|
-
const const_1 = require("./const");
|
|
45
|
-
const audit_api_1 = __importDefault(require("./audit-api"));
|
|
46
|
-
const logger_1 = __importDefault(require("./logger"));
|
|
47
|
-
let auditLogger;
|
|
48
|
-
const enableAuditing = (options) => {
|
|
49
|
-
if (process.env.DISABLE_AUDIT_LOGS !== 'true') {
|
|
50
|
-
auditLogger = new audit_logger_1.default(options);
|
|
51
|
-
(0, audit_api_1.default)(options);
|
|
52
|
-
auditLogger.registerHooks();
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
exports.enableAuditing = enableAuditing;
|
|
56
|
-
const setAuditContext = (entityType, action) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
57
|
-
var _a, _b;
|
|
58
|
-
try {
|
|
59
|
-
logger_1.default.error('OMRYYY - setAuditContext - Start', { entityType, action });
|
|
60
|
-
const currentTrace = (0, zehut_1.getCurrentPayload)();
|
|
61
|
-
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)) {
|
|
62
|
-
logger_1.default.error('OMRYYY - setAuditContext - in first if: returning', {
|
|
63
|
-
entityType,
|
|
64
|
-
action,
|
|
65
|
-
DISABLE_AUDIT_LOGS: process.env.DISABLE_AUDIT_LOGS,
|
|
66
|
-
});
|
|
67
|
-
return next();
|
|
68
|
-
}
|
|
69
|
-
logger_1.default.error('OMRYYY - setAuditContext - after first if', {
|
|
70
|
-
context: currentTrace.context,
|
|
71
|
-
});
|
|
72
|
-
const user = currentTrace.context.get(const_1.USER_CONTEXT_KEY);
|
|
73
|
-
logger_1.default.error('OMRYYY - setAuditContext - user = ', {
|
|
74
|
-
user,
|
|
75
|
-
});
|
|
76
|
-
// This is where we can fix the System, user/driver/client, and API-empty-user bug
|
|
77
|
-
const { performedBy, actionOrigin } = (user === null || user === void 0 ? void 0 : user.id)
|
|
78
|
-
? { performedBy: user.id, actionOrigin: "user" /* ActionOrigin.USER */ }
|
|
79
|
-
: { 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 };
|
|
80
|
-
const auditLogContext = {
|
|
81
|
-
entityType,
|
|
82
|
-
action,
|
|
83
|
-
endpoint: req.url,
|
|
84
|
-
method: req.method,
|
|
85
|
-
performedBy,
|
|
86
|
-
actionOrigin,
|
|
87
|
-
};
|
|
88
|
-
currentTrace.context.set(const_1.AUDIT_LOG_CONTEXT_KEY, auditLogContext);
|
|
89
|
-
const sendAuditLogContextEvent = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
90
|
-
logger_1.default.error('OMRYYY - sendAuditLogContextEvent - Start', { entityType, action, auditLogContext });
|
|
91
|
-
res.off('finish', sendAuditLogContextEvent);
|
|
92
|
-
res.off('close', sendAuditLogContextEvent);
|
|
93
|
-
res.off('error', sendAuditLogContextEvent);
|
|
94
|
-
yield auditLogger.sendAuditLogContext(auditLogContext);
|
|
95
|
-
});
|
|
96
|
-
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
|
|
97
|
-
yield auditLogger.sendAuditLogContext(auditLogContext);
|
|
98
|
-
}
|
|
99
|
-
else {
|
|
100
|
-
res.once('finish', sendAuditLogContextEvent);
|
|
101
|
-
res.once('close', sendAuditLogContextEvent);
|
|
102
|
-
res.once('error', sendAuditLogContextEvent);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
catch (err) {
|
|
106
|
-
logger_1.default.error('coudln\'t set audit context', err);
|
|
107
|
-
}
|
|
108
|
-
return next();
|
|
109
|
-
});
|
|
110
|
-
exports.setAuditContext = setAuditContext;
|
|
111
|
-
const setRabbitAuditContext = (entityType, action) => (endpoint, { userId, automationId }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
112
|
-
var _c;
|
|
113
|
-
if (process.env.DISABLE_AUDIT_LOGS === 'true') {
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
const currentTrace = (0, zehut_1.getCurrentPayload)();
|
|
117
|
-
if ((_c = currentTrace === null || currentTrace === void 0 ? void 0 : currentTrace.context) === null || _c === void 0 ? void 0 : _c.get) {
|
|
118
|
-
const { performedBy, actionOrigin } = userId
|
|
119
|
-
? { performedBy: userId, actionOrigin: "user" /* ActionOrigin.USER */ }
|
|
120
|
-
: { performedBy: automationId !== null && automationId !== void 0 ? automationId : null, actionOrigin: automationId ? "automation" /* ActionOrigin.AUTOMATION */ : null };
|
|
121
|
-
const auditLogContext = {
|
|
122
|
-
entityType,
|
|
123
|
-
action,
|
|
124
|
-
endpoint,
|
|
125
|
-
method: 'rabbit',
|
|
126
|
-
performedBy,
|
|
127
|
-
actionOrigin,
|
|
128
|
-
};
|
|
129
|
-
currentTrace.context.set(const_1.AUDIT_LOG_CONTEXT_KEY, auditLogContext);
|
|
130
|
-
yield auditLogger.sendAuditLogContext(auditLogContext);
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
exports.setRabbitAuditContext = setRabbitAuditContext;
|
|
134
|
-
__exportStar(require("./types"), exports);
|
|
135
|
-
__exportStar(require("./const"), exports);
|
|
136
|
-
exports.default = audit_logger_1.default;
|
package/dist/logger.d.ts
DELETED
package/dist/logger.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const logger_1 = __importDefault(require("@autofleet/logger"));
|
|
7
|
-
exports.default = (0, logger_1.default)(null);
|
package/dist/types.d.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { Sequelize } from 'sequelize-typescript';
|
|
2
|
-
import RabbitMq from '@autofleet/rabbit';
|
|
3
|
-
export type AuditLoggerOptions = {
|
|
4
|
-
rabbit: RabbitMq;
|
|
5
|
-
sequelize: Sequelize;
|
|
6
|
-
logger: any;
|
|
7
|
-
};
|
|
8
|
-
export interface AuditLogContext {
|
|
9
|
-
entityType: string;
|
|
10
|
-
entityId?: string;
|
|
11
|
-
action: string;
|
|
12
|
-
performedBy: string;
|
|
13
|
-
endpoint: string;
|
|
14
|
-
method: string;
|
|
15
|
-
actionOrigin?: string;
|
|
16
|
-
}
|
|
17
|
-
export interface AuditLogRow {
|
|
18
|
-
property: string;
|
|
19
|
-
previousValue: any;
|
|
20
|
-
newValue: any;
|
|
21
|
-
}
|
|
22
|
-
export interface AuditLogPayload {
|
|
23
|
-
entityType: string;
|
|
24
|
-
entityId: string;
|
|
25
|
-
rows: AuditLogRow[];
|
|
26
|
-
}
|
|
27
|
-
export declare enum Action {
|
|
28
|
-
CREATE = "create",
|
|
29
|
-
BULK_CREATE = "bulk-create",
|
|
30
|
-
BULK_EDIT = "bulk-edit",
|
|
31
|
-
DELETE = "delete",
|
|
32
|
-
UPDATE = "update",
|
|
33
|
-
CANCEL = "cancel",
|
|
34
|
-
FAIL = "fail",
|
|
35
|
-
UNASSIGN = "unassign",
|
|
36
|
-
BULK_ASSIGN = "bulk-assign",
|
|
37
|
-
REASSIGN = "reassign",
|
|
38
|
-
DISPATCH = "dispatch",
|
|
39
|
-
BULK_DISPATCH = "bulk-dispatch",
|
|
40
|
-
BULK_UPSERT = "bulk-upsert",
|
|
41
|
-
UPSERT = "upsert",
|
|
42
|
-
JOIN = "join",
|
|
43
|
-
MOVE = "move",
|
|
44
|
-
REOPTIMIZATION = "reoptimization"
|
|
45
|
-
}
|
|
46
|
-
export declare enum EntityType {
|
|
47
|
-
RIDE = "Ride",
|
|
48
|
-
VEHICLE = "Vehicle",
|
|
49
|
-
DRIVER = "Driver",
|
|
50
|
-
PRICE_CALCULATION = "PriceCalculation"
|
|
51
|
-
}
|
|
52
|
-
export declare const enum ActionOrigin {
|
|
53
|
-
USER = "user",
|
|
54
|
-
AUTOMATION = "automation"
|
|
55
|
-
}
|
package/dist/types.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.EntityType = exports.Action = void 0;
|
|
4
|
-
var Action;
|
|
5
|
-
(function (Action) {
|
|
6
|
-
Action["CREATE"] = "create";
|
|
7
|
-
Action["BULK_CREATE"] = "bulk-create";
|
|
8
|
-
Action["BULK_EDIT"] = "bulk-edit";
|
|
9
|
-
Action["DELETE"] = "delete";
|
|
10
|
-
Action["UPDATE"] = "update";
|
|
11
|
-
Action["CANCEL"] = "cancel";
|
|
12
|
-
Action["FAIL"] = "fail";
|
|
13
|
-
Action["UNASSIGN"] = "unassign";
|
|
14
|
-
Action["BULK_ASSIGN"] = "bulk-assign";
|
|
15
|
-
Action["REASSIGN"] = "reassign";
|
|
16
|
-
Action["DISPATCH"] = "dispatch";
|
|
17
|
-
Action["BULK_DISPATCH"] = "bulk-dispatch";
|
|
18
|
-
Action["BULK_UPSERT"] = "bulk-upsert";
|
|
19
|
-
Action["UPSERT"] = "upsert";
|
|
20
|
-
Action["JOIN"] = "join";
|
|
21
|
-
Action["MOVE"] = "move";
|
|
22
|
-
Action["REOPTIMIZATION"] = "reoptimization";
|
|
23
|
-
})(Action = exports.Action || (exports.Action = {}));
|
|
24
|
-
var EntityType;
|
|
25
|
-
(function (EntityType) {
|
|
26
|
-
EntityType["RIDE"] = "Ride";
|
|
27
|
-
EntityType["VEHICLE"] = "Vehicle";
|
|
28
|
-
EntityType["DRIVER"] = "Driver";
|
|
29
|
-
EntityType["PRICE_CALCULATION"] = "PriceCalculation";
|
|
30
|
-
})(EntityType = exports.EntityType || (exports.EntityType = {}));
|