@autofleet/shtinker 1.1.7-beta.9 → 1.1.7-beta3

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,9 @@ 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"));
17
18
  const logger_1 = __importDefault(require("./logger"));
19
+ const CUSTOM_FIELDS_PROPERTY = 'customFields';
18
20
  const getAuditContext = () => {
19
21
  var _a;
20
22
  const currentTrace = (0, zehut_1.getCurrentPayload)();
@@ -37,8 +39,34 @@ const filterOutEmptyFields = (fields, instance) => fields.filter((field) => !isE
37
39
  const getChangedFields = (instance, options) => {
38
40
  // When bulk updating in sequelize using the "returning" option, the instance.changed() stops working.
39
41
  // It's a known issut with sequelize.
40
- const changedProperties = options.returning ? options.fields : instance.changed();
41
- return filterOutEmptyFields(changedProperties, instance);
42
+ 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);
46
+ };
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) => {
53
+ const customFieldsChanged = instance.changed().includes(CUSTOM_FIELDS_PROPERTY);
54
+ if (!customFieldsChanged) {
55
+ return [];
56
+ }
57
+ const customFieldsValues = instance.get(CUSTOM_FIELDS_PROPERTY);
58
+ const previousCustomFieldsValues = instance.previous(CUSTOM_FIELDS_PROPERTY);
59
+ const diffObj = (0, object_diff_1.default)(previousCustomFieldsValues, customFieldsValues);
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);
65
+ return changedCustomFields.map((changedCustomField) => ({
66
+ 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],
69
+ }));
42
70
  };
43
71
  class AuditLogger {
44
72
  constructor(options) {
@@ -52,15 +80,14 @@ class AuditLogger {
52
80
  try {
53
81
  const auditContext = getAuditContext();
54
82
  if (auditContext) {
55
- const changedProperties = getChangedFields(instance, options);
83
+ const changedFields = getChangedFields(instance, options);
84
+ const changedFieldsRows = getChangedFieldsRows(instance, changedFields);
85
+ const changedCustomFields = getChangedCustomFields(instance);
86
+ const changedCustomFieldsRows = getChangedCustomFieldsRows(instance, changedCustomFields);
56
87
  const payload = {
57
88
  entityType: modelName,
58
89
  entityId: instance.id,
59
- rows: changedProperties.map((property) => ({
60
- property,
61
- previousValue: instance.previous(property),
62
- newValue: instance.get(property),
63
- })),
90
+ rows: [...changedFieldsRows, ...changedCustomFieldsRows],
64
91
  };
65
92
  this.sendAuditLogRows(payload);
66
93
  }
package/dist/index.js CHANGED
@@ -34,18 +34,13 @@ const audit_api_1 = __importDefault(require("./audit-api"));
34
34
  const logger_1 = __importDefault(require("./logger"));
35
35
  let auditLogger;
36
36
  const enableAuditing = (options) => {
37
- if (process.env.DISABLE_AUDIT_LOGS !== 'true') {
38
- auditLogger = new audit_logger_1.default(options);
39
- (0, audit_api_1.default)(options);
40
- auditLogger.registerHooks();
41
- }
37
+ auditLogger = new audit_logger_1.default(options);
38
+ (0, audit_api_1.default)(options);
39
+ auditLogger.registerHooks();
42
40
  };
43
41
  exports.enableAuditing = enableAuditing;
44
42
  const setAuditContext = (entityType, action) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
45
43
  try {
46
- if (process.env.DISABLE_AUDIT_LOGS === 'true') {
47
- return next();
48
- }
49
44
  const currentTrace = (0, zehut_1.getCurrentPayload)();
50
45
  if (currentTrace && currentTrace.context && currentTrace.context.get) {
51
46
  const user = currentTrace.context.get(const_1.USER_CONTEXT_KEY);
@@ -68,9 +63,6 @@ const setAuditContext = (entityType, action) => (req, res, next) => __awaiter(vo
68
63
  exports.setAuditContext = setAuditContext;
69
64
  const setRabbitAuditContext = (entityType, action) => (endpoint) => __awaiter(void 0, void 0, void 0, function* () {
70
65
  var _a;
71
- if (process.env.DISABLE_AUDIT_LOGS === 'true') {
72
- return;
73
- }
74
66
  const currentTrace = (0, zehut_1.getCurrentPayload)();
75
67
  if ((_a = currentTrace === null || currentTrace === void 0 ? void 0 : currentTrace.context) === null || _a === void 0 ? void 0 : _a.get) {
76
68
  const user = currentTrace.context.get(const_1.USER_CONTEXT_KEY);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/shtinker",
3
- "version": "1.1.7-beta.9",
3
+ "version": "1.1.7-beta3",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -18,6 +18,7 @@
18
18
  "@autofleet/netwrok": "^1.0.0",
19
19
  "@autofleet/rabbit": "^3.2.18",
20
20
  "@autofleet/zehut": "^3.0.10",
21
+ "object-diff": "^0.0.4",
21
22
  "sequelize-typescript": "^2.1.5"
22
23
  },
23
24
  "devDependencies": {
@@ -3,9 +3,12 @@ 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';
6
7
 
7
8
  import logger from './logger';
8
9
 
10
+ const CUSTOM_FIELDS_PROPERTY = 'customFields';
11
+
9
12
  const getAuditContext = () => {
10
13
  const currentTrace = getCurrentTrace();
11
14
  const auditContext = currentTrace?.context?.get(AUDIT_LOG_CONTEXT_KEY);
@@ -30,8 +33,37 @@ const filterOutEmptyFields = (fields, instance) => fields.filter((field) => !isE
30
33
  const getChangedFields = (instance, options) => {
31
34
  // When bulk updating in sequelize using the "returning" option, the instance.changed() stops working.
32
35
  // It's a known issut with sequelize.
33
- const changedProperties = options.returning ? options.fields : instance.changed();
34
- return filterOutEmptyFields(changedProperties, instance);
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);
40
+ };
41
+
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) => {
49
+ const customFieldsChanged = instance.changed().includes(CUSTOM_FIELDS_PROPERTY);
50
+ if (!customFieldsChanged) {
51
+ return [];
52
+ }
53
+ const customFieldsValues = instance.get(CUSTOM_FIELDS_PROPERTY);
54
+ const previousCustomFieldsValues = instance.previous(CUSTOM_FIELDS_PROPERTY);
55
+ const diffObj = diff(previousCustomFieldsValues, customFieldsValues);
56
+ return Object.keys(diffObj).filter((field) => !isEmpty(customFieldsValues?.[field]) || !isEmpty(previousCustomFieldsValues?.[field]));
57
+ };
58
+
59
+ const getChangedCustomFieldsRows = (instance, changedCustomFields: string[]) => {
60
+ const customFieldsValues = instance.get(CUSTOM_FIELDS_PROPERTY);
61
+ const previousCustomFieldsValues = instance.previous(CUSTOM_FIELDS_PROPERTY);
62
+ return changedCustomFields.map((changedCustomField) => ({
63
+ property: `customFields.${changedCustomField}`,
64
+ previousValue: previousCustomFieldsValues?.[changedCustomField],
65
+ newValue: customFieldsValues?.[changedCustomField],
66
+ }));
35
67
  };
36
68
 
37
69
  class AuditLogger {
@@ -53,15 +85,14 @@ class AuditLogger {
53
85
  try {
54
86
  const auditContext = getAuditContext();
55
87
  if (auditContext) {
56
- const changedProperties = getChangedFields(instance, options);
88
+ const changedFields = getChangedFields(instance, options);
89
+ const changedFieldsRows = getChangedFieldsRows(instance, changedFields);
90
+ const changedCustomFields = getChangedCustomFields(instance);
91
+ const changedCustomFieldsRows = getChangedCustomFieldsRows(instance, changedCustomFields);
57
92
  const payload: AuditLogPayload = {
58
93
  entityType: modelName,
59
94
  entityId: instance.id,
60
- rows: changedProperties.map((property: string) => ({
61
- property,
62
- previousValue: instance.previous(property),
63
- newValue: instance.get(property),
64
- })),
95
+ rows: [...changedFieldsRows, ...changedCustomFieldsRows],
65
96
  };
66
97
  this.sendAuditLogRows(payload);
67
98
  }
package/src/index.ts CHANGED
@@ -8,11 +8,9 @@ import logger from './logger';
8
8
  let auditLogger: AuditLogger;
9
9
 
10
10
  export const enableAuditing = (options: AuditLoggerOptions) => {
11
- if (process.env.DISABLE_AUDIT_LOGS !== 'true') {
12
- auditLogger = new AuditLogger(options);
13
- addAuditApi(options as any);
14
- auditLogger.registerHooks();
15
- }
11
+ auditLogger = new AuditLogger(options);
12
+ addAuditApi(options as any);
13
+ auditLogger.registerHooks();
16
14
  };
17
15
 
18
16
  export const setAuditContext = (
@@ -20,9 +18,6 @@ export const setAuditContext = (
20
18
  action: string,
21
19
  ) => async (req: any, res: any, next: any): Promise<any> => {
22
20
  try {
23
- if (process.env.DISABLE_AUDIT_LOGS === 'true') {
24
- return next();
25
- }
26
21
  const currentTrace = getCurrentTrace();
27
22
  if (currentTrace && currentTrace.context && currentTrace.context.get) {
28
23
  const user = currentTrace.context.get(USER_CONTEXT_KEY);
@@ -46,9 +41,6 @@ export const setRabbitAuditContext = (
46
41
  entityType: string,
47
42
  action: string,
48
43
  ) => async (endpoint: string): Promise<any> => {
49
- if (process.env.DISABLE_AUDIT_LOGS === 'true') {
50
- return;
51
- }
52
44
  const currentTrace = getCurrentTrace();
53
45
  if (currentTrace?.context?.get) {
54
46
  const user = currentTrace.context.get(USER_CONTEXT_KEY);