@autofleet/shtinker 0.0.2 → 0.0.4

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.
@@ -0,0 +1,9 @@
1
+ import { AuditLoggerOptions, AuditLogContext } from './types';
2
+ declare class AuditLogger {
3
+ private rabbit;
4
+ private sequelize;
5
+ constructor(options: AuditLoggerOptions);
6
+ addModelHooks(): void;
7
+ sendAuditLog(context: AuditLogContext): Promise<void>;
8
+ }
9
+ export default AuditLogger;
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const zehut_1 = require("@autofleet/zehut");
13
+ const const_1 = require("./const");
14
+ const getAuditContext = () => {
15
+ var _a;
16
+ const currentTrace = (0, zehut_1.getCurrentPayload)();
17
+ 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);
18
+ return auditContext;
19
+ };
20
+ class AuditLogger {
21
+ constructor(options) {
22
+ this.rabbit = options.rabbit;
23
+ this.sequelize = options.sequelize;
24
+ }
25
+ addModelHooks() {
26
+ Object.entries(this.sequelize.models).forEach(([modelName, modelType]) => {
27
+ modelType.addHook('afterSave', (instance) => __awaiter(this, void 0, void 0, function* () {
28
+ const auditContext = getAuditContext();
29
+ if (auditContext) {
30
+ const changedProperties = instance.changed();
31
+ const payload = {
32
+ entityType: modelName,
33
+ entityId: instance.id,
34
+ rows: changedProperties.map((property) => ({
35
+ property,
36
+ previousValue: instance.previous(property),
37
+ newValue: instance.get(property),
38
+ })),
39
+ };
40
+ yield this.rabbit.sendToQueue(const_1.AUDIT_LOG_ROWS_QUEUE, payload);
41
+ }
42
+ }));
43
+ });
44
+ }
45
+ sendAuditLog(context) {
46
+ return __awaiter(this, void 0, void 0, function* () {
47
+ yield this.rabbit.sendToQueue(const_1.AUDIT_LOG_CONTEXT_QUEUE, context);
48
+ });
49
+ }
50
+ }
51
+ exports.default = AuditLogger;
package/dist/index.d.ts CHANGED
@@ -1,10 +1,5 @@
1
- import { AuditLoggerOptions, AuditLogContext } from './types';
2
- declare class AuditLogger {
3
- private rabbit;
4
- private sequelize;
5
- constructor(options: AuditLoggerOptions);
6
- addModelHooks(): void;
7
- sendAuditLog(context: AuditLogContext): Promise<void>;
8
- }
1
+ import AuditLogger from './audit-logger';
2
+ import { AuditLoggerOptions } from './types';
3
+ export declare const enableAuditing: (options: AuditLoggerOptions) => void;
9
4
  export declare const setAuditContext: (entityType: string, entityId: string, action: string) => (req: any, res: any, next: any) => Promise<any>;
10
5
  export default AuditLogger;
package/dist/index.js CHANGED
@@ -8,47 +8,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.setAuditContext = void 0;
15
+ exports.setAuditContext = exports.enableAuditing = void 0;
13
16
  const zehut_1 = require("@autofleet/zehut");
17
+ const audit_logger_1 = __importDefault(require("./audit-logger"));
14
18
  const const_1 = require("./const");
15
- const getAuditContext = () => {
16
- var _a;
17
- const currentTrace = (0, zehut_1.getCurrentPayload)();
18
- 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);
19
- return auditContext;
19
+ let auditLogger;
20
+ const enableAuditing = (options) => {
21
+ auditLogger = new audit_logger_1.default(options);
22
+ auditLogger.addModelHooks();
20
23
  };
21
- class AuditLogger {
22
- constructor(options) {
23
- this.rabbit = options.rabbit;
24
- this.sequelize = options.sequelize;
25
- }
26
- addModelHooks() {
27
- Object.entries(this.sequelize.models).forEach(([modelName, modelType]) => {
28
- modelType.addHook('afterSave', (instance) => __awaiter(this, void 0, void 0, function* () {
29
- const auditContext = getAuditContext();
30
- if (auditContext) {
31
- const changedProperties = instance.changed();
32
- const payload = {
33
- entityType: modelName,
34
- entityId: instance.id,
35
- rows: changedProperties.map((property) => ({
36
- property,
37
- previousValue: instance.previousValue(property),
38
- newValue: instance.get(property),
39
- })),
40
- };
41
- yield this.rabbit.sendToQueue(const_1.AUDIT_LOG_ROWS_QUEUE, payload);
42
- }
43
- }));
44
- });
45
- }
46
- sendAuditLog(context) {
47
- return __awaiter(this, void 0, void 0, function* () {
48
- yield this.rabbit.sendToQueue(const_1.AUDIT_LOG_CONTEXT_QUEUE, context);
49
- });
50
- }
51
- }
24
+ exports.enableAuditing = enableAuditing;
52
25
  const setAuditContext = (entityType, entityId, action) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
53
26
  const currentTrace = (0, zehut_1.getCurrentPayload)();
54
27
  if (currentTrace) {
@@ -62,8 +35,9 @@ const setAuditContext = (entityType, entityId, action) => (req, res, next) => __
62
35
  performedBy: user.id,
63
36
  };
64
37
  currentTrace.context.set(const_1.AUDIT_LOG_CONTEXT_KEY, auditLogContext);
38
+ yield auditLogger.sendAuditLog(auditLogContext);
65
39
  }
66
40
  return next();
67
41
  });
68
42
  exports.setAuditContext = setAuditContext;
69
- exports.default = AuditLogger;
43
+ exports.default = audit_logger_1.default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/shtinker",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -0,0 +1,47 @@
1
+ import { getCurrentPayload as getCurrentTrace } from '@autofleet/zehut';
2
+ import { AuditLogPayload, AuditLoggerOptions, AuditLogContext } from './types';
3
+ import { AUDIT_LOG_CONTEXT_QUEUE, AUDIT_LOG_ROWS_QUEUE, AUDIT_LOG_CONTEXT_KEY } from './const';
4
+
5
+ const getAuditContext = () => {
6
+ const currentTrace = getCurrentTrace();
7
+ const auditContext = currentTrace?.context?.get(AUDIT_LOG_CONTEXT_KEY);
8
+ return auditContext;
9
+ };
10
+
11
+ class AuditLogger {
12
+ private rabbit;
13
+
14
+ private sequelize;
15
+
16
+ constructor(options: AuditLoggerOptions) {
17
+ this.rabbit = options.rabbit;
18
+ this.sequelize = options.sequelize;
19
+ }
20
+
21
+ addModelHooks(): void {
22
+ Object.entries(this.sequelize.models).forEach(([modelName, modelType]) => {
23
+ modelType.addHook('afterSave', async (instance: any) => {
24
+ const auditContext = getAuditContext();
25
+ if (auditContext) {
26
+ const changedProperties = instance.changed();
27
+ const payload: AuditLogPayload = {
28
+ entityType: modelName,
29
+ entityId: instance.id,
30
+ rows: changedProperties.map((property: string) => ({
31
+ property,
32
+ previousValue: instance.previous(property),
33
+ newValue: instance.get(property),
34
+ })),
35
+ };
36
+ await this.rabbit.sendToQueue(AUDIT_LOG_ROWS_QUEUE, payload);
37
+ }
38
+ });
39
+ });
40
+ }
41
+
42
+ async sendAuditLog(context: AuditLogContext): Promise<void> {
43
+ await this.rabbit.sendToQueue(AUDIT_LOG_CONTEXT_QUEUE, context);
44
+ }
45
+ }
46
+
47
+ export default AuditLogger;
package/src/index.ts CHANGED
@@ -1,48 +1,14 @@
1
1
  import { getCurrentPayload as getCurrentTrace } from '@autofleet/zehut';
2
- import { AuditLogPayload, AuditLoggerOptions, AuditLogContext } from './types';
3
- import { AUDIT_LOG_CONTEXT_QUEUE, AUDIT_LOG_ROWS_QUEUE, AUDIT_LOG_CONTEXT_KEY } from './const';
2
+ import AuditLogger from './audit-logger';
3
+ import { AuditLogContext, AuditLoggerOptions } from './types';
4
+ import { AUDIT_LOG_CONTEXT_KEY } from './const';
4
5
 
5
- const getAuditContext = () => {
6
- const currentTrace = getCurrentTrace();
7
- const auditContext = currentTrace?.context?.get(AUDIT_LOG_CONTEXT_KEY);
8
- return auditContext;
9
- };
10
-
11
- class AuditLogger {
12
- private rabbit;
13
-
14
- private sequelize;
15
-
16
- constructor(options: AuditLoggerOptions) {
17
- this.rabbit = options.rabbit;
18
- this.sequelize = options.sequelize;
19
- }
20
-
21
- addModelHooks(): void {
22
- Object.entries(this.sequelize.models).forEach(([modelName, modelType]) => {
23
- modelType.addHook('afterSave', async (instance: any) => {
24
- const auditContext = getAuditContext();
25
- if (auditContext) {
26
- const changedProperties = instance.changed();
27
- const payload: AuditLogPayload = {
28
- entityType: modelName,
29
- entityId: instance.id,
30
- rows: changedProperties.map((property: string) => ({
31
- property,
32
- previousValue: instance.previousValue(property),
33
- newValue: instance.get(property),
34
- })),
35
- };
36
- await this.rabbit.sendToQueue(AUDIT_LOG_ROWS_QUEUE, payload);
37
- }
38
- });
39
- });
40
- }
6
+ let auditLogger: AuditLogger;
41
7
 
42
- async sendAuditLog(context: AuditLogContext): Promise<void> {
43
- await this.rabbit.sendToQueue(AUDIT_LOG_CONTEXT_QUEUE, context);
44
- }
45
- }
8
+ export const enableAuditing = (options: AuditLoggerOptions) => {
9
+ auditLogger = new AuditLogger(options);
10
+ auditLogger.addModelHooks();
11
+ };
46
12
 
47
13
  export const setAuditContext = (
48
14
  entityType: string,
@@ -61,6 +27,7 @@ export const setAuditContext = (
61
27
  performedBy: user.id,
62
28
  };
63
29
  currentTrace.context.set(AUDIT_LOG_CONTEXT_KEY, auditLogContext);
30
+ await auditLogger.sendAuditLog(auditLogContext);
64
31
  }
65
32
  return next();
66
33
  };