From 0725bce0ea55e698d3c0bbd54835731594d5fd0c Mon Sep 17 00:00:00 2001 From: Adisak Date: Thu, 5 Feb 2026 14:42:04 +0700 Subject: [PATCH] #2282 --- .../KpiUserEvaluationController.ts | 249 ++++++++++++++---- 1 file changed, 193 insertions(+), 56 deletions(-) diff --git a/src/controllers/KpiUserEvaluationController.ts b/src/controllers/KpiUserEvaluationController.ts index fe82f5f..d430507 100644 --- a/src/controllers/KpiUserEvaluationController.ts +++ b/src/controllers/KpiUserEvaluationController.ts @@ -249,64 +249,197 @@ export class KpiUserEvaluationController extends Controller { let conditionFullName = "CONCAT(kpiUserEvaluation.prefix, kpiUserEvaluation.firstName, ' ', kpiUserEvaluation.lastName) LIKE :keyword"; let _data = await new permission().PermissionOrgList(request, "SYS_RESULT"); - 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 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") - .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, - }, - ) .andWhere( requestBody.kpiPeriodId ? "kpiUserEvaluation.kpiPeriodId LIKE :kpiPeriodId" : "1=1", { @@ -388,6 +521,10 @@ export class KpiUserEvaluationController extends Controller { }), ); + if (typeCondition.query) { + query.andWhere(typeCondition.query, typeCondition.params); + } + if (requestBody.sortBy) { if (requestBody.sortBy === "root") { query = query.orderBy(`kpiUserEvaluation.org`, requestBody.descending ? "DESC" : "ASC");