From d19fa67cf21ac0eed43b2ec69359c49355744ddf Mon Sep 17 00:00:00 2001 From: Adisak Date: Wed, 28 Jan 2026 18:05:56 +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 11abdcb..c7a9433 100644 --- a/src/interfaces/permission.ts +++ b/src/interfaces/permission.ts @@ -250,6 +250,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 416a03c..41d77ff 100644 --- a/src/middlewares/logs.ts +++ b/src/middlewares/logs.ts @@ -49,7 +49,7 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) { let rootId = null try { - rootId = token ? await new permission().checkOrg(token, req.app.locals.logData.userId) : null + rootId = token ? await new permission().checkRootDna(token, req.app.locals.logData.userId) : null } catch (err) { console.warn("Error fetching rootId:", err) } @@ -57,7 +57,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: "probation", startTimeStamp: timestamp, endTimeStamp: new Date().toISOString(),