@vasrefil/api-toolkit 1.0.22 → 1.0.24
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.
- package/dist/env.d.ts +4 -0
- package/dist/env.js +4 -0
- package/dist/interfaces/interface.d.ts +6 -4
- package/dist/middlewares/index.d.ts +1 -0
- package/dist/middlewares/index.js +1 -0
- package/dist/middlewares/validator.midware.d.ts +3 -3
- package/dist/middlewares/validator.midware.js +7 -10
- package/dist/public-api.d.ts +1 -0
- package/dist/public-api.js +1 -0
- package/dist/routes/sample.route.js +2 -2
- package/dist/services/_root.service.d.ts +3 -1
- package/dist/services/_root.service.js +83 -14
- package/dist/services/index.d.ts +1 -0
- package/dist/services/index.js +17 -0
- package/dist/utilities/api-request.util.d.ts +1 -0
- package/dist/utilities/api-request.util.js +30 -6
- package/dist/utilities/index.d.ts +1 -0
- package/dist/utilities/index.js +1 -0
- package/dist/utilities/log.util.d.ts +85 -0
- package/dist/utilities/log.util.js +93 -0
- package/package.json +1 -1
package/dist/env.d.ts
CHANGED
package/dist/env.js
CHANGED
|
@@ -17,6 +17,10 @@ const env = {
|
|
|
17
17
|
BASEURL: NODE_ENV === NODE_ENVS.PROD ? 'https://api.vasrefil.com' : 'https://api-v2-test.vasrefil.com',
|
|
18
18
|
API_KEY: process.env.VASREFIL_API_KEY
|
|
19
19
|
},
|
|
20
|
+
LOG: {
|
|
21
|
+
BASEURL: process.env.NODE_ENV === NODE_ENVS.PROD ? 'https://log-api.vasrefil.com' : 'https://log-api-dev.vasrefil.com',
|
|
22
|
+
API_KEY: process.env.VASREFIL_LOG_API_KEY
|
|
23
|
+
},
|
|
20
24
|
AIRBRAKE: {
|
|
21
25
|
PROJECT_ID: process.env.AIRBRAKE_PROJECT_ID,
|
|
22
26
|
PROJECT_KEY: process.env.AIRBRAKE_PROJECT_KEY
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Status } from "./status.interface";
|
|
2
|
-
import {
|
|
2
|
+
import { Response } from 'express';
|
|
3
|
+
import { UserRequestI } from "./user.interface";
|
|
3
4
|
export interface SampleI {
|
|
4
5
|
_id: string;
|
|
5
6
|
name: string;
|
|
@@ -7,15 +8,16 @@ export interface SampleI {
|
|
|
7
8
|
createdAt: number;
|
|
8
9
|
}
|
|
9
10
|
export interface ServiceRespI {
|
|
10
|
-
req:
|
|
11
|
+
req: UserRequestI;
|
|
12
|
+
request?: UserRequestI;
|
|
11
13
|
res: Response;
|
|
12
14
|
status: Status;
|
|
13
|
-
request?: Request;
|
|
14
15
|
actionType: string;
|
|
15
16
|
data?: any;
|
|
16
17
|
message?: string;
|
|
17
18
|
error?: any;
|
|
18
|
-
|
|
19
|
+
NODE_ENV?: string;
|
|
20
|
+
code?: string;
|
|
19
21
|
}
|
|
20
22
|
export interface FiltersQueryI {
|
|
21
23
|
filterSet: {
|
|
@@ -16,3 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./admin-auth.midware"), exports);
|
|
18
18
|
__exportStar(require("./user-auth.midware"), exports);
|
|
19
|
+
__exportStar(require("./validator.midware"), exports);
|
|
@@ -5,8 +5,8 @@ import { Request, Response, NextFunction } from 'express';
|
|
|
5
5
|
* Validation middleware that uses joi to validate the request body.
|
|
6
6
|
* @param schema Joi schema to use to validate the request body
|
|
7
7
|
*/
|
|
8
|
-
export declare class
|
|
8
|
+
export declare class JoiVdtor_ extends RootService {
|
|
9
9
|
vdtor(schema: joi.Schema, field?: 'body' | 'query'): (req: Request, res: Response, next: NextFunction) => Promise<any>;
|
|
10
10
|
}
|
|
11
|
-
declare const
|
|
12
|
-
export
|
|
11
|
+
declare const Joi: JoiVdtor_;
|
|
12
|
+
export { Joi };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Joi = void 0;
|
|
3
|
+
exports.Joi = exports.JoiVdtor_ = void 0;
|
|
4
4
|
const _root_service_1 = require("../services/_root.service");
|
|
5
5
|
const status_interface_1 = require("../interfaces/status.interface");
|
|
6
6
|
const { FAILED_VALIDATION } = status_interface_1.Status;
|
|
@@ -8,7 +8,7 @@ const { FAILED_VALIDATION } = status_interface_1.Status;
|
|
|
8
8
|
* Validation middleware that uses joi to validate the request body.
|
|
9
9
|
* @param schema Joi schema to use to validate the request body
|
|
10
10
|
*/
|
|
11
|
-
class
|
|
11
|
+
class JoiVdtor_ extends _root_service_1.RootService {
|
|
12
12
|
vdtor(schema, field = 'body') {
|
|
13
13
|
return async (req, res, next) => {
|
|
14
14
|
const actionType = 'SCHEMA_VALIDATION';
|
|
@@ -17,16 +17,13 @@ class Joi extends _root_service_1.RootService {
|
|
|
17
17
|
next();
|
|
18
18
|
}
|
|
19
19
|
catch (err) {
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
errors: errorDetails,
|
|
24
|
-
};
|
|
25
|
-
return this.sendResponse({ req, res, status: FAILED_VALIDATION, actionType, data: response });
|
|
20
|
+
let message = 'Some validation errors occured';
|
|
21
|
+
const errorDetails = err && err.details && err.details;
|
|
22
|
+
return this.sendResponse({ req, res, status: FAILED_VALIDATION, actionType, message, error: errorDetails });
|
|
26
23
|
}
|
|
27
24
|
};
|
|
28
25
|
}
|
|
29
26
|
}
|
|
27
|
+
exports.JoiVdtor_ = JoiVdtor_;
|
|
28
|
+
const Joi = new JoiVdtor_;
|
|
30
29
|
exports.Joi = Joi;
|
|
31
|
-
const newJoi = new Joi();
|
|
32
|
-
exports.default = newJoi;
|
package/dist/public-api.d.ts
CHANGED
package/dist/public-api.js
CHANGED
|
@@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const sample_service_1 = __importDefault(require("../services/sample.service"));
|
|
7
|
-
const validator_midware_1 =
|
|
7
|
+
const validator_midware_1 = require("../middlewares/validator.midware");
|
|
8
8
|
const sample_validator_1 = __importDefault(require("../validations/sample.validator"));
|
|
9
9
|
class SampleRoute {
|
|
10
10
|
loadRoutes(prefix, router) {
|
|
@@ -20,7 +20,7 @@ class SampleRoute {
|
|
|
20
20
|
this.deleteById(prefix, router);
|
|
21
21
|
}
|
|
22
22
|
create(prefix, router) {
|
|
23
|
-
router.post(`${prefix}`, validator_midware_1.
|
|
23
|
+
router.post(`${prefix}`, validator_midware_1.Joi.vdtor(sample_validator_1.default.create), sample_service_1.default.create);
|
|
24
24
|
}
|
|
25
25
|
getAll(prefix, router) {
|
|
26
26
|
router.get(`${prefix}`, sample_service_1.default.getAll);
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { Status } from '../interfaces/status.interface';
|
|
2
2
|
import { ServiceRespI } from "../interfaces/interface";
|
|
3
3
|
export declare class RootService {
|
|
4
|
+
SUCCESS: Status;
|
|
4
5
|
sendResponse: (serviceResponse: ServiceRespI) => any;
|
|
5
6
|
get_error(error: any): {
|
|
6
7
|
status: Status;
|
|
7
8
|
message: string;
|
|
8
9
|
data: any;
|
|
9
10
|
};
|
|
11
|
+
private sanitize_service_resp;
|
|
12
|
+
private get_response;
|
|
10
13
|
private getHttpStatus;
|
|
11
|
-
private get_error_;
|
|
12
14
|
}
|
|
@@ -1,42 +1,111 @@
|
|
|
1
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 () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
2
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
39
|
exports.RootService = void 0;
|
|
4
40
|
const logger_util_1 = require("../utilities/logger.util");
|
|
5
41
|
const status_interface_1 = require("../interfaces/status.interface");
|
|
42
|
+
const airbrake_1 = require("../app-middlewares/airbrake");
|
|
43
|
+
const log_util_1 = require("../utilities/log.util");
|
|
44
|
+
const env_1 = __importDefault(require("../env"));
|
|
45
|
+
const mongoose = __importStar(require("mongoose"));
|
|
6
46
|
class RootService {
|
|
7
47
|
constructor() {
|
|
48
|
+
this.SUCCESS = status_interface_1.Status.SUCCESS;
|
|
8
49
|
this.sendResponse = (serviceResponse) => {
|
|
9
|
-
let { res,
|
|
50
|
+
let { res, data, actionType } = serviceResponse;
|
|
10
51
|
try {
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
const response = { status, data, message, code };
|
|
14
|
-
if (error) {
|
|
15
|
-
response.error = this.get_error_(error);
|
|
16
|
-
}
|
|
17
|
-
const status_code = this.getHttpStatus(status);
|
|
52
|
+
const log_service_resp = this.sanitize_service_resp(serviceResponse);
|
|
53
|
+
const { response, status_code } = this.get_response(serviceResponse);
|
|
18
54
|
res.status(status_code).json(response);
|
|
19
55
|
if (status_code >= 400) {
|
|
20
56
|
const dataErr = data ? JSON.stringify(data) : data;
|
|
21
57
|
const error = `[${actionType || 'App Error'}] ${response.message} ${dataErr}`;
|
|
22
58
|
logger_util_1.winston.error(error);
|
|
23
59
|
}
|
|
60
|
+
log_util_1.LogUtil.user_event({ serviceResponse: log_service_resp });
|
|
24
61
|
}
|
|
25
62
|
catch (error) {
|
|
26
|
-
|
|
63
|
+
logger_util_1.winston.error(error);
|
|
64
|
+
(0, airbrake_1.airbrake_request_logger)({ serviceResponse });
|
|
65
|
+
res.status(500).json({ status: 'ERROR', data: error, message: error?.message });
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
this.sanitize_service_resp = (serviceResponse) => {
|
|
69
|
+
const { user } = serviceResponse.req;
|
|
70
|
+
let user_;
|
|
71
|
+
if (user) {
|
|
72
|
+
user_ = {
|
|
73
|
+
email: user.email,
|
|
74
|
+
user_name: user.user_name,
|
|
75
|
+
};
|
|
27
76
|
}
|
|
77
|
+
const request_data = log_util_1.LogUtil.get_request(serviceResponse.req);
|
|
78
|
+
const request = { ...request_data, user: user_, };
|
|
79
|
+
serviceResponse.request = request;
|
|
80
|
+
serviceResponse.NODE_ENV = env_1.default.NODE_ENV;
|
|
81
|
+
return serviceResponse;
|
|
28
82
|
};
|
|
29
|
-
this.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
83
|
+
this.get_response = (serviceResponse) => {
|
|
84
|
+
try {
|
|
85
|
+
let { status, data, message, error, code: code_ } = serviceResponse;
|
|
86
|
+
status = status || status_interface_1.Status.ERROR;
|
|
87
|
+
const code = error && error.code ? error.code : (code_ || null);
|
|
88
|
+
const status_code = this.getHttpStatus(status);
|
|
89
|
+
let response = { status, code, message, data };
|
|
90
|
+
return { response, status_code };
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
logger_util_1.winston.error(error);
|
|
94
|
+
(0, airbrake_1.airbrake_request_logger)({ serviceResponse });
|
|
95
|
+
throw {
|
|
96
|
+
message: 'Error occured try again later',
|
|
97
|
+
error
|
|
98
|
+
};
|
|
99
|
+
}
|
|
33
100
|
};
|
|
34
101
|
}
|
|
35
102
|
get_error(error) {
|
|
36
103
|
let response = { status: status_interface_1.Status.ERROR, message: 'Request failed', data: null };
|
|
37
104
|
const { status, message, data } = error;
|
|
38
105
|
response.status = status ? status : response.status;
|
|
39
|
-
|
|
106
|
+
if (!(error instanceof mongoose.Error)) {
|
|
107
|
+
response.message = message ? message : response.message;
|
|
108
|
+
}
|
|
40
109
|
response.data = data ? data : response.data;
|
|
41
110
|
return response;
|
|
42
111
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './_root.service';
|
|
@@ -0,0 +1,17 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./_root.service"), exports);
|
|
@@ -3,16 +3,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.VasrefilApiRequest = void 0;
|
|
6
|
+
exports.LogApiRequest = exports.VasrefilApiRequest = void 0;
|
|
7
7
|
const axios_1 = __importDefault(require("axios"));
|
|
8
8
|
const env_1 = __importDefault(require("../env"));
|
|
9
9
|
const helpers_1 = require("../helpers");
|
|
10
|
-
const
|
|
11
|
-
const BASEURL = env_1.default.VASREFIL.BASEURL;
|
|
12
|
-
const VasrefilApiRequest = async (request) => {
|
|
10
|
+
const ApiRequest = async (request, option) => {
|
|
13
11
|
try {
|
|
14
|
-
|
|
15
|
-
const headers = { 'api-key': API_KEY };
|
|
12
|
+
const baseurl = request.baseurl || option.BASEURL;
|
|
13
|
+
const headers = { 'api-key': option.API_KEY };
|
|
16
14
|
const { data } = await axios_1.default.request({
|
|
17
15
|
method: request.method,
|
|
18
16
|
url: `${baseurl}/${request.endpoint}`,
|
|
@@ -22,6 +20,17 @@ const VasrefilApiRequest = async (request) => {
|
|
|
22
20
|
});
|
|
23
21
|
return data;
|
|
24
22
|
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
throw error;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
const VasrefilApiRequest = async (request) => {
|
|
28
|
+
try {
|
|
29
|
+
const API_KEY = env_1.default.VASREFIL.API_KEY;
|
|
30
|
+
const BASEURL = env_1.default.VASREFIL.BASEURL;
|
|
31
|
+
const data = await ApiRequest(request, { BASEURL, API_KEY });
|
|
32
|
+
return data;
|
|
33
|
+
}
|
|
25
34
|
catch (error) {
|
|
26
35
|
throw {
|
|
27
36
|
error: helpers_1.RequestHelper.get_error(error),
|
|
@@ -30,3 +39,18 @@ const VasrefilApiRequest = async (request) => {
|
|
|
30
39
|
}
|
|
31
40
|
};
|
|
32
41
|
exports.VasrefilApiRequest = VasrefilApiRequest;
|
|
42
|
+
const LogApiRequest = async (request) => {
|
|
43
|
+
try {
|
|
44
|
+
const API_KEY = env_1.default.LOG.API_KEY;
|
|
45
|
+
const BASEURL = env_1.default.LOG.BASEURL;
|
|
46
|
+
const data = await ApiRequest(request, { BASEURL, API_KEY });
|
|
47
|
+
return data;
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
throw {
|
|
51
|
+
error: helpers_1.RequestHelper.get_error(error),
|
|
52
|
+
admin_message: 'Vasrefil-Log-API: send api request failed'
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
exports.LogApiRequest = LogApiRequest;
|
package/dist/utilities/index.js
CHANGED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { ServiceRespI } from '../interfaces/interface';
|
|
2
|
+
import { UserRequestI } from '../interfaces/user.interface';
|
|
3
|
+
declare class LogUtil_ {
|
|
4
|
+
user_event: (dto: {
|
|
5
|
+
serviceResponse: ServiceRespI;
|
|
6
|
+
}) => Promise<void>;
|
|
7
|
+
get_request: (req: UserRequestI) => {
|
|
8
|
+
params: import("express-serve-static-core").ParamsDictionary;
|
|
9
|
+
query: import("qs").ParsedQs;
|
|
10
|
+
headers: {
|
|
11
|
+
[key: string]: string | string[] | undefined;
|
|
12
|
+
accept?: string | undefined;
|
|
13
|
+
"accept-language"?: string | undefined;
|
|
14
|
+
"accept-patch"?: string | undefined;
|
|
15
|
+
"accept-ranges"?: string | undefined;
|
|
16
|
+
"access-control-allow-credentials"?: string | undefined;
|
|
17
|
+
"access-control-allow-headers"?: string | undefined;
|
|
18
|
+
"access-control-allow-methods"?: string | undefined;
|
|
19
|
+
"access-control-allow-origin"?: string | undefined;
|
|
20
|
+
"access-control-expose-headers"?: string | undefined;
|
|
21
|
+
"access-control-max-age"?: string | undefined;
|
|
22
|
+
"access-control-request-headers"?: string | undefined;
|
|
23
|
+
"access-control-request-method"?: string | undefined;
|
|
24
|
+
age?: string | undefined;
|
|
25
|
+
allow?: string | undefined;
|
|
26
|
+
"alt-svc"?: string | undefined;
|
|
27
|
+
"cache-control"?: string | undefined;
|
|
28
|
+
connection?: string | undefined;
|
|
29
|
+
"content-disposition"?: string | undefined;
|
|
30
|
+
"content-encoding"?: string | undefined;
|
|
31
|
+
"content-language"?: string | undefined;
|
|
32
|
+
"content-length"?: string | undefined;
|
|
33
|
+
"content-location"?: string | undefined;
|
|
34
|
+
"content-range"?: string | undefined;
|
|
35
|
+
"content-type"?: string | undefined;
|
|
36
|
+
cookie?: string | undefined;
|
|
37
|
+
date?: string | undefined;
|
|
38
|
+
etag?: string | undefined;
|
|
39
|
+
expect?: string | undefined;
|
|
40
|
+
expires?: string | undefined;
|
|
41
|
+
forwarded?: string | undefined;
|
|
42
|
+
from?: string | undefined;
|
|
43
|
+
host?: string | undefined;
|
|
44
|
+
"if-match"?: string | undefined;
|
|
45
|
+
"if-modified-since"?: string | undefined;
|
|
46
|
+
"if-none-match"?: string | undefined;
|
|
47
|
+
"if-unmodified-since"?: string | undefined;
|
|
48
|
+
"last-modified"?: string | undefined;
|
|
49
|
+
location?: string | undefined;
|
|
50
|
+
origin?: string | undefined;
|
|
51
|
+
pragma?: string | undefined;
|
|
52
|
+
"proxy-authenticate"?: string | undefined;
|
|
53
|
+
"proxy-authorization"?: string | undefined;
|
|
54
|
+
"public-key-pins"?: string | undefined;
|
|
55
|
+
range?: string | undefined;
|
|
56
|
+
referer?: string | undefined;
|
|
57
|
+
"retry-after"?: string | undefined;
|
|
58
|
+
"sec-websocket-accept"?: string | undefined;
|
|
59
|
+
"sec-websocket-extensions"?: string | undefined;
|
|
60
|
+
"sec-websocket-key"?: string | undefined;
|
|
61
|
+
"sec-websocket-protocol"?: string | undefined;
|
|
62
|
+
"sec-websocket-version"?: string | undefined;
|
|
63
|
+
"set-cookie"?: string[] | undefined;
|
|
64
|
+
"strict-transport-security"?: string | undefined;
|
|
65
|
+
tk?: string | undefined;
|
|
66
|
+
trailer?: string | undefined;
|
|
67
|
+
"transfer-encoding"?: string | undefined;
|
|
68
|
+
upgrade?: string | undefined;
|
|
69
|
+
"user-agent"?: string | undefined;
|
|
70
|
+
vary?: string | undefined;
|
|
71
|
+
via?: string | undefined;
|
|
72
|
+
warning?: string | undefined;
|
|
73
|
+
"www-authenticate"?: string | undefined;
|
|
74
|
+
};
|
|
75
|
+
method: string;
|
|
76
|
+
url: string;
|
|
77
|
+
statusCode: number | undefined;
|
|
78
|
+
body: any;
|
|
79
|
+
};
|
|
80
|
+
private get_user_event_response;
|
|
81
|
+
private get_user;
|
|
82
|
+
private create_activity_log;
|
|
83
|
+
}
|
|
84
|
+
declare const LogUtil: LogUtil_;
|
|
85
|
+
export { LogUtil };
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LogUtil = void 0;
|
|
4
|
+
const airbrake_1 = require("../app-middlewares/airbrake");
|
|
5
|
+
const api_request_util_1 = require("./api-request.util");
|
|
6
|
+
const json_stringify_safe = require('json-stringify-safe');
|
|
7
|
+
class LogUtil_ {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.user_event = async (dto) => {
|
|
10
|
+
const { request, actionType, status } = dto.serviceResponse;
|
|
11
|
+
try {
|
|
12
|
+
const statuses = ['SUCCESS', 'CREATED', 'PROCESSING'];
|
|
13
|
+
if (!statuses.includes(status)) {
|
|
14
|
+
const user = this.get_user(dto.serviceResponse);
|
|
15
|
+
const response = this.get_user_event_response(dto.serviceResponse);
|
|
16
|
+
const payload = {
|
|
17
|
+
distinct_id: user.distinct_id,
|
|
18
|
+
request: JSON.stringify(request),
|
|
19
|
+
response: JSON.stringify(response),
|
|
20
|
+
request_url: request?.url,
|
|
21
|
+
event: actionType,
|
|
22
|
+
type: 'USER_EVENT',
|
|
23
|
+
response_status: status,
|
|
24
|
+
};
|
|
25
|
+
await this.create_activity_log(payload);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
airbrake_1.airbrake.notify({
|
|
30
|
+
error: JSON.stringify(error),
|
|
31
|
+
context: { component: 'USER_EVENT' },
|
|
32
|
+
params: { actionType, status, request },
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
this.get_request = (req) => {
|
|
37
|
+
const { authorization, pin_token, ...headers_ } = req.headers;
|
|
38
|
+
const { password, pin, old_pin, new_pin, bvn, nin, file, ...body_ } = req.body;
|
|
39
|
+
const { params, query, method, url, statusCode } = req;
|
|
40
|
+
delete headers_['pin-token'];
|
|
41
|
+
const request = {
|
|
42
|
+
params, query, headers: headers_, method, url, statusCode,
|
|
43
|
+
body: body_,
|
|
44
|
+
};
|
|
45
|
+
return request;
|
|
46
|
+
};
|
|
47
|
+
this.get_user_event_response = (serviceResponse) => {
|
|
48
|
+
const { data, message, error } = serviceResponse;
|
|
49
|
+
let response = {};
|
|
50
|
+
if (data || message || error) {
|
|
51
|
+
if (error) {
|
|
52
|
+
delete error.req;
|
|
53
|
+
}
|
|
54
|
+
const data_ = data ? json_stringify_safe(data) : null;
|
|
55
|
+
const error_ = error ? json_stringify_safe(error, null) : null;
|
|
56
|
+
const message_ = message ? message : null;
|
|
57
|
+
response = {
|
|
58
|
+
message: message_, error: error_
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
return response;
|
|
62
|
+
};
|
|
63
|
+
this.get_user = (serviceResponse) => {
|
|
64
|
+
const { req, actionType } = serviceResponse;
|
|
65
|
+
let distinct_id = req.user ? req.user.email : (req.body.email || '--');
|
|
66
|
+
// if(this.cronjobs_events.includes(actionType)) {
|
|
67
|
+
// distinct_id = 'cron-job.org';
|
|
68
|
+
// }
|
|
69
|
+
if (actionType.toLowerCase().includes('webhook')) {
|
|
70
|
+
distinct_id = actionType.toLowerCase();
|
|
71
|
+
}
|
|
72
|
+
if (req.admin_user) {
|
|
73
|
+
distinct_id = req.admin_user.email;
|
|
74
|
+
}
|
|
75
|
+
return { distinct_id };
|
|
76
|
+
};
|
|
77
|
+
this.create_activity_log = async (body) => {
|
|
78
|
+
try {
|
|
79
|
+
const resp = await (0, api_request_util_1.LogApiRequest)({
|
|
80
|
+
method: 'POST',
|
|
81
|
+
endpoint: 'activities',
|
|
82
|
+
body
|
|
83
|
+
});
|
|
84
|
+
return resp;
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const LogUtil = new LogUtil_;
|
|
93
|
+
exports.LogUtil = LogUtil;
|