add root id for log
This commit is contained in:
parent
aa5e69776e
commit
650cd842af
5 changed files with 124 additions and 7 deletions
51
package-lock.json
generated
51
package-lock.json
generated
|
|
@ -21,6 +21,7 @@
|
||||||
"mysql2": "^3.9.1",
|
"mysql2": "^3.9.1",
|
||||||
"node-cron": "^3.0.3",
|
"node-cron": "^3.0.3",
|
||||||
"promise.any": "^2.0.6",
|
"promise.any": "^2.0.6",
|
||||||
|
"redis": "~3.1.2",
|
||||||
"reflect-metadata": "^0.2.1",
|
"reflect-metadata": "^0.2.1",
|
||||||
"swagger-ui-express": "^5.0.0",
|
"swagger-ui-express": "^5.0.0",
|
||||||
"tsoa": "^6.0.1",
|
"tsoa": "^6.0.1",
|
||||||
|
|
@ -3609,6 +3610,56 @@
|
||||||
"node": ">=8.10.0"
|
"node": ">=8.10.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/redis": {
|
||||||
|
"version": "3.1.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/redis/-/redis-3.1.2.tgz",
|
||||||
|
"integrity": "sha512-grn5KoZLr/qrRQVwoSkmzdbw6pwF+/rwODtrOr6vuBRiR/f3rjSTGupbF90Zpqm2oenix8Do6RV7pYEkGwlKkw==",
|
||||||
|
"dependencies": {
|
||||||
|
"denque": "^1.5.0",
|
||||||
|
"redis-commands": "^1.7.0",
|
||||||
|
"redis-errors": "^1.2.0",
|
||||||
|
"redis-parser": "^3.0.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=10"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"type": "opencollective",
|
||||||
|
"url": "https://opencollective.com/node-redis"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/redis-commands": {
|
||||||
|
"version": "1.7.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.7.0.tgz",
|
||||||
|
"integrity": "sha512-nJWqw3bTFy21hX/CPKHth6sfhZbdiHP6bTawSgQBlKOVRG7EZkfHbbHwQJnrE4vsQf0CMNE+3gJ4Fmm16vdVlQ=="
|
||||||
|
},
|
||||||
|
"node_modules/redis-errors": {
|
||||||
|
"version": "1.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz",
|
||||||
|
"integrity": "sha512-1qny3OExCf0UvUV/5wpYKf2YwPcOqXzkwKKSmKHiE6ZMQs5heeE/c8eXK+PNllPvmjgAbfnsbpkGZWy8cBpn9w==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=4"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/redis-parser": {
|
||||||
|
"version": "3.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz",
|
||||||
|
"integrity": "sha512-DJnGAeenTdpMEH6uAJRK/uiyEIH9WVsUmoLwzudwGJUwZPp80PDBWPHXSAGNPwNvIXAbe7MSUB1zQFugFml66A==",
|
||||||
|
"dependencies": {
|
||||||
|
"redis-errors": "^1.0.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=4"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/redis/node_modules/denque": {
|
||||||
|
"version": "1.5.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/denque/-/denque-1.5.1.tgz",
|
||||||
|
"integrity": "sha512-XwE+iZ4D6ZUB7mfYRMb5wByE8L74HCn30FBN7sWnXksWc1LO1bPDl67pBR9o/kC4z/xSNAwkMYcGgqDV3BE3Hw==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.10"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/reflect-metadata": {
|
"node_modules/reflect-metadata": {
|
||||||
"version": "0.2.1",
|
"version": "0.2.1",
|
||||||
"resolved": "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.2.1.tgz",
|
"resolved": "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.2.1.tgz",
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,7 @@
|
||||||
"tsoa": "^6.0.1",
|
"tsoa": "^6.0.1",
|
||||||
"typeorm": "^0.3.19",
|
"typeorm": "^0.3.19",
|
||||||
"typeorm-cli": "^1.0.7",
|
"typeorm-cli": "^1.0.7",
|
||||||
"xlsx": "^0.18.5"
|
"xlsx": "^0.18.5",
|
||||||
|
"redis": "~3.1.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ import { addLogSequence } from "./utils";
|
||||||
|
|
||||||
class CallAPI {
|
class CallAPI {
|
||||||
//Get
|
//Get
|
||||||
public async GetData(request: any, @Path() path: any) {
|
public async GetData(request: any, @Path() path: any, log = true) {
|
||||||
const token = "Bearer " + request.headers.authorization.replace("Bearer ", "");
|
const token = "Bearer " + request.headers.authorization.replace("Bearer ", "");
|
||||||
const url = process.env.API_URL + path;
|
const url = process.env.API_URL + path;
|
||||||
try {
|
try {
|
||||||
|
|
@ -15,7 +15,7 @@ class CallAPI {
|
||||||
api_key: process.env.API_KEY,
|
api_key: process.env.API_KEY,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
addLogSequence(request, {
|
if(log) addLogSequence(request, {
|
||||||
action: "request",
|
action: "request",
|
||||||
status: "success",
|
status: "success",
|
||||||
description: "connected",
|
description: "connected",
|
||||||
|
|
@ -27,7 +27,7 @@ class CallAPI {
|
||||||
});
|
});
|
||||||
return response.data.result;
|
return response.data.result;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
addLogSequence(request, {
|
if(log) addLogSequence(request, {
|
||||||
action: "request",
|
action: "request",
|
||||||
status: "error",
|
status: "error",
|
||||||
description: "unconnected",
|
description: "unconnected",
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,11 @@ import { RequestWithUser } from "../middlewares/user";
|
||||||
import CallAPI from "./call-api";
|
import CallAPI from "./call-api";
|
||||||
import HttpError from "./http-error";
|
import HttpError from "./http-error";
|
||||||
import HttpStatus from "./http-status";
|
import HttpStatus from "./http-status";
|
||||||
|
import { promisify } from "util";
|
||||||
|
|
||||||
class CheckAuth {
|
class CheckAuth {
|
||||||
|
private redis = require("redis");
|
||||||
|
|
||||||
public async Permission(req: RequestWithUser, system: string, action: string) {
|
public async Permission(req: RequestWithUser, system: string, action: string) {
|
||||||
if (
|
if (
|
||||||
req.headers.hasOwnProperty("api_key") &&
|
req.headers.hasOwnProperty("api_key") &&
|
||||||
|
|
@ -180,6 +183,62 @@ class CheckAuth {
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
public async checkOrg(token: any,keycloakId: string) {
|
||||||
|
const redisClient = await this.redis.createClient({
|
||||||
|
host: process.env.REDIS_HOST,
|
||||||
|
port: process.env.REDIS_PORT,
|
||||||
|
});
|
||||||
|
const getAsync = promisify(redisClient.get).bind(redisClient);
|
||||||
|
let reply = await getAsync("org_" + keycloakId);
|
||||||
|
if (reply != null) {
|
||||||
|
reply = JSON.parse(reply);
|
||||||
|
} else {
|
||||||
|
// await new CallAPI()
|
||||||
|
// .GetData(
|
||||||
|
// {
|
||||||
|
// headers: { authorization: token },
|
||||||
|
// }, `/org/permission/checkOrg/${keycloakId}`,false)
|
||||||
|
// .then((x) => {
|
||||||
|
// console.log("[In Then]");
|
||||||
|
// console.log("[res]",x);
|
||||||
|
// let data: any = {
|
||||||
|
// orgRootId: x.orgRootId,
|
||||||
|
// orgChild1Id: x.orgChild1Id,
|
||||||
|
// orgChild2Id: x.orgChild2Id,
|
||||||
|
// orgChild3Id: x.orgChild3Id,
|
||||||
|
// orgChild4Id: x.orgChild4Id,
|
||||||
|
// };
|
||||||
|
// console.log("[data]",data);
|
||||||
|
// return data;
|
||||||
|
// },
|
||||||
|
// )
|
||||||
|
// .catch((error) => {
|
||||||
|
// console.error("Error calling API:", error);
|
||||||
|
// });
|
||||||
|
try {
|
||||||
|
const x = await new CallAPI().GetData(
|
||||||
|
{
|
||||||
|
headers: { authorization: token },
|
||||||
|
},
|
||||||
|
`/org/permission/checkOrg/${keycloakId}`,
|
||||||
|
false
|
||||||
|
);
|
||||||
|
|
||||||
|
const data = {
|
||||||
|
orgRootId: x.orgRootId,
|
||||||
|
orgChild1Id: x.orgChild1Id,
|
||||||
|
orgChild2Id: x.orgChild2Id,
|
||||||
|
orgChild3Id: x.orgChild3Id,
|
||||||
|
orgChild4Id: x.orgChild4Id,
|
||||||
|
};
|
||||||
|
|
||||||
|
return data;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error calling API:", error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
public async PermissionCreate(req: RequestWithUser, system: string) {
|
public async PermissionCreate(req: RequestWithUser, system: string) {
|
||||||
return await this.Permission(req, system, "CREATE");
|
return await this.Permission(req, system, "CREATE");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import { NextFunction, Request, Response } from "express";
|
import { NextFunction, Request, Response } from "express";
|
||||||
import { Client } from "@elastic/elasticsearch";
|
import { Client } from "@elastic/elasticsearch";
|
||||||
|
import permission from "../interfaces/permission";
|
||||||
|
|
||||||
if (!process.env.ELASTICSEARCH_INDEX) {
|
if (!process.env.ELASTICSEARCH_INDEX) {
|
||||||
throw new Error("Require ELASTICSEARCH_INDEX to store log.");
|
throw new Error("Require ELASTICSEARCH_INDEX to store log.");
|
||||||
|
|
@ -22,7 +23,7 @@ const elasticsearch = new Client({
|
||||||
async function logMiddleware(req: Request, res: Response, next: NextFunction) {
|
async function logMiddleware(req: Request, res: Response, next: NextFunction) {
|
||||||
if (!req.url.startsWith("/api/")) return next();
|
if (!req.url.startsWith("/api/")) return next();
|
||||||
|
|
||||||
let data: any;
|
let data: any;
|
||||||
|
|
||||||
const originalJson = res.json;
|
const originalJson = res.json;
|
||||||
|
|
||||||
|
|
@ -36,7 +37,7 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) {
|
||||||
|
|
||||||
req.app.locals.logData = {};
|
req.app.locals.logData = {};
|
||||||
|
|
||||||
res.on("finish", () => {
|
res.on("finish", async() => {
|
||||||
if (!req.url.startsWith("/api/")) return;
|
if (!req.url.startsWith("/api/")) return;
|
||||||
|
|
||||||
const level = LOG_LEVEL_MAP[process.env.LOG_LEVEL ?? "debug"] || 4;
|
const level = LOG_LEVEL_MAP[process.env.LOG_LEVEL ?? "debug"] || 4;
|
||||||
|
|
@ -44,10 +45,15 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) {
|
||||||
if (level === 1 && res.statusCode < 500) return;
|
if (level === 1 && res.statusCode < 500) return;
|
||||||
if (level === 2 && res.statusCode < 400) return;
|
if (level === 2 && res.statusCode < 400) return;
|
||||||
if (level === 3 && res.statusCode < 200) return;
|
if (level === 3 && res.statusCode < 200) return;
|
||||||
|
let token: any;
|
||||||
|
token = req.headers['authorization']
|
||||||
|
|
||||||
|
const rootId = await new permission().checkOrg(token,req.app.locals.logData.userId);
|
||||||
|
|
||||||
const obj = {
|
const obj = {
|
||||||
logType: res.statusCode >= 500 ? "error" : res.statusCode >= 400 ? "warning" : "info",
|
logType: res.statusCode >= 500 ? "error" : res.statusCode >= 400 ? "warning" : "info",
|
||||||
ip: req.ip,
|
ip: req.ip,
|
||||||
|
rootId: rootId?rootId.orgRootId:null,
|
||||||
systemName: "development",
|
systemName: "development",
|
||||||
startTimeStamp: timestamp,
|
startTimeStamp: timestamp,
|
||||||
endTimeStamp: new Date().toISOString(),
|
endTimeStamp: new Date().toISOString(),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue