@autofleet/shtinker 0.0.7 → 0.0.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.
@@ -1,9 +1,11 @@
1
- import { AuditLoggerOptions, AuditLogContext } from './types';
1
+ import { AuditLogPayload, AuditLoggerOptions, AuditLogContext } from './types';
2
2
  declare class AuditLogger {
3
3
  private rabbit;
4
4
  private sequelize;
5
+ private logger;
5
6
  constructor(options: AuditLoggerOptions);
6
- addModelHooks(): void;
7
- sendAuditLog(context: AuditLogContext): Promise<void>;
7
+ registerHooks(): void;
8
+ sendAuditLogContext(context: AuditLogContext): Promise<void>;
9
+ sendAuditLogRows(payload: AuditLogPayload): Promise<void>;
8
10
  }
9
11
  export default AuditLogger;
@@ -21,8 +21,9 @@ class AuditLogger {
21
21
  constructor(options) {
22
22
  this.rabbit = options.rabbit;
23
23
  this.sequelize = options.sequelize;
24
+ this.logger = options.logger;
24
25
  }
25
- addModelHooks() {
26
+ registerHooks() {
26
27
  Object.entries(this.sequelize.models).forEach(([modelName, modelType]) => {
27
28
  modelType.addHook('afterSave', (instance) => __awaiter(this, void 0, void 0, function* () {
28
29
  const auditContext = getAuditContext();
@@ -37,14 +38,29 @@ class AuditLogger {
37
38
  newValue: instance.get(property),
38
39
  })),
39
40
  };
40
- yield this.rabbit.sendToQueue(const_1.AUDIT_LOG_ROWS_QUEUE, payload);
41
+ this.sendAuditLogRows(payload);
41
42
  }
42
43
  }));
43
44
  });
44
45
  }
45
- sendAuditLog(context) {
46
+ sendAuditLogContext(context) {
46
47
  return __awaiter(this, void 0, void 0, function* () {
47
- yield this.rabbit.sendToQueue(const_1.AUDIT_LOG_CONTEXT_QUEUE, context);
48
+ try {
49
+ yield this.rabbit.sendToQueue(const_1.AUDIT_LOG_CONTEXT_QUEUE, context);
50
+ }
51
+ catch (err) {
52
+ this.logger.error('Failed to send audit log context', err);
53
+ }
54
+ });
55
+ }
56
+ sendAuditLogRows(payload) {
57
+ return __awaiter(this, void 0, void 0, function* () {
58
+ try {
59
+ yield this.rabbit.sendToQueue(const_1.AUDIT_LOG_ROWS_QUEUE, payload);
60
+ }
61
+ catch (err) {
62
+ this.logger.error('Failed to send audit log rows', err);
63
+ }
48
64
  });
49
65
  }
50
66
  }
package/dist/const.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export declare const AUDIT_LOG_CONTEXT_QUEUE = "audit-log-context";
2
2
  export declare const AUDIT_LOG_ROWS_QUEUE = "audit-log-rows";
3
3
  export declare const AUDIT_LOG_CONTEXT_KEY = "auditLogContext";
4
+ export declare const USER_CONTEXT_KEY = "userObject";
package/dist/const.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AUDIT_LOG_CONTEXT_KEY = exports.AUDIT_LOG_ROWS_QUEUE = exports.AUDIT_LOG_CONTEXT_QUEUE = void 0;
3
+ exports.USER_CONTEXT_KEY = exports.AUDIT_LOG_CONTEXT_KEY = exports.AUDIT_LOG_ROWS_QUEUE = exports.AUDIT_LOG_CONTEXT_QUEUE = void 0;
4
4
  exports.AUDIT_LOG_CONTEXT_QUEUE = 'audit-log-context';
5
5
  exports.AUDIT_LOG_ROWS_QUEUE = 'audit-log-rows';
6
6
  exports.AUDIT_LOG_CONTEXT_KEY = 'auditLogContext';
7
+ exports.USER_CONTEXT_KEY = 'userObject';
package/dist/index.js CHANGED
@@ -33,13 +33,13 @@ const const_1 = require("./const");
33
33
  let auditLogger;
34
34
  const enableAuditing = (options) => {
35
35
  auditLogger = new audit_logger_1.default(options);
36
- auditLogger.addModelHooks();
36
+ auditLogger.registerHooks();
37
37
  };
38
38
  exports.enableAuditing = enableAuditing;
39
39
  const setAuditContext = (entityType, entityIdFetcher, action) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
40
40
  const currentTrace = (0, zehut_1.getCurrentPayload)();
41
41
  if (currentTrace) {
42
- const user = currentTrace.context.get('userObject');
42
+ const user = currentTrace.context.get(const_1.USER_CONTEXT_KEY);
43
43
  const auditLogContext = {
44
44
  entityType,
45
45
  entityId: entityIdFetcher(req),
@@ -49,7 +49,7 @@ const setAuditContext = (entityType, entityIdFetcher, action) => (req, res, next
49
49
  performedBy: user.id,
50
50
  };
51
51
  currentTrace.context.set(const_1.AUDIT_LOG_CONTEXT_KEY, auditLogContext);
52
- yield auditLogger.sendAuditLog(auditLogContext);
52
+ yield auditLogger.sendAuditLogContext(auditLogContext);
53
53
  }
54
54
  return next();
55
55
  });
package/dist/types.d.ts CHANGED
@@ -3,6 +3,7 @@ import RabbitMq from '@autofleet/rabbit';
3
3
  export type AuditLoggerOptions = {
4
4
  rabbit: RabbitMq;
5
5
  sequelize: Sequelize;
6
+ logger: any;
6
7
  };
7
8
  export interface AuditLogContext {
8
9
  entityType: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/shtinker",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -13,12 +13,15 @@ class AuditLogger {
13
13
 
14
14
  private sequelize;
15
15
 
16
+ private logger;
17
+
16
18
  constructor(options: AuditLoggerOptions) {
17
19
  this.rabbit = options.rabbit;
18
20
  this.sequelize = options.sequelize;
21
+ this.logger = options.logger;
19
22
  }
20
23
 
21
- addModelHooks(): void {
24
+ registerHooks(): void {
22
25
  Object.entries(this.sequelize.models).forEach(([modelName, modelType]) => {
23
26
  modelType.addHook('afterSave', async (instance: any) => {
24
27
  const auditContext = getAuditContext();
@@ -33,14 +36,26 @@ class AuditLogger {
33
36
  newValue: instance.get(property),
34
37
  })),
35
38
  };
36
- await this.rabbit.sendToQueue(AUDIT_LOG_ROWS_QUEUE, payload);
39
+ this.sendAuditLogRows(payload);
37
40
  }
38
41
  });
39
42
  });
40
43
  }
41
44
 
42
- async sendAuditLog(context: AuditLogContext): Promise<void> {
43
- await this.rabbit.sendToQueue(AUDIT_LOG_CONTEXT_QUEUE, context);
45
+ async sendAuditLogContext(context: AuditLogContext): Promise<void> {
46
+ try {
47
+ await this.rabbit.sendToQueue(AUDIT_LOG_CONTEXT_QUEUE, context);
48
+ } catch (err) {
49
+ this.logger.error('Failed to send audit log context', err);
50
+ }
51
+ }
52
+
53
+ async sendAuditLogRows(payload: AuditLogPayload): Promise<void> {
54
+ try {
55
+ await this.rabbit.sendToQueue(AUDIT_LOG_ROWS_QUEUE, payload);
56
+ } catch (err) {
57
+ this.logger.error('Failed to send audit log rows', err);
58
+ }
44
59
  }
45
60
  }
46
61
 
package/src/const.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export const AUDIT_LOG_CONTEXT_QUEUE = 'audit-log-context';
2
2
  export const AUDIT_LOG_ROWS_QUEUE = 'audit-log-rows';
3
3
  export const AUDIT_LOG_CONTEXT_KEY = 'auditLogContext';
4
+ export const USER_CONTEXT_KEY = 'userObject';
package/src/index.ts CHANGED
@@ -1,13 +1,13 @@
1
1
  import { getCurrentPayload as getCurrentTrace } from '@autofleet/zehut';
2
2
  import AuditLogger from './audit-logger';
3
3
  import { AuditLogContext, AuditLoggerOptions } from './types';
4
- import { AUDIT_LOG_CONTEXT_KEY } from './const';
4
+ import { AUDIT_LOG_CONTEXT_KEY, USER_CONTEXT_KEY } from './const';
5
5
 
6
6
  let auditLogger: AuditLogger;
7
7
 
8
8
  export const enableAuditing = (options: AuditLoggerOptions) => {
9
9
  auditLogger = new AuditLogger(options);
10
- auditLogger.addModelHooks();
10
+ auditLogger.registerHooks();
11
11
  };
12
12
 
13
13
  export const setAuditContext = (
@@ -17,7 +17,7 @@ export const setAuditContext = (
17
17
  ) => async (req: any, res: any, next: any): Promise<any> => {
18
18
  const currentTrace = getCurrentTrace();
19
19
  if (currentTrace) {
20
- const user = currentTrace.context.get('userObject');
20
+ const user = currentTrace.context.get(USER_CONTEXT_KEY);
21
21
  const auditLogContext: AuditLogContext = {
22
22
  entityType,
23
23
  entityId: entityIdFetcher(req),
@@ -27,7 +27,7 @@ export const setAuditContext = (
27
27
  performedBy: user.id,
28
28
  };
29
29
  currentTrace.context.set(AUDIT_LOG_CONTEXT_KEY, auditLogContext);
30
- await auditLogger.sendAuditLog(auditLogContext);
30
+ await auditLogger.sendAuditLogContext(auditLogContext);
31
31
  }
32
32
  return next();
33
33
  };
package/src/types.ts CHANGED
@@ -4,6 +4,7 @@ import RabbitMq from '@autofleet/rabbit';
4
4
  export type AuditLoggerOptions = {
5
5
  rabbit: RabbitMq;
6
6
  sequelize: Sequelize;
7
+ logger: any;
7
8
  };
8
9
 
9
10
  export interface AuditLogContext {
@@ -14,6 +15,7 @@ export interface AuditLogContext {
14
15
  endpoint: string;
15
16
  method: string;
16
17
  }
18
+
17
19
  export interface AuditLogRow {
18
20
  property: string;
19
21
  previousValue: any;