@autofleet/shtinker 1.1.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.
@@ -21,13 +21,24 @@ 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)));
24
37
  const getChangedFields = (instance, options) => {
25
38
  // When bulk updating in sequelize using the "returning" option, the instance.changed() stops working.
26
39
  // It's a known issut with sequelize.
27
- if (options.returning) {
28
- return options.fields;
29
- }
30
- return instance.changed();
40
+ const changedProperties = options.returning ? options.fields : instance.changed();
41
+ return filterOutEmptyFields(changedProperties, instance);
31
42
  };
32
43
  class AuditLogger {
33
44
  constructor(options) {
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"));
@@ -44,12 +45,16 @@ const setAuditContext = (entityType, action) => (req, res, next) => __awaiter(vo
44
45
  const currentTrace = (0, zehut_1.getCurrentPayload)();
45
46
  if (currentTrace && currentTrace.context && currentTrace.context.get) {
46
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 };
47
51
  const auditLogContext = {
48
52
  entityType,
49
53
  action,
50
54
  endpoint: req.url,
51
55
  method: req.method,
52
- performedBy: user === null || user === void 0 ? void 0 : user.id,
56
+ performedBy,
57
+ actionOrigin,
53
58
  };
54
59
  currentTrace.context.set(const_1.AUDIT_LOG_CONTEXT_KEY, auditLogContext);
55
60
  yield auditLogger.sendAuditLogContext(auditLogContext);
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.5",
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.14",
19
+ "@autofleet/rabbit": "^3.2.18",
20
20
  "@autofleet/zehut": "^3.0.10",
21
21
  "sequelize-typescript": "^2.1.5"
22
22
  },
@@ -12,13 +12,26 @@ 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
+
15
30
  const getChangedFields = (instance, options) => {
16
31
  // When bulk updating in sequelize using the "returning" option, the instance.changed() stops working.
17
32
  // It's a known issut with sequelize.
18
- if (options.returning) {
19
- return options.fields;
20
- }
21
- return instance.changed();
33
+ const changedProperties = options.returning ? options.fields : instance.changed();
34
+ return filterOutEmptyFields(changedProperties, instance);
22
35
  };
23
36
 
24
37
  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 { 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';
@@ -21,12 +21,16 @@ export const setAuditContext = (
21
21
  const currentTrace = getCurrentTrace();
22
22
  if (currentTrace && currentTrace.context && currentTrace.context.get) {
23
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 };
24
27
  const auditLogContext: AuditLogContext = {
25
28
  entityType,
26
29
  action,
27
30
  endpoint: req.url,
28
31
  method: req.method,
29
- performedBy: user?.id,
32
+ performedBy,
33
+ actionOrigin,
30
34
  };
31
35
  currentTrace.context.set(AUDIT_LOG_CONTEXT_KEY, auditLogContext);
32
36
  await auditLogger.sendAuditLogContext(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
+ }