add root id for log
This commit is contained in:
parent
aa5e69776e
commit
650cd842af
5 changed files with 124 additions and 7 deletions
|
|
@ -4,7 +4,7 @@ import { addLogSequence } from "./utils";
|
|||
|
||||
class CallAPI {
|
||||
//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 url = process.env.API_URL + path;
|
||||
try {
|
||||
|
|
@ -15,7 +15,7 @@ class CallAPI {
|
|||
api_key: process.env.API_KEY,
|
||||
},
|
||||
});
|
||||
addLogSequence(request, {
|
||||
if(log) addLogSequence(request, {
|
||||
action: "request",
|
||||
status: "success",
|
||||
description: "connected",
|
||||
|
|
@ -27,7 +27,7 @@ class CallAPI {
|
|||
});
|
||||
return response.data.result;
|
||||
} catch (error) {
|
||||
addLogSequence(request, {
|
||||
if(log) addLogSequence(request, {
|
||||
action: "request",
|
||||
status: "error",
|
||||
description: "unconnected",
|
||||
|
|
|
|||
|
|
@ -3,8 +3,11 @@ import { RequestWithUser } from "../middlewares/user";
|
|||
import CallAPI from "./call-api";
|
||||
import HttpError from "./http-error";
|
||||
import HttpStatus from "./http-status";
|
||||
import { promisify } from "util";
|
||||
|
||||
class CheckAuth {
|
||||
private redis = require("redis");
|
||||
|
||||
public async Permission(req: RequestWithUser, system: string, action: string) {
|
||||
if (
|
||||
req.headers.hasOwnProperty("api_key") &&
|
||||
|
|
@ -180,6 +183,62 @@ class CheckAuth {
|
|||
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) {
|
||||
return await this.Permission(req, system, "CREATE");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { NextFunction, Request, Response } from "express";
|
||||
import { Client } from "@elastic/elasticsearch";
|
||||
import permission from "../interfaces/permission";
|
||||
|
||||
if (!process.env.ELASTICSEARCH_INDEX) {
|
||||
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) {
|
||||
if (!req.url.startsWith("/api/")) return next();
|
||||
|
||||
let data: any;
|
||||
let data: any;
|
||||
|
||||
const originalJson = res.json;
|
||||
|
||||
|
|
@ -36,7 +37,7 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) {
|
|||
|
||||
req.app.locals.logData = {};
|
||||
|
||||
res.on("finish", () => {
|
||||
res.on("finish", async() => {
|
||||
if (!req.url.startsWith("/api/")) return;
|
||||
|
||||
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 === 2 && res.statusCode < 400) 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 = {
|
||||
logType: res.statusCode >= 500 ? "error" : res.statusCode >= 400 ? "warning" : "info",
|
||||
ip: req.ip,
|
||||
rootId: rootId?rootId.orgRootId:null,
|
||||
systemName: "development",
|
||||
startTimeStamp: timestamp,
|
||||
endTimeStamp: new Date().toISOString(),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue