From ff1823dfb3c20540b3f0512c9e210abb0675a147 Mon Sep 17 00:00:00 2001 From: Adisak Date: Wed, 28 Jan 2026 17:57:54 +0700 Subject: [PATCH] add checkRootDna --- src/interfaces/permission.ts | 33 +++++++++++++++++++++++++++++++++ src/middlewares/logs.ts | 4 ++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/interfaces/permission.ts b/src/interfaces/permission.ts index a47c923..31e6550 100644 --- a/src/interfaces/permission.ts +++ b/src/interfaces/permission.ts @@ -236,6 +236,39 @@ class CheckAuth { throw error; } } + + public async checkRootDna(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); + try { + let reply = await getAsync("org_" + keycloakId); + if (reply != null) { + reply = JSON.parse(reply); + } else { + if (!keycloakId) throw new Error("No KeycloakId provided"); + const x = await new CallAPI().GetData( + { + headers: { authorization: token }, + }, + `/org/dotnet/user-logs/${keycloakId}`, + false, + ); + + const data = { + rootDnaId: x.rootDnaId, + }; + + 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"); } diff --git a/src/middlewares/logs.ts b/src/middlewares/logs.ts index fb2b6e2..7273708 100644 --- a/src/middlewares/logs.ts +++ b/src/middlewares/logs.ts @@ -50,7 +50,7 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) { try { rootId = token - ? await new permission().checkOrg(token, req.app.locals.logData.userId) + ? await new permission().checkRootDna(token, req.app.locals.logData.userId) : null; } catch (err) { console.warn("Error fetching rootId:", err); @@ -59,7 +59,7 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) { const obj = { logType: res.statusCode >= 500 ? "error" : res.statusCode >= 400 ? "warning" : "info", ip: req.ip, - rootId: rootId?.orgRootId ?? null, + rootId: rootId?.rootDnaId ?? null, systemName: "development", startTimeStamp: timestamp, endTimeStamp: new Date().toISOString(),