@autofleet/shtinker 0.0.8 → 0.0.9-beta.1

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,12 @@
1
+ export const testEnvironment: string;
2
+ export const roots: string[];
3
+ export const transform: {
4
+ '^.+\\.tsx?$': string;
5
+ };
6
+ export const testRegex: string;
7
+ export const moduleFileExtensions: string[];
8
+ export namespace coverageThreshold {
9
+ namespace global {
10
+ const lines: number;
11
+ }
12
+ }
@@ -0,0 +1,6 @@
1
+ import { Router } from 'express';
2
+ declare const _default: ({ router, entiyScopedModelMap, }: {
3
+ router: Router;
4
+ entiyScopedModelMap: Array<any>;
5
+ }) => Promise<void>;
6
+ export default _default;
@@ -0,0 +1,38 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const errors_1 = require("@autofleet/errors");
16
+ const logger_1 = __importDefault(require("./logger"));
17
+ const audit_ms_1 = __importDefault(require("./audit-ms"));
18
+ exports.default = ({ router, entiyScopedModelMap, }) => __awaiter(void 0, void 0, void 0, function* () {
19
+ logger_1.default.info('*** SEPHI got here', { entiyScopedModelMap });
20
+ if (entiyScopedModelMap) {
21
+ Object.entries(entiyScopedModelMap).forEach(([entity, ScopedModel]) => {
22
+ router.get(`${entity}/:id/audit`, (req, res) => __awaiter(void 0, void 0, void 0, function* () {
23
+ try {
24
+ const { id } = req.params;
25
+ const entityData = yield ScopedModel.findByPk(id);
26
+ if (!entityData) {
27
+ return (0, errors_1.handleError)(new errors_1.ResourceNotFoundError(), res);
28
+ }
29
+ const auditData = audit_ms_1.default.getByEntityId(id);
30
+ return res.json(auditData);
31
+ }
32
+ catch (err) {
33
+ return (0, errors_1.handleError)(new errors_1.UnexpectedError(err), res);
34
+ }
35
+ }));
36
+ });
37
+ }
38
+ });
@@ -8,13 +8,18 @@ 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
15
  const zehut_1 = require("@autofleet/zehut");
13
16
  const const_1 = require("./const");
17
+ const logger_1 = __importDefault(require("./logger"));
14
18
  const getAuditContext = () => {
15
19
  var _a;
16
20
  const currentTrace = (0, zehut_1.getCurrentPayload)();
17
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
+ logger_1.default.info('*** SEPHI getAuditContext', { currentTrace, auditContext });
18
23
  return auditContext;
19
24
  };
