@autofleet/shtinker 1.1.6-beta.6 → 1.1.6-beta.8

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,17 +21,21 @@ 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 = (value) => {
25
+ return Array.isArray(value) ? value.length === 0 : !value;
26
+ };
24
27
  const getChangedFields = (instance, options, loggerLocal) => {
25
28
  // When bulk updating in sequelize using the "returning" option, the instance.changed() stops working.
26
29
  // It's a known issut with sequelize.
27
30
  if (options.returning) {
31
+ loggerLocal.info('BULK UPDATING', { options });
28
32
  return options.fields;
29
33
  }
30
- // const changedProperties = instance.changed();
31
- // loggerLocal.info('changedProperties: ', { changedProperties });
32
- // const changedPropertiesWithoutEmptyFields = changedProperties.filter((property) => instance.previous(property) != null || instance.get(property) != null);
33
- // loggerLocal.info('changedPropertiesWithoutEmptyFields: ', { changedPropertiesWithoutEmptyFields });
34
- return instance.changed();
34
+ const changedProperties = instance.changed();
35
+ loggerLocal.info('changedProperties: ', { changedProperties });
36
+ const nonEmptyChangedProperties = changedProperties.filter((property) => isEmpty(instance.previous(property)) || isEmpty(instance.get(property)));
37
+ loggerLocal.info('nonEmptyChangedProperties: ', { nonEmptyChangedProperties });
38
+ return nonEmptyChangedProperties;
35
39
  };
36
40
  class AuditLogger {
37
41
  constructor(options) {
@@ -47,13 +51,10 @@ class AuditLogger {
47
51
  this.logger.info('auditContext:', { auditContext });
48
52
  if (auditContext) {
49
53
  const changedProperties = getChangedFields(instance, options, this.logger);
50
- this.logger.info('changedProperties:', { changedProperties });
51
- const changedPropertiesWithoutEmptyFields = changedProperties.filter((property) => instance.previous(property) != null || instance.get(property) != null);
52
- this.logger.info('changedPropertiesWithoutEmptyFields:', { changedPropertiesWithoutEmptyFields });
53
54
  const payload = {
54
55
  entityType: modelName,
55
56
  entityId: instance.id,
56
- rows: changedPropertiesWithoutEmptyFields.map((property) => ({
57
+ rows: changedProperties.map((property) => ({
57
58
  property,
58
59
  previousValue: instance.previous(property),
59
60
  newValue: instance.get(property),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/shtinker",
3
- "version": "1.1.6-beta.6",
3
+ "version": "1.1.6-beta.8",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -12,17 +12,22 @@ const getAuditContext = () => {
12
12
  return auditContext;
13
13
  };
14
14
 
15
+ const isEmpty = (value) => {
16
+ return Array.isArray(value) ? value.length === 0 : !value;
17
+ };
18
+
15
19
  const getChangedFields = (instance, options, loggerLocal) => {
16
20
  // When bulk updating in sequelize using the "returning" option, the instance.changed() stops working.
17
21
  // It's a known issut with sequelize.
18
22
  if (options.returning) {
23
+ loggerLocal.info('BULK UPDATING', { options });
19
24
  return options.fields;
20
25
  }
21
- // const changedProperties = instance.changed();
22
- // loggerLocal.info('changedProperties: ', { changedProperties });
23
- // const changedPropertiesWithoutEmptyFields = changedProperties.filter((property) => instance.previous(property) != null || instance.get(property) != null);
24
- // loggerLocal.info('changedPropertiesWithoutEmptyFields: ', { changedPropertiesWithoutEmptyFields });
25
- return instance.changed();
26
+ const changedProperties = instance.changed();
27
+ loggerLocal.info('changedProperties: ', { changedProperties });
28
+ const nonEmptyChangedProperties = changedProperties.filter((property) => isEmpty(instance.previous(property)) || isEmpty(instance.get(property)));
29
+ loggerLocal.info('nonEmptyChangedProperties: ', { nonEmptyChangedProperties });
30
+ return nonEmptyChangedProperties;
26
31
  };
27
32
 
28
33
  class AuditLogger {
@@ -46,13 +51,10 @@ class AuditLogger {
46
51
  this.logger.info('auditContext:', { auditContext });
47
52
  if (auditContext) {
48
53
  const changedProperties = getChangedFields(instance, options, this.logger);
49
- this.logger.info('changedProperties:', { changedProperties });
50
- const changedPropertiesWithoutEmptyFields = changedProperties.filter((property) => instance.previous(property) != null || instance.get(property) != null);
51
- this.logger.info('changedPropertiesWithoutEmptyFields:', { changedPropertiesWithoutEmptyFields });
52
54
  const payload: AuditLogPayload = {
53
55
  entityType: modelName,
54
56
  entityId: instance.id,
55
- rows: changedPropertiesWithoutEmptyFields.map((property: string) => ({
57
+ rows: changedProperties.map((property: string) => ({
56
58
  property,
57
59
  previousValue: instance.previous(property),
58
60
  newValue: instance.get(property),