@autofleet/shtinker 0.0.1 → 0.0.3

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.previousValue(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,9 +1,5 @@
1
+ import AuditLogger from './audit-logger';
1
2
  import { AuditLoggerOptions } from './types';
2
- declare class AuditLogger {
3
- private rabbit;
4
- private sequelize;
5
- constructor(options: AuditLoggerOptions);
6
- enableAuditLogging(): void;
7
- setAuditingContext: (entityType: string, entityId: string, action: string) => (req: any, res: any, next: any) => Promise<void>;
8
- }
3
+ export declare const enableAuditing: (options: AuditLoggerOptions) => void;
4
+ export declare const setAuditContext: (entityType: string, entityId: string, action: string) => (req: any, res: any, next: any) => Promise<any>;
9
5
  export default AuditLogger;
package/dist/index.js CHANGED
@@ -8,56 +8,36 @@ 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 });
15
+ exports.setAuditContext = exports.enableAuditing = void 0;
12
16
  const zehut_1 = require("@autofleet/zehut");
17
+ const audit_logger_1 = __importDefault(require("./audit-logger"));
13
18
  const const_1 = require("./const");
14
- const getAuditingContext = () => {
15
- var _a;
16
- const currentTrace = (0, zehut_1.getCurrentPayload)();
17
- const auditingContext = (_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 auditingContext;
19
+ let auditLogger;
20
+ const enableAuditing = (options) => {
21
+ auditLogger = new audit_logger_1.default(options);
22
+ auditLogger.addModelHooks();
19
23
  };
20
- class AuditLogger {
21
- constructor(options) {
22
- this.setAuditingContext = (entityType, entityId, action) => (req, res, next) => __awaiter(this, void 0, void 0, function* () {
23
- const currentTrace = (0, zehut_1.getCurrentPayload)();
24
- if (currentTrace) {
25
- const user = currentTrace.context.get('userObject');
26
- const auditLogContext = {
27
- entityType,
28
- entityId,
29
- action,
30
- endpoint: req.url,
31
- method: req.method,
32
- performedBy: user.id,
33
- };
34
- currentTrace.context.set(const_1.AUDIT_LOG_CONTEXT_KEY, auditLogContext);
35
- yield this.rabbit.sendToQueue(const_1.AUDIT_LOG_CONTEXT_QUEUE, auditLogContext);
36
- next();
37
- }
38
- });
39
- this.rabbit = options.rabbit;
40
- this.sequelize = options.sequelize;
41
- }
42
- enableAuditLogging() {
43
- Object.entries(this.sequelize.models).forEach(([modelName, modelType]) => {
44
- modelType.addHook('afterSave', (instance) => __awaiter(this, void 0, void 0, function* () {
45
- const auditingContext = getAuditingContext();
46
- if (auditingContext) {
47
- const changedProperties = instance.changed();
48
- const payload = {
49
- entityType: modelName,
50
- entityId: instance.id,
51
- rows: changedProperties.map((property) => ({
52
- property,
53
- previousValue: instance.previousValue(property),
54
- newValue: instance.get(property),
55
- })),
56
- };
57
- yield this.rabbit.sendToQueue(const_1.AUDIT_LOG_ROWS_QUEUE, payload);
58
- }
59
- }));
60
- });
24
+ exports.enableAuditing = enableAuditing;
25
+ const setAuditContext = (entityType, entityId, action) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
26
+ const currentTrace = (0, zehut_1.getCurrentPayload)();
27
+ if (currentTrace) {
28
+ const user = currentTrace.context.get('userObject');
29
+ const auditLogContext = {
30
+ entityType,
31
+ entityId,
32
+ action,
33
+ endpoint: req.url,
34
+ method: req.method,
35
+ performedBy: user.id,
36
+ };
37
+ currentTrace.context.set(const_1.AUDIT_LOG_CONTEXT_KEY, auditLogContext);
38
+ yield auditLogger.sendAuditLog(auditLogContext);
61
39
  }
62
- }
63
- exports.default = AuditLogger;
40
+ return next();
41
+ });
42
+ exports.setAuditContext = setAuditContext;
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.1",
3
+ "version": "0.0.3",
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.previousValue(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,61 +1,35 @@
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 getAuditingContext = () => {
6
- const currentTrace = getCurrentTrace();
7
- const auditingContext = currentTrace?.context?.get(AUDIT_LOG_CONTEXT_KEY);
8
- return auditingContext;
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
- }
6
+ let auditLogger: AuditLogger;
20
7
 
21
- enableAuditLogging(): void {
22
- Object.entries(this.sequelize.models).forEach(([modelName, modelType]) => {
23
- modelType.addHook('afterSave', async (instance: any) => {
24
- const auditingContext = getAuditingContext();
25
- if (auditingContext) {
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
- }
8
+ export const enableAuditing = (options: AuditLoggerOptions) => {
9
+ auditLogger = new AuditLogger(options);
10
+ auditLogger.addModelHooks();
11
+ };
41
12
 
42
- setAuditingContext = (entityType: string, entityId: string, action: string) => async (req: any, res: any, next: any) => {
43
- const currentTrace = getCurrentTrace();
44
- if (currentTrace) {
45
- const user = currentTrace.context.get('userObject');
46
- const auditLogContext: AuditLogContext = {
47
- entityType,
48
- entityId,
49
- action,
50
- endpoint: req.url,
51
- method: req.method,
52
- performedBy: user.id,
53
- };
54
- currentTrace.context.set(AUDIT_LOG_CONTEXT_KEY, auditLogContext);
55
- await this.rabbit.sendToQueue(AUDIT_LOG_CONTEXT_QUEUE, auditLogContext);
56
- next();
57
- }
13
+ export const setAuditContext = (
14
+ entityType: string,
15
+ entityId: string,
16
+ action: string,
17
+ ) => async (req: any, res: any, next: any): Promise<any> => {
18
+ const currentTrace = getCurrentTrace();
19
+ if (currentTrace) {
20
+ const user = currentTrace.context.get('userObject');
21
+ const auditLogContext: AuditLogContext = {
22
+ entityType,
23
+ entityId,
24
+ action,
25
+ endpoint: req.url,
26
+ method: req.method,
27
+ performedBy: user.id,
28
+ };
29
+ currentTrace.context.set(AUDIT_LOG_CONTEXT_KEY, auditLogContext);
30
+ await auditLogger.sendAuditLog(auditLogContext);
58
31
  }
59
- }
32
+ return next();
33
+ };
60
34
 
61
35
  export default AuditLogger;