@autofleet/shtinker 1.1.7-beta4 → 1.1.7

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.
@@ -14,7 +14,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const zehut_1 = require("@autofleet/zehut");
16
16
  const const_1 = require("./const");
17
- const object_diff_1 = __importDefault(require("object-diff"));
18
17
  const logger_1 = __importDefault(require("./logger"));
19
18
  const CUSTOM_FIELDS_PROPERTY = 'customFields';
20
19
  const getAuditContext = () => {
@@ -36,43 +35,37 @@ const isEmpty = (field) => {
36
35
  return false;
37
36
  };
38
37
  const filterOutEmptyFields = (fields, instance) => fields.filter((field) => !isEmpty(instance.get(field)) || !isEmpty(instance.previous(field)));
39
- const getChangedFields = (instance, options) => {
38
+ const getChangedFieldsRows = (instance, options) => {
40
39
  // When bulk updating in sequelize using the "returning" option, the instance.changed() stops working.
41
- // It's a known issut with sequelize.
40
+ // It's a known issue with sequelize.
42
41
  const changedFields = options.returning ? options.fields : instance.changed();
43
- //Filter customFields - we'll handle them later
44
- const filteredChangedProperties = changedFields.filter(p => p !== CUSTOM_FIELDS_PROPERTY);
45
- return filterOutEmptyFields(filteredChangedProperties, instance);
42
+ // Filter customFields - we'll handle them later
43
+ const filteredChangedFields = filterOutEmptyFields(changedFields.filter((field) => field !== CUSTOM_FIELDS_PROPERTY), instance);
44
+ return filteredChangedFields.map((property) => ({
45
+ property,
46
+ previousValue: instance.previous(property),
47
+ newValue: instance.get(property),
48
+ }));
46
49
  };
47
- const getChangedFieldsRows = (instance, changedFields) => changedFields.map((property) => ({
48
- property,
49
- previousValue: instance.previous(property),
50
- newValue: instance.get(property),
51
- }));
52
- const getChangedCustomFields = (instance) => {
50
+ const getChangedCustomFieldsRows = (instance) => {
53
51
  const customFieldsChanged = instance.changed().includes(CUSTOM_FIELDS_PROPERTY);
54
52
  if (!customFieldsChanged) {
55
53
  return [];
56
54
  }
57
- const customFieldsValues = instance.get(CUSTOM_FIELDS_PROPERTY);
58
- const previousCustomFieldsValues = instance.previous(CUSTOM_FIELDS_PROPERTY);
59
- const diffObj = previousCustomFieldsValues && customFieldsValues ? (0, object_diff_1.default)(previousCustomFieldsValues, customFieldsValues) : customFieldsValues || previousCustomFieldsValues;
60
- return Object.keys(diffObj).filter((field) => !isEmpty(customFieldsValues === null || customFieldsValues === void 0 ? void 0 : customFieldsValues[field]) || !isEmpty(previousCustomFieldsValues === null || previousCustomFieldsValues === void 0 ? void 0 : previousCustomFieldsValues[field]));
61
- };
62
- const getChangedCustomFieldsRows = (instance, changedCustomFields) => {
63
- const customFieldsValues = instance.get(CUSTOM_FIELDS_PROPERTY);
64
- const previousCustomFieldsValues = instance.previous(CUSTOM_FIELDS_PROPERTY);
55
+ const customFields = instance.get(CUSTOM_FIELDS_PROPERTY);
56
+ const previousCustomFields = instance.previous(CUSTOM_FIELDS_PROPERTY);
57
+ const changedCustomFields = Object.keys(customFields).filter((field) => !isEmpty(customFields === null || customFields === void 0 ? void 0 : customFields[field]) || !isEmpty(previousCustomFields === null || previousCustomFields === void 0 ? void 0 : previousCustomFields[field]));
65
58
  return changedCustomFields.map((changedCustomField) => ({
66
59
  property: `customFields.${changedCustomField}`,
67
- previousValue: previousCustomFieldsValues === null || previousCustomFieldsValues === void 0 ? void 0 : previousCustomFieldsValues[changedCustomField],
68
- newValue: customFieldsValues === null || customFieldsValues === void 0 ? void 0 : customFieldsValues[changedCustomField],
60
+ previousValue: previousCustomFields === null || previousCustomFields === void 0 ? void 0 : previousCustomFields[changedCustomField],
61
+ newValue: customFields === null || customFields === void 0 ? void 0 : customFields[changedCustomField],
69
62
  }));
70
63
  };
71
64
  class AuditLogger {
72
65
  constructor(options) {
73
66
  this.rabbit = options.rabbit;
74
67
  this.sequelize = options.sequelize;
75
- this.logger = options.logger;
68
+ this.logger = options.logger || logger_1.default;
76
69
  }
77
70
  registerHooks() {
78
71
  Object.entries(this.sequelize.models).forEach(([modelName, modelType]) => {
@@ -80,10 +73,8 @@ class AuditLogger {
80
73
  try {
81
74
  const auditContext = getAuditContext();
82
75
  if (auditContext) {
83
- const changedFields = getChangedFields(instance, options);
84
- const changedFieldsRows = getChangedFieldsRows(instance, changedFields);
85
- const changedCustomFields = getChangedCustomFields(instance);
86
- const changedCustomFieldsRows = getChangedCustomFieldsRows(instance, changedCustomFields);
76
+ const changedFieldsRows = getChangedFieldsRows(instance, options);
77
+ const changedCustomFieldsRows = getChangedCustomFieldsRows(instance);
87
78
  const payload = {
88
79
  entityType: modelName,
89
80
  entityId: instance.id,
@@ -93,7 +84,7 @@ class AuditLogger {
93
84
  }
94
85
  }
95
86
  catch (error) {
96
- logger_1.default.error('Failed to send audit log rows', error);
87
+ this.logger.error('Failed to send audit log rows', error);
97
88
  }
98
89
  }));
99
90
  });
@@ -104,7 +95,7 @@ class AuditLogger {
104
95
  yield this.rabbit.sendToQueue(const_1.AUDIT_LOG_CONTEXT_QUEUE, context);
105
96
  }
106
97
  catch (err) {
107
- logger_1.default.error('Failed to send audit log context', err);
98
+ this.logger.error('Failed to send audit log context', err);
108
99
  }
109
100
  });
110
101
  }
@@ -114,7 +105,7 @@ class AuditLogger {
114
105
  yield this.rabbit.sendToQueue(const_1.AUDIT_LOG_ROWS_QUEUE, payload);
115
106
  }
116
107
  catch (err) {
117
- logger_1.default.error('Failed to send audit log rows', err);
108
+ this.logger.error('Failed to send audit log rows', err);
118
109
  }
119
110
  });
120
111
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/shtinker",
3
- "version": "1.1.7-beta4",
3
+ "version": "1.1.7",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -15,10 +15,8 @@
15
15
  "@autofleet/errors": "^1.2.2",
16
16
  "@autofleet/logger": "^4.0.6",
17
17
  "@autofleet/network": "^1.4.7",
18
- "@autofleet/netwrok": "^1.0.0",
19
18
  "@autofleet/rabbit": "^3.2.18",
20
19
  "@autofleet/zehut": "^3.0.10",
21
- "object-diff": "^0.0.4",
22
20
  "sequelize-typescript": "^2.1.5"
23
21
  },
24
22
  "devDependencies": {
@@ -3,7 +3,6 @@ import { Sequelize } from 'sequelize';
3
3
 
4
4
  import { AuditLogPayload, AuditLoggerOptions, AuditLogContext } from './types';
5
5
  import { AUDIT_LOG_CONTEXT_QUEUE, AUDIT_LOG_ROWS_QUEUE, AUDIT_LOG_CONTEXT_KEY } from './const';
6
- import diff from 'object-diff';
7
6
 
8
7
  import logger from './logger';
9
8
 
@@ -28,41 +27,34 @@ const isEmpty = (field) => {
28
27
  return false;
29
28
  };
30
29
 
31
- const filterOutEmptyFields = (fields, instance) => fields.filter((field) => !isEmpty(instance.get(field)) || !isEmpty(instance.previous(field)));
30
+ const filterOutEmptyFields = (fields: string[], instance) => fields.filter((field) => !isEmpty(instance.get(field)) || !isEmpty(instance.previous(field)));
32
31
 
33
- const getChangedFields = (instance, options) => {
32
+ const getChangedFieldsRows = (instance, options) => {
34
33
  // When bulk updating in sequelize using the "returning" option, the instance.changed() stops working.
35
- // It's a known issut with sequelize.
36
- const changedFields = options.returning ? options.fields : instance.changed();
37
- //Filter customFields - we'll handle them later
38
- const filteredChangedProperties = changedFields.filter(p => p !== CUSTOM_FIELDS_PROPERTY);
39
- return filterOutEmptyFields(filteredChangedProperties, instance);
34
+ // It's a known issue with sequelize.
35
+ const changedFields: string[] = options.returning ? options.fields : instance.changed();
36
+ // Filter customFields - we'll handle them later
37
+ const filteredChangedFields = filterOutEmptyFields(changedFields.filter((field) => field !== CUSTOM_FIELDS_PROPERTY), instance);
38
+ return filteredChangedFields.map((property: string) => ({
39
+ property,
40
+ previousValue: instance.previous(property),
41
+ newValue: instance.get(property),
42
+ }));
40
43
  };
41
44
 
42
- const getChangedFieldsRows = (instance, changedFields: string[]) => changedFields.map((property: string) => ({
43
- property,
44
- previousValue: instance.previous(property),
45
- newValue: instance.get(property),
46
- }));
47
-
48
- const getChangedCustomFields = (instance) => {
45
+ const getChangedCustomFieldsRows = (instance) => {
49
46
  const customFieldsChanged = instance.changed().includes(CUSTOM_FIELDS_PROPERTY);
50
47
  if (!customFieldsChanged) {
51
48
  return [];
52
49
  }
53
- const customFieldsValues = instance.get(CUSTOM_FIELDS_PROPERTY);
54
- const previousCustomFieldsValues = instance.previous(CUSTOM_FIELDS_PROPERTY);
55
- const diffObj = previousCustomFieldsValues && customFieldsValues ? diff(previousCustomFieldsValues, customFieldsValues) : customFieldsValues || previousCustomFieldsValues;
56
- return Object.keys(diffObj).filter((field) => !isEmpty(customFieldsValues?.[field]) || !isEmpty(previousCustomFieldsValues?.[field]));
57
- };
58
50
 
59
- const getChangedCustomFieldsRows = (instance, changedCustomFields: string[]) => {
60
- const customFieldsValues = instance.get(CUSTOM_FIELDS_PROPERTY);
61
- const previousCustomFieldsValues = instance.previous(CUSTOM_FIELDS_PROPERTY);
51
+ const customFields = instance.get(CUSTOM_FIELDS_PROPERTY);
52
+ const previousCustomFields = instance.previous(CUSTOM_FIELDS_PROPERTY);
53
+ const changedCustomFields = Object.keys(customFields).filter((field) => !isEmpty(customFields?.[field]) || !isEmpty(previousCustomFields?.[field]));
62
54
  return changedCustomFields.map((changedCustomField) => ({
63
55
  property: `customFields.${changedCustomField}`,
64
- previousValue: previousCustomFieldsValues?.[changedCustomField],
65
- newValue: customFieldsValues?.[changedCustomField],
56
+ previousValue: previousCustomFields?.[changedCustomField],
57
+ newValue: customFields?.[changedCustomField],
66
58
  }));
67
59
  };
68
60
 
@@ -76,7 +68,7 @@ class AuditLogger {
76
68
  constructor(options: AuditLoggerOptions) {
77
69
  this.rabbit = options.rabbit;
78
70
  this.sequelize = options.sequelize;
79
- this.logger = options.logger;
71
+ this.logger = options.logger || logger;
80
72
  }
81
73
 
82
74
  registerHooks(): void {
@@ -85,10 +77,8 @@ class AuditLogger {
85
77
  try {
86
78
  const auditContext = getAuditContext();
87
79
  if (auditContext) {
88
- const changedFields = getChangedFields(instance, options);
89
- const changedFieldsRows = getChangedFieldsRows(instance, changedFields);
90
- const changedCustomFields = getChangedCustomFields(instance);
91
- const changedCustomFieldsRows = getChangedCustomFieldsRows(instance, changedCustomFields);
80
+ const changedFieldsRows = getChangedFieldsRows(instance, options);
81
+ const changedCustomFieldsRows = getChangedCustomFieldsRows(instance);
92
82
  const payload: AuditLogPayload = {
93
83
  entityType: modelName,
94
84
  entityId: instance.id,
@@ -97,7 +87,7 @@ class AuditLogger {
97
87
  this.sendAuditLogRows(payload);
98
88
  }
99
89
  } catch (error) {
100
- logger.error('Failed to send audit log rows', error);
90
+ this.logger.error('Failed to send audit log rows', error);
101
91
  }
102
92
  });
103
93
  });
@@ -107,7 +97,7 @@ class AuditLogger {
107
97
  try {
108
98
  await this.rabbit.sendToQueue(AUDIT_LOG_CONTEXT_QUEUE, context);
109
99
  } catch (err) {
110
- logger.error('Failed to send audit log context', err);
100
+ this.logger.error('Failed to send audit log context', err);
111
101
  }
112
102
  }
113
103
 
@@ -115,7 +105,7 @@ class AuditLogger {
115
105
  try {
116
106
  await this.rabbit.sendToQueue(AUDIT_LOG_ROWS_QUEUE, payload);
117
107
  } catch (err) {
118
- logger.error('Failed to send audit log rows', err);
108
+ this.logger.error('Failed to send audit log rows', err);
119
109
  }
120
110
  }
121
111
  }