From 81dc6fee572a52dc00617a3a1ac878c7e5b94633 Mon Sep 17 00:00:00 2001 From: Adisak Date: Thu, 5 Feb 2026 13:13:00 +0700 Subject: [PATCH] #2271 --- .../KpiUserEvaluationController.ts | 252 ++++++++++++++---- src/interfaces/permission.ts | 15 ++ src/interfaces/utils.ts | 47 ++++ 3 files changed, 257 insertions(+), 57 deletions(-) diff --git a/src/controllers/KpiUserEvaluationController.ts b/src/controllers/KpiUserEvaluationController.ts index 2284d03..fe82f5f 100644 --- a/src/controllers/KpiUserEvaluationController.ts +++ b/src/controllers/KpiUserEvaluationController.ts @@ -37,7 +37,7 @@ import { Position } from "../entities/position"; import { KpiLink } from "../entities/kpiLink"; import { RequestWithUser } from "../middlewares/user"; import permission from "../interfaces/permission"; -import { setLogDataDiff } from "../interfaces/utils"; +import { resolveNodeId, resolveNodeLevel, setLogDataDiff } from "../interfaces/utils"; import { KpiUserRejectAgreement } from "../entities/kpiUserRejectAgreement"; import { KpiUserRejectResult } from "../entities/kpiUserRejectResult"; @@ -469,14 +469,198 @@ export class KpiUserEvaluationController extends Controller { }, ) { let _data = await new permission().PermissionOrgList(request, "SYS_KPI_LIST"); - await new CallAPI() - .PostData(request, "/org/finddna", _data) - .then((x) => { - _data = x; - }) - .catch((x) => { }); + const orgDna = await new permission().checkDna(request, request.user.sub) + let conditionFullName = "CONCAT(kpiUserEvaluation.prefix, kpiUserEvaluation.firstName, ' ', kpiUserEvaluation.lastName) LIKE :keyword"; + + let typeCondition: { + query?: string; + params?: any; + } = {}; + let level = resolveNodeLevel(orgDna); + let nodeId = resolveNodeId(orgDna); + let conditions: string[] = []; + let params: any = {}; + + if (_data.privilege === "CHILD" || _data.privilege === "PARENT" || _data.privilege === "BROTHER") { + if (_data.privilege === "CHILD") { + + if (level === 0 && orgDna.rootDnaId) { + conditions.push("kpiUserEvaluation.orgDnaId = :root"); + params.root = orgDna.rootDnaId; + } + + if (level != null && level >= 1 && orgDna.child1DnaId) { + conditions.push("kpiUserEvaluation.child1DnaId = :child1"); + params.child1 = orgDna.child1DnaId; + } + + if (level != null && level >= 2 && orgDna.child2DnaId) { + conditions.push("kpiUserEvaluation.child2DnaId = :child2"); + params.child2 = orgDna.child2DnaId; + } + + if (level != null && level >= 3 && orgDna.child3DnaId) { + conditions.push("kpiUserEvaluation.child3DnaId = :child3"); + params.child3 = orgDna.child3DnaId; + } + + if (level != null && level >= 4 && orgDna.child4DnaId) { + conditions.push("kpiUserEvaluation.child4DnaId = :child4"); + params.child4 = orgDna.child4DnaId; + } + + if (conditions.length > 0) { + typeCondition = { + query: conditions.join(" AND "), + params, + }; + } + } else if (_data.privilege === "BROTHER") { + const parentLevel = level !== null ? level - 1 : null; + if (parentLevel != null && parentLevel === 0 && orgDna.rootDnaId) { + conditions.push("kpiUserEvaluation.orgDnaId = :root"); + params.root = orgDna.rootDnaId; + } + + if (parentLevel != null && parentLevel >= 1 && orgDna.child1DnaId) { + conditions.push("kpiUserEvaluation.child1DnaId = :child1"); + params.child1 = orgDna.child1DnaId; + } + + if (parentLevel != null && parentLevel >= 2 && orgDna.child2DnaId) { + conditions.push("kpiUserEvaluation.child2DnaId = :child2"); + params.child2 = orgDna.child2DnaId; + } + + if (parentLevel != null && parentLevel >= 3 && orgDna.child3DnaId) { + conditions.push("kpiUserEvaluation.child3DnaId = :child3"); + params.child3 = orgDna.child3DnaId; + } + + if (conditions.length > 0) { + typeCondition = { + query: conditions.join(" AND "), + params, + }; + } + } else if (_data.privilege === "PARENT") { + if (level === 0) { + if (orgDna.rootDnaId) { + conditions.push("kpiUserEvaluation.orgDnaId = :root"); + params.root = orgDna.rootDnaId; + } + } else if (level === 1) { + if (orgDna.rootDnaId) { + conditions.push("kpiUserEvaluation.orgDnaId = :root AND kpiUserEvaluation.child1DnaId IS NOT NULL"); + params.root = orgDna.rootDnaId; + } + } else if (level === 2) { + conditions.push("kpiUserEvaluation.child1DnaId = :child1 AND kpiUserEvaluation.child2DnaId IS NOT NULL"); + params.child1 = orgDna.child1DnaId; + } else if (level === 3) { + conditions.push("kpiUserEvaluation.child2DnaId = :child2 AND kpiUserEvaluation.child3DnaId IS NOT NULL"); + params.child2 = orgDna.child2DnaId; + } else if (level === 4) { + conditions.push("kpiUserEvaluation.child3DnaId = :child3 AND kpiUserEvaluation.child4DnaId IS NOT NULL"); + params.child3 = orgDna.child3DnaId; + } + + if (conditions.length > 0) { + typeCondition = { + query: conditions.join(" AND "), + params, + }; + } + } + } else if (_data.privilege === "OWNER" || _data.privilege === "ROOT") { + if (orgDna.rootDnaId) { + conditions.push("kpiUserEvaluation.orgDnaId = :root"); + params.root = orgDna.rootDnaId; + } + + if (orgDna.child1DnaId) { + conditions.push("kpiUserEvaluation.child1DnaId = :child1"); + params.child1 = orgDna.child1DnaId; + } + + if (orgDna.child2DnaId) { + conditions.push("kpiUserEvaluation.child2DnaId = :child2"); + params.child2 = orgDna.child2DnaId; + } + + if (orgDna.child3DnaId) { + conditions.push("kpiUserEvaluation.child3DnaId = :child3"); + params.child3 = orgDna.child3DnaId; + } + + if (orgDna.child4DnaId) { + conditions.push("kpiUserEvaluation.child4DnaId = :child4"); + params.child4 = orgDna.child4DnaId; + } + + if (conditions.length > 0) { + typeCondition = { + query: conditions.join(" AND "), + params, + }; + } + } else if (_data.privilege === "NORMAL") { + if (level !== null && nodeId) { + switch (level) { + case 0: + typeCondition = { + query: ` + kpiUserEvaluation.orgDnaId = :nodeId + AND kpiUserEvaluation.child1DnaId IS NULL + `, + params: { nodeId }, + }; + break; + + case 1: + typeCondition = { + query: ` + kpiUserEvaluation.child1DnaId = :nodeId + AND kpiUserEvaluation.child2DnaId IS NULL + `, + params: { nodeId }, + }; + break; + + case 2: + typeCondition = { + query: ` + kpiUserEvaluation.child2DnaId = :nodeId + AND kpiUserEvaluation.child3DnaId IS NULL + `, + params: { nodeId }, + }; + break; + + case 3: + typeCondition = { + query: ` + kpiUserEvaluation.child3DnaId = :nodeId + AND kpiUserEvaluation.child4DnaId IS NULL + `, + params: { nodeId }, + }; + break; + + case 4: + typeCondition = { + query: ` + kpiUserEvaluation.child4DnaId = :nodeId + `, + params: { nodeId }, + }; + break; + } + } + } + let query = await AppDataSource.getRepository(KpiUserEvaluation) .createQueryBuilder("kpiUserEvaluation") .leftJoinAndSelect("kpiUserEvaluation.kpiPeriod", "kpiPeriod") @@ -539,56 +723,10 @@ export class KpiUserEvaluationController extends Controller { }); }), ) - .andWhere( - _data.root != undefined && _data.root != null - ? _data.root[0] != null - ? `kpiUserEvaluation.orgDnaId IN (:...root)` - : `kpiUserEvaluation.orgDnaId is null` - : "1=1", - { - root: _data.root, - }, - ) - .andWhere( - _data.child1 != undefined && _data.child1 != null - ? _data.child1[0] != null - ? `kpiUserEvaluation.child1DnaId IN (:...child1)` - : `kpiUserEvaluation.child1DnaId is ${_data.privilege == "PARENT" ? "not null" : "null"}` - : "1=1", - { - child1: _data.child1, - }, - ) - .andWhere( - _data.child2 != undefined && _data.child2 != null - ? _data.child2[0] != null - ? `kpiUserEvaluation.child2DnaId IN (:...child2)` - : `kpiUserEvaluation.child2DnaId is null` - : "1=1", - { - child2: _data.child2, - }, - ) - .andWhere( - _data.child3 != undefined && _data.child3 != null - ? _data.child3[0] != null - ? `kpiUserEvaluation.child3DnaId IN (:...child3)` - : `kpiUserEvaluation.child3DnaId is null` - : "1=1", - { - child3: _data.child3, - }, - ) - .andWhere( - _data.child4 != undefined && _data.child4 != null - ? _data.child4[0] != null - ? `kpiUserEvaluation.child4DnaId IN (:...child4)` - : `kpiUserEvaluation.child4DnaId is null` - : "1=1", - { - child4: _data.child4, - }, - ); + + if (typeCondition.query) { + query.andWhere(typeCondition.query, typeCondition.params); + } if (requestBody.sortBy) { if (requestBody.sortBy === "root") { diff --git a/src/interfaces/permission.ts b/src/interfaces/permission.ts index 64bb1a2..09c517a 100644 --- a/src/interfaces/permission.ts +++ b/src/interfaces/permission.ts @@ -259,6 +259,21 @@ class CheckAuth { } } + public async checkDna(request: RequestWithUser, keycloakId: any) { + try { + const result = await new CallAPI().GetData( + request, + `/org/finddna-by-keycloak/${keycloakId}`, + false + ); + + return result; + } catch (error) { + console.error("Error calling API:", error); + throw error; + } + } + public async checkRootDna(token: any, keycloakId: string) { const redisClient = await this.redis.createClient({ host: process.env.REDIS_HOST, diff --git a/src/interfaces/utils.ts b/src/interfaces/utils.ts index c667f54..d9ec2a2 100644 --- a/src/interfaces/utils.ts +++ b/src/interfaces/utils.ts @@ -45,3 +45,50 @@ export function addLogSequence(req: RequestWithUser, data: LogSequence) { export function editLogSequence(req: RequestWithUser, index: number, data: LogSequence) { req.app.locals.logData.sequence[index] = data; } + +export function resolveNodeLevel(data: any) { + if (data.child4DnaId) return 4; + if (data.child3DnaId) return 3; + if (data.child2DnaId) return 2; + if (data.child1DnaId) return 1; + if (data.rootDnaId) return 0; + return null; +} + + +export function resolveNodeId(data: any) { + return ( + data.child4DnaId ?? + data.child3DnaId ?? + data.child2DnaId ?? + data.child1DnaId ?? + data.rootDnaId ?? + null + ); +} + +export type OrgDnaPayload = { + root?: string[] | null; + child1?: string[] | null; + child2?: string[] | null; + child3?: string[] | null; + child4?: string[] | null; + [key: string]: any; +}; + +export function normalizeOrgDnaPayload(data: T): T { + const clone = { ...data }; + + (["root", "child1", "child2", "child3", "child4"] as const).forEach((key) => { + const value = clone[key]; + + if (Array.isArray(value) && value.every((x) => x == null)) { + clone[key] = null; + } + }); + + return clone; +} + + +