20
25
  class AuditLogger {
@@ -27,7 +32,9 @@ class AuditLogger {
27
32
  Object.entries(this.sequelize.models).forEach(([modelName, modelType]) => {
28
33
  modelType.addHook('afterSave', (instance) => __awaiter(this, void 0, void 0, function* () {
29
34
  const auditContext = getAuditContext();
35
+ logger_1.default.info('*** SEPHI after save', { instance, auditContext });
30
36
  if (auditContext) {
37
+ logger_1.default.info('*** SEPHI after save got context', { instance, auditContext });
31
38
  const changedProperties = instance.changed();
32
39
  const payload = {
33
40
  entityType: modelName,
@@ -38,6 +45,7 @@ class AuditLogger {
38
45
  newValue: instance.get(property),
39
46
  })),
40
47
  };
48
+ logger_1.default.info('*** SEPHI after save got context payload', { payload, changedProperties });
41
49
  this.sendAuditLogRows(payload);
42
50
  }
43
51
  }));
@@ -45,21 +53,23 @@ class AuditLogger {
45
53
  }
46
54
  sendAuditLogContext(context) {
47
55
  return __awaiter(this, void 0, void 0, function* () {
56
+ logger_1.default.info('*** SEPHI sendAuditLogContext', { context });
48
57
  try {
49
58
  yield this.rabbit.sendToQueue(const_1.AUDIT_LOG_CONTEXT_QUEUE, context);
50
59
  }
51
60
  catch (err) {
52
- this.logger.error('Failed to send audit log context', err);
61
+ logger_1.default.error('Failed to send audit log context', err);
53
62
  }
54
63
  });
55
64
  }
56
65
  sendAuditLogRows(payload) {
57
66
  return __awaiter(this, void 0, void 0, function* () {
67
+ logger_1.default.info('*** SEPHI sendAuditLogRows', { payload });
58
68
  try {
59
69
  yield this.rabbit.sendToQueue(const_1.AUDIT_LOG_ROWS_QUEUE, payload);
60
70
  }
61
71
  catch (err) {
62
- this.logger.error('Failed to send audit log rows', err);
72
+ logger_1.default.error('Failed to send audit log rows', err);
63
73
  }
64
74
  });
65
75
  }
@@ -0,0 +1,4 @@
1
+ declare const _default: {
2
+ getByEntityId: (entityId: string) => any;
3
+ };
4
+ export default _default;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ const Network = __importStar(require("@autofleet/network"));
27
+ const auditMs = new Network({ serviceName: 'AUDIT_MS', timeout: 1000 * 60 });
28
+ const getByEntityId = (entityId) => {
29
+ const { data } = auditMs.get(`api/v1/audit-logs/${entityId}`);
30
+ return data;
31
+ };
32
+ exports.default = {
33
+ getByEntityId,
34
+ };
@@ -1,7 +1,7 @@
1
1
  import AuditLogger from './audit-logger';
2
2
  import { AuditLoggerOptions } from './types';
3
3
  export declare const enableAuditing: (options: AuditLoggerOptions) => void;
4
- export declare const setAuditContext: (entityType: string, entityIdFetcher: (req: any) => any, action: string) => (req: any, res: any, next: any) => Promise<any>;
4
+ export declare const setAuditContext: (entityType: string, entityId: string, action: string) => (req: any, res: any, next: any) => Promise<any>;
5
5
  export * from './types';
6
6
  export * from './const';
7
7
  export default AuditLogger;
@@ -30,26 +30,40 @@ exports.setAuditContext = exports.enableAuditing = void 0;
30
30
  const zehut_1 = require("@autofleet/zehut");
31
31
  const audit_logger_1 = __importDefault(require("./audit-logger"));
32
32
  const const_1 = require("./const");
33
+ const audit_api_1 = __importDefault(require("./audit-api"));
34
+ const logger_1 = __importDefault(require("./logger"));
33
35
  let auditLogger;
36
+ // export let getCurrentTrace: () => any;
34
37
  const enableAuditing = (options) => {
35
38
  auditLogger = new audit_logger_1.default(options);
39
+ // getCurrentTrace = options.getCurrentTrace;
40
+ (0, audit_api_1.default)(options);
36
41
  auditLogger.registerHooks();
37
42
  };
38
43
  exports.enableAuditing = enableAuditing;
39
- const setAuditContext = (entityType, entityIdFetcher, action) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
44
+ const setAuditContext = (entityType, entityId, action) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
40
45
  const currentTrace = (0, zehut_1.getCurrentPayload)();
46
+ logger_1.default.info('*** SEPHI current trace', { currentTrace });
41
47
  if (currentTrace) {
42
- const user = currentTrace.context.get(const_1.USER_CONTEXT_KEY);
43
- const auditLogContext = {
44
- entityType,
45
- entityId: entityIdFetcher(req),
46
- action,
47
- endpoint: req.url,
48
- method: req.method,
49
- performedBy: user.id,
50
- };
51
- currentTrace.context.set(const_1.AUDIT_LOG_CONTEXT_KEY, auditLogContext);
52
- yield auditLogger.sendAuditLogContext(auditLogContext);
48
+ try {
49
+ logger_1.default.info('*** SEPHI entered if');
50
+ const user = currentTrace.context.get(const_1.USER_CONTEXT_KEY);
51
+ logger_1.default.info('*** SEPHI user', { user });
52
+ const auditLogContext = {
53
+ entityType,
54
+ entityId,
55
+ action,
56
+ endpoint: req.url,
57
+ method: req.method,
58
+ performedBy: user.id,
59
+ };
60
+ logger_1.default.info('*** SEPHI auditLogContext', { auditLogContext });
61
+ currentTrace.context.set(const_1.AUDIT_LOG_CONTEXT_KEY, auditLogContext);
62
+ yield auditLogger.sendAuditLogContext(auditLogContext);
63
+ }
64
+ catch (err) {
65
+ logger_1.default.error('*** SEPHI something wrong with trace', err);
66
+ }
53
67
  }
54
68
  return next();
55
69
  });
@@ -0,0 +1,2 @@
1
+ declare const _default: import("winston").Logger;
2
+ export default _default;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const logger_1 = __importDefault(require("@autofleet/logger"));
7
+ exports.default = (0, logger_1.default)(null);
@@ -3,6 +3,7 @@ import RabbitMq from '@autofleet/rabbit';
3
3
  export type AuditLoggerOptions = {
4
4
  rabbit: RabbitMq;
5
5
  sequelize: Sequelize;
6
+ getCurrentTrace: () => any;
6
7
  logger: any;
7
8
  };
8
9
  export interface AuditLogContext {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/shtinker",
3
- "version": "0.0.8",
3
+ "version": "0.0.9-beta.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -12,14 +12,18 @@
12
12
  "dev": "nodemon"
13
13
  },
14
14
  "dependencies": {
15
+ "@autofleet/errors": "^1.2.2",
15
16
  "@autofleet/logger": "^2.0.5",
17
+ "@autofleet/network": "^1.4.7",
16
18
  "@autofleet/rabbit": "^2.4.1",
17
19
  "@autofleet/zehut": "^2.0.4",
18
20
  "sequelize-typescript": "^2.1.5"
19
21
  },
20
22
  "devDependencies": {
23
+ "@types/express": "^4.17.17",
21
24
  "@types/jest": "^27.0.9",
22
25
  "@types/node": "^18.11.19",
26
+ "@types/sequelize": "^4.28.14",
23
27
  "@typescript-eslint/eslint-plugin": "^4.8.1",
24
28
  "eslint": "^7.13.0",
25
29
  "eslint-config-airbnb-typescript": "^12.0.0",
@@ -0,0 +1,32 @@
1
+ import { Router } from 'express';
2
+ import { handleError, ResourceNotFoundError, UnexpectedError } from '@autofleet/errors';
3
+ import logger from './logger';
4
+
5
+ import auditMs from './audit-ms';
6
+
7
+ export default async ({
8
+ router,
9
+ entiyScopedModelMap,
10
+ } : {
11
+ router: Router,
12
+ entiyScopedModelMap: Array<any>,
13
+ }): Promise<void> => {
14
+ logger.info('*** SEPHI got here', { entiyScopedModelMap });
15
+ if (entiyScopedModelMap) {
16
+ Object.entries(entiyScopedModelMap).forEach(([entity, ScopedModel]) => {
17
+ router.get(`${entity}/:id/audit`, async (req, res) => {
18
+ try {
19
+ const { id } = req.params;
20
+ const entityData = await ScopedModel.findByPk(id);
21
+ if (!entityData) {
22
+ return handleError(new ResourceNotFoundError(), res);
23
+ }
24
+ const auditData = auditMs.getByEntityId(id);
25
+ return res.json(auditData);
26
+ } catch (err) {
27
+ return handleError(new UnexpectedError(err as Error), res);
28
+ }
29
+ });
30
+ });
31
+ }
32
+ };
@@ -1,10 +1,14 @@
1
1
  import { getCurrentPayload as getCurrentTrace } from '@autofleet/zehut';
2
+ // import { getCurrentTrace } from './index';
2
3
  import { AuditLogPayload, AuditLoggerOptions, AuditLogContext } from './types';
3
4
  import { AUDIT_LOG_CONTEXT_QUEUE, AUDIT_LOG_ROWS_QUEUE, AUDIT_LOG_CONTEXT_KEY } from './const';
4
5
 
6
+ import logger from './logger';
7
+
5
8
  const getAuditContext = () => {
6
9
  const currentTrace = getCurrentTrace();
7
10
  const auditContext = currentTrace?.context?.get(AUDIT_LOG_CONTEXT_KEY);
11
+ logger.info('*** SEPHI getAuditContext', { currentTrace, auditContext });
8
12
  return auditContext;
9
13
  };
10
14
 
@@ -23,9 +27,11 @@ class AuditLogger {
23
27
 
24
28
  registerHooks(): void {
25
29
  Object.entries(this.sequelize.models).forEach(([modelName, modelType]) => {
26
- modelType.addHook('afterSave', async (instance: any) => {
30
+ (modelType as any).addHook('afterSave', async (instance: any) => {
27
31
  const auditContext = getAuditContext();
32
+ logger.info('*** SEPHI after save', { instance, auditContext });
28
33
  if (auditContext) {
34
+ logger.info('*** SEPHI after save got context', { instance, auditContext });
29
35
  const changedProperties = instance.changed();
30
36
  const payload: AuditLogPayload = {
31
37
  entityType: modelName,
@@ -36,6 +42,7 @@ class AuditLogger {
36
42
  newValue: instance.get(property),
37
43
  })),
38
44
  };
45
+ logger.info('*** SEPHI after save got context payload', { payload, changedProperties });
39
46
  this.sendAuditLogRows(payload);
40
47
  }
41
48
  });
@@ -43,18 +50,20 @@ class AuditLogger {
43
50
  }
44
51
 
45
52
  async sendAuditLogContext(context: AuditLogContext): Promise<void> {
53
+ logger.info('*** SEPHI sendAuditLogContext', { context });
46
54
  try {
47
55
  await this.rabbit.sendToQueue(AUDIT_LOG_CONTEXT_QUEUE, context);
48
56
  } catch (err) {
49
- this.logger.error('Failed to send audit log context', err);
57
+ logger.error('Failed to send audit log context', err);
50
58
  }
51
59
  }
52
60
 
53
61
  async sendAuditLogRows(payload: AuditLogPayload): Promise<void> {
62
+ logger.info('*** SEPHI sendAuditLogRows', { payload });
54
63
  try {
55
64
  await this.rabbit.sendToQueue(AUDIT_LOG_ROWS_QUEUE, payload);
56
65
  } catch (err) {
57
- this.logger.error('Failed to send audit log rows', err);
66
+ logger.error('Failed to send audit log rows', err);
58
67
  }
59
68
  }
60
69
  }
@@ -0,0 +1,12 @@
1
+ import * as Network from '@autofleet/network';
2
+
3
+ const auditMs = new Network({ serviceName: 'AUDIT_MS', timeout: 1000 * 60 });
4
+
5
+ const getByEntityId = (entityId: string) => {
6
+ const { data } = auditMs.get(`api/v1/audit-logs/${entityId}`);
7
+ return data;
8
+ };
9
+
10
+ export default {
11
+ getByEntityId,
12
+ };
package/src/index.ts CHANGED
@@ -2,32 +2,47 @@ import { getCurrentPayload as getCurrentTrace } from '@autofleet/zehut';
2
2
  import AuditLogger from './audit-logger';
3
3
  import { AuditLogContext, AuditLoggerOptions } from './types';
4
4
  import { AUDIT_LOG_CONTEXT_KEY, USER_CONTEXT_KEY } from './const';
5
+ import addAuditApi from './audit-api';
6
+
7
+ import logger from './logger';
5
8
 
6
9
  let auditLogger: AuditLogger;
7
10
 
11
+ // export let getCurrentTrace: () => any;
12
+
8
13
  export const enableAuditing = (options: AuditLoggerOptions) => {
9
14
  auditLogger = new AuditLogger(options);
15
+ // getCurrentTrace = options.getCurrentTrace;
16
+ addAuditApi(options as any);
10
17
  auditLogger.registerHooks();
11
18
  };
12
19
 
13
20
  export const setAuditContext = (
14
21
  entityType: string,
15
- entityIdFetcher: (req: any) => any,
22
+ entityId: string,
16
23
  action: string,
17
24
  ) => async (req: any, res: any, next: any): Promise<any> => {
18
25
  const currentTrace = getCurrentTrace();
26
+ logger.info('*** SEPHI current trace', { currentTrace });
19
27
  if (currentTrace) {
20
- const user = currentTrace.context.get(USER_CONTEXT_KEY);
21
- const auditLogContext: AuditLogContext = {
22
- entityType,
23
- entityId: entityIdFetcher(req),
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.sendAuditLogContext(auditLogContext);
28
+ try {
29
+ logger.info('*** SEPHI entered if');
30
+ const user = currentTrace.context.get(USER_CONTEXT_KEY);
31
+ logger.info('*** SEPHI user', { user });
32
+ const auditLogContext: AuditLogContext = {
33
+ entityType,
34
+ entityId,
35
+ action,
36
+ endpoint: req.url,
37
+ method: req.method,
38
+ performedBy: user.id,
39
+ };
40
+ logger.info('*** SEPHI auditLogContext', { auditLogContext });
41
+ currentTrace.context.set(AUDIT_LOG_CONTEXT_KEY, auditLogContext);
42
+ await auditLogger.sendAuditLogContext(auditLogContext);
43
+ } catch (err) {
44
+ logger.error('*** SEPHI something wrong with trace', err);
45
+ }
31
46
  }
32
47
  return next();
33
48
  };
package/src/logger.ts ADDED
@@ -0,0 +1,3 @@
1
+ import Logger from '@autofleet/logger';
2
+
3
+ export default Logger(null);
package/src/types.ts CHANGED
@@ -1,9 +1,11 @@
1
1
  import { Sequelize } from 'sequelize-typescript';
2
2
  import RabbitMq from '@autofleet/rabbit';
3
+ import { getCurrentTrace } from '@autofleet/zehut/lib/tracer';
3
4
 
4
5
  export type AuditLoggerOptions = {
5
6
  rabbit: RabbitMq;
6
7
  sequelize: Sequelize;
8
+ getCurrentTrace: () => any,
7
9
  logger: any;
8
10
  };
9
11
 
package/tsconfig.json CHANGED
@@ -4,10 +4,16 @@
4
4
  "module": "commonjs",
5
5
  "declaration": true,
6
6
  "outDir": "./dist",
7
- "strict": true,
8
7
  "esModuleInterop": true,
9
8
  "experimentalDecorators": true,
10
- "emitDecoratorMetadata": true
9
+ "emitDecoratorMetadata": true,
10
+ "allowJs": true,
11
+ "baseUrl": ".",
12
+ "paths": {
13
+ "*": [
14
+ "node_modules/*"
15
+ ]
16
+ }
11
17
  },
12
- "exclude": ["node_modules", "**/*.test.ts"]
18
+ "exclude": ["node_modules", "**/*.test.ts", "dist"]
13
19
  }
File without changes
File without changes
File without changes