@autofleet/shtinker 1.1.6-beta-5 → 1.1.6-beta.0

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.
@@ -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 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)));
37
24
  const getChangedFields = (instance, options) => {
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
- const changedProperties = options.returning ? options.fields : instance.changed();
41
- return filterOutEmptyFields(changedProperties, instance);
27
+ if (options.returning) {
28
+ return options.fields;
29
+ }
30
+ const changedFields = instance.changed();
31
+ logger_1.default.info('changedFields: ', { changedFields });
32
+ const changedFieldsWithoutEmptyFields = changedFields.filter((property) => instance.previous(property) != null || instance.get(property) != null);
33
+ logger_1.default.info('changedFieldsWithoutEmptyFields: ', { changedFieldsWithoutEmptyFields });
34
+ return changedFieldsWithoutEmptyFields;
42
35
  };
43
36
  class AuditLogger {
44
37
  constructor(options) {
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, { userId, automationId }: {
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, { userId, automationId }) => __awaiter(void 0, void 0, void 0, function* () {
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: (user === null || user === void 0 ? void 0 : user.id) || contextAutomationId,
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("@autofleet/logger").LoggerInstanceManager;
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.ActionOrigin = exports.EntityType = exports.Action = void 0;
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-5",
3
+ "version": "1.1.6-beta.0",
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.20-beta-1",
19
+ "@autofleet/rabbit": "^3.2.14",
20
20
  "@autofleet/zehut": "^3.0.10",
21
21
  "sequelize-typescript": "^2.1.5"
22
22
  },
@@ -12,26 +12,17 @@ const getAuditContext = () => {
12
12
  return auditContext;
13
13
  };
14
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
-
30
15
  const getChangedFields = (instance, options) => {
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
- const changedProperties = options.returning ? options.fields : instance.changed();
34
- return filterOutEmptyFields(changedProperties, instance);
18
+ if (options.returning) {
19
+ return options.fields;
20
+ }
21
+ const changedFields = instance.changed();
22
+ logger.info('changedFields: ', { changedFields });
23
+ const changedFieldsWithoutEmptyFields = changedFields.filter((property) => instance.previous(property) != null || instance.get(property) != null);
24
+ logger.info('changedFieldsWithoutEmptyFields: ', { changedFieldsWithoutEmptyFields });
25
+ return changedFieldsWithoutEmptyFields;
35
26
  };
36
27
 
37
28
  class AuditLogger {
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 { ActionOrigin, AuditLogContext, AuditLoggerOptions } from './types';
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, { userId, automationId }): Promise<any> => {
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 || contextAutomationId,
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
- }