From 26ca23213cf1a2bba2aa60617fa18958640f954b Mon Sep 17 00:00:00 2001 From: Suchin Sapphasitthatha Date: Tue, 5 Aug 2025 05:32:09 +0000 Subject: [PATCH 01/39] test node version --- docker/Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 07492be8..4d2fa1ea 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,5 +1,5 @@ # Build Stage -FROM node:lts-alpine AS build-stage +FROM node:22.18.0-alpine AS build-stage # Create app directory WORKDIR /app @@ -14,7 +14,7 @@ COPY . . RUN npm run build # Production Stage -FROM node:lts-alpine +FROM node:22.18.0-alpine ENV NODE_ENV production USER node From b8ce008d9eb8bb141a06e631803a157f39e9affa Mon Sep 17 00:00:00 2001 From: Suchin Sapphasitthatha Date: Tue, 5 Aug 2025 05:35:23 +0000 Subject: [PATCH 02/39] test node version 2 --- docker/Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 4d2fa1ea..91f85eca 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,5 +1,5 @@ # Build Stage -FROM node:22.18.0-alpine AS build-stage +FROM node:22.17.1-alpine AS build-stage # Create app directory WORKDIR /app @@ -14,7 +14,7 @@ COPY . . RUN npm run build # Production Stage -FROM node:22.18.0-alpine +FROM node:22.17.1-alpine ENV NODE_ENV production USER node From 83bf892997a8c0d2a0ed7bda0b49ae33346a65a3 Mon Sep 17 00:00:00 2001 From: Suchin Sapphasitthatha Date: Tue, 5 Aug 2025 05:50:25 +0000 Subject: [PATCH 03/39] rollback node to v20 lts --- docker/Dockerfile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 91f85eca..019c99f2 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,5 +1,5 @@ # Build Stage -FROM node:22.17.1-alpine AS build-stage +FROM node:20.19.4-alpine AS build-stage # Create app directory WORKDIR /app @@ -14,9 +14,9 @@ COPY . . RUN npm run build # Production Stage -FROM node:22.17.1-alpine +FROM node:20.19.4-alpine -ENV NODE_ENV production +ENV NODE_ENV=production USER node # Create app directory From 768be704cf5a48936e4f2de847c504fae44c086c Mon Sep 17 00:00:00 2001 From: Suchin Sapphasitthatha Date: Tue, 5 Aug 2025 05:54:13 +0000 Subject: [PATCH 04/39] =?UTF-8?q?=E0=B8=97=E0=B8=94=E0=B8=AA=E0=B8=AD?= =?UTF-8?q?=E0=B8=9A=E0=B9=81=E0=B8=81=E0=B9=89=E0=B9=84=E0=B8=82=E0=B8=84?= =?UTF-8?q?=E0=B8=B3=E0=B8=AA=E0=B8=B1=E0=B9=88=E0=B8=87=20npm=20ci?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker/Dockerfile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 019c99f2..cc4e6d2e 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -6,8 +6,8 @@ WORKDIR /app # Install app dependencies COPY package*.json ./ - -RUN npm ci +RUN npm install +# RUN npm ci # Copy source files and build the app COPY . . @@ -17,7 +17,6 @@ RUN npm run build FROM node:20.19.4-alpine ENV NODE_ENV=production -USER node # Create app directory WORKDIR /app @@ -28,6 +27,7 @@ COPY --from=build-stage /app/dist ./dist # Install only production dependencies COPY package*.json ./ RUN npm ci --production +USER node # Define the entrypoint and default command # If you have a custom entrypoint script From 9c242dd57a7b660e9d4b289d07931f4e18e5805a Mon Sep 17 00:00:00 2001 From: Suchin Sapphasitthatha Date: Tue, 5 Aug 2025 06:03:24 +0000 Subject: [PATCH 05/39] =?UTF-8?q?=E0=B8=A2=E0=B8=81=E0=B9=80=E0=B8=A5?= =?UTF-8?q?=E0=B8=B4=E0=B8=81=E0=B8=81=E0=B8=B2=E0=B8=A3=20copy=20package-?= =?UTF-8?q?lock.json=20=E0=B8=95=E0=B8=AD=E0=B8=99=20production?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index cc4e6d2e..ecdfa640 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -25,7 +25,7 @@ WORKDIR /app COPY --from=build-stage /app/dist ./dist # Install only production dependencies -COPY package*.json ./ +COPY package.json ./ RUN npm ci --production USER node From 4a603850078ebfe1f112c344ca39dcc4d97cff95 Mon Sep 17 00:00:00 2001 From: Suchin Sapphasitthatha Date: Tue, 5 Aug 2025 13:09:53 +0700 Subject: [PATCH 06/39] update package-lock.json --- docker/Dockerfile | 6 ++---- package-lock.json | 53 ----------------------------------------------- 2 files changed, 2 insertions(+), 57 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index ecdfa640..9a6d5c3f 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -6,8 +6,7 @@ WORKDIR /app # Install app dependencies COPY package*.json ./ -RUN npm install -# RUN npm ci +RUN npm ci # Copy source files and build the app COPY . . @@ -15,7 +14,6 @@ RUN npm run build # Production Stage FROM node:20.19.4-alpine - ENV NODE_ENV=production # Create app directory @@ -25,7 +23,7 @@ WORKDIR /app COPY --from=build-stage /app/dist ./dist # Install only production dependencies -COPY package.json ./ +COPY package*.json ./ RUN npm ci --production USER node diff --git a/package-lock.json b/package-lock.json index 54d03929..1a566e3e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5072,39 +5072,6 @@ "url": "https://github.com/sponsors/isaacs" } }, - "node_modules/typeorm-cli/node_modules/iconv-lite": { - "version": "0.6.3", - "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz", - "integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==", - "optional": true, - "peer": true, - "dependencies": { - "safer-buffer": ">= 2.1.2 < 3.0.0" - }, - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/typeorm-cli/node_modules/long": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz", - "integrity": "sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==", - "optional": true, - "peer": true - }, - "node_modules/typeorm-cli/node_modules/lru-cache": { - "version": "6.0.0", - "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", - "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", - "optional": true, - "peer": true, - "dependencies": { - "yallist": "^4.0.0" - }, - "engines": { - "node": ">=10" - } - }, "node_modules/typeorm-cli/node_modules/mkdirp": { "version": "0.5.6", "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.6.tgz", @@ -5121,26 +5088,6 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, - "node_modules/typeorm-cli/node_modules/mysql2": { - "version": "2.3.3", - "resolved": "https://registry.npmjs.org/mysql2/-/mysql2-2.3.3.tgz", - "integrity": "sha512-wxJUev6LgMSgACDkb/InIFxDprRa6T95+VEoR+xPvtngtccNH2dGjEB/fVZ8yg1gWv1510c9CvXuJHi5zUm0ZA==", - "optional": true, - "peer": true, - "dependencies": { - "denque": "^2.0.1", - "generate-function": "^2.3.1", - "iconv-lite": "^0.6.3", - "long": "^4.0.0", - "lru-cache": "^6.0.0", - "named-placeholders": "^1.1.2", - "seq-queue": "^0.0.5", - "sqlstring": "^2.3.2" - }, - "engines": { - "node": ">= 8.0" - } - }, "node_modules/typeorm-cli/node_modules/reflect-metadata": { "version": "0.1.14", "resolved": "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.1.14.tgz", From 81c7e00fb4a83b7f100da345e67e9169544cb26f Mon Sep 17 00:00:00 2001 From: Suchin Sapphasitthatha Date: Tue, 5 Aug 2025 06:15:52 +0000 Subject: [PATCH 07/39] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89=E0=B9=80?= =?UTF-8?q?=E0=B8=9B=E0=B9=87=E0=B8=99=20npm=20install?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker/Dockerfile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 9a6d5c3f..1b119c40 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -6,7 +6,8 @@ WORKDIR /app # Install app dependencies COPY package*.json ./ -RUN npm ci +#RUN npm ci +RUN npm install # Copy source files and build the app COPY . . @@ -24,7 +25,8 @@ COPY --from=build-stage /app/dist ./dist # Install only production dependencies COPY package*.json ./ -RUN npm ci --production +#RUN npm ci --production +RUN npm install USER node # Define the entrypoint and default command From 8239dfe3bf26031365d3e85da13f894fe694c5ff Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Tue, 16 Sep 2025 15:23:37 +0700 Subject: [PATCH 08/39] #1456 --- src/controllers/ReportController.ts | 98 ++++++++++++++++++++++++----- 1 file changed, 82 insertions(+), 16 deletions(-) diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index 94ac4362..996f60a5 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -761,7 +761,17 @@ export class ReportController extends Controller { .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { if (posMaster.orgChild1Id == null) { - const positionName = [...new Set(posMaster.positions.map((x) => x.positionName))]; + const positionName = [ + ...new Set( + posMaster.positions + .sort((a:any, b:any) => a.orderNo - b.orderNo) + .map((x) => + x.positionArea + ? `${x.positionName} (${x.positionArea})` + : x.positionName + ) + ), + ]; const posType = [ ...new Set( posMaster.positions @@ -780,7 +790,11 @@ export class ReportController extends Controller { ...new Set( posMaster.positions .filter((x: any) => x.posExecutive != null) - .map((x) => x.posExecutive.posExecutiveName), + .map((x: any) => + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName + ) ), ]; @@ -867,7 +881,17 @@ export class ReportController extends Controller { .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { if (posMaster.orgChild2Id == null) { - const positionName = [...new Set(posMaster.positions.map((x) => x.positionName))]; + const positionName = [ + ...new Set( + posMaster.positions + .sort((a:any, b:any) => a.orderNo - b.orderNo) + .map((x) => + x.positionArea + ? `${x.positionName} (${x.positionArea})` + : x.positionName + ) + ), + ]; const posType = [ ...new Set( posMaster.positions @@ -886,7 +910,11 @@ export class ReportController extends Controller { ...new Set( posMaster.positions .filter((x: any) => x.posExecutive != null) - .map((x) => x.posExecutive.posExecutiveName), + .map((x: any) => + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName + ) ), ]; @@ -975,7 +1003,17 @@ export class ReportController extends Controller { .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { if (posMaster.orgChild3Id == null) { - const positionName = [...new Set(posMaster.positions.map((x) => x.positionName))]; + const positionName = [ + ...new Set( + posMaster.positions + .sort((a:any, b:any) => a.orderNo - b.orderNo) + .map((x) => + x.positionArea + ? `${x.positionName} (${x.positionArea})` + : x.positionName + ) + ), + ]; const posType = [ ...new Set( posMaster.positions @@ -990,13 +1028,17 @@ export class ReportController extends Controller { .map((x) => x.posLevel.posLevelName), ), ]; - const posExecutive = [ - ...new Set( - posMaster.positions - .filter((x: any) => x.posExecutive != null) - .map((x) => x.posExecutive.posExecutiveName), - ), - ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName + ) + ), + ]; let node = { orgTreeName: orgChild2.orgChild2Name, @@ -1081,7 +1123,15 @@ export class ReportController extends Controller { .map(async (posMaster) => { if (posMaster.orgChild4Id == null) { const positionName = [ - ...new Set(posMaster.positions.map((x) => x.positionName)), + ...new Set( + posMaster.positions + .sort((a:any, b:any) => a.orderNo - b.orderNo) + .map((x) => + x.positionArea + ? `${x.positionName} (${x.positionArea})` + : x.positionName + ) + ), ]; const posType = [ ...new Set( @@ -1101,7 +1151,11 @@ export class ReportController extends Controller { ...new Set( posMaster.positions .filter((x: any) => x.posExecutive != null) - .map((x) => x.posExecutive.posExecutiveName), + .map((x: any) => + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName + ) ), ]; @@ -1187,7 +1241,15 @@ export class ReportController extends Controller { .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { const positionName = [ - ...new Set(posMaster.positions.map((x) => x.positionName)), + ...new Set( + posMaster.positions + .sort((a:any, b:any) => a.orderNo - b.orderNo) + .map((x) => + x.positionArea + ? `${x.positionName} (${x.positionArea})` + : x.positionName + ) + ), ]; const posType = [ ...new Set( @@ -1207,7 +1269,11 @@ export class ReportController extends Controller { ...new Set( posMaster.positions .filter((x: any) => x.posExecutive != null) - .map((x) => x.posExecutive.posExecutiveName), + .map((x: any) => + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName + ) ), ]; From 6a43edef46f5de39f623163988ec4d0eaaf9affd Mon Sep 17 00:00:00 2001 From: Bright Date: Wed, 17 Sep 2025 15:50:01 +0700 Subject: [PATCH 09/39] report1 --- src/controllers/ReportController.ts | 2009 +++++++++++++++++++++++++++ 1 file changed, 2009 insertions(+) diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index 996f60a5..2c309096 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -8,6 +8,8 @@ import { Response, Path, Query, + Post, + Body } from "tsoa"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; @@ -660,6 +662,2013 @@ export class ReportController extends Controller { }); } + /** + * API Report1 + * + * @summary Report1 + * + */ + @Post("report1") + async NewReport1( + @Body() + reqBody: { + node: number; + nodeId: string; + } + ) { + let _nodeId:string = reqBody.nodeId ? reqBody.nodeId : ""; + let _node:number = reqBody.node ? reqBody.node : 0; + if(_nodeId === "" || _node < 0 || _node > 4) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + + let orgName:string = ""; + let data = new Array(); + let no = 1; + + switch (_node) { + case 0: { + // ดึงข้อมูล orgRoot ตาม nodeId และลูกทั้งหมด (cascade 0..4) + const orgRootData = await this.orgRootRepository.find({ + where: { id: _nodeId }, + order: { orgRootOrder: "ASC" }, + relations: [ + "posMasters", + "posMasters.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + "posMasters.positions.posExecutive", + ], + }); + if (!orgRootData || orgRootData.length === 0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล orgRoot"); + } + orgName = orgRootData[0].orgRootName ?? ""; + + const orgRootIds = orgRootData.map((r) => r.id) || null; + const orgChild1Data = await this.child1Repository.find({ + where: { orgRootId: In(orgRootIds) }, + order: { orgChild1Order: "ASC" }, + relations: [ + "posMasters", + "posMasters.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + "posMasters.positions.posExecutive", + ], + }); + const orgChild1Ids = orgChild1Data.map((c) => c.id) || null; + const orgChild2Data = await this.child2Repository.find({ + where: { orgChild1: In(orgChild1Ids) }, + order: { orgChild2Order: "ASC" }, + relations: [ + "posMasters", + "posMasters.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + "posMasters.positions.posExecutive", + ], + }); + const orgChild2Ids = orgChild2Data.map((c) => c.id) || null; + const orgChild3Data = await this.child3Repository.find({ + where: { orgChild2: In(orgChild2Ids) }, + order: { orgChild3Order: "ASC" }, + relations: [ + "posMasters", + "posMasters.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + "posMasters.positions.posExecutive", + ], + }); + const orgChild3Ids = orgChild3Data.map((c) => c.id) || null; + const orgChild4Data = await this.child4Repository.find({ + where: { orgChild3: In(orgChild3Ids) }, + order: { orgChild4Order: "ASC" }, + relations: [ + "posMasters", + "posMasters.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + "posMasters.positions.posExecutive", + ], + }); + + let _nodeTemp: any = null; + for (let orgRoot of orgRootData) { + await Promise.all( + orgRoot.posMasters + .filter((x) => x.orgChild1Id == null) + .sort((a, b) => a.posMasterOrder - b.posMasterOrder) + .map(async (posMaster) => { + if (posMaster.orgChild1Id == null) { + const positionName = [ + ...new Set( + posMaster.positions + .sort((a: any, b: any) => a.orderNo - b.orderNo) + .map((x) => + x.positionArea + ? `${x.positionName} (${x.positionArea})` + : x.positionName + ) + ), + ]; + const posType = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posType != null) + .map((x) => x.posType.posTypeName), + ), + ]; + const posLevel = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posLevel != null) + .map((x) => x.posLevel.posLevelName), + ), + ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName + ) + ), + ]; + + let node = { + orgTreeName: orgRoot.orgRootName, + orgTreeShortName: orgRoot.orgRootShortName, + posMasterNo: posMaster.posMasterNo, + positionName: positionName.join(" หรือ "), + posType: posType.join(" หรือ "), + posLevel: posLevel.join(" หรือ "), + posExecutive: posExecutive.join(" หรือ "), + reason: posMaster.reason == null ? "" : posMaster.reason, + }; + if (_nodeTemp == null) { + const head = { + posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), + posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + const _head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _nodeTemp.orgTreeShortName || + node.orgTreeName != _nodeTemp.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + posExecutive: + node.orgTreeName == _nodeTemp.orgTreeName + ? "" + : Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + _nodeTemp = null; + } + const head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(head); + } + no += 1; + _nodeTemp = node; + } + }) + ); + _nodeTemp = null; + + for (let orgChild1 of orgChild1Data.filter((orgChild1) => orgChild1.orgRootId === orgRoot.id)) { + await Promise.all( + orgChild1.posMasters + .filter((x) => x.orgChild2Id == null) + .sort((a, b) => a.posMasterOrder - b.posMasterOrder) + .map(async (posMaster) => { + if (posMaster.orgChild2Id == null) { + const positionName = [ + ...new Set( + posMaster.positions + .sort((a: any, b: any) => a.orderNo - b.orderNo) + .map((x) => + x.positionArea + ? `${x.positionName} (${x.positionArea})` + : x.positionName + ) + ), + ]; + const posType = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posType != null) + .map((x) => x.posType.posTypeName), + ), + ]; + const posLevel = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posLevel != null) + .map((x) => x.posLevel.posLevelName), + ), + ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName + ) + ), + ]; + + let node = { + orgTreeName: orgChild1.orgChild1Name, + orgTreeShortName: orgChild1.orgChild1ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: positionName.join(" หรือ "), + posType: posType.join(" หรือ "), + posLevel: posLevel.join(" หรือ "), + posExecutive: posExecutive.join(" หรือ "), + reason: posMaster.reason == null ? "" : posMaster.reason, + }; + if (_nodeTemp == null) { + const head = { + posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName?.toString() ?? ""), + posExecutive: Extension.ToThaiNumber(node.orgTreeName?.toString() ?? ""), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + const _head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _nodeTemp.orgTreeShortName || + node.orgTreeName != _nodeTemp.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + posExecutive: + node.orgTreeName == _nodeTemp.orgTreeName + ? "" + : Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + _nodeTemp = null; + } + const head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(head); + } + no += 1; + _nodeTemp = node; + } + }) + ); + + for (let orgChild2 of orgChild2Data.filter((orgChild2) => orgChild2.orgChild1Id === orgChild1.id)) { + await Promise.all( + orgChild2.posMasters + .filter((x) => x.orgChild3Id == null) + .sort((a, b) => a.posMasterOrder - b.posMasterOrder) + .map(async (posMaster) => { + if (posMaster.orgChild3Id == null) { + const positionName = [ + ...new Set( + posMaster.positions + .sort((a: any, b: any) => a.orderNo - b.orderNo) + .map((x) => + x.positionArea + ? `${x.positionName} (${x.positionArea})` + : x.positionName + ) + ), + ]; + const posType = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posType != null) + .map((x) => x.posType.posTypeName), + ), + ]; + const posLevel = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posLevel != null) + .map((x) => x.posLevel.posLevelName), + ), + ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName + ) + ), + ]; + + let node = { + orgTreeName: orgChild2.orgChild2Name, + orgTreeShortName: orgChild2.orgChild2ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: positionName.join(" หรือ "), + posType: posType.join(" หรือ "), + posLevel: posLevel.join(" หรือ "), + posExecutive: posExecutive.join(" หรือ "), + reason: posMaster.reason == null ? "" : posMaster.reason, + }; + if (_nodeTemp == null) { + const head = { + posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName?.toString() ?? ""), + posExecutive: Extension.ToThaiNumber(node.orgTreeName?.toString() ?? ""), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + const _head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _nodeTemp.orgTreeShortName || + node.orgTreeName != _nodeTemp.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + posExecutive: + node.orgTreeName == _nodeTemp.orgTreeName + ? "" + : Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + _nodeTemp = null; + } + const head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(head); + } + no += 1; + _nodeTemp = node; + } + }) + ); + + for (let orgChild3 of orgChild3Data.filter((orgChild3) => orgChild3.orgChild2Id === orgChild2.id)) { + await Promise.all( + orgChild3.posMasters + .filter((x) => x.orgChild4Id == null) + .sort((a, b) => a.posMasterOrder - b.posMasterOrder) + .map(async (posMaster) => { + if (posMaster.orgChild4Id == null) { + const positionName = [ + ...new Set( + posMaster.positions + .sort((a: any, b: any) => a.orderNo - b.orderNo) + .map((x) => + x.positionArea + ? `${x.positionName} (${x.positionArea})` + : x.positionName + ) + ), + ]; + const posType = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posType != null) + .map((x) => x.posType.posTypeName), + ), + ]; + const posLevel = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posLevel != null) + .map((x) => x.posLevel.posLevelName), + ), + ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName + ) + ), + ]; + + let node = { + orgTreeName: orgChild3.orgChild3Name, + orgTreeShortName: orgChild3.orgChild3ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: positionName.join(" หรือ "), + posType: posType.join(" หรือ "), + posLevel: posLevel.join(" หรือ "), + posExecutive: posExecutive.join(" หรือ "), + reason: posMaster.reason == null ? "" : posMaster.reason, + }; + if (_nodeTemp == null) { + const head = { + posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName?.toString() ?? ""), + posExecutive: Extension.ToThaiNumber(node.orgTreeName?.toString() ?? ""), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + const _head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _nodeTemp.orgTreeShortName || + node.orgTreeName != _nodeTemp.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + posExecutive: + node.orgTreeName == _nodeTemp.orgTreeName + ? "" + : Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + _nodeTemp = null; + } + const head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(head); + } + no += 1; + _nodeTemp = node; + } + }) + ); + + for (let orgChild4 of orgChild4Data.filter((orgChild4) => orgChild4.orgChild3Id === orgChild3.id)) { + await Promise.all( + orgChild4.posMasters + .sort((a, b) => a.posMasterOrder - b.posMasterOrder) + .map(async (posMaster) => { + const positionName = [ + ...new Set( + posMaster.positions + .sort((a: any, b: any) => a.orderNo - b.orderNo) + .map((x) => + x.positionArea + ? `${x.positionName} (${x.positionArea})` + : x.positionName + ) + ), + ]; + const posType = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posType != null) + .map((x) => x.posType.posTypeName), + ), + ]; + const posLevel = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posLevel != null) + .map((x) => x.posLevel.posLevelName), + ), + ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName + ) + ), + ]; + + let node = { + orgTreeName: orgChild4.orgChild4Name, + orgTreeShortName: orgChild4.orgChild4ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: positionName.join(" หรือ "), + posType: posType.join(" หรือ "), + posLevel: posLevel.join(" หรือ "), + posExecutive: posExecutive.join(" หรือ "), + reason: posMaster.reason == null ? "" : posMaster.reason, + }; + if (_nodeTemp == null) { + const head = { + posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName?.toString() ?? ""), + posExecutive: Extension.ToThaiNumber(node.orgTreeName?.toString() ?? ""), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + const _head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _nodeTemp.orgTreeShortName || + node.orgTreeName != _nodeTemp.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + posExecutive: + node.orgTreeName == _nodeTemp.orgTreeName + ? "" + : Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + _nodeTemp = null; + } + const head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(head); + } + no += 1; + _nodeTemp = node; + }) + ); + } + } + } + } + } + break; + } + case 1: { + // ดึง orgChild1 และลูก (orgChild2, orgChild3, orgChild4) + const orgChild1Data = await this.child1Repository.find({ + where: { id: _nodeId }, + order: { orgChild1Order: "ASC" }, + relations: [ + "posMasters", + "posMasters.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + "posMasters.positions.posExecutive", + ], + }); + if (!orgChild1Data || orgChild1Data.length === 0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล orgChild1"); + } + orgName = orgChild1Data[0].orgChild1Name ?? ""; + // cascade ลูก + const orgChild1Ids = orgChild1Data.map((c) => c.id) || null; + const orgChild2Data = await this.child2Repository.find({ + where: { orgChild1: In(orgChild1Ids) }, + order: { orgChild2Order: "ASC" }, + relations: [ + "posMasters", + "posMasters.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + "posMasters.positions.posExecutive", + ], + }); + const orgChild2Ids = orgChild2Data.map((c) => c.id) || null; + const orgChild3Data = await this.child3Repository.find({ + where: { orgChild2: In(orgChild2Ids) }, + order: { orgChild3Order: "ASC" }, + relations: [ + "posMasters", + "posMasters.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + "posMasters.positions.posExecutive", + ], + }); + const orgChild3Ids = orgChild3Data.map((c) => c.id) || null; + const orgChild4Data = await this.child4Repository.find({ + where: { orgChild3: In(orgChild3Ids) }, + order: { orgChild4Order: "ASC" }, + relations: [ + "posMasters", + "posMasters.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + "posMasters.positions.posExecutive", + ], + }); + let _nodeTemp: any = null; + for (let orgChild1 of orgChild1Data) { + await Promise.all( + orgChild1.posMasters + .filter((x) => x.orgChild2Id == null) + .sort((a, b) => a.posMasterOrder - b.posMasterOrder) + .map(async (posMaster) => { + if (posMaster.orgChild2Id == null) { + // --- push data logic (เหมือน node 0) --- + const positionName = [ + ...new Set( + posMaster.positions + .sort((a: any, b: any) => a.orderNo - b.orderNo) + .map((x) => + x.positionArea + ? `${x.positionName} (${x.positionArea})` + : x.positionName + ) + ), + ]; + const posType = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posType != null) + .map((x) => x.posType.posTypeName), + ), + ]; + const posLevel = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posLevel != null) + .map((x) => x.posLevel.posLevelName), + ), + ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName + ) + ), + ]; + let node = { + orgTreeName: orgChild1.orgChild1Name, + orgTreeShortName: orgChild1.orgChild1ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: positionName.join(" หรือ "), + posType: posType.join(" หรือ "), + posLevel: posLevel.join(" หรือ "), + posExecutive: posExecutive.join(" หรือ "), + reason: posMaster.reason == null ? "" : posMaster.reason, + }; + if (_nodeTemp == null) { + const head = { + posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName?.toString() ?? ""), + posExecutive: Extension.ToThaiNumber(node.orgTreeName?.toString() ?? ""), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + const _head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _nodeTemp.orgTreeShortName || + node.orgTreeName != _nodeTemp.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + posExecutive: + node.orgTreeName == _nodeTemp.orgTreeName + ? "" + : Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + _nodeTemp = null; + } + const head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(head); + } + no += 1; + _nodeTemp = node; + } + }) + ); + for (let orgChild2 of orgChild2Data.filter((orgChild2) => orgChild2.orgChild1Id === orgChild1.id)) { + await Promise.all( + orgChild2.posMasters + .filter((x) => x.orgChild3Id == null) + .sort((a, b) => a.posMasterOrder - b.posMasterOrder) + .map(async (posMaster) => { + if (posMaster.orgChild3Id == null) { + // --- push data logic (เหมือน node 0) --- + const positionName = [ + ...new Set( + posMaster.positions + .sort((a: any, b: any) => a.orderNo - b.orderNo) + .map((x) => + x.positionArea + ? `${x.positionName} (${x.positionArea})` + : x.positionName + ) + ), + ]; + const posType = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posType != null) + .map((x) => x.posType.posTypeName), + ), + ]; + const posLevel = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posLevel != null) + .map((x) => x.posLevel.posLevelName), + ), + ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName + ) + ), + ]; + let node = { + orgTreeName: orgChild2.orgChild2Name, + orgTreeShortName: orgChild2.orgChild2ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: positionName.join(" หรือ "), + posType: posType.join(" หรือ "), + posLevel: posLevel.join(" หรือ "), + posExecutive: posExecutive.join(" หรือ "), + reason: posMaster.reason == null ? "" : posMaster.reason, + }; + if (_nodeTemp == null) { + const head = { + posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName?.toString() ?? ""), + posExecutive: Extension.ToThaiNumber(node.orgTreeName?.toString() ?? ""), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + const _head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _nodeTemp.orgTreeShortName || + node.orgTreeName != _nodeTemp.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + posExecutive: + node.orgTreeName == _nodeTemp.orgTreeName + ? "" + : Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + _nodeTemp = null; + } + const head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(head); + } + no += 1; + _nodeTemp = node; + } + }) + ); + for (let orgChild3 of orgChild3Data.filter((orgChild3) => orgChild3.orgChild2Id === orgChild2.id)) { + await Promise.all( + orgChild3.posMasters + .filter((x) => x.orgChild4Id == null) + .sort((a, b) => a.posMasterOrder - b.posMasterOrder) + .map(async (posMaster) => { + if (posMaster.orgChild4Id == null) { + // --- push data logic (เหมือน node 0) --- + const positionName = [ + ...new Set( + posMaster.positions + .sort((a: any, b: any) => a.orderNo - b.orderNo) + .map((x) => + x.positionArea + ? `${x.positionName} (${x.positionArea})` + : x.positionName + ) + ), + ]; + const posType = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posType != null) + .map((x) => x.posType.posTypeName), + ), + ]; + const posLevel = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posLevel != null) + .map((x) => x.posLevel.posLevelName), + ), + ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName + ) + ), + ]; + let node = { + orgTreeName: orgChild3.orgChild3Name, + orgTreeShortName: orgChild3.orgChild3ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: positionName.join(" หรือ "), + posType: posType.join(" หรือ "), + posLevel: posLevel.join(" หรือ "), + posExecutive: posExecutive.join(" หรือ "), + reason: posMaster.reason == null ? "" : posMaster.reason, + }; + if (_nodeTemp == null) { + const head = { + posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName?.toString() ?? ""), + posExecutive: Extension.ToThaiNumber(node.orgTreeName?.toString() ?? ""), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + const _head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _nodeTemp.orgTreeShortName || + node.orgTreeName != _nodeTemp.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + posExecutive: + node.orgTreeName == _nodeTemp.orgTreeName + ? "" + : Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + _nodeTemp = null; + } + const head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(head); + } + no += 1; + _nodeTemp = node; + } + }) + ); + for (let orgChild4 of orgChild4Data.filter((orgChild4) => orgChild4.orgChild3Id === orgChild3.id)) { + await Promise.all( + orgChild4.posMasters + .sort((a, b) => a.posMasterOrder - b.posMasterOrder) + .map(async (posMaster) => { + // --- push data logic (เหมือน node 0) --- + const positionName = [ + ...new Set( + posMaster.positions + .sort((a: any, b: any) => a.orderNo - b.orderNo) + .map((x) => + x.positionArea + ? `${x.positionName} (${x.positionArea})` + : x.positionName + ) + ), + ]; + const posType = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posType != null) + .map((x) => x.posType.posTypeName), + ), + ]; + const posLevel = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posLevel != null) + .map((x) => x.posLevel.posLevelName), + ), + ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName + ) + ), + ]; + let node = { + orgTreeName: orgChild4.orgChild4Name, + orgTreeShortName: orgChild4.orgChild4ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: positionName.join(" หรือ "), + posType: posType.join(" หรือ "), + posLevel: posLevel.join(" หรือ "), + posExecutive: posExecutive.join(" หรือ "), + reason: posMaster.reason == null ? "" : posMaster.reason, + }; + if (_nodeTemp == null) { + const head = { + posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName?.toString() ?? ""), + posExecutive: Extension.ToThaiNumber(node.orgTreeName?.toString() ?? ""), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + const _head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _nodeTemp.orgTreeShortName || + node.orgTreeName != _nodeTemp.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + posExecutive: + node.orgTreeName == _nodeTemp.orgTreeName + ? "" + : Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + _nodeTemp = null; + } + const head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(head); + } + no += 1; + _nodeTemp = node; + }) + ); + } + } + } + } + break; + } + case 2: { + // ดึง orgChild2 และลูก (orgChild3, orgChild4) + const orgChild2Data = await this.child2Repository.find({ + where: { id: _nodeId }, + order: { orgChild2Order: "ASC" }, + relations: [ + "posMasters", + "posMasters.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + "posMasters.positions.posExecutive", + ], + }); + if (!orgChild2Data || orgChild2Data.length === 0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล orgChild2"); + } + orgName = orgChild2Data[0].orgChild2Name ?? ""; + // cascade ลูก + const orgChild2Ids = orgChild2Data.map((c) => c.id) || null; + const orgChild3Data = await this.child3Repository.find({ + where: { orgChild2: In(orgChild2Ids) }, + order: { orgChild3Order: "ASC" }, + relations: [ + "posMasters", + "posMasters.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + "posMasters.positions.posExecutive", + ], + }); + const orgChild3Ids = orgChild3Data.map((c) => c.id) || null; + const orgChild4Data = await this.child4Repository.find({ + where: { orgChild3: In(orgChild3Ids) }, + order: { orgChild4Order: "ASC" }, + relations: [ + "posMasters", + "posMasters.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + "posMasters.positions.posExecutive", + ], + }); + let _nodeTemp: any = null; + for (let orgChild2 of orgChild2Data) { + await Promise.all( + orgChild2.posMasters + .filter((x) => x.orgChild3Id == null) + .sort((a, b) => a.posMasterOrder - b.posMasterOrder) + .map(async (posMaster) => { + if (posMaster.orgChild3Id == null) { + // --- push data logic (เหมือน node 0) --- + const positionName = [ + ...new Set( + posMaster.positions + .sort((a: any, b: any) => a.orderNo - b.orderNo) + .map((x) => + x.positionArea + ? `${x.positionName} (${x.positionArea})` + : x.positionName + ) + ), + ]; + const posType = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posType != null) + .map((x) => x.posType.posTypeName), + ), + ]; + const posLevel = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posLevel != null) + .map((x) => x.posLevel.posLevelName), + ), + ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName + ) + ), + ]; + let node = { + orgTreeName: orgChild2.orgChild2Name, + orgTreeShortName: orgChild2.orgChild2ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: positionName.join(" หรือ "), + posType: posType.join(" หรือ "), + posLevel: posLevel.join(" หรือ "), + posExecutive: posExecutive.join(" หรือ "), + reason: posMaster.reason == null ? "" : posMaster.reason, + }; + if (_nodeTemp == null) { + const head = { + posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName?.toString() ?? ""), + posExecutive: Extension.ToThaiNumber(node.orgTreeName?.toString() ?? ""), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + const _head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _nodeTemp.orgTreeShortName || + node.orgTreeName != _nodeTemp.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + posExecutive: + node.orgTreeName == _nodeTemp.orgTreeName + ? "" + : Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + _nodeTemp = null; + } + const head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(head); + } + no += 1; + _nodeTemp = node; + } + }) + ); + for (let orgChild3 of orgChild3Data.filter((orgChild3) => orgChild3.orgChild2Id === orgChild2.id)) { + await Promise.all( + orgChild3.posMasters + .filter((x) => x.orgChild4Id == null) + .sort((a, b) => a.posMasterOrder - b.posMasterOrder) + .map(async (posMaster) => { + if (posMaster.orgChild4Id == null) { + // --- push data logic (เหมือน node 0) --- + const positionName = [ + ...new Set( + posMaster.positions + .sort((a: any, b: any) => a.orderNo - b.orderNo) + .map((x) => + x.positionArea + ? `${x.positionName} (${x.positionArea})` + : x.positionName + ) + ), + ]; + const posType = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posType != null) + .map((x) => x.posType.posTypeName), + ), + ]; + const posLevel = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posLevel != null) + .map((x) => x.posLevel.posLevelName), + ), + ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName + ) + ), + ]; + let node = { + orgTreeName: orgChild3.orgChild3Name, + orgTreeShortName: orgChild3.orgChild3ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: positionName.join(" หรือ "), + posType: posType.join(" หรือ "), + posLevel: posLevel.join(" หรือ "), + posExecutive: posExecutive.join(" หรือ "), + reason: posMaster.reason == null ? "" : posMaster.reason, + }; + if (_nodeTemp == null) { + const head = { + posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName?.toString() ?? ""), + posExecutive: Extension.ToThaiNumber(node.orgTreeName?.toString() ?? ""), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + const _head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _nodeTemp.orgTreeShortName || + node.orgTreeName != _nodeTemp.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + posExecutive: + node.orgTreeName == _nodeTemp.orgTreeName + ? "" + : Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + _nodeTemp = null; + } + const head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(head); + } + no += 1; + _nodeTemp = node; + } + }) + ); + for (let orgChild4 of orgChild4Data.filter((orgChild4) => orgChild4.orgChild3Id === orgChild3.id)) { + await Promise.all( + orgChild4.posMasters + .sort((a, b) => a.posMasterOrder - b.posMasterOrder) + .map(async (posMaster) => { + // --- push data logic (เหมือน node 0) --- + const positionName = [ + ...new Set( + posMaster.positions + .sort((a: any, b: any) => a.orderNo - b.orderNo) + .map((x) => + x.positionArea + ? `${x.positionName} (${x.positionArea})` + : x.positionName + ) + ), + ]; + const posType = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posType != null) + .map((x) => x.posType.posTypeName), + ), + ]; + const posLevel = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posLevel != null) + .map((x) => x.posLevel.posLevelName), + ), + ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName + ) + ), + ]; + let node = { + orgTreeName: orgChild4.orgChild4Name, + orgTreeShortName: orgChild4.orgChild4ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: positionName.join(" หรือ "), + posType: posType.join(" หรือ "), + posLevel: posLevel.join(" หรือ "), + posExecutive: posExecutive.join(" หรือ "), + reason: posMaster.reason == null ? "" : posMaster.reason, + }; + if (_nodeTemp == null) { + const head = { + posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName?.toString() ?? ""), + posExecutive: Extension.ToThaiNumber(node.orgTreeName?.toString() ?? ""), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + const _head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _nodeTemp.orgTreeShortName || + node.orgTreeName != _nodeTemp.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + posExecutive: + node.orgTreeName == _nodeTemp.orgTreeName + ? "" + : Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + _nodeTemp = null; + } + const head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(head); + } + no += 1; + _nodeTemp = node; + }) + ); + } + } + } + break; + } + case 3: { + // ดึง orgChild3 และลูก (orgChild4) + const orgChild3Data = await this.child3Repository.find({ + where: { id: _nodeId }, + order: { orgChild3Order: "ASC" }, + relations: [ + "posMasters", + "posMasters.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + "posMasters.positions.posExecutive", + ], + }); + if (!orgChild3Data || orgChild3Data.length === 0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล orgChild3"); + } + orgName = orgChild3Data[0].orgChild3Name ?? ""; + // cascade ลูก + const orgChild3Ids = orgChild3Data.map((c) => c.id) || null; + const orgChild4Data = await this.child4Repository.find({ + where: { orgChild3: In(orgChild3Ids) }, + order: { orgChild4Order: "ASC" }, + relations: [ + "posMasters", + "posMasters.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + "posMasters.positions.posExecutive", + ], + }); + let _nodeTemp: any = null; + for (let orgChild3 of orgChild3Data) { + await Promise.all( + orgChild3.posMasters + .filter((x) => x.orgChild4Id == null) + .sort((a, b) => a.posMasterOrder - b.posMasterOrder) + .map(async (posMaster) => { + if (posMaster.orgChild4Id == null) { + // --- push data logic (เหมือน node 0) --- + const positionName = [ + ...new Set( + posMaster.positions + .sort((a: any, b: any) => a.orderNo - b.orderNo) + .map((x) => + x.positionArea + ? `${x.positionName} (${x.positionArea})` + : x.positionName + ) + ), + ]; + const posType = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posType != null) + .map((x) => x.posType.posTypeName), + ), + ]; + const posLevel = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posLevel != null) + .map((x) => x.posLevel.posLevelName), + ), + ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName + ) + ), + ]; + let node = { + orgTreeName: orgChild3.orgChild3Name, + orgTreeShortName: orgChild3.orgChild3ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: positionName.join(" หรือ "), + posType: posType.join(" หรือ "), + posLevel: posLevel.join(" หรือ "), + posExecutive: posExecutive.join(" หรือ "), + reason: posMaster.reason == null ? "" : posMaster.reason, + }; + if (_nodeTemp == null) { + const head = { + posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName?.toString() ?? ""), + posExecutive: Extension.ToThaiNumber(node.orgTreeName?.toString() ?? ""), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + const _head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _nodeTemp.orgTreeShortName || + node.orgTreeName != _nodeTemp.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + posExecutive: + node.orgTreeName == _nodeTemp.orgTreeName + ? "" + : Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + _nodeTemp = null; + } + const head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(head); + } + no += 1; + _nodeTemp = node; + } + }) + ); + for (let orgChild4 of orgChild4Data.filter((orgChild4) => orgChild4.orgChild3Id === orgChild3.id)) { + await Promise.all( + orgChild4.posMasters + .sort((a, b) => a.posMasterOrder - b.posMasterOrder) + .map(async (posMaster) => { + // --- push data logic (เหมือน node 0) --- + const positionName = [ + ...new Set( + posMaster.positions + .sort((a: any, b: any) => a.orderNo - b.orderNo) + .map((x) => + x.positionArea + ? `${x.positionName} (${x.positionArea})` + : x.positionName + ) + ), + ]; + const posType = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posType != null) + .map((x) => x.posType.posTypeName), + ), + ]; + const posLevel = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posLevel != null) + .map((x) => x.posLevel.posLevelName), + ), + ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName + ) + ), + ]; + let node = { + orgTreeName: orgChild4.orgChild4Name, + orgTreeShortName: orgChild4.orgChild4ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: positionName.join(" หรือ "), + posType: posType.join(" หรือ "), + posLevel: posLevel.join(" หรือ "), + posExecutive: posExecutive.join(" หรือ "), + reason: posMaster.reason == null ? "" : posMaster.reason, + }; + if (_nodeTemp == null) { + const head = { + posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName?.toString() ?? ""), + posExecutive: Extension.ToThaiNumber(node.orgTreeName?.toString() ?? ""), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + const _head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _nodeTemp.orgTreeShortName || + node.orgTreeName != _nodeTemp.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + posExecutive: + node.orgTreeName == _nodeTemp.orgTreeName + ? "" + : Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + _nodeTemp = null; + } + const head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(head); + } + no += 1; + _nodeTemp = node; + }) + ); + } + } + break; + } + case 4: { + // ดึงข้อมูล orgChild4 ตาม nodeId + const orgChild4Data = await this.child4Repository.find({ + where: { id: _nodeId }, + order: { orgChild4Order: "ASC" }, + relations: [ + "posMasters", + "posMasters.positions", + "posMasters.positions.posLevel", + "posMasters.positions.posType", + "posMasters.positions.posExecutive", + ], + }); + if (!orgChild4Data || orgChild4Data.length === 0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล orgChild4"); + } + orgName = orgChild4Data[0].orgChild4Name ?? ""; + let _nodeTemp: any = null; + for (let orgChild4 of orgChild4Data) { + await Promise.all( + orgChild4.posMasters + .sort((a, b) => a.posMasterOrder - b.posMasterOrder) + .map(async (posMaster) => { + const positionName = [ + ...new Set( + posMaster.positions + .sort((a: any, b: any) => a.orderNo - b.orderNo) + .map((x) => + x.positionArea + ? `${x.positionName} (${x.positionArea})` + : x.positionName + ) + ), + ]; + const posType = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posType != null) + .map((x) => x.posType.posTypeName), + ), + ]; + const posLevel = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posLevel != null) + .map((x) => x.posLevel.posLevelName), + ), + ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName + ) + ), + ]; + + let node = { + orgTreeName: orgChild4.orgChild4Name, + orgTreeShortName: orgChild4.orgChild4ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: positionName.join(" หรือ "), + posType: posType.join(" หรือ "), + posLevel: posLevel.join(" หรือ "), + posExecutive: posExecutive.join(" หรือ "), + reason: posMaster.reason == null ? "" : posMaster.reason, + }; + if (_nodeTemp == null) { + const head = { + posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName?.toString() ?? ""), + posExecutive: Extension.ToThaiNumber(node.orgTreeName?.toString() ?? ""), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + const _head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _nodeTemp.orgTreeShortName || + node.orgTreeName != _nodeTemp.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + posExecutive: + node.orgTreeName == _nodeTemp.orgTreeName + ? "" + : Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + _nodeTemp = null; + } + const head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(head); + } + no += 1; + _nodeTemp = node; + }) + ); + } + break; + } + default: { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + } + + return new HttpSuccess({ + template: "report1", + reportName: "report1", + data: { org: orgName, data }, + }); + } + /** * API Report1 * From aa2831307bcc78fbf67ecd12b9c13f86c8c41b37 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Wed, 17 Sep 2025 17:58:44 +0700 Subject: [PATCH 10/39] #158 --- src/controllers/PositionController.ts | 1 + src/controllers/ReportController.ts | 124 ++++++++++++++++++++++---- 2 files changed, 109 insertions(+), 16 deletions(-) diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 7b5c27c4..21e79209 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -2355,6 +2355,7 @@ export class PositionController extends Controller { .addOrderBy("orgChild3.orgChild3Order", "ASC") .addOrderBy("orgChild4.orgChild4Order", "ASC") .addOrderBy("posMaster.posMasterOrder", "ASC") + .addOrderBy("posMaster.createdAt", "ASC") .skip((body.page - 1) * body.pageSize) .take(body.pageSize) .getManyAndCount(); diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index 2c309096..2c4be4c4 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -691,7 +691,13 @@ export class ReportController extends Controller { // ดึงข้อมูล orgRoot ตาม nodeId และลูกทั้งหมด (cascade 0..4) const orgRootData = await this.orgRootRepository.find({ where: { id: _nodeId }, - order: { orgRootOrder: "ASC" }, + order: { + orgRootOrder: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -708,7 +714,13 @@ export class ReportController extends Controller { const orgRootIds = orgRootData.map((r) => r.id) || null; const orgChild1Data = await this.child1Repository.find({ where: { orgRootId: In(orgRootIds) }, - order: { orgChild1Order: "ASC" }, + order: { + orgChild1Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -720,7 +732,13 @@ export class ReportController extends Controller { const orgChild1Ids = orgChild1Data.map((c) => c.id) || null; const orgChild2Data = await this.child2Repository.find({ where: { orgChild1: In(orgChild1Ids) }, - order: { orgChild2Order: "ASC" }, + order: { + orgChild2Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -732,7 +750,13 @@ export class ReportController extends Controller { const orgChild2Ids = orgChild2Data.map((c) => c.id) || null; const orgChild3Data = await this.child3Repository.find({ where: { orgChild2: In(orgChild2Ids) }, - order: { orgChild3Order: "ASC" }, + order: { + orgChild3Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -744,7 +768,13 @@ export class ReportController extends Controller { const orgChild3Ids = orgChild3Data.map((c) => c.id) || null; const orgChild4Data = await this.child4Repository.find({ where: { orgChild3: In(orgChild3Ids) }, - order: { orgChild4Order: "ASC" }, + order: { + orgChild4Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -1348,7 +1378,13 @@ export class ReportController extends Controller { // ดึง orgChild1 และลูก (orgChild2, orgChild3, orgChild4) const orgChild1Data = await this.child1Repository.find({ where: { id: _nodeId }, - order: { orgChild1Order: "ASC" }, + order: { + orgChild1Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -1365,7 +1401,13 @@ export class ReportController extends Controller { const orgChild1Ids = orgChild1Data.map((c) => c.id) || null; const orgChild2Data = await this.child2Repository.find({ where: { orgChild1: In(orgChild1Ids) }, - order: { orgChild2Order: "ASC" }, + order: { + orgChild2Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -1377,7 +1419,13 @@ export class ReportController extends Controller { const orgChild2Ids = orgChild2Data.map((c) => c.id) || null; const orgChild3Data = await this.child3Repository.find({ where: { orgChild2: In(orgChild2Ids) }, - order: { orgChild3Order: "ASC" }, + order: { + orgChild3Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -1389,7 +1437,13 @@ export class ReportController extends Controller { const orgChild3Ids = orgChild3Data.map((c) => c.id) || null; const orgChild4Data = await this.child4Repository.find({ where: { orgChild3: In(orgChild3Ids) }, - order: { orgChild4Order: "ASC" }, + order: { + orgChild4Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -1869,8 +1923,16 @@ export class ReportController extends Controller { case 2: { // ดึง orgChild2 และลูก (orgChild3, orgChild4) const orgChild2Data = await this.child2Repository.find({ - where: { id: _nodeId }, - order: { orgChild2Order: "ASC" }, + where: { + id: _nodeId, + }, + order: { + orgChild2Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -1887,7 +1949,13 @@ export class ReportController extends Controller { const orgChild2Ids = orgChild2Data.map((c) => c.id) || null; const orgChild3Data = await this.child3Repository.find({ where: { orgChild2: In(orgChild2Ids) }, - order: { orgChild3Order: "ASC" }, + order: { + orgChild3Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -1899,7 +1967,13 @@ export class ReportController extends Controller { const orgChild3Ids = orgChild3Data.map((c) => c.id) || null; const orgChild4Data = await this.child4Repository.find({ where: { orgChild3: In(orgChild3Ids) }, - order: { orgChild4Order: "ASC" }, + order: { + orgChild4Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -2263,7 +2337,13 @@ export class ReportController extends Controller { // ดึง orgChild3 และลูก (orgChild4) const orgChild3Data = await this.child3Repository.find({ where: { id: _nodeId }, - order: { orgChild3Order: "ASC" }, + order: { + orgChild3Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -2280,7 +2360,13 @@ export class ReportController extends Controller { const orgChild3Ids = orgChild3Data.map((c) => c.id) || null; const orgChild4Data = await this.child4Repository.find({ where: { orgChild3: In(orgChild3Ids) }, - order: { orgChild4Order: "ASC" }, + order: { + orgChild4Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", @@ -2527,7 +2613,13 @@ export class ReportController extends Controller { // ดึงข้อมูล orgChild4 ตาม nodeId const orgChild4Data = await this.child4Repository.find({ where: { id: _nodeId }, - order: { orgChild4Order: "ASC" }, + order: { + orgChild4Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + createdAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.positions", From d823a448bc0c71fa8e972a7759d0d58fb7792ed9 Mon Sep 17 00:00:00 2001 From: Bright Date: Wed, 17 Sep 2025 18:16:39 +0700 Subject: [PATCH 11/39] =?UTF-8?q?=E0=B8=9B=E0=B8=A3=E0=B8=B1=E0=B8=9A?= =?UTF-8?q?=E0=B9=80=E0=B8=9B=E0=B9=87=E0=B8=99=20POST=20=E0=B9=81?= =?UTF-8?q?=E0=B8=97=E0=B8=99=20GET=20>>=20report=202=203=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/ReportController.ts | 45 ++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index 2c4be4c4..a5df992d 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -3469,8 +3469,16 @@ export class ReportController extends Controller { * @summary Report2 * */ - @Get("report2/{rootId}") - async findReport2(@Path() rootId: string) { + @Post("report2") + async NewReport2( + @Body() + reqBody: { + node: number; + nodeId: string; + } + ) { + // @Get("report2/{rootId}") + // async findReport2(@Path() rootId: string) { // const orgRevision = await this.orgRevisionRepository.findOne({ // where: { orgRevisionIsDraft: true, orgRevisionIsCurrent: false }, // relations: ["orgRoots"], @@ -3481,7 +3489,8 @@ export class ReportController extends Controller { // (async () => { const orgRootData = await this.orgRootRepository.find({ where: { - id: rootId, + id: reqBody.nodeId, + // id: rootId, // orgRevisionId: orgRevision.id, }, order: { orgRootOrder: "ASC" }, @@ -7822,8 +7831,16 @@ export class ReportController extends Controller { * @summary Report3 * */ - @Get("report3/{rootId}") - async findReport3(@Path() rootId: string) { + @Post("report3") + async NewReport3( + @Body() + reqBody: { + node: number; + nodeId: string; + } + ) { + // @Get("report3/{rootId}") + // async findReport3(@Path() rootId: string) { // const orgRevision = await this.orgRevisionRepository.findOne({ // where: { orgRevisionIsDraft: true, orgRevisionIsCurrent: false }, // relations: ["orgRoots"], @@ -7834,7 +7851,7 @@ export class ReportController extends Controller { const orgRootData = await this.orgRootRepository.find({ where: { - id: rootId, + id: reqBody.nodeId, // orgRevisionId: orgRevision.id, }, order: { orgRootOrder: "ASC" }, @@ -9063,13 +9080,21 @@ export class ReportController extends Controller { }); } - @Get("report4/{rootId}") - async findReport4(@Path() rootId: string) { + @Post("report4") + async NewReport4( + @Body() + reqBody: { + node: number; + nodeId: string; + } + ) { + // @Get("report4/{rootId}") + // async findReport4(@Path() rootId: string) { const orgRootData = await this.orgRootRepository.findOne({ - where: { id: rootId }, + where: { id: reqBody.nodeId }, }); if (!orgRootData) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); - + let rootId:string = reqBody.nodeId; const posMaster = await this.posMasterRepository .createQueryBuilder("posMaster") .leftJoinAndSelect("posMaster.positions", "position") From aa386455612c3a3cef07840aff9845e5e3d69423 Mon Sep 17 00:00:00 2001 From: adisak Date: Thu, 18 Sep 2025 10:42:30 +0700 Subject: [PATCH 12/39] fix bug sort position org --- src/controllers/PositionController.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 21e79209..4deffbaa 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -2355,7 +2355,7 @@ export class PositionController extends Controller { .addOrderBy("orgChild3.orgChild3Order", "ASC") .addOrderBy("orgChild4.orgChild4Order", "ASC") .addOrderBy("posMaster.posMasterOrder", "ASC") - .addOrderBy("posMaster.createdAt", "ASC") + .addOrderBy("posMaster.posMasterCreatedAt", "ASC") .skip((body.page - 1) * body.pageSize) .take(body.pageSize) .getManyAndCount(); From 489b67945bac2ae7bba8393565695d272101ef45 Mon Sep 17 00:00:00 2001 From: adisak Date: Thu, 18 Sep 2025 10:51:43 +0700 Subject: [PATCH 13/39] update report --- src/controllers/ReportController.ts | 30 ++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index a5df992d..c937c84e 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -695,7 +695,7 @@ export class ReportController extends Controller { orgRootOrder: "ASC", posMasters:{ posMasterOrder: "ASC", - createdAt: "ASC" + posMasterCreatedAt: "ASC" } }, relations: [ @@ -718,7 +718,7 @@ export class ReportController extends Controller { orgChild1Order: "ASC", posMasters:{ posMasterOrder: "ASC", - createdAt: "ASC" + posMasterCreatedAt: "ASC" } }, relations: [ @@ -736,7 +736,7 @@ export class ReportController extends Controller { orgChild2Order: "ASC", posMasters:{ posMasterOrder: "ASC", - createdAt: "ASC" + posMasterCreatedAt: "ASC" } }, relations: [ @@ -754,7 +754,7 @@ export class ReportController extends Controller { orgChild3Order: "ASC", posMasters:{ posMasterOrder: "ASC", - createdAt: "ASC" + posMasterCreatedAt: "ASC" } }, relations: [ @@ -772,7 +772,7 @@ export class ReportController extends Controller { orgChild4Order: "ASC", posMasters:{ posMasterOrder: "ASC", - createdAt: "ASC" + posMasterCreatedAt: "ASC" } }, relations: [ @@ -1382,7 +1382,7 @@ export class ReportController extends Controller { orgChild1Order: "ASC", posMasters:{ posMasterOrder: "ASC", - createdAt: "ASC" + posMasterCreatedAt: "ASC" } }, relations: [ @@ -1405,7 +1405,7 @@ export class ReportController extends Controller { orgChild2Order: "ASC", posMasters:{ posMasterOrder: "ASC", - createdAt: "ASC" + posMasterCreatedAt: "ASC" } }, relations: [ @@ -1423,7 +1423,7 @@ export class ReportController extends Controller { orgChild3Order: "ASC", posMasters:{ posMasterOrder: "ASC", - createdAt: "ASC" + posMasterCreatedAt: "ASC" } }, relations: [ @@ -1441,7 +1441,7 @@ export class ReportController extends Controller { orgChild4Order: "ASC", posMasters:{ posMasterOrder: "ASC", - createdAt: "ASC" + posMasterCreatedAt: "ASC" } }, relations: [ @@ -1930,7 +1930,7 @@ export class ReportController extends Controller { orgChild2Order: "ASC", posMasters:{ posMasterOrder: "ASC", - createdAt: "ASC" + posMasterCreatedAt: "ASC" } }, relations: [ @@ -1953,7 +1953,7 @@ export class ReportController extends Controller { orgChild3Order: "ASC", posMasters:{ posMasterOrder: "ASC", - createdAt: "ASC" + posMasterCreatedAt: "ASC" } }, relations: [ @@ -1971,7 +1971,7 @@ export class ReportController extends Controller { orgChild4Order: "ASC", posMasters:{ posMasterOrder: "ASC", - createdAt: "ASC" + posMasterCreatedAt: "ASC" } }, relations: [ @@ -2341,7 +2341,7 @@ export class ReportController extends Controller { orgChild3Order: "ASC", posMasters:{ posMasterOrder: "ASC", - createdAt: "ASC" + posMasterCreatedAt: "ASC" } }, relations: [ @@ -2364,7 +2364,7 @@ export class ReportController extends Controller { orgChild4Order: "ASC", posMasters:{ posMasterOrder: "ASC", - createdAt: "ASC" + posMasterCreatedAt: "ASC" } }, relations: [ @@ -2617,7 +2617,7 @@ export class ReportController extends Controller { orgChild4Order: "ASC", posMasters:{ posMasterOrder: "ASC", - createdAt: "ASC" + posMasterCreatedAt: "ASC" } }, relations: [ From 4fd22aac36acecb3d925429040a130a1acb61b31 Mon Sep 17 00:00:00 2001 From: Bright Date: Thu, 18 Sep 2025 11:13:46 +0700 Subject: [PATCH 14/39] =?UTF-8?q?=E0=B8=9B=E0=B8=A3=E0=B8=B1=E0=B8=9A=20re?= =?UTF-8?q?port=202=203=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/ReportController.ts | 215 ++++++++++++++++++++++++++-- 1 file changed, 206 insertions(+), 9 deletions(-) diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index a5df992d..036e6285 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -3487,9 +3487,70 @@ export class ReportController extends Controller { // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); // } // (async () => { + let nodeId:string = reqBody.nodeId ? reqBody.nodeId : ""; + let node:number = reqBody.node ? reqBody.node : 0; + if(nodeId === "" || node < 0 || node > 4) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + // ดึงแค่ rootId ก่อน ถ้าลูกค้า confirm ค่อยปรับ + switch (node) { + case 0: { + const orgRoot = await this.orgRootRepository.findOne({ + select: ["id"], + where: { id: nodeId } + }); + if (!orgRoot) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + nodeId = orgRoot.id; + break; + } + case 1: { + const orgChild1 = await this.child1Repository.findOne({ + select: ["id", "orgRootId"], + where: { id: nodeId } + }); + if (!orgChild1) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + nodeId = orgChild1.orgRootId; + break; + } + case 2: { + const orgChild2 = await this.child2Repository.findOne({ + select: ["id", "orgRootId"], + where: { id: nodeId } + }); + if (!orgChild2) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + nodeId = orgChild2.orgRootId; + break; + } + case 3: { + const orgChild3 = await this.child3Repository.findOne({ + select: ["id", "orgRootId"], + where: { id: nodeId } + }); + if (!orgChild3) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + nodeId = orgChild3.orgRootId; + break; + } + case 4: { + const orgChild4 = await this.child4Repository.findOne({ + select: ["id", "orgRootId"], + where: { id: nodeId } + }); + if (!orgChild4) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + nodeId = orgChild4.orgRootId; + break; + } + default: + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + const orgRootData = await this.orgRootRepository.find({ where: { - id: reqBody.nodeId, + id: nodeId, // id: rootId, // orgRevisionId: orgRevision.id, }, @@ -7848,10 +7909,70 @@ export class ReportController extends Controller { // if (!orgRevision) { // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); // } + let nodeId:string = reqBody.nodeId ? reqBody.nodeId : ""; + let node:number = reqBody.node ? reqBody.node : 0; + if(nodeId === "" || node < 0 || node > 4) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + // ดึงแค่ rootId ก่อน ถ้าลูกค้า confirm ค่อยปรับ + switch (node) { + case 0: { + const orgRoot = await this.orgRootRepository.findOne({ + select: ["id"], + where: { id: nodeId } + }); + if (!orgRoot) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + nodeId = orgRoot.id; + break; + } + case 1: { + const orgChild1 = await this.child1Repository.findOne({ + select: ["id", "orgRootId"], + where: { id: nodeId } + }); + if (!orgChild1) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + nodeId = orgChild1.orgRootId; + break; + } + case 2: { + const orgChild2 = await this.child2Repository.findOne({ + select: ["id", "orgRootId"], + where: { id: nodeId } + }); + if (!orgChild2) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + nodeId = orgChild2.orgRootId; + break; + } + case 3: { + const orgChild3 = await this.child3Repository.findOne({ + select: ["id", "orgRootId"], + where: { id: nodeId } + }); + if (!orgChild3) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + nodeId = orgChild3.orgRootId; + break; + } + case 4: { + const orgChild4 = await this.child4Repository.findOne({ + select: ["id", "orgRootId"], + where: { id: nodeId } + }); + if (!orgChild4) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + nodeId = orgChild4.orgRootId; + break; + } + default: + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } const orgRootData = await this.orgRootRepository.find({ where: { - id: reqBody.nodeId, + id: nodeId, // orgRevisionId: orgRevision.id, }, order: { orgRootOrder: "ASC" }, @@ -9090,17 +9211,93 @@ export class ReportController extends Controller { ) { // @Get("report4/{rootId}") // async findReport4(@Path() rootId: string) { - const orgRootData = await this.orgRootRepository.findOne({ - where: { id: reqBody.nodeId }, - }); - if (!orgRootData) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); - let rootId:string = reqBody.nodeId; + + let nodeId:string = reqBody.nodeId ? reqBody.nodeId : ""; + let node:number = reqBody.node ? reqBody.node : 0; + let rootName:string = ""; + let whereCase: string = ""; + let params: any = {}; + + if(nodeId === "" || node < 0 || node > 4) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + switch (node) { + case 0: { + const orgRoot = await this.orgRootRepository.findOne({ + select: ["id", "orgRootName"], + where: { id: nodeId } + }); + if (!orgRoot) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + rootName = orgRoot.orgRootName; + whereCase = "posMaster.orgRootId = :nodeId"; + params = { nodeId }; + break; + } + case 1: { + const orgChild1 = await this.child1Repository.findOne({ + select: ["id", "orgChild1Name"], + where: { id: nodeId } + }); + if (!orgChild1) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + rootName = orgChild1.orgChild1Name; + whereCase = "posMaster.orgChild1Id = :nodeId"; + params = { nodeId }; + break; + } + case 2: { + const orgChild2 = await this.child2Repository.findOne({ + select: ["id", "orgChild2Name"], + where: { id: nodeId } + }); + if (!orgChild2) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + rootName = orgChild2.orgChild2Name; + whereCase = "posMaster.orgChild2Id = :nodeId"; + params = { nodeId }; + break; + } + case 3: { + const orgChild3 = await this.child3Repository.findOne({ + select: ["id", "orgChild3Name"], + where: { id: nodeId } + }); + if (!orgChild3) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + rootName = orgChild3.orgChild3Name; + whereCase = "posMaster.orgChild3Id = :nodeId"; + params = { nodeId }; + break; + } + case 4: { + const orgChild4 = await this.child4Repository.findOne({ + select: ["id", "orgChild4Name"], + where: { id: nodeId } + }); + if (!orgChild4) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + rootName = orgChild4.orgChild4Name; + whereCase = "posMaster.orgChild4Id = :nodeId"; + params = { nodeId }; + break; + } + default: + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + + // const orgRootData = await this.orgRootRepository.findOne({ + // where: { id: reqBody.nodeId }, + // }); + // if (!orgRootData) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + // let rootId:string = reqBody.nodeId; const posMaster = await this.posMasterRepository .createQueryBuilder("posMaster") .leftJoinAndSelect("posMaster.positions", "position") .leftJoinAndSelect("position.posType", "posType") .leftJoinAndSelect("position.posLevel", "posLevel") - .where("posMaster.orgRootId = :rootId", { rootId }) + // .where("posMaster.orgRootId = :rootId", { rootId }) + .where(whereCase, params) .orderBy("posType.posTypeRank", "ASC") .addOrderBy("posLevel.posLevelRank", "ASC") .getMany(); @@ -9199,7 +9396,7 @@ export class ReportController extends Controller { reportName: "report4", data: { dateCurrent: Extension.ToThaiShortDate(new Date()), - rootName: orgRootData ? orgRootData.orgRootName : "-", + rootName: rootName ? rootName : "-", data: _reslut, }, }); From 68245072e2ed6cfa4408feccfa82e1c7e9b366fa Mon Sep 17 00:00:00 2001 From: Bright Date: Thu, 18 Sep 2025 16:56:58 +0700 Subject: [PATCH 15/39] =?UTF-8?q?Fix=20=E0=B8=A3=E0=B8=B2=E0=B8=A2?= =?UTF-8?q?=E0=B8=87=E0=B8=B2=E0=B8=99=20"=20=E0=B8=9A=E0=B8=B1=E0=B8=8D?= =?UTF-8?q?=E0=B8=8A=E0=B8=B5=202"=20=E0=B8=82=E0=B9=89=E0=B8=AD=E0=B8=A1?= =?UTF-8?q?=E0=B8=B9=E0=B8=A5=E0=B9=81=E0=B8=AA=E0=B8=94=E0=B8=87=E0=B9=84?= =?UTF-8?q?=E0=B8=A1=E0=B9=88=E0=B8=84=E0=B8=A3=E0=B8=9A=20#164?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/ReportController.ts | 208 ++++++++++++++++++++++++++-- 1 file changed, 195 insertions(+), 13 deletions(-) diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index a7c71883..9e8063a9 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -3562,8 +3562,9 @@ export class ReportController extends Controller { "posMasters.orgChild2", "posMasters.orgChild3", "posMasters.orgChild4", - "posMasters.next_holder", - "posMasters.next_holder", + "posMasters.current_holder", + "posMasters.current_holder.profileSalary", + "posMasters.current_holder.profileEducations", "posMasters.next_holder", "posMasters.next_holder.posLevel", "posMasters.next_holder.posType", @@ -3596,6 +3597,9 @@ export class ReportController extends Controller { "posMasters.orgChild2", "posMasters.orgChild3", "posMasters.orgChild4", + "posMasters.current_holder", + "posMasters.current_holder.profileSalary", + "posMasters.current_holder.profileEducations", "posMasters.next_holder", "posMasters.next_holder.posLevel", "posMasters.next_holder.posType", @@ -3628,6 +3632,9 @@ export class ReportController extends Controller { "posMasters.orgChild2", "posMasters.orgChild3", "posMasters.orgChild4", + "posMasters.current_holder", + "posMasters.current_holder.profileSalary", + "posMasters.current_holder.profileEducations", "posMasters.next_holder", "posMasters.next_holder.posLevel", "posMasters.next_holder.posType", @@ -3660,6 +3667,9 @@ export class ReportController extends Controller { "posMasters.orgChild2", "posMasters.orgChild3", "posMasters.orgChild4", + "posMasters.current_holder", + "posMasters.current_holder.profileSalary", + "posMasters.current_holder.profileEducations", "posMasters.next_holder", "posMasters.next_holder.posLevel", "posMasters.next_holder.posType", @@ -3693,6 +3703,9 @@ export class ReportController extends Controller { "posMasters.orgChild2", "posMasters.orgChild3", "posMasters.orgChild4", + "posMasters.current_holder", + "posMasters.current_holder.profileSalary", + "posMasters.current_holder.profileEducations", "posMasters.next_holder", "posMasters.next_holder.posLevel", "posMasters.next_holder.posType", @@ -3826,6 +3839,22 @@ export class ReportController extends Controller { education = _education[0]; } } + else { + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileEducations != null && + posMaster.current_holder.profileEducations.length > 0 + ) { + let _education: any = posMaster.current_holder.profileEducations.sort( + (a, b) => + (b.finishDate == null ? 0 : b.finishDate.getTime()) - + (a.finishDate == null ? 0 : a.finishDate.getTime()), + ); + if (_education.length > 0) { + education = _education[0]; + } + } + } let salary: any = ""; if ( posMaster.next_holder != null && @@ -3841,7 +3870,22 @@ export class ReportController extends Controller { salary = _salary[0]; } } - + else { + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileSalary != null && + posMaster.current_holder.profileSalary.length > 0 + ) { + let _salary: any = posMaster.current_holder.profileSalary.sort( + (a, b) => + (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - + (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), + ); + if (_salary.length > 0) { + salary = _salary[0]; + } + } + } let node = { posMasterOrder: posMaster.posMasterOrder, // isSit: posMaster.isSit, // @@ -3919,7 +3963,9 @@ export class ReportController extends Controller { : "-", profileFullname: posMaster.next_holder == null - ? "- ว่าง -" + ? posMaster.current_holder == null + ? "- ว่าง -" + : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}` : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, profilePosMasterNo: posMaster.next_holder == null @@ -4200,6 +4246,22 @@ export class ReportController extends Controller { education = _education[0]; } } + else { + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileEducations != null && + posMaster.current_holder.profileEducations.length > 0 + ) { + let _education: any = posMaster.current_holder.profileEducations.sort( + (a, b) => + (b.finishDate == null ? 0 : b.finishDate.getTime()) - + (a.finishDate == null ? 0 : a.finishDate.getTime()), + ); + if (_education.length > 0) { + education = _education[0]; + } + } + } let salary: any = ""; if ( posMaster.next_holder != null && @@ -4215,6 +4277,22 @@ export class ReportController extends Controller { salary = _salary[0]; } } + else { + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileSalary != null && + posMaster.current_holder.profileSalary.length > 0 + ) { + let _salary: any = posMaster.current_holder.profileSalary.sort( + (a, b) => + (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - + (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), + ); + if (_salary.length > 0) { + salary = _salary[0]; + } + } + } let node = { posMasterOrder: posMaster.posMasterOrder, // @@ -4293,7 +4371,9 @@ export class ReportController extends Controller { : "-", profileFullname: posMaster.next_holder == null - ? "- ว่าง -" + ? posMaster.current_holder == null + ? "- ว่าง -" + : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}` : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, profilePosMasterNo: posMaster.next_holder == null @@ -4582,6 +4662,22 @@ export class ReportController extends Controller { education = _education[0]; } } + else { + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileEducations != null && + posMaster.current_holder.profileEducations.length > 0 + ) { + let _education: any = posMaster.current_holder.profileEducations.sort( + (a, b) => + (b.finishDate == null ? 0 : b.finishDate.getTime()) - + (a.finishDate == null ? 0 : a.finishDate.getTime()), + ); + if (_education.length > 0) { + education = _education[0]; + } + } + } let salary: any = ""; if ( posMaster.next_holder != null && @@ -4597,6 +4693,22 @@ export class ReportController extends Controller { salary = _salary[0]; } } + else { + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileSalary != null && + posMaster.current_holder.profileSalary.length > 0 + ) { + let _salary: any = posMaster.current_holder.profileSalary.sort( + (a, b) => + (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - + (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), + ); + if (_salary.length > 0) { + salary = _salary[0]; + } + } + } let node = { posMasterOrder: posMaster.posMasterOrder, // @@ -4675,7 +4787,9 @@ export class ReportController extends Controller { : "-", profileFullname: posMaster.next_holder == null - ? "- ว่าง -" + ? posMaster.current_holder == null + ? "- ว่าง -" + : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}` : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, profilePosMasterNo: posMaster.next_holder == null @@ -4980,6 +5094,22 @@ export class ReportController extends Controller { education = _education[0]; } } + else { + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileEducations != null && + posMaster.current_holder.profileEducations.length > 0 + ) { + let _education: any = posMaster.current_holder.profileEducations.sort( + (a, b) => + (b.finishDate == null ? 0 : b.finishDate.getTime()) - + (a.finishDate == null ? 0 : a.finishDate.getTime()), + ); + if (_education.length > 0) { + education = _education[0]; + } + } + } let salary: any = ""; if ( posMaster.next_holder != null && @@ -4995,6 +5125,22 @@ export class ReportController extends Controller { salary = _salary[0]; } } + else { + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileSalary != null && + posMaster.current_holder.profileSalary.length > 0 + ) { + let _salary: any = posMaster.current_holder.profileSalary.sort( + (a, b) => + (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - + (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), + ); + if (_salary.length > 0) { + salary = _salary[0]; + } + } + } let node = { posMasterOrder: posMaster.posMasterOrder, // @@ -5073,7 +5219,9 @@ export class ReportController extends Controller { : "-", profileFullname: posMaster.next_holder == null - ? "- ว่าง -" + ? posMaster.current_holder == null + ? "- ว่าง -" + : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}` : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, profilePosMasterNo: posMaster.next_holder == null @@ -5382,6 +5530,22 @@ export class ReportController extends Controller { education = _education[0]; } } + else { + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileEducations != null && + posMaster.current_holder.profileEducations.length > 0 + ) { + let _education: any = posMaster.current_holder.profileEducations.sort( + (a, b) => + (b.finishDate == null ? 0 : b.finishDate.getTime()) - + (a.finishDate == null ? 0 : a.finishDate.getTime()), + ); + if (_education.length > 0) { + education = _education[0]; + } + } + } let salary: any = ""; if ( posMaster.next_holder != null && @@ -5397,6 +5561,22 @@ export class ReportController extends Controller { salary = _salary[0]; } } + else { + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileSalary != null && + posMaster.current_holder.profileSalary.length > 0 + ) { + let _salary: any = posMaster.current_holder.profileSalary.sort( + (a, b) => + (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - + (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), + ); + if (_salary.length > 0) { + salary = _salary[0]; + } + } + } let node = { posMasterOrder: posMaster.posMasterOrder, // @@ -5475,7 +5655,9 @@ export class ReportController extends Controller { : "-", profileFullname: posMaster.next_holder == null - ? "- ว่าง -" + ? posMaster.current_holder == null + ? "- ว่าง -" + : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}` : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, profilePosMasterNo: posMaster.next_holder == null @@ -5693,11 +5875,11 @@ export class ReportController extends Controller { }), ); _node = null; - } - } - } - } - } + } // End for orgChild4 + } // End for orgChild3 + } // End for orgChild2 + } // End for orgChild1 + } // End for orgRoot // const metaData = { template: "report2", reportName: "report2", data: { data } }; // sendWebSocket(metaData); From d58464ce91a1aa64cb76ffd498d3222fff854e7d Mon Sep 17 00:00:00 2001 From: adisak Date: Thu, 18 Sep 2025 17:08:38 +0700 Subject: [PATCH 16/39] #165 --- src/services/PositionService.ts | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/services/PositionService.ts b/src/services/PositionService.ts index 3003cdb1..9d0d5c3b 100644 --- a/src/services/PositionService.ts +++ b/src/services/PositionService.ts @@ -1,6 +1,7 @@ import { AppDataSource } from "../database/data-source"; import { EmployeePosMaster } from "../entities/EmployeePosMaster"; import { EmployeeTempPosMaster } from "../entities/EmployeeTempPosMaster"; +import { OrgRevision } from "../entities/OrgRevision"; import { PosMaster } from "../entities/PosMaster"; import { PosMasterEmployeeHistory } from "../entities/PosMasterEmployeeHistory"; import { PosMasterEmployeeTempHistory } from "../entities/PosMasterEmployeeTempHistory"; @@ -17,6 +18,7 @@ export async function CreatePosMasterHistoryOfficer( await AppDataSource.transaction(async (manager) => { const repoPosmaster = manager.getRepository(PosMaster); const repoHistory = manager.getRepository(PosMasterHistory); + const repoOrgRevision = manager.getRepository(OrgRevision); const pm = await repoPosmaster.findOne({ where: { id: posMasterId }, @@ -31,11 +33,20 @@ export async function CreatePosMasterHistoryOfficer( "orgChild3", "orgChild4", "current_holder", + "next_holder", ], }); if (!pm) return false; if (!pm.ancestorDNA) return false; + + const checkCurrentRevision = await repoOrgRevision.findOne({ + where:{ + id: pm.orgRevisionId, + orgRevisionIsCurrent: true, + orgRevisionIsDraft: false + } + }) const _null: any = null; const h = new PosMasterHistory(); const selectedPosition = @@ -44,9 +55,15 @@ export async function CreatePosMasterHistoryOfficer( : null; h.ancestorDNA = pm.ancestorDNA ? pm.ancestorDNA : _null; if(!type || type != "DELETE"){ - h.prefix = pm.current_holder?.prefix || _null; - h.firstName = pm.current_holder?.firstName || _null; - h.lastName = pm.current_holder?.lastName || _null; + if(checkCurrentRevision){ + h.prefix = pm.current_holder?.prefix || _null; + h.firstName = pm.current_holder?.firstName || _null; + h.lastName = pm.current_holder?.lastName || _null; + }else{ + h.prefix = pm.next_holder?.prefix || _null; + h.firstName = pm.next_holder?.firstName || _null; + h.lastName = pm.next_holder?.lastName || _null; + } h.position = selectedPosition?.positionName ?? _null; h.posType = selectedPosition?.posType?.posTypeName ?? _null; h.posLevel = selectedPosition?.posLevel?.posLevelName ?? _null; From 479f703ca784173d5c1832225d798b747644a596 Mon Sep 17 00:00:00 2001 From: mamoss <> Date: Fri, 19 Sep 2025 01:02:10 +0700 Subject: [PATCH 17/39] test report --- .github/workflows/release.yaml | 72 +- src/controllers/ReportController.ts | 4858 +++++++++++++++------------ 2 files changed, 2758 insertions(+), 2172 deletions(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index c4b02bcc..21dc9ba3 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -67,40 +67,40 @@ jobs: docker compose pull docker compose up -d echo "${{ steps.gen_ver.outputs.image_ver }}"> success - - name: Notify Discord Success - if: success() - run: | - curl -H "Content-Type: application/json" \ - -X POST \ - -d '{ - "embeds": [{ - "title": "✅ Deployment Success!", - "description": "**Details:**\n- Image: `${{env.IMAGE_NAME}}`\n- Version: `${{ steps.gen_ver.outputs.image_ver }}`\n- Deployed by: `${{github.actor}}`", - "color": 3066993, - "footer": { - "text": "Release Notification", - "icon_url": "https://example.com/success-icon.png" - }, - "timestamp": "'$(date -u +%Y-%m-%dT%H:%M:%SZ)'" - }] - }' \ - ${{ secrets.DISCORD_WEBHOOK }} + # - name: Notify Discord Success + # if: success() + # run: | + # curl -H "Content-Type: application/json" \ + # -X POST \ + # -d '{ + # "embeds": [{ + # "title": "✅ Deployment Success!", + # "description": "**Details:**\n- Image: `${{env.IMAGE_NAME}}`\n- Version: `${{ steps.gen_ver.outputs.image_ver }}`\n- Deployed by: `${{github.actor}}`", + # "color": 3066993, + # "footer": { + # "text": "Release Notification", + # "icon_url": "https://example.com/success-icon.png" + # }, + # "timestamp": "'$(date -u +%Y-%m-%dT%H:%M:%SZ)'" + # }] + # }' \ + # ${{ secrets.DISCORD_WEBHOOK }} - - name: Notify Discord Failure - if: failure() - run: | - curl -H "Content-Type: application/json" \ - -X POST \ - -d '{ - "embeds": [{ - "title": "❌ Deployment Failed!", - "description": "**Details:**\n- Image: `${{env.IMAGE_NAME}}`\n- Version: `${{ steps.gen_ver.outputs.image_ver }}`\n- Attempted by: `${{github.actor}}`", - "color": 15158332, - "footer": { - "text": "Release Notification", - "icon_url": "https://example.com/failure-icon.png" - }, - "timestamp": "'$(date -u +%Y-%m-%dT%H:%M:%SZ)'" - }] - }' \ - ${{ secrets.DISCORD_WEBHOOK }} + # - name: Notify Discord Failure + # if: failure() + # run: | + # curl -H "Content-Type: application/json" \ + # -X POST \ + # -d '{ + # "embeds": [{ + # "title": "❌ Deployment Failed!", + # "description": "**Details:**\n- Image: `${{env.IMAGE_NAME}}`\n- Version: `${{ steps.gen_ver.outputs.image_ver }}`\n- Attempted by: `${{github.actor}}`", + # "color": 15158332, + # "footer": { + # "text": "Release Notification", + # "icon_url": "https://example.com/failure-icon.png" + # }, + # "timestamp": "'$(date -u +%Y-%m-%dT%H:%M:%SZ)'" + # }] + # }' \ + # ${{ secrets.DISCORD_WEBHOOK }} diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index 9e8063a9..4b993247 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -9,7 +9,7 @@ import { Path, Query, Post, - Body + Body, } from "tsoa"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; @@ -30,6 +30,9 @@ import Extension from "../interfaces/extension"; import { LeaveType } from "../entities/LeaveType"; import HttpStatus from "../interfaces/http-status"; import { Profile } from "../entities/Profile"; +import { Position } from "../entities/Position"; +import { ProfileEducation } from "../entities/ProfileEducation"; +import { ProfileSalary } from "../entities/ProfileSalary"; import { viewRegistryOfficer } from "../entities/view/viewRegistryOfficer"; import { viewRegistryEmployee } from "../entities/view/viewRegistryEmployee"; import { EmployeeTempPosMaster } from "../entities/EmployeeTempPosMaster"; @@ -56,6 +59,9 @@ export class ReportController extends Controller { private profileRepository = AppDataSource.getRepository(Profile); private empPosMasterRepository = AppDataSource.getRepository(EmployeePosMaster); private empTempPosMasterRepository = AppDataSource.getRepository(EmployeeTempPosMaster); + private positionRepository = AppDataSource.getRepository(Position); + private profileEducationRepository = AppDataSource.getRepository(ProfileEducation); + private profileSalaryRepository = AppDataSource.getRepository(ProfileSalary); /** * API รายงานสถิติข้อมูลข้าราชการ กทม. สามัญ @@ -674,15 +680,15 @@ export class ReportController extends Controller { reqBody: { node: number; nodeId: string; - } + }, ) { - let _nodeId:string = reqBody.nodeId ? reqBody.nodeId : ""; - let _node:number = reqBody.node ? reqBody.node : 0; - if(_nodeId === "" || _node < 0 || _node > 4) { + let _nodeId: string = reqBody.nodeId ? reqBody.nodeId : ""; + let _node: number = reqBody.node ? reqBody.node : 0; + if (_nodeId === "" || _node < 0 || _node > 4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); - } + } - let orgName:string = ""; + let orgName: string = ""; let data = new Array(); let no = 1; @@ -691,12 +697,12 @@ export class ReportController extends Controller { // ดึงข้อมูล orgRoot ตาม nodeId และลูกทั้งหมด (cascade 0..4) const orgRootData = await this.orgRootRepository.find({ where: { id: _nodeId }, - order: { + order: { orgRootOrder: "ASC", - posMasters:{ + posMasters: { posMasterOrder: "ASC", - posMasterCreatedAt: "ASC" - } + posMasterCreatedAt: "ASC", + }, }, relations: [ "posMasters", @@ -714,12 +720,12 @@ export class ReportController extends Controller { const orgRootIds = orgRootData.map((r) => r.id) || null; const orgChild1Data = await this.child1Repository.find({ where: { orgRootId: In(orgRootIds) }, - order: { + order: { orgChild1Order: "ASC", - posMasters:{ + posMasters: { posMasterOrder: "ASC", - posMasterCreatedAt: "ASC" - } + posMasterCreatedAt: "ASC", + }, }, relations: [ "posMasters", @@ -732,12 +738,12 @@ export class ReportController extends Controller { const orgChild1Ids = orgChild1Data.map((c) => c.id) || null; const orgChild2Data = await this.child2Repository.find({ where: { orgChild1: In(orgChild1Ids) }, - order: { + order: { orgChild2Order: "ASC", - posMasters:{ + posMasters: { posMasterOrder: "ASC", - posMasterCreatedAt: "ASC" - } + posMasterCreatedAt: "ASC", + }, }, relations: [ "posMasters", @@ -750,12 +756,12 @@ export class ReportController extends Controller { const orgChild2Ids = orgChild2Data.map((c) => c.id) || null; const orgChild3Data = await this.child3Repository.find({ where: { orgChild2: In(orgChild2Ids) }, - order: { + order: { orgChild3Order: "ASC", - posMasters:{ + posMasters: { posMasterOrder: "ASC", - posMasterCreatedAt: "ASC" - } + posMasterCreatedAt: "ASC", + }, }, relations: [ "posMasters", @@ -768,12 +774,12 @@ export class ReportController extends Controller { const orgChild3Ids = orgChild3Data.map((c) => c.id) || null; const orgChild4Data = await this.child4Repository.find({ where: { orgChild3: In(orgChild3Ids) }, - order: { + order: { orgChild4Order: "ASC", - posMasters:{ + posMasters: { posMasterOrder: "ASC", - posMasterCreatedAt: "ASC" - } + posMasterCreatedAt: "ASC", + }, }, relations: [ "posMasters", @@ -786,21 +792,256 @@ export class ReportController extends Controller { let _nodeTemp: any = null; for (let orgRoot of orgRootData) { - await Promise.all( - orgRoot.posMasters - .filter((x) => x.orgChild1Id == null) - .sort((a, b) => a.posMasterOrder - b.posMasterOrder) - .map(async (posMaster) => { - if (posMaster.orgChild1Id == null) { + const posMastersFiltered = orgRoot.posMasters + .filter((x) => x.orgChild1Id == null) + .sort((a, b) => a.posMasterOrder - b.posMasterOrder); + + for (let posMaster of posMastersFiltered) { + if (posMaster.orgChild1Id == null) { + const positionName = [ + ...new Set( + posMaster.positions + .sort((a: any, b: any) => a.orderNo - b.orderNo) + .map((x) => + x.positionArea ? `${x.positionName} (${x.positionArea})` : x.positionName, + ), + ), + ]; + const posType = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posType != null) + .map((x) => x.posType.posTypeName), + ), + ]; + const posLevel = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posLevel != null) + .map((x) => x.posLevel.posLevelName), + ), + ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName, + ), + ), + ]; + + let node = { + orgTreeName: orgRoot.orgRootName, + orgTreeShortName: orgRoot.orgRootShortName, + posMasterNo: posMaster.posMasterNo, + positionName: positionName.join(" หรือ "), + posType: posType.join(" หรือ "), + posLevel: posLevel.join(" หรือ "), + posExecutive: posExecutive.join(" หรือ "), + reason: posMaster.reason == null ? "" : posMaster.reason, + }; + if (_nodeTemp == null) { + const head = { + posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), + posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + const _head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _nodeTemp.orgTreeShortName || + node.orgTreeName != _nodeTemp.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _nodeTemp.orgTreeShortName + ? "" + : node.orgTreeShortName, + posExecutive: + node.orgTreeName == _nodeTemp.orgTreeName + ? "" + : Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + _nodeTemp = null; + } + const head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(head); + } + no += 1; + _nodeTemp = node; + } + } + _nodeTemp = null; + + for (let orgChild1 of orgChild1Data.filter( + (orgChild1) => orgChild1.orgRootId === orgRoot.id, + )) { + const posMastersFiltered = orgChild1.posMasters + .filter((x) => x.orgChild2Id == null) + .sort((a, b) => a.posMasterOrder - b.posMasterOrder); + + for (let posMaster of posMastersFiltered) { + if (posMaster.orgChild2Id == null) { + const positionName = [ + ...new Set( + posMaster.positions + .sort((a: any, b: any) => a.orderNo - b.orderNo) + .map((x) => + x.positionArea ? `${x.positionName} (${x.positionArea})` : x.positionName, + ), + ), + ]; + const posType = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posType != null) + .map((x) => x.posType.posTypeName), + ), + ]; + const posLevel = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posLevel != null) + .map((x) => x.posLevel.posLevelName), + ), + ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName, + ), + ), + ]; + + let node = { + orgTreeName: orgChild1.orgChild1Name, + orgTreeShortName: orgChild1.orgChild1ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: positionName.join(" หรือ "), + posType: posType.join(" หรือ "), + posLevel: posLevel.join(" หรือ "), + posExecutive: posExecutive.join(" หรือ "), + reason: posMaster.reason == null ? "" : posMaster.reason, + }; + if (_nodeTemp == null) { + const head = { + posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName?.toString() ?? ""), + posExecutive: Extension.ToThaiNumber(node.orgTreeName?.toString() ?? ""), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + const _head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _nodeTemp.orgTreeShortName || + node.orgTreeName != _nodeTemp.orgTreeName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _nodeTemp.orgTreeShortName + ? "" + : node.orgTreeShortName, + posExecutive: + node.orgTreeName == _nodeTemp.orgTreeName + ? "" + : Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + reason: "", + }; + data.push(head); + _nodeTemp = null; + } + const head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: Extension.ToThaiNumber(node.positionName.toString()), + posType: Extension.ToThaiNumber(node.posType.toString()), + posLevel: Extension.ToThaiNumber(node.posLevel.toString()), + reason: Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(head); + } + no += 1; + _nodeTemp = node; + } + } + + for (let orgChild2 of orgChild2Data.filter( + (orgChild2) => orgChild2.orgChild1Id === orgChild1.id, + )) { + const posMastersFiltered = orgChild2.posMasters + .filter((x) => x.orgChild3Id == null) + .sort((a, b) => a.posMasterOrder - b.posMasterOrder); + + for (let posMaster of posMastersFiltered) { + if (posMaster.orgChild3Id == null) { const positionName = [ ...new Set( posMaster.positions .sort((a: any, b: any) => a.orderNo - b.orderNo) .map((x) => - x.positionArea - ? `${x.positionName} (${x.positionArea})` - : x.positionName - ) + x.positionArea ? `${x.positionName} (${x.positionArea})` : x.positionName, + ), ), ]; const posType = [ @@ -824,14 +1065,14 @@ export class ReportController extends Controller { .map((x: any) => x.positionExecutiveField ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` - : x.posExecutive.posExecutiveName - ) + : x.posExecutive.posExecutiveName, + ), ), ]; let node = { - orgTreeName: orgRoot.orgRootName, - orgTreeShortName: orgRoot.orgRootShortName, + orgTreeName: orgChild2.orgChild2Name, + orgTreeShortName: orgChild2.orgChild2ShortName, posMasterNo: posMaster.posMasterNo, positionName: positionName.join(" หรือ "), posType: posType.join(" หรือ "), @@ -841,8 +1082,8 @@ export class ReportController extends Controller { }; if (_nodeTemp == null) { const head = { - posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), - posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), + posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName?.toString() ?? ""), + posExecutive: Extension.ToThaiNumber(node.orgTreeName?.toString() ?? ""), positionName: "", posType: "", posLevel: "", @@ -869,7 +1110,9 @@ export class ReportController extends Controller { ) { const head = { posMasterNo: - node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + node.orgTreeShortName == _nodeTemp.orgTreeShortName + ? "" + : node.orgTreeShortName, posExecutive: node.orgTreeName == _nodeTemp.orgTreeName ? "" @@ -899,245 +1142,11 @@ export class ReportController extends Controller { no += 1; _nodeTemp = node; } - }) - ); - _nodeTemp = null; + } - for (let orgChild1 of orgChild1Data.filter((orgChild1) => orgChild1.orgRootId === orgRoot.id)) { - await Promise.all( - orgChild1.posMasters - .filter((x) => x.orgChild2Id == null) - .sort((a, b) => a.posMasterOrder - b.posMasterOrder) - .map(async (posMaster) => { - if (posMaster.orgChild2Id == null) { - const positionName = [ - ...new Set( - posMaster.positions - .sort((a: any, b: any) => a.orderNo - b.orderNo) - .map((x) => - x.positionArea - ? `${x.positionName} (${x.positionArea})` - : x.positionName - ) - ), - ]; - const posType = [ - ...new Set( - posMaster.positions - .filter((x: any) => x.posType != null) - .map((x) => x.posType.posTypeName), - ), - ]; - const posLevel = [ - ...new Set( - posMaster.positions - .filter((x: any) => x.posLevel != null) - .map((x) => x.posLevel.posLevelName), - ), - ]; - const posExecutive = [ - ...new Set( - posMaster.positions - .filter((x: any) => x.posExecutive != null) - .map((x: any) => - x.positionExecutiveField - ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` - : x.posExecutive.posExecutiveName - ) - ), - ]; - - let node = { - orgTreeName: orgChild1.orgChild1Name, - orgTreeShortName: orgChild1.orgChild1ShortName, - posMasterNo: posMaster.posMasterNo, - positionName: positionName.join(" หรือ "), - posType: posType.join(" หรือ "), - posLevel: posLevel.join(" หรือ "), - posExecutive: posExecutive.join(" หรือ "), - reason: posMaster.reason == null ? "" : posMaster.reason, - }; - if (_nodeTemp == null) { - const head = { - posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName?.toString() ?? ""), - posExecutive: Extension.ToThaiNumber(node.orgTreeName?.toString() ?? ""), - positionName: "", - posType: "", - posLevel: "", - reason: "", - }; - data.push(head); - const _head = { - no: Extension.ToThaiNumber(no.toString()), - posMasterNo: - node.posMasterNo == null - ? "" - : Extension.ToThaiNumber(node.posMasterNo.toString()), - posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), - positionName: Extension.ToThaiNumber(node.positionName.toString()), - posType: Extension.ToThaiNumber(node.posType.toString()), - posLevel: Extension.ToThaiNumber(node.posLevel.toString()), - reason: Extension.ToThaiNumber(node.reason.toString()), - }; - data.push(_head); - } else { - if ( - node.orgTreeShortName != _nodeTemp.orgTreeShortName || - node.orgTreeName != _nodeTemp.orgTreeName - ) { - const head = { - posMasterNo: - node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, - posExecutive: - node.orgTreeName == _nodeTemp.orgTreeName - ? "" - : Extension.ToThaiNumber(node.orgTreeName.toString()), - positionName: "", - posType: "", - posLevel: "", - reason: "", - }; - data.push(head); - _nodeTemp = null; - } - const head = { - no: Extension.ToThaiNumber(no.toString()), - posMasterNo: - node.posMasterNo == null - ? "" - : Extension.ToThaiNumber(node.posMasterNo.toString()), - posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), - positionName: Extension.ToThaiNumber(node.positionName.toString()), - posType: Extension.ToThaiNumber(node.posType.toString()), - posLevel: Extension.ToThaiNumber(node.posLevel.toString()), - reason: Extension.ToThaiNumber(node.reason.toString()), - }; - data.push(head); - } - no += 1; - _nodeTemp = node; - } - }) - ); - - for (let orgChild2 of orgChild2Data.filter((orgChild2) => orgChild2.orgChild1Id === orgChild1.id)) { - await Promise.all( - orgChild2.posMasters - .filter((x) => x.orgChild3Id == null) - .sort((a, b) => a.posMasterOrder - b.posMasterOrder) - .map(async (posMaster) => { - if (posMaster.orgChild3Id == null) { - const positionName = [ - ...new Set( - posMaster.positions - .sort((a: any, b: any) => a.orderNo - b.orderNo) - .map((x) => - x.positionArea - ? `${x.positionName} (${x.positionArea})` - : x.positionName - ) - ), - ]; - const posType = [ - ...new Set( - posMaster.positions - .filter((x: any) => x.posType != null) - .map((x) => x.posType.posTypeName), - ), - ]; - const posLevel = [ - ...new Set( - posMaster.positions - .filter((x: any) => x.posLevel != null) - .map((x) => x.posLevel.posLevelName), - ), - ]; - const posExecutive = [ - ...new Set( - posMaster.positions - .filter((x: any) => x.posExecutive != null) - .map((x: any) => - x.positionExecutiveField - ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` - : x.posExecutive.posExecutiveName - ) - ), - ]; - - let node = { - orgTreeName: orgChild2.orgChild2Name, - orgTreeShortName: orgChild2.orgChild2ShortName, - posMasterNo: posMaster.posMasterNo, - positionName: positionName.join(" หรือ "), - posType: posType.join(" หรือ "), - posLevel: posLevel.join(" หรือ "), - posExecutive: posExecutive.join(" หรือ "), - reason: posMaster.reason == null ? "" : posMaster.reason, - }; - if (_nodeTemp == null) { - const head = { - posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName?.toString() ?? ""), - posExecutive: Extension.ToThaiNumber(node.orgTreeName?.toString() ?? ""), - positionName: "", - posType: "", - posLevel: "", - reason: "", - }; - data.push(head); - const _head = { - no: Extension.ToThaiNumber(no.toString()), - posMasterNo: - node.posMasterNo == null - ? "" - : Extension.ToThaiNumber(node.posMasterNo.toString()), - posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), - positionName: Extension.ToThaiNumber(node.positionName.toString()), - posType: Extension.ToThaiNumber(node.posType.toString()), - posLevel: Extension.ToThaiNumber(node.posLevel.toString()), - reason: Extension.ToThaiNumber(node.reason.toString()), - }; - data.push(_head); - } else { - if ( - node.orgTreeShortName != _nodeTemp.orgTreeShortName || - node.orgTreeName != _nodeTemp.orgTreeName - ) { - const head = { - posMasterNo: - node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, - posExecutive: - node.orgTreeName == _nodeTemp.orgTreeName - ? "" - : Extension.ToThaiNumber(node.orgTreeName.toString()), - positionName: "", - posType: "", - posLevel: "", - reason: "", - }; - data.push(head); - _nodeTemp = null; - } - const head = { - no: Extension.ToThaiNumber(no.toString()), - posMasterNo: - node.posMasterNo == null - ? "" - : Extension.ToThaiNumber(node.posMasterNo.toString()), - posExecutive: Extension.ToThaiNumber(node.posExecutive.toString()), - positionName: Extension.ToThaiNumber(node.positionName.toString()), - posType: Extension.ToThaiNumber(node.posType.toString()), - posLevel: Extension.ToThaiNumber(node.posLevel.toString()), - reason: Extension.ToThaiNumber(node.reason.toString()), - }; - data.push(head); - } - no += 1; - _nodeTemp = node; - } - }) - ); - - for (let orgChild3 of orgChild3Data.filter((orgChild3) => orgChild3.orgChild2Id === orgChild2.id)) { + for (let orgChild3 of orgChild3Data.filter( + (orgChild3) => orgChild3.orgChild2Id === orgChild2.id, + )) { await Promise.all( orgChild3.posMasters .filter((x) => x.orgChild4Id == null) @@ -1151,8 +1160,8 @@ export class ReportController extends Controller { .map((x) => x.positionArea ? `${x.positionName} (${x.positionArea})` - : x.positionName - ) + : x.positionName, + ), ), ]; const posType = [ @@ -1176,8 +1185,8 @@ export class ReportController extends Controller { .map((x: any) => x.positionExecutiveField ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` - : x.posExecutive.posExecutiveName - ) + : x.posExecutive.posExecutiveName, + ), ), ]; @@ -1193,8 +1202,12 @@ export class ReportController extends Controller { }; if (_nodeTemp == null) { const head = { - posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName?.toString() ?? ""), - posExecutive: Extension.ToThaiNumber(node.orgTreeName?.toString() ?? ""), + posMasterNo: Extension.ToThaiNumber( + node.orgTreeShortName?.toString() ?? "", + ), + posExecutive: Extension.ToThaiNumber( + node.orgTreeName?.toString() ?? "", + ), positionName: "", posType: "", posLevel: "", @@ -1221,7 +1234,9 @@ export class ReportController extends Controller { ) { const head = { posMasterNo: - node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + node.orgTreeShortName == _nodeTemp.orgTreeShortName + ? "" + : node.orgTreeShortName, posExecutive: node.orgTreeName == _nodeTemp.orgTreeName ? "" @@ -1251,10 +1266,12 @@ export class ReportController extends Controller { no += 1; _nodeTemp = node; } - }) + }), ); - for (let orgChild4 of orgChild4Data.filter((orgChild4) => orgChild4.orgChild3Id === orgChild3.id)) { + for (let orgChild4 of orgChild4Data.filter( + (orgChild4) => orgChild4.orgChild3Id === orgChild3.id, + )) { await Promise.all( orgChild4.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) @@ -1266,8 +1283,8 @@ export class ReportController extends Controller { .map((x) => x.positionArea ? `${x.positionName} (${x.positionArea})` - : x.positionName - ) + : x.positionName, + ), ), ]; const posType = [ @@ -1291,8 +1308,8 @@ export class ReportController extends Controller { .map((x: any) => x.positionExecutiveField ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` - : x.posExecutive.posExecutiveName - ) + : x.posExecutive.posExecutiveName, + ), ), ]; @@ -1308,8 +1325,12 @@ export class ReportController extends Controller { }; if (_nodeTemp == null) { const head = { - posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName?.toString() ?? ""), - posExecutive: Extension.ToThaiNumber(node.orgTreeName?.toString() ?? ""), + posMasterNo: Extension.ToThaiNumber( + node.orgTreeShortName?.toString() ?? "", + ), + posExecutive: Extension.ToThaiNumber( + node.orgTreeName?.toString() ?? "", + ), positionName: "", posType: "", posLevel: "", @@ -1336,7 +1357,9 @@ export class ReportController extends Controller { ) { const head = { posMasterNo: - node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + node.orgTreeShortName == _nodeTemp.orgTreeShortName + ? "" + : node.orgTreeShortName, posExecutive: node.orgTreeName == _nodeTemp.orgTreeName ? "" @@ -1365,7 +1388,7 @@ export class ReportController extends Controller { } no += 1; _nodeTemp = node; - }) + }), ); } } @@ -1378,12 +1401,12 @@ export class ReportController extends Controller { // ดึง orgChild1 และลูก (orgChild2, orgChild3, orgChild4) const orgChild1Data = await this.child1Repository.find({ where: { id: _nodeId }, - order: { + order: { orgChild1Order: "ASC", - posMasters:{ + posMasters: { posMasterOrder: "ASC", - posMasterCreatedAt: "ASC" - } + posMasterCreatedAt: "ASC", + }, }, relations: [ "posMasters", @@ -1401,12 +1424,12 @@ export class ReportController extends Controller { const orgChild1Ids = orgChild1Data.map((c) => c.id) || null; const orgChild2Data = await this.child2Repository.find({ where: { orgChild1: In(orgChild1Ids) }, - order: { + order: { orgChild2Order: "ASC", - posMasters:{ + posMasters: { posMasterOrder: "ASC", - posMasterCreatedAt: "ASC" - } + posMasterCreatedAt: "ASC", + }, }, relations: [ "posMasters", @@ -1419,12 +1442,12 @@ export class ReportController extends Controller { const orgChild2Ids = orgChild2Data.map((c) => c.id) || null; const orgChild3Data = await this.child3Repository.find({ where: { orgChild2: In(orgChild2Ids) }, - order: { + order: { orgChild3Order: "ASC", - posMasters:{ + posMasters: { posMasterOrder: "ASC", - posMasterCreatedAt: "ASC" - } + posMasterCreatedAt: "ASC", + }, }, relations: [ "posMasters", @@ -1437,12 +1460,12 @@ export class ReportController extends Controller { const orgChild3Ids = orgChild3Data.map((c) => c.id) || null; const orgChild4Data = await this.child4Repository.find({ where: { orgChild3: In(orgChild3Ids) }, - order: { + order: { orgChild4Order: "ASC", - posMasters:{ + posMasters: { posMasterOrder: "ASC", - posMasterCreatedAt: "ASC" - } + posMasterCreatedAt: "ASC", + }, }, relations: [ "posMasters", @@ -1466,10 +1489,8 @@ export class ReportController extends Controller { posMaster.positions .sort((a: any, b: any) => a.orderNo - b.orderNo) .map((x) => - x.positionArea - ? `${x.positionName} (${x.positionArea})` - : x.positionName - ) + x.positionArea ? `${x.positionName} (${x.positionArea})` : x.positionName, + ), ), ]; const posType = [ @@ -1493,8 +1514,8 @@ export class ReportController extends Controller { .map((x: any) => x.positionExecutiveField ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` - : x.posExecutive.posExecutiveName - ) + : x.posExecutive.posExecutiveName, + ), ), ]; let node = { @@ -1537,7 +1558,9 @@ export class ReportController extends Controller { ) { const head = { posMasterNo: - node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + node.orgTreeShortName == _nodeTemp.orgTreeShortName + ? "" + : node.orgTreeShortName, posExecutive: node.orgTreeName == _nodeTemp.orgTreeName ? "" @@ -1567,9 +1590,11 @@ export class ReportController extends Controller { no += 1; _nodeTemp = node; } - }) + }), ); - for (let orgChild2 of orgChild2Data.filter((orgChild2) => orgChild2.orgChild1Id === orgChild1.id)) { + for (let orgChild2 of orgChild2Data.filter( + (orgChild2) => orgChild2.orgChild1Id === orgChild1.id, + )) { await Promise.all( orgChild2.posMasters .filter((x) => x.orgChild3Id == null) @@ -1584,8 +1609,8 @@ export class ReportController extends Controller { .map((x) => x.positionArea ? `${x.positionName} (${x.positionArea})` - : x.positionName - ) + : x.positionName, + ), ), ]; const posType = [ @@ -1609,8 +1634,8 @@ export class ReportController extends Controller { .map((x: any) => x.positionExecutiveField ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` - : x.posExecutive.posExecutiveName - ) + : x.posExecutive.posExecutiveName, + ), ), ]; let node = { @@ -1625,7 +1650,9 @@ export class ReportController extends Controller { }; if (_nodeTemp == null) { const head = { - posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName?.toString() ?? ""), + posMasterNo: Extension.ToThaiNumber( + node.orgTreeShortName?.toString() ?? "", + ), posExecutive: Extension.ToThaiNumber(node.orgTreeName?.toString() ?? ""), positionName: "", posType: "", @@ -1653,7 +1680,9 @@ export class ReportController extends Controller { ) { const head = { posMasterNo: - node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + node.orgTreeShortName == _nodeTemp.orgTreeShortName + ? "" + : node.orgTreeShortName, posExecutive: node.orgTreeName == _nodeTemp.orgTreeName ? "" @@ -1683,9 +1712,11 @@ export class ReportController extends Controller { no += 1; _nodeTemp = node; } - }) + }), ); - for (let orgChild3 of orgChild3Data.filter((orgChild3) => orgChild3.orgChild2Id === orgChild2.id)) { + for (let orgChild3 of orgChild3Data.filter( + (orgChild3) => orgChild3.orgChild2Id === orgChild2.id, + )) { await Promise.all( orgChild3.posMasters .filter((x) => x.orgChild4Id == null) @@ -1700,8 +1731,8 @@ export class ReportController extends Controller { .map((x) => x.positionArea ? `${x.positionName} (${x.positionArea})` - : x.positionName - ) + : x.positionName, + ), ), ]; const posType = [ @@ -1725,8 +1756,8 @@ export class ReportController extends Controller { .map((x: any) => x.positionExecutiveField ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` - : x.posExecutive.posExecutiveName - ) + : x.posExecutive.posExecutiveName, + ), ), ]; let node = { @@ -1741,7 +1772,9 @@ export class ReportController extends Controller { }; if (_nodeTemp == null) { const head = { - posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName?.toString() ?? ""), + posMasterNo: Extension.ToThaiNumber( + node.orgTreeShortName?.toString() ?? "", + ), posExecutive: Extension.ToThaiNumber(node.orgTreeName?.toString() ?? ""), positionName: "", posType: "", @@ -1769,7 +1802,9 @@ export class ReportController extends Controller { ) { const head = { posMasterNo: - node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + node.orgTreeShortName == _nodeTemp.orgTreeShortName + ? "" + : node.orgTreeShortName, posExecutive: node.orgTreeName == _nodeTemp.orgTreeName ? "" @@ -1799,9 +1834,11 @@ export class ReportController extends Controller { no += 1; _nodeTemp = node; } - }) + }), ); - for (let orgChild4 of orgChild4Data.filter((orgChild4) => orgChild4.orgChild3Id === orgChild3.id)) { + for (let orgChild4 of orgChild4Data.filter( + (orgChild4) => orgChild4.orgChild3Id === orgChild3.id, + )) { await Promise.all( orgChild4.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) @@ -1814,8 +1851,8 @@ export class ReportController extends Controller { .map((x) => x.positionArea ? `${x.positionName} (${x.positionArea})` - : x.positionName - ) + : x.positionName, + ), ), ]; const posType = [ @@ -1839,8 +1876,8 @@ export class ReportController extends Controller { .map((x: any) => x.positionExecutiveField ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` - : x.posExecutive.posExecutiveName - ) + : x.posExecutive.posExecutiveName, + ), ), ]; let node = { @@ -1855,7 +1892,9 @@ export class ReportController extends Controller { }; if (_nodeTemp == null) { const head = { - posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName?.toString() ?? ""), + posMasterNo: Extension.ToThaiNumber( + node.orgTreeShortName?.toString() ?? "", + ), posExecutive: Extension.ToThaiNumber(node.orgTreeName?.toString() ?? ""), positionName: "", posType: "", @@ -1883,7 +1922,9 @@ export class ReportController extends Controller { ) { const head = { posMasterNo: - node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + node.orgTreeShortName == _nodeTemp.orgTreeShortName + ? "" + : node.orgTreeShortName, posExecutive: node.orgTreeName == _nodeTemp.orgTreeName ? "" @@ -1912,7 +1953,7 @@ export class ReportController extends Controller { } no += 1; _nodeTemp = node; - }) + }), ); } } @@ -1923,16 +1964,16 @@ export class ReportController extends Controller { case 2: { // ดึง orgChild2 และลูก (orgChild3, orgChild4) const orgChild2Data = await this.child2Repository.find({ - where: { + where: { id: _nodeId, - }, - order: { + }, + order: { orgChild2Order: "ASC", - posMasters:{ + posMasters: { posMasterOrder: "ASC", - posMasterCreatedAt: "ASC" - } - }, + posMasterCreatedAt: "ASC", + }, + }, relations: [ "posMasters", "posMasters.positions", @@ -1949,13 +1990,13 @@ export class ReportController extends Controller { const orgChild2Ids = orgChild2Data.map((c) => c.id) || null; const orgChild3Data = await this.child3Repository.find({ where: { orgChild2: In(orgChild2Ids) }, - order: { + order: { orgChild3Order: "ASC", - posMasters:{ + posMasters: { posMasterOrder: "ASC", - posMasterCreatedAt: "ASC" - } - }, + posMasterCreatedAt: "ASC", + }, + }, relations: [ "posMasters", "posMasters.positions", @@ -1967,13 +2008,13 @@ export class ReportController extends Controller { const orgChild3Ids = orgChild3Data.map((c) => c.id) || null; const orgChild4Data = await this.child4Repository.find({ where: { orgChild3: In(orgChild3Ids) }, - order: { + order: { orgChild4Order: "ASC", - posMasters:{ + posMasters: { posMasterOrder: "ASC", - posMasterCreatedAt: "ASC" - } - }, + posMasterCreatedAt: "ASC", + }, + }, relations: [ "posMasters", "posMasters.positions", @@ -1996,10 +2037,8 @@ export class ReportController extends Controller { posMaster.positions .sort((a: any, b: any) => a.orderNo - b.orderNo) .map((x) => - x.positionArea - ? `${x.positionName} (${x.positionArea})` - : x.positionName - ) + x.positionArea ? `${x.positionName} (${x.positionArea})` : x.positionName, + ), ), ]; const posType = [ @@ -2023,8 +2062,8 @@ export class ReportController extends Controller { .map((x: any) => x.positionExecutiveField ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` - : x.posExecutive.posExecutiveName - ) + : x.posExecutive.posExecutiveName, + ), ), ]; let node = { @@ -2067,7 +2106,9 @@ export class ReportController extends Controller { ) { const head = { posMasterNo: - node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + node.orgTreeShortName == _nodeTemp.orgTreeShortName + ? "" + : node.orgTreeShortName, posExecutive: node.orgTreeName == _nodeTemp.orgTreeName ? "" @@ -2097,9 +2138,11 @@ export class ReportController extends Controller { no += 1; _nodeTemp = node; } - }) + }), ); - for (let orgChild3 of orgChild3Data.filter((orgChild3) => orgChild3.orgChild2Id === orgChild2.id)) { + for (let orgChild3 of orgChild3Data.filter( + (orgChild3) => orgChild3.orgChild2Id === orgChild2.id, + )) { await Promise.all( orgChild3.posMasters .filter((x) => x.orgChild4Id == null) @@ -2114,8 +2157,8 @@ export class ReportController extends Controller { .map((x) => x.positionArea ? `${x.positionName} (${x.positionArea})` - : x.positionName - ) + : x.positionName, + ), ), ]; const posType = [ @@ -2139,8 +2182,8 @@ export class ReportController extends Controller { .map((x: any) => x.positionExecutiveField ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` - : x.posExecutive.posExecutiveName - ) + : x.posExecutive.posExecutiveName, + ), ), ]; let node = { @@ -2155,7 +2198,9 @@ export class ReportController extends Controller { }; if (_nodeTemp == null) { const head = { - posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName?.toString() ?? ""), + posMasterNo: Extension.ToThaiNumber( + node.orgTreeShortName?.toString() ?? "", + ), posExecutive: Extension.ToThaiNumber(node.orgTreeName?.toString() ?? ""), positionName: "", posType: "", @@ -2183,7 +2228,9 @@ export class ReportController extends Controller { ) { const head = { posMasterNo: - node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + node.orgTreeShortName == _nodeTemp.orgTreeShortName + ? "" + : node.orgTreeShortName, posExecutive: node.orgTreeName == _nodeTemp.orgTreeName ? "" @@ -2213,9 +2260,11 @@ export class ReportController extends Controller { no += 1; _nodeTemp = node; } - }) + }), ); - for (let orgChild4 of orgChild4Data.filter((orgChild4) => orgChild4.orgChild3Id === orgChild3.id)) { + for (let orgChild4 of orgChild4Data.filter( + (orgChild4) => orgChild4.orgChild3Id === orgChild3.id, + )) { await Promise.all( orgChild4.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) @@ -2228,8 +2277,8 @@ export class ReportController extends Controller { .map((x) => x.positionArea ? `${x.positionName} (${x.positionArea})` - : x.positionName - ) + : x.positionName, + ), ), ]; const posType = [ @@ -2253,8 +2302,8 @@ export class ReportController extends Controller { .map((x: any) => x.positionExecutiveField ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` - : x.posExecutive.posExecutiveName - ) + : x.posExecutive.posExecutiveName, + ), ), ]; let node = { @@ -2269,7 +2318,9 @@ export class ReportController extends Controller { }; if (_nodeTemp == null) { const head = { - posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName?.toString() ?? ""), + posMasterNo: Extension.ToThaiNumber( + node.orgTreeShortName?.toString() ?? "", + ), posExecutive: Extension.ToThaiNumber(node.orgTreeName?.toString() ?? ""), positionName: "", posType: "", @@ -2297,7 +2348,9 @@ export class ReportController extends Controller { ) { const head = { posMasterNo: - node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + node.orgTreeShortName == _nodeTemp.orgTreeShortName + ? "" + : node.orgTreeShortName, posExecutive: node.orgTreeName == _nodeTemp.orgTreeName ? "" @@ -2326,7 +2379,7 @@ export class ReportController extends Controller { } no += 1; _nodeTemp = node; - }) + }), ); } } @@ -2337,12 +2390,12 @@ export class ReportController extends Controller { // ดึง orgChild3 และลูก (orgChild4) const orgChild3Data = await this.child3Repository.find({ where: { id: _nodeId }, - order: { + order: { orgChild3Order: "ASC", - posMasters:{ + posMasters: { posMasterOrder: "ASC", - posMasterCreatedAt: "ASC" - } + posMasterCreatedAt: "ASC", + }, }, relations: [ "posMasters", @@ -2360,12 +2413,12 @@ export class ReportController extends Controller { const orgChild3Ids = orgChild3Data.map((c) => c.id) || null; const orgChild4Data = await this.child4Repository.find({ where: { orgChild3: In(orgChild3Ids) }, - order: { + order: { orgChild4Order: "ASC", - posMasters:{ + posMasters: { posMasterOrder: "ASC", - posMasterCreatedAt: "ASC" - } + posMasterCreatedAt: "ASC", + }, }, relations: [ "posMasters", @@ -2389,10 +2442,8 @@ export class ReportController extends Controller { posMaster.positions .sort((a: any, b: any) => a.orderNo - b.orderNo) .map((x) => - x.positionArea - ? `${x.positionName} (${x.positionArea})` - : x.positionName - ) + x.positionArea ? `${x.positionName} (${x.positionArea})` : x.positionName, + ), ), ]; const posType = [ @@ -2416,8 +2467,8 @@ export class ReportController extends Controller { .map((x: any) => x.positionExecutiveField ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` - : x.posExecutive.posExecutiveName - ) + : x.posExecutive.posExecutiveName, + ), ), ]; let node = { @@ -2460,7 +2511,9 @@ export class ReportController extends Controller { ) { const head = { posMasterNo: - node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + node.orgTreeShortName == _nodeTemp.orgTreeShortName + ? "" + : node.orgTreeShortName, posExecutive: node.orgTreeName == _nodeTemp.orgTreeName ? "" @@ -2490,9 +2543,11 @@ export class ReportController extends Controller { no += 1; _nodeTemp = node; } - }) + }), ); - for (let orgChild4 of orgChild4Data.filter((orgChild4) => orgChild4.orgChild3Id === orgChild3.id)) { + for (let orgChild4 of orgChild4Data.filter( + (orgChild4) => orgChild4.orgChild3Id === orgChild3.id, + )) { await Promise.all( orgChild4.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) @@ -2503,10 +2558,8 @@ export class ReportController extends Controller { posMaster.positions .sort((a: any, b: any) => a.orderNo - b.orderNo) .map((x) => - x.positionArea - ? `${x.positionName} (${x.positionArea})` - : x.positionName - ) + x.positionArea ? `${x.positionName} (${x.positionArea})` : x.positionName, + ), ), ]; const posType = [ @@ -2530,8 +2583,8 @@ export class ReportController extends Controller { .map((x: any) => x.positionExecutiveField ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` - : x.posExecutive.posExecutiveName - ) + : x.posExecutive.posExecutiveName, + ), ), ]; let node = { @@ -2574,7 +2627,9 @@ export class ReportController extends Controller { ) { const head = { posMasterNo: - node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + node.orgTreeShortName == _nodeTemp.orgTreeShortName + ? "" + : node.orgTreeShortName, posExecutive: node.orgTreeName == _nodeTemp.orgTreeName ? "" @@ -2603,7 +2658,7 @@ export class ReportController extends Controller { } no += 1; _nodeTemp = node; - }) + }), ); } } @@ -2613,12 +2668,12 @@ export class ReportController extends Controller { // ดึงข้อมูล orgChild4 ตาม nodeId const orgChild4Data = await this.child4Repository.find({ where: { id: _nodeId }, - order: { + order: { orgChild4Order: "ASC", - posMasters:{ + posMasters: { posMasterOrder: "ASC", - posMasterCreatedAt: "ASC" - } + posMasterCreatedAt: "ASC", + }, }, relations: [ "posMasters", @@ -2643,10 +2698,8 @@ export class ReportController extends Controller { posMaster.positions .sort((a: any, b: any) => a.orderNo - b.orderNo) .map((x) => - x.positionArea - ? `${x.positionName} (${x.positionArea})` - : x.positionName - ) + x.positionArea ? `${x.positionName} (${x.positionArea})` : x.positionName, + ), ), ]; const posType = [ @@ -2670,8 +2723,8 @@ export class ReportController extends Controller { .map((x: any) => x.positionExecutiveField ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` - : x.posExecutive.posExecutiveName - ) + : x.posExecutive.posExecutiveName, + ), ), ]; @@ -2715,7 +2768,9 @@ export class ReportController extends Controller { ) { const head = { posMasterNo: - node.orgTreeShortName == _nodeTemp.orgTreeShortName ? "" : node.orgTreeShortName, + node.orgTreeShortName == _nodeTemp.orgTreeShortName + ? "" + : node.orgTreeShortName, posExecutive: node.orgTreeName == _nodeTemp.orgTreeName ? "" @@ -2744,7 +2799,7 @@ export class ReportController extends Controller { } no += 1; _nodeTemp = node; - }) + }), ); } break; @@ -2865,12 +2920,10 @@ export class ReportController extends Controller { const positionName = [ ...new Set( posMaster.positions - .sort((a:any, b:any) => a.orderNo - b.orderNo) + .sort((a: any, b: any) => a.orderNo - b.orderNo) .map((x) => - x.positionArea - ? `${x.positionName} (${x.positionArea})` - : x.positionName - ) + x.positionArea ? `${x.positionName} (${x.positionArea})` : x.positionName, + ), ), ]; const posType = [ @@ -2891,11 +2944,11 @@ export class ReportController extends Controller { ...new Set( posMaster.positions .filter((x: any) => x.posExecutive != null) - .map((x: any) => - x.positionExecutiveField - ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` - : x.posExecutive.posExecutiveName - ) + .map((x: any) => + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName, + ), ), ]; @@ -2985,12 +3038,10 @@ export class ReportController extends Controller { const positionName = [ ...new Set( posMaster.positions - .sort((a:any, b:any) => a.orderNo - b.orderNo) + .sort((a: any, b: any) => a.orderNo - b.orderNo) .map((x) => - x.positionArea - ? `${x.positionName} (${x.positionArea})` - : x.positionName - ) + x.positionArea ? `${x.positionName} (${x.positionArea})` : x.positionName, + ), ), ]; const posType = [ @@ -3012,10 +3063,10 @@ export class ReportController extends Controller { posMaster.positions .filter((x: any) => x.posExecutive != null) .map((x: any) => - x.positionExecutiveField - ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` - : x.posExecutive.posExecutiveName - ) + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName, + ), ), ]; @@ -3106,13 +3157,11 @@ export class ReportController extends Controller { if (posMaster.orgChild3Id == null) { const positionName = [ ...new Set( - posMaster.positions - .sort((a:any, b:any) => a.orderNo - b.orderNo) - .map((x) => - x.positionArea - ? `${x.positionName} (${x.positionArea})` - : x.positionName - ) + posMaster.positions + .sort((a: any, b: any) => a.orderNo - b.orderNo) + .map((x) => + x.positionArea ? `${x.positionName} (${x.positionArea})` : x.positionName, + ), ), ]; const posType = [ @@ -3129,17 +3178,17 @@ export class ReportController extends Controller { .map((x) => x.posLevel.posLevelName), ), ]; - const posExecutive = [ - ...new Set( - posMaster.positions - .filter((x: any) => x.posExecutive != null) - .map((x: any) => - x.positionExecutiveField - ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` - : x.posExecutive.posExecutiveName - ) - ), - ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName, + ), + ), + ]; let node = { orgTreeName: orgChild2.orgChild2Name, @@ -3225,13 +3274,13 @@ export class ReportController extends Controller { if (posMaster.orgChild4Id == null) { const positionName = [ ...new Set( - posMaster.positions - .sort((a:any, b:any) => a.orderNo - b.orderNo) - .map((x) => - x.positionArea - ? `${x.positionName} (${x.positionArea})` - : x.positionName - ) + posMaster.positions + .sort((a: any, b: any) => a.orderNo - b.orderNo) + .map((x) => + x.positionArea + ? `${x.positionName} (${x.positionArea})` + : x.positionName, + ), ), ]; const posType = [ @@ -3253,10 +3302,10 @@ export class ReportController extends Controller { posMaster.positions .filter((x: any) => x.posExecutive != null) .map((x: any) => - x.positionExecutiveField - ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` - : x.posExecutive.posExecutiveName - ) + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName, + ), ), ]; @@ -3343,13 +3392,13 @@ export class ReportController extends Controller { .map(async (posMaster) => { const positionName = [ ...new Set( - posMaster.positions - .sort((a:any, b:any) => a.orderNo - b.orderNo) - .map((x) => - x.positionArea - ? `${x.positionName} (${x.positionArea})` - : x.positionName - ) + posMaster.positions + .sort((a: any, b: any) => a.orderNo - b.orderNo) + .map((x) => + x.positionArea + ? `${x.positionName} (${x.positionArea})` + : x.positionName, + ), ), ]; const posType = [ @@ -3371,10 +3420,10 @@ export class ReportController extends Controller { posMaster.positions .filter((x: any) => x.posExecutive != null) .map((x: any) => - x.positionExecutiveField - ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` - : x.posExecutive.posExecutiveName - ) + x.positionExecutiveField + ? `${x.posExecutive.posExecutiveName} (${x.positionExecutiveField})` + : x.posExecutive.posExecutiveName, + ), ), ]; @@ -3475,10 +3524,10 @@ export class ReportController extends Controller { reqBody: { node: number; nodeId: string; - } + }, ) { - // @Get("report2/{rootId}") - // async findReport2(@Path() rootId: string) { + // @Get("report2/{rootId}") + // async findReport2(@Path() rootId: string) { // const orgRevision = await this.orgRevisionRepository.findOne({ // where: { orgRevisionIsDraft: true, orgRevisionIsCurrent: false }, // relations: ["orgRoots"], @@ -3487,9 +3536,9 @@ export class ReportController extends Controller { // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); // } // (async () => { - let nodeId:string = reqBody.nodeId ? reqBody.nodeId : ""; - let node:number = reqBody.node ? reqBody.node : 0; - if(nodeId === "" || node < 0 || node > 4) { + let nodeId: string = reqBody.nodeId ? reqBody.nodeId : ""; + let node: number = reqBody.node ? reqBody.node : 0; + if (nodeId === "" || node < 0 || node > 4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } // ดึงแค่ rootId ก่อน ถ้าลูกค้า confirm ค่อยปรับ @@ -3497,308 +3546,1390 @@ export class ReportController extends Controller { case 0: { const orgRoot = await this.orgRootRepository.findOne({ select: ["id"], - where: { id: nodeId } + where: { id: nodeId }, }); - if (!orgRoot) - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + if (!orgRoot) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); nodeId = orgRoot.id; break; } case 1: { const orgChild1 = await this.child1Repository.findOne({ select: ["id", "orgRootId"], - where: { id: nodeId } + where: { id: nodeId }, }); - if (!orgChild1) - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + if (!orgChild1) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); nodeId = orgChild1.orgRootId; break; } case 2: { const orgChild2 = await this.child2Repository.findOne({ select: ["id", "orgRootId"], - where: { id: nodeId } + where: { id: nodeId }, }); - if (!orgChild2) - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + if (!orgChild2) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); nodeId = orgChild2.orgRootId; break; } case 3: { const orgChild3 = await this.child3Repository.findOne({ select: ["id", "orgRootId"], - where: { id: nodeId } + where: { id: nodeId }, }); - if (!orgChild3) - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + if (!orgChild3) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); nodeId = orgChild3.orgRootId; break; } case 4: { const orgChild4 = await this.child4Repository.findOne({ select: ["id", "orgRootId"], - where: { id: nodeId } + where: { id: nodeId }, }); - if (!orgChild4) - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + if (!orgChild4) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); nodeId = orgChild4.orgRootId; break; } default: throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } + // Step 1: ดึงข้อมูล OrgRoot พื้นฐาน + const orgRootData = await this.orgRootRepository + .createQueryBuilder("orgRoot") + .select([ + "orgRoot.id", + "orgRoot.orgRootName", + "orgRoot.orgRootShortName", + "orgRoot.orgRootOrder", + ]) + .where("orgRoot.id = :nodeId", { nodeId }) + .orderBy("orgRoot.orgRootOrder", "ASC") + .getMany(); + + if (!orgRootData || orgRootData.length === 0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลองค์กร"); + } + + // Step 2: ดึงข้อมูล PosMasters ที่เกี่ยวข้องเท่านั้น + const posMastersData = await this.posMasterRepository + .createQueryBuilder("posMaster") + .select([ + "posMaster.id", + "posMaster.posMasterOrder", + "posMaster.isSit", + "posMaster.posMasterNo", + "posMaster.ancestorDNA", + "posMaster.reason", + "posMaster.orgRootId", + "posMaster.orgChild1Id", + "posMaster.current_holderId", + "posMaster.next_holderId", + ]) + .where("posMaster.orgRootId = :nodeId", { nodeId }) + .andWhere("posMaster.orgChild1Id IS NULL") // เฉพาะ root level + .orderBy("posMaster.posMasterOrder", "ASC") + .getMany(); + + // Step 3: ดึงข้อมูล Profiles ที่ใช้จริงเท่านั้น + const profileIds = [ + ...new Set([ + ...posMastersData.map((pm: any) => pm.current_holderId).filter(Boolean), + ...posMastersData.map((pm: any) => pm.next_holderId).filter(Boolean), + ]), + ]; + + let profilesData: any[] = []; + if (profileIds.length > 0) { + profilesData = await this.profileRepository + .createQueryBuilder("profile") + .select([ + "profile.id", + "profile.prefix", + "profile.firstName", + "profile.lastName", + "profile.position", + "profile.posTypeId", + "profile.posLevelId", + ]) + .where("profile.id IN (:...profileIds)", { profileIds }) + .getMany(); + } + + // Step 4: ดึงข้อมูล Education ล่าสุดของแต่ละ Profile + let educationsData: any[] = []; + if (profileIds.length > 0) { + educationsData = await this.profileEducationRepository + .createQueryBuilder("pe") + .select([ + "pe.profileId", + "pe.finishDate", + "pe.degree", + "ROW_NUMBER() OVER (PARTITION BY pe.profileId ORDER BY pe.finishDate DESC) as rn", + ]) + .where("pe.profileId IN (:...profileIds)", { profileIds }) + .getRawMany(); + + // Filter เฉพาะ latest education + educationsData = educationsData.filter((ed: any) => ed.rn === 1); + } + + // Step 5: ดึงข้อมูล Salary ล่าสุดของแต่ละ Profile + let salariesData: any[] = []; + if (profileIds.length > 0) { + salariesData = await this.profileSalaryRepository + .createQueryBuilder("ps") + .select([ + "ps.profileId", + "ps.commandDateAffect", + "ps.amount", + "ROW_NUMBER() OVER (PARTITION BY ps.profileId ORDER BY ps.commandDateAffect DESC) as rn", + ]) + .where("ps.profileId IN (:...profileIds)", { profileIds }) + .getRawMany(); + + // Filter เฉพาะ latest salary + salariesData = salariesData.filter((sal: any) => sal.rn === 1); + } + + // Step 6: ดึงข้อมูล Positions ที่เกี่ยวข้อง + const posMasterIds = posMastersData.map((pm: any) => pm.id); + let positionsData: any[] = []; + if (posMasterIds.length > 0) { + positionsData = await this.positionRepository + .createQueryBuilder("position") + .leftJoinAndSelect("position.posType", "posType") + .leftJoinAndSelect("position.posLevel", "posLevel") + .leftJoinAndSelect("position.posExecutive", "posExecutive") + .select([ + "position.id", + "position.positionName", + "position.positionIsSelected", + "position.posMasterId", + "posType.id", + "posType.posTypeName", + "posLevel.id", + "posLevel.posLevelName", + "posExecutive.id", + "posExecutive.posExecutiveName", + ]) + .where("position.posMasterId IN (:...posMasterIds)", { posMasterIds }) + .getMany(); + } + + // Step 7: Combine ข้อมูลกลับเหมือนเดิม + for (let orgRoot of orgRootData) { + orgRoot.posMasters = posMastersData.filter((pm: any) => pm.orgRootId === orgRoot.id); + + for (let posMaster of orgRoot.posMasters) { + // Attach current_holder + if (posMaster.current_holderId) { + posMaster.current_holder = profilesData.find( + (p: any) => p.id === posMaster.current_holderId, + ); + if (posMaster.current_holder) { + posMaster.current_holder.profileEducations = educationsData.filter( + (ed: any) => ed.profileId === posMaster.current_holderId, + ); + posMaster.current_holder.profileSalary = salariesData.filter( + (sal: any) => sal.profileId === posMaster.current_holderId, + ); + } + } + + // Attach next_holder + if (posMaster.next_holderId) { + posMaster.next_holder = profilesData.find((p: any) => p.id === posMaster.next_holderId); + if (posMaster.next_holder) { + posMaster.next_holder.profileEducations = educationsData.filter( + (ed: any) => ed.profileId === posMaster.next_holderId, + ); + posMaster.next_holder.profileSalary = salariesData.filter( + (sal: any) => sal.profileId === posMaster.next_holderId, + ); + } + } + + // Attach positions + posMaster.positions = positionsData.filter((pos: any) => pos.posMasterId === posMaster.id); + } + } + + if (!orgRootData || orgRootData.length === 0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลองค์กร"); + } - const orgRootData = await this.orgRootRepository.find({ - where: { - id: nodeId, - // id: rootId, - // orgRevisionId: orgRevision.id, - }, - order: { orgRootOrder: "ASC" }, - relations: [ - "posMasters", - "posMasters.orgRoot", - "posMasters.orgChild1", - "posMasters.orgChild2", - "posMasters.orgChild3", - "posMasters.orgChild4", - "posMasters.current_holder", - "posMasters.current_holder.profileSalary", - "posMasters.current_holder.profileEducations", - "posMasters.next_holder", - "posMasters.next_holder.posLevel", - "posMasters.next_holder.posType", - "posMasters.next_holder.profileSalary", - "posMasters.next_holder.profileEducations", - "posMasters.next_holder.current_holders", - "posMasters.next_holder.current_holders.positions", - "posMasters.next_holder.current_holders.orgRoot", - "posMasters.next_holder.current_holders.orgChild1", - "posMasters.next_holder.current_holders.orgChild2", - "posMasters.next_holder.current_holders.orgChild3", - "posMasters.next_holder.current_holders.orgChild4", - "posMasters.positions", - "posMasters.positions.posLevel", - "posMasters.positions.posType", - "posMasters.positions.posExecutive", - ], - }); const orgName = orgRootData[0].orgRootName ?? ""; const orgRootIds = orgRootData.map((orgRoot) => orgRoot.id) || null; - const orgChild1Data = await this.child1Repository.find({ - where: { - orgRootId: In(orgRootIds), - }, - order: { orgChild1Order: "ASC" }, - relations: [ - "posMasters", - "posMasters.orgRoot", - "posMasters.orgChild1", - "posMasters.orgChild2", - "posMasters.orgChild3", - "posMasters.orgChild4", - "posMasters.current_holder", - "posMasters.current_holder.profileSalary", - "posMasters.current_holder.profileEducations", - "posMasters.next_holder", - "posMasters.next_holder.posLevel", - "posMasters.next_holder.posType", - "posMasters.next_holder.profileSalary", - "posMasters.next_holder.profileEducations", - "posMasters.next_holder.current_holders", - "posMasters.next_holder.current_holders.positions", - "posMasters.next_holder.current_holders.orgRoot", - "posMasters.next_holder.current_holders.orgChild1", - "posMasters.next_holder.current_holders.orgChild2", - "posMasters.next_holder.current_holders.orgChild3", - "posMasters.next_holder.current_holders.orgChild4", - "posMasters.positions", - "posMasters.positions.posLevel", - "posMasters.positions.posType", - "posMasters.positions.posExecutive", - ], - }); - const orgChild1Ids = orgChild1Data.map((orgChild1) => orgChild1.id) || null; - const orgChild2Data = await this.child2Repository.find({ - where: { - orgChild1: In(orgChild1Ids), - }, - order: { orgChild2Order: "ASC" }, - relations: [ - "posMasters", - "posMasters.orgRoot", - "posMasters.orgChild1", - "posMasters.orgChild2", - "posMasters.orgChild3", - "posMasters.orgChild4", - "posMasters.current_holder", - "posMasters.current_holder.profileSalary", - "posMasters.current_holder.profileEducations", - "posMasters.next_holder", - "posMasters.next_holder.posLevel", - "posMasters.next_holder.posType", - "posMasters.next_holder.profileSalary", - "posMasters.next_holder.profileEducations", - "posMasters.next_holder.current_holders", - "posMasters.next_holder.current_holders.positions", - "posMasters.next_holder.current_holders.orgRoot", - "posMasters.next_holder.current_holders.orgChild1", - "posMasters.next_holder.current_holders.orgChild2", - "posMasters.next_holder.current_holders.orgChild3", - "posMasters.next_holder.current_holders.orgChild4", - "posMasters.positions", - "posMasters.positions.posLevel", - "posMasters.positions.posType", - "posMasters.positions.posExecutive", - ], - }); + // Step 8: ปรับปรุง orgChild1Data - ใช้ optimized approach + const orgChild1Data = await this.child1Repository + .createQueryBuilder("orgChild1") + .select([ + "orgChild1.id", + "orgChild1.orgRootId", + "orgChild1.orgChild1Name", + "orgChild1.orgChild1ShortName", + "orgChild1.orgChild1Order", + ]) + .where("orgChild1.orgRootId IN (:...orgRootIds)", { orgRootIds }) + .orderBy("orgChild1.orgChild1Order", "ASC") + .getMany(); - const orgChild2Ids = orgChild2Data.map((orgChild2) => orgChild2.id) || null; - const orgChild3Data = await this.child3Repository.find({ - where: { - orgChild2: In(orgChild2Ids), - }, - order: { orgChild3Order: "ASC" }, - relations: [ - "posMasters", - "posMasters.orgRoot", - "posMasters.orgChild1", - "posMasters.orgChild2", - "posMasters.orgChild3", - "posMasters.orgChild4", - "posMasters.current_holder", - "posMasters.current_holder.profileSalary", - "posMasters.current_holder.profileEducations", - "posMasters.next_holder", - "posMasters.next_holder.posLevel", - "posMasters.next_holder.posType", - "posMasters.next_holder.profileSalary", - "posMasters.next_holder.profileEducations", - "posMasters.next_holder.current_holders", - "posMasters.next_holder.current_holders.positions", - "posMasters.next_holder.current_holders.orgRoot", - "posMasters.next_holder.current_holders.orgChild1", - "posMasters.next_holder.current_holders.orgChild2", - "posMasters.next_holder.current_holders.orgChild3", - "posMasters.next_holder.current_holders.orgChild4", - "posMasters.next_holder.current_holders.orgChild4", - "posMasters.positions", - "posMasters.positions.posLevel", - "posMasters.positions.posType", - "posMasters.positions.posExecutive", - ], - }); + // ดึงข้อมูล PosMasters สำหรับ orgChild1 + const orgChild1Ids = orgChild1Data.map((orgChild1) => orgChild1.id); + let child1PosMasters: any[] = []; + if (orgChild1Ids.length > 0) { + child1PosMasters = await this.posMasterRepository + .createQueryBuilder("posMaster") + .select([ + "posMaster.id", + "posMaster.posMasterOrder", + "posMaster.isSit", + "posMaster.posMasterNo", + "posMaster.ancestorDNA", + "posMaster.reason", + "posMaster.orgChild1Id", + "posMaster.orgChild2Id", + "posMaster.current_holderId", + "posMaster.next_holderId", + ]) + .where("posMaster.orgChild1Id IN (:...orgChild1Ids)", { orgChild1Ids }) + .andWhere("posMaster.orgChild2Id IS NULL") // เฉพาะ child1 level + .orderBy("posMaster.posMasterOrder", "ASC") + .getMany(); + } - const orgChild3Ids = orgChild3Data.map((orgChild3) => orgChild3.id) || null; - const orgChild4Data = await this.child4Repository.find({ - where: { - orgChild3: In(orgChild3Ids), - }, - order: { orgChild4Order: "ASC" }, - relations: [ - "posMasters", - "posMasters.orgRoot", - "posMasters.orgChild1", - "posMasters.orgChild2", - "posMasters.orgChild3", - "posMasters.orgChild4", - "posMasters.current_holder", - "posMasters.current_holder.profileSalary", - "posMasters.current_holder.profileEducations", - "posMasters.next_holder", - "posMasters.next_holder.posLevel", - "posMasters.next_holder.posType", - "posMasters.next_holder.profileSalary", - "posMasters.next_holder.profileEducations", - "posMasters.next_holder.current_holders", - "posMasters.next_holder.current_holders.positions", - "posMasters.next_holder.current_holders.orgRoot", - "posMasters.next_holder.current_holders.orgChild1", - "posMasters.next_holder.current_holders.orgChild2", - "posMasters.next_holder.current_holders.orgChild3", - "posMasters.next_holder.current_holders.orgChild4", - "posMasters.positions", - "posMasters.positions.posLevel", - "posMasters.positions.posType", - "posMasters.positions.posExecutive", - ], - }); + // รวบรวม Profile IDs สำหรับ child1 + const child1ProfileIds = + child1PosMasters.length > 0 + ? [ + ...new Set([ + ...child1PosMasters.map((pm: any) => pm.current_holderId).filter(Boolean), + ...child1PosMasters.map((pm: any) => pm.next_holderId).filter(Boolean), + ]), + ] + : []; + + // ดึงข้อมูล Profiles, Education, Salary, Positions สำหรับ child1 + let child1Profiles: any[] = []; + let child1Educations: any[] = []; + let child1Salaries: any[] = []; + let child1Positions: any[] = []; + + if (child1ProfileIds.length > 0) { + [child1Profiles, child1Educations, child1Salaries] = await Promise.all([ + this.profileRepository + .createQueryBuilder("profile") + .select([ + "profile.id", + "profile.prefix", + "profile.firstName", + "profile.lastName", + "profile.position", + "profile.posTypeId", + "profile.posLevelId", + ]) + .where("profile.id IN (:...child1ProfileIds)", { child1ProfileIds }) + .getMany(), + + this.profileEducationRepository + .createQueryBuilder("pe") + .select([ + "pe.profileId", + "pe.finishDate", + "pe.degree", + "ROW_NUMBER() OVER (PARTITION BY pe.profileId ORDER BY pe.finishDate DESC) as rn", + ]) + .where("pe.profileId IN (:...child1ProfileIds)", { child1ProfileIds }) + .getRawMany() + .then((data: any[]) => data.filter((ed: any) => ed.rn === 1)), + + this.profileSalaryRepository + .createQueryBuilder("ps") + .select([ + "ps.profileId", + "ps.commandDateAffect", + "ps.amount", + "ROW_NUMBER() OVER (PARTITION BY ps.profileId ORDER BY ps.commandDateAffect DESC) as rn", + ]) + .where("ps.profileId IN (:...child1ProfileIds)", { child1ProfileIds }) + .getRawMany() + .then((data: any[]) => data.filter((sal: any) => sal.rn === 1)), + ]); + } + + if (child1PosMasters.length > 0) { + const child1PosMasterIds = child1PosMasters.map((pm: any) => pm.id); + child1Positions = await this.positionRepository + .createQueryBuilder("position") + .leftJoinAndSelect("position.posType", "posType") + .leftJoinAndSelect("position.posLevel", "posLevel") + .leftJoinAndSelect("position.posExecutive", "posExecutive") + .select([ + "position.id", + "position.positionName", + "position.positionIsSelected", + "position.posMasterId", + "posType.id", + "posType.posTypeName", + "posLevel.id", + "posLevel.posLevelName", + "posExecutive.id", + "posExecutive.posExecutiveName", + ]) + .where("position.posMasterId IN (:...child1PosMasterIds)", { child1PosMasterIds }) + .getMany(); + } + + // Combine ข้อมูล orgChild1 + for (let orgChild1 of orgChild1Data) { + orgChild1.posMasters = child1PosMasters.filter((pm: any) => pm.orgChild1Id === orgChild1.id); + + for (let posMaster of orgChild1.posMasters) { + if (posMaster.current_holderId) { + posMaster.current_holder = child1Profiles.find( + (p: any) => p.id === posMaster.current_holderId, + ); + if (posMaster.current_holder) { + posMaster.current_holder.profileEducations = child1Educations.filter( + (ed: any) => ed.profileId === posMaster.current_holderId, + ); + posMaster.current_holder.profileSalary = child1Salaries.filter( + (sal: any) => sal.profileId === posMaster.current_holderId, + ); + } + } + + if (posMaster.next_holderId) { + posMaster.next_holder = child1Profiles.find((p: any) => p.id === posMaster.next_holderId); + if (posMaster.next_holder) { + posMaster.next_holder.profileEducations = child1Educations.filter( + (ed: any) => ed.profileId === posMaster.next_holderId, + ); + posMaster.next_holder.profileSalary = child1Salaries.filter( + (sal: any) => sal.profileId === posMaster.next_holderId, + ); + } + } + + posMaster.positions = child1Positions.filter( + (pos: any) => pos.posMasterId === posMaster.id, + ); + } + } + + // Step 9: ปรับปรุง orgChild2Data + const originalOrgChild1Ids = + orgChild1Data.length > 0 ? orgChild1Data.map((orgChild1) => orgChild1.id) : []; + const orgChild2Data = await this.child2Repository + .createQueryBuilder("orgChild2") + .select([ + "orgChild2.id", + "orgChild2.orgChild1Id", + "orgChild2.orgChild2Name", + "orgChild2.orgChild2ShortName", + "orgChild2.orgChild2Order", + ]) + .where( + originalOrgChild1Ids.length > 0 + ? "orgChild2.orgChild1Id IN (:...originalOrgChild1Ids)" + : "1=0", + { + originalOrgChild1Ids, + }, + ) + .orderBy("orgChild2.orgChild2Order", "ASC") + .getMany(); + + // ดึงข้อมูล PosMasters สำหรับ orgChild2 + const orgChild2Ids = orgChild2Data.map((orgChild2) => orgChild2.id); + let child2PosMasters: any[] = []; + if (orgChild2Ids.length > 0) { + child2PosMasters = await this.posMasterRepository + .createQueryBuilder("posMaster") + .select([ + "posMaster.id", + "posMaster.posMasterOrder", + "posMaster.isSit", + "posMaster.posMasterNo", + "posMaster.ancestorDNA", + "posMaster.reason", + "posMaster.orgChild2Id", + "posMaster.orgChild3Id", + "posMaster.current_holderId", + "posMaster.next_holderId", + ]) + .where("posMaster.orgChild2Id IN (:...orgChild2Ids)", { orgChild2Ids }) + .andWhere("posMaster.orgChild3Id IS NULL") // เฉพาะ child2 level + .orderBy("posMaster.posMasterOrder", "ASC") + .getMany(); + } + + // รวบรวม Profile IDs สำหรับ child2 + const child2ProfileIds = + child2PosMasters.length > 0 + ? [ + ...new Set([ + ...child2PosMasters.map((pm: any) => pm.current_holderId).filter(Boolean), + ...child2PosMasters.map((pm: any) => pm.next_holderId).filter(Boolean), + ]), + ] + : []; + + // ดึงข้อมูล Profiles, Education, Salary, Positions สำหรับ child2 + let child2Profiles: any[] = []; + let child2Educations: any[] = []; + let child2Salaries: any[] = []; + let child2Positions: any[] = []; + + if (child2ProfileIds.length > 0) { + [child2Profiles, child2Educations, child2Salaries] = await Promise.all([ + this.profileRepository + .createQueryBuilder("profile") + .select([ + "profile.id", + "profile.prefix", + "profile.firstName", + "profile.lastName", + "profile.position", + "profile.posTypeId", + "profile.posLevelId", + ]) + .where("profile.id IN (:...child2ProfileIds)", { child2ProfileIds }) + .getMany(), + + this.profileEducationRepository + .createQueryBuilder("pe") + .select([ + "pe.profileId", + "pe.finishDate", + "pe.degree", + "ROW_NUMBER() OVER (PARTITION BY pe.profileId ORDER BY pe.finishDate DESC) as rn", + ]) + .where("pe.profileId IN (:...child2ProfileIds)", { child2ProfileIds }) + .getRawMany() + .then((data: any[]) => data.filter((ed: any) => ed.rn === 1)), + + this.profileSalaryRepository + .createQueryBuilder("ps") + .select([ + "ps.profileId", + "ps.commandDateAffect", + "ps.amount", + "ROW_NUMBER() OVER (PARTITION BY ps.profileId ORDER BY ps.commandDateAffect DESC) as rn", + ]) + .where("ps.profileId IN (:...child2ProfileIds)", { child2ProfileIds }) + .getRawMany() + .then((data: any[]) => data.filter((sal: any) => sal.rn === 1)), + ]); + } + + if (child2PosMasters.length > 0) { + const child2PosMasterIds = child2PosMasters.map((pm: any) => pm.id); + child2Positions = await this.positionRepository + .createQueryBuilder("position") + .leftJoinAndSelect("position.posType", "posType") + .leftJoinAndSelect("position.posLevel", "posLevel") + .leftJoinAndSelect("position.posExecutive", "posExecutive") + .select([ + "position.id", + "position.positionName", + "position.positionIsSelected", + "position.posMasterId", + "posType.id", + "posType.posTypeName", + "posLevel.id", + "posLevel.posLevelName", + "posExecutive.id", + "posExecutive.posExecutiveName", + ]) + .where("position.posMasterId IN (:...child2PosMasterIds)", { child2PosMasterIds }) + .getMany(); + } + + // Combine ข้อมูล orgChild2 + for (let orgChild2 of orgChild2Data) { + orgChild2.posMasters = child2PosMasters.filter((pm: any) => pm.orgChild2Id === orgChild2.id); + + for (let posMaster of orgChild2.posMasters) { + if (posMaster.current_holderId) { + posMaster.current_holder = child2Profiles.find( + (p: any) => p.id === posMaster.current_holderId, + ); + if (posMaster.current_holder) { + posMaster.current_holder.profileEducations = child2Educations.filter( + (ed: any) => ed.profileId === posMaster.current_holderId, + ); + posMaster.current_holder.profileSalary = child2Salaries.filter( + (sal: any) => sal.profileId === posMaster.current_holderId, + ); + } + } + + if (posMaster.next_holderId) { + posMaster.next_holder = child2Profiles.find((p: any) => p.id === posMaster.next_holderId); + if (posMaster.next_holder) { + posMaster.next_holder.profileEducations = child2Educations.filter( + (ed: any) => ed.profileId === posMaster.next_holderId, + ); + posMaster.next_holder.profileSalary = child2Salaries.filter( + (sal: any) => sal.profileId === posMaster.next_holderId, + ); + } + } + + posMaster.positions = child2Positions.filter( + (pos: any) => pos.posMasterId === posMaster.id, + ); + } + } + + // Step 10: ปรับปรุง orgChild3Data + const originalOrgChild2Ids = + orgChild2Data.length > 0 ? orgChild2Data.map((orgChild2) => orgChild2.id) : []; + const orgChild3Data = await this.child3Repository + .createQueryBuilder("orgChild3") + .select([ + "orgChild3.id", + "orgChild3.orgChild2Id", + "orgChild3.orgChild3Name", + "orgChild3.orgChild3ShortName", + "orgChild3.orgChild3Order", + ]) + .where( + originalOrgChild2Ids.length > 0 + ? "orgChild3.orgChild2Id IN (:...originalOrgChild2Ids)" + : "1=0", + { + originalOrgChild2Ids, + }, + ) + .orderBy("orgChild3.orgChild3Order", "ASC") + .getMany(); + + // ดึงข้อมูล PosMasters สำหรับ orgChild3 + const orgChild3Ids = orgChild3Data.map((orgChild3) => orgChild3.id); + let child3PosMasters: any[] = []; + if (orgChild3Ids.length > 0) { + child3PosMasters = await this.posMasterRepository + .createQueryBuilder("posMaster") + .select([ + "posMaster.id", + "posMaster.posMasterOrder", + "posMaster.isSit", + "posMaster.posMasterNo", + "posMaster.ancestorDNA", + "posMaster.reason", + "posMaster.orgChild3Id", + "posMaster.orgChild4Id", + "posMaster.current_holderId", + "posMaster.next_holderId", + ]) + .where("posMaster.orgChild3Id IN (:...orgChild3Ids)", { orgChild3Ids }) + .andWhere("posMaster.orgChild4Id IS NULL") // เฉพาะ child3 level + .orderBy("posMaster.posMasterOrder", "ASC") + .getMany(); + } + + // รวบรวม Profile IDs สำหรับ child3 + const child3ProfileIds = + child3PosMasters.length > 0 + ? [ + ...new Set([ + ...child3PosMasters.map((pm: any) => pm.current_holderId).filter(Boolean), + ...child3PosMasters.map((pm: any) => pm.next_holderId).filter(Boolean), + ]), + ] + : []; + + // ดึงข้อมูล Profiles, Education, Salary, Positions สำหรับ child3 + let child3Profiles: any[] = []; + let child3Educations: any[] = []; + let child3Salaries: any[] = []; + let child3Positions: any[] = []; + + if (child3ProfileIds.length > 0) { + [child3Profiles, child3Educations, child3Salaries] = await Promise.all([ + this.profileRepository + .createQueryBuilder("profile") + .select([ + "profile.id", + "profile.prefix", + "profile.firstName", + "profile.lastName", + "profile.position", + "profile.posTypeId", + "profile.posLevelId", + ]) + .where("profile.id IN (:...child3ProfileIds)", { child3ProfileIds }) + .getMany(), + + this.profileEducationRepository + .createQueryBuilder("pe") + .select([ + "pe.profileId", + "pe.finishDate", + "pe.degree", + "ROW_NUMBER() OVER (PARTITION BY pe.profileId ORDER BY pe.finishDate DESC) as rn", + ]) + .where("pe.profileId IN (:...child3ProfileIds)", { child3ProfileIds }) + .getRawMany() + .then((data: any[]) => data.filter((ed: any) => ed.rn === 1)), + + this.profileSalaryRepository + .createQueryBuilder("ps") + .select([ + "ps.profileId", + "ps.commandDateAffect", + "ps.amount", + "ROW_NUMBER() OVER (PARTITION BY ps.profileId ORDER BY ps.commandDateAffect DESC) as rn", + ]) + .where("ps.profileId IN (:...child3ProfileIds)", { child3ProfileIds }) + .getRawMany() + .then((data: any[]) => data.filter((sal: any) => sal.rn === 1)), + ]); + } + + if (child3PosMasters.length > 0) { + const child3PosMasterIds = child3PosMasters.map((pm: any) => pm.id); + child3Positions = await this.positionRepository + .createQueryBuilder("position") + .leftJoinAndSelect("position.posType", "posType") + .leftJoinAndSelect("position.posLevel", "posLevel") + .leftJoinAndSelect("position.posExecutive", "posExecutive") + .select([ + "position.id", + "position.positionName", + "position.positionIsSelected", + "position.posMasterId", + "posType.id", + "posType.posTypeName", + "posLevel.id", + "posLevel.posLevelName", + "posExecutive.id", + "posExecutive.posExecutiveName", + ]) + .where("position.posMasterId IN (:...child3PosMasterIds)", { child3PosMasterIds }) + .getMany(); + } + + // Combine ข้อมูล orgChild3 + for (let orgChild3 of orgChild3Data) { + orgChild3.posMasters = child3PosMasters.filter((pm: any) => pm.orgChild3Id === orgChild3.id); + + for (let posMaster of orgChild3.posMasters) { + if (posMaster.current_holderId) { + posMaster.current_holder = child3Profiles.find( + (p: any) => p.id === posMaster.current_holderId, + ); + if (posMaster.current_holder) { + posMaster.current_holder.profileEducations = child3Educations.filter( + (ed: any) => ed.profileId === posMaster.current_holderId, + ); + posMaster.current_holder.profileSalary = child3Salaries.filter( + (sal: any) => sal.profileId === posMaster.current_holderId, + ); + } + } + + if (posMaster.next_holderId) { + posMaster.next_holder = child3Profiles.find((p: any) => p.id === posMaster.next_holderId); + if (posMaster.next_holder) { + posMaster.next_holder.profileEducations = child3Educations.filter( + (ed: any) => ed.profileId === posMaster.next_holderId, + ); + posMaster.next_holder.profileSalary = child3Salaries.filter( + (sal: any) => sal.profileId === posMaster.next_holderId, + ); + } + } + + posMaster.positions = child3Positions.filter( + (pos: any) => pos.posMasterId === posMaster.id, + ); + } + } + + // Step 11: ปรับปรุง orgChild4Data + const originalOrgChild3Ids = + orgChild3Data.length > 0 ? orgChild3Data.map((orgChild3) => orgChild3.id) : []; + const orgChild4Data = await this.child4Repository + .createQueryBuilder("orgChild4") + .select([ + "orgChild4.id", + "orgChild4.orgChild3Id", + "orgChild4.orgChild4Name", + "orgChild4.orgChild4ShortName", + "orgChild4.orgChild4Order", + ]) + .where( + originalOrgChild3Ids.length > 0 + ? "orgChild4.orgChild3Id IN (:...originalOrgChild3Ids)" + : "1=0", + { + originalOrgChild3Ids, + }, + ) + .orderBy("orgChild4.orgChild4Order", "ASC") + .getMany(); + + // ดึงข้อมูล PosMasters สำหรับ orgChild4 + const orgChild4Ids = orgChild4Data.map((orgChild4) => orgChild4.id); + let child4PosMasters: any[] = []; + if (orgChild4Ids.length > 0) { + child4PosMasters = await this.posMasterRepository + .createQueryBuilder("posMaster") + .select([ + "posMaster.id", + "posMaster.posMasterOrder", + "posMaster.isSit", + "posMaster.posMasterNo", + "posMaster.ancestorDNA", + "posMaster.reason", + "posMaster.orgChild4Id", + "posMaster.current_holderId", + "posMaster.next_holderId", + ]) + .where("posMaster.orgChild4Id IN (:...orgChild4Ids)", { orgChild4Ids }) + .orderBy("posMaster.posMasterOrder", "ASC") + .getMany(); + } + + // รวบรวม Profile IDs สำหรับ child4 + const child4ProfileIds = + child4PosMasters.length > 0 + ? [ + ...new Set([ + ...child4PosMasters.map((pm: any) => pm.current_holderId).filter(Boolean), + ...child4PosMasters.map((pm: any) => pm.next_holderId).filter(Boolean), + ]), + ] + : []; + + // ดึงข้อมูล Profiles, Education, Salary, Positions สำหรับ child4 + let child4Profiles: any[] = []; + let child4Educations: any[] = []; + let child4Salaries: any[] = []; + let child4Positions: any[] = []; + + if (child4ProfileIds.length > 0) { + [child4Profiles, child4Educations, child4Salaries] = await Promise.all([ + this.profileRepository + .createQueryBuilder("profile") + .select([ + "profile.id", + "profile.prefix", + "profile.firstName", + "profile.lastName", + "profile.position", + "profile.posTypeId", + "profile.posLevelId", + ]) + .where("profile.id IN (:...child4ProfileIds)", { child4ProfileIds }) + .getMany(), + + this.profileEducationRepository + .createQueryBuilder("pe") + .select([ + "pe.profileId", + "pe.finishDate", + "pe.degree", + "ROW_NUMBER() OVER (PARTITION BY pe.profileId ORDER BY pe.finishDate DESC) as rn", + ]) + .where("pe.profileId IN (:...child4ProfileIds)", { child4ProfileIds }) + .getRawMany() + .then((data: any[]) => data.filter((ed: any) => ed.rn === 1)), + + this.profileSalaryRepository + .createQueryBuilder("ps") + .select([ + "ps.profileId", + "ps.commandDateAffect", + "ps.amount", + "ROW_NUMBER() OVER (PARTITION BY ps.profileId ORDER BY ps.commandDateAffect DESC) as rn", + ]) + .where("ps.profileId IN (:...child4ProfileIds)", { child4ProfileIds }) + .getRawMany() + .then((data: any[]) => data.filter((sal: any) => sal.rn === 1)), + ]); + } + + if (child4PosMasters.length > 0) { + const child4PosMasterIds = child4PosMasters.map((pm: any) => pm.id); + child4Positions = await this.positionRepository + .createQueryBuilder("position") + .leftJoinAndSelect("position.posType", "posType") + .leftJoinAndSelect("position.posLevel", "posLevel") + .leftJoinAndSelect("position.posExecutive", "posExecutive") + .select([ + "position.id", + "position.positionName", + "position.positionIsSelected", + "position.posMasterId", + "posType.id", + "posType.posTypeName", + "posLevel.id", + "posLevel.posLevelName", + "posExecutive.id", + "posExecutive.posExecutiveName", + ]) + .where("position.posMasterId IN (:...child4PosMasterIds)", { child4PosMasterIds }) + .getMany(); + } + + // Combine ข้อมูล orgChild4 + for (let orgChild4 of orgChild4Data) { + orgChild4.posMasters = child4PosMasters.filter((pm: any) => pm.orgChild4Id === orgChild4.id); + + for (let posMaster of orgChild4.posMasters) { + if (posMaster.current_holderId) { + posMaster.current_holder = child4Profiles.find( + (p: any) => p.id === posMaster.current_holderId, + ); + if (posMaster.current_holder) { + posMaster.current_holder.profileEducations = child4Educations.filter( + (ed: any) => ed.profileId === posMaster.current_holderId, + ); + posMaster.current_holder.profileSalary = child4Salaries.filter( + (sal: any) => sal.profileId === posMaster.current_holderId, + ); + } + } + + if (posMaster.next_holderId) { + posMaster.next_holder = child4Profiles.find((p: any) => p.id === posMaster.next_holderId); + if (posMaster.next_holder) { + posMaster.next_holder.profileEducations = child4Educations.filter( + (ed: any) => ed.profileId === posMaster.next_holderId, + ); + posMaster.next_holder.profileSalary = child4Salaries.filter( + (sal: any) => sal.profileId === posMaster.next_holderId, + ); + } + } + + posMaster.positions = child4Positions.filter( + (pos: any) => pos.posMasterId === posMaster.id, + ); + } + } + + let orgRevisionActive: any = await this.orgRevisionRepository + .createQueryBuilder("orgRevision") + .select([ + "orgRevision.id", + "orgRevision.orgRevisionIsCurrent", + "orgRevision.orgRevisionIsDraft", + "orgRevision.createdAt", + ]) + .leftJoinAndSelect("orgRevision.posMasters", "posMasters") + .leftJoinAndSelect("posMasters.positions", "positions") + .leftJoinAndSelect("positions.posType", "posType") + .leftJoinAndSelect("positions.posLevel", "posLevel") + .leftJoinAndSelect("positions.posExecutive", "posExecutive") + .addSelect([ + "posMasters.posMasterOrder", + "posMasters.posMasterNo", + "posMasters.ancestorDNA", + "posMasters.orgRevisionId", + ]) + .addSelect(["positions.positionName", "positions.positionIsSelected"]) + .addSelect(["posType.posTypeName"]) + .addSelect(["posLevel.posLevelName"]) + .addSelect(["posExecutive.posExecutiveName"]) + .where("orgRevision.orgRevisionIsCurrent = :isCurrent", { isCurrent: true }) + .andWhere("orgRevision.orgRevisionIsDraft = :isDraft", { isDraft: false }) + .getOne(); - let orgRevisionActive: any = await this.orgRevisionRepository.findOne({ - where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, - relations: [ - "posMasters", - "posMasters.positions", - "posMasters.positions.posLevel", - "posMasters.positions.posType", - "posMasters.positions.posExecutive", - ], - }); if (orgRevisionActive == null) { - const _orgRevisionActive = await this.orgRevisionRepository.find({ - order: { createdAt: "DESC" }, - skip: 1, - relations: [ - "posMasters", - "posMasters.positions", - "posMasters.positions.posLevel", - "posMasters.positions.posType", - "posMasters.positions.posExecutive", - ], - }); + const _orgRevisionActive = await this.orgRevisionRepository + .createQueryBuilder("orgRevision") + .select([ + "orgRevision.id", + "orgRevision.orgRevisionIsCurrent", + "orgRevision.orgRevisionIsDraft", + "orgRevision.createdAt", + ]) + .leftJoinAndSelect("orgRevision.posMasters", "posMasters") + .leftJoinAndSelect("posMasters.positions", "positions") + .leftJoinAndSelect("positions.posType", "posType") + .leftJoinAndSelect("positions.posLevel", "posLevel") + .leftJoinAndSelect("positions.posExecutive", "posExecutive") + .addSelect([ + "posMasters.posMasterOrder", + "posMasters.posMasterNo", + "posMasters.ancestorDNA", + "posMasters.orgRevisionId", + ]) + .addSelect(["positions.positionName", "positions.positionIsSelected"]) + .addSelect(["posType.posTypeName"]) + .addSelect(["posLevel.posLevelName"]) + .addSelect(["posExecutive.posExecutiveName"]) + .orderBy("orgRevision.createdAt", "DESC") + .skip(1) + .take(1) + .getMany(); if (_orgRevisionActive.length > 0) orgRevisionActive = _orgRevisionActive[0]; } let data = new Array(); let _node: any; let no = 1; - for (let orgRoot of orgRootData) { - await Promise.all( - orgRoot.posMasters - .sort((a, b) => a.posMasterOrder - b.posMasterOrder) - .map(async (posMaster) => { - if (posMaster.orgChild1Id == null) { - const positionName = [...new Set(posMaster.positions.map((x) => x.positionName))]; - const posType = [ - ...new Set( - posMaster.positions - .filter((x: any) => x.posType != null) - .map((x) => x.posType.posTypeName), - ), - ]; - const posLevel = [ - ...new Set( - posMaster.positions - .filter((x: any) => x.posLevel != null) - .map((x) => x.posLevel.posLevelName), - ), - ]; - const posExecutive = [ - ...new Set( - posMaster.positions - .filter((x: any) => x.posExecutive != null) - .map((x) => x.posExecutive.posExecutiveName), - ), - ]; - let positionMasterProfileOld: any = null; - if (posMaster.next_holder != null) { - positionMasterProfileOld = posMaster.next_holder.current_holders.find( - (x) => x.orgRevisionId == orgRevisionActive.id, - ); - } - let positionMasterOld: any = null; - let profilePositionName: any = []; - let profilePosType: any = []; - let profilePosLevel: any = []; - let profilePosExecutive: any = []; - if (posMaster.ancestorDNA != null && posMaster.ancestorDNA != "") { - positionMasterOld = orgRevisionActive.posMasters.find( - (x: any) => - x.orgRevisionId == orgRevisionActive.id && - x.ancestorDNA == posMaster.ancestorDNA, - ); + for (let orgRoot of orgRootData) { + const posMastersFiltered = orgRoot.posMasters.sort( + (a, b) => a.posMasterOrder - b.posMasterOrder, + ); + + for (let posMaster of posMastersFiltered) { + if (posMaster.orgChild1Id == null) { + // Use temporary arrays to reduce memory allocation + const posNames = posMaster.positions.map((x) => x.positionName); + const positionName = [...new Set(posNames)]; + + const typeNames = posMaster.positions + .filter((x: any) => x.posType != null) + .map((x) => x.posType.posTypeName); + const posType = [...new Set(typeNames)]; + + const levelNames = posMaster.positions + .filter((x: any) => x.posLevel != null) + .map((x) => x.posLevel.posLevelName); + const posLevel = [...new Set(levelNames)]; + + const execNames = posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x) => x.posExecutive.posExecutiveName); + const posExecutive = [...new Set(execNames)]; + let positionMasterProfileOld: any = null; + if (posMaster.next_holder != null && posMaster.next_holder.current_holders) { + positionMasterProfileOld = posMaster.next_holder.current_holders.find( + (x) => x.orgRevisionId == orgRevisionActive.id, + ); + } + + let positionMasterOld: any = null; + let profilePositionName: any = []; + let profilePosType: any = []; + let profilePosLevel: any = []; + let profilePosExecutive: any = []; + if ( + posMaster.ancestorDNA != null && + posMaster.ancestorDNA != "" && + orgRevisionActive && + orgRevisionActive.posMasters + ) { + positionMasterOld = orgRevisionActive.posMasters.find( + (x: any) => + x.orgRevisionId == orgRevisionActive.id && x.ancestorDNA == posMaster.ancestorDNA, + ); + if (positionMasterOld && positionMasterOld.positions) { + profilePositionName = [ + ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), + ]; + } + if (positionMasterOld && positionMasterOld.positions) { + profilePositionName = [ + ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), + ]; + profilePosType = [ + ...new Set( + positionMasterOld.positions + .filter((x: any) => x.posType != null) + .map((x: any) => x.posType.posTypeName), + ), + ]; + profilePosLevel = [ + ...new Set( + positionMasterOld.positions + .filter((x: any) => x.posLevel != null) + .map((x: any) => x.posLevel.posLevelName), + ), + ]; + profilePosExecutive = [ + ...new Set( + positionMasterOld.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => x.posExecutive.posExecutiveName), + ), + ]; + } + } + let education: any = ""; + if ( + posMaster.next_holder != null && + posMaster.next_holder.profileEducations != null && + posMaster.next_holder.profileEducations.length > 0 + ) { + let _education: any = posMaster.next_holder.profileEducations.sort( + (a, b) => + (b.finishDate == null ? 0 : b.finishDate.getTime()) - + (a.finishDate == null ? 0 : a.finishDate.getTime()), + ); + if (_education.length > 0) { + education = _education[0]; + } + } else { + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileEducations != null && + posMaster.current_holder.profileEducations.length > 0 + ) { + let _education: any = posMaster.current_holder.profileEducations.sort( + (a, b) => + (b.finishDate == null ? 0 : b.finishDate.getTime()) - + (a.finishDate == null ? 0 : a.finishDate.getTime()), + ); + if (_education.length > 0) { + education = _education[0]; + } + } + } + let salary: any = ""; + if ( + posMaster.next_holder != null && + posMaster.next_holder.profileSalary != null && + posMaster.next_holder.profileSalary.length > 0 + ) { + let _salary: any = posMaster.next_holder.profileSalary.sort( + (a, b) => + (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - + (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), + ); + if (_salary.length > 0) { + salary = _salary[0]; + } + } else { + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileSalary != null && + posMaster.current_holder.profileSalary.length > 0 + ) { + let _salary: any = posMaster.current_holder.profileSalary.sort( + (a, b) => + (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - + (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), + ); + if (_salary.length > 0) { + salary = _salary[0]; + } + } + } + let node = { + posMasterOrder: posMaster.posMasterOrder, // + isSit: posMaster.isSit, // + orgTreeName: orgRoot.orgRootName, + orgTreeShortName: orgRoot.orgRootShortName, + posMasterNo: posMaster.posMasterNo, + positionName: + posMaster.next_holder == null + ? positionName.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true)?.positionName + : posMaster.next_holder.position, + posType: + posMaster.next_holder == null + ? posType.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true)?.posType + ?.posTypeName + : posMaster.next_holder.posType == null + ? "-" + : posMaster.next_holder.posType.posTypeName, + posLevel: + posMaster.next_holder == null + ? posLevel.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true)?.posLevel + ?.posLevelName + : posMaster.next_holder.posLevel == null + ? "-" + : posMaster.next_holder.posLevel.posLevelName, + posExecutive: + posMaster.next_holder == null + ? posExecutive.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true)?.posExecutive + ?.posExecutiveName + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posExecutive?.posExecutiveName, + + profileOrgName: + posMaster.next_holder == null + ? orgRoot.orgRootName + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.orgChild4 != null + ? positionMasterProfileOld.orgChild4.orgChild4Name + : positionMasterProfileOld.orgChild3 != null + ? positionMasterProfileOld.orgChild3.orgChild3Name + : positionMasterProfileOld.orgChild2 != null + ? positionMasterProfileOld.orgChild2.orgChild2Name + : positionMasterProfileOld.orgChild1 != null + ? positionMasterProfileOld.orgChild1.orgChild1Name + : positionMasterProfileOld.orgRoot != null + ? positionMasterProfileOld.orgRoot.orgRootName + : "-", + profileOrgShortName: + posMaster.next_holder == null + ? orgRoot.orgRootShortName + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.orgChild4 != null + ? positionMasterProfileOld.orgChild4.orgChild4ShortName + : positionMasterProfileOld.orgChild3 != null + ? positionMasterProfileOld.orgChild3.orgChild3ShortName + : positionMasterProfileOld.orgChild2 != null + ? positionMasterProfileOld.orgChild2.orgChild2ShortName + : positionMasterProfileOld.orgChild1 != null + ? positionMasterProfileOld.orgChild1.orgChild1ShortName + : positionMasterProfileOld.orgRoot != null + ? positionMasterProfileOld.orgRoot.orgRootShortName + : "-", + profileFullname: + posMaster.next_holder == null + ? posMaster.current_holder == null + ? "- ว่าง -" + : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}` + : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, + profilePosMasterNo: + posMaster.next_holder == null + ? positionMasterOld == null + ? "-" + : positionMasterOld.posMasterNo + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.posMasterNo, + profilePositionName: + posMaster.next_holder == null + ? positionMasterOld == null + ? positionName.join(" หรือ ") + : profilePositionName.join(" หรือ ") + : posMaster.next_holder.position, + profilePosType: + posMaster.next_holder == null + ? positionMasterOld == null + ? posType.join(" หรือ ") + : profilePosType.join(" หรือ ") + : posMaster.next_holder.posType == null + ? "-" + : posMaster.next_holder.posType.posTypeName, + profilePosLevel: + posMaster.next_holder == null + ? positionMasterOld == null + ? posLevel.join(" หรือ ") + : profilePosLevel.join(" หรือ ") + : posMaster.next_holder.posLevel == null + ? "-" + : posMaster.next_holder.posLevel.posLevelName, + profilePosExecutive: + posMaster.next_holder == null + ? positionMasterOld == null + ? posExecutive.join(" หรือ ") + : profilePosExecutive.join(" หรือ ") + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posExecutive?.posExecutiveName, + education: education == "" ? "" : education.degree, + salary: salary == "" ? "" : salary.amount, + reason: posMaster.reason, + }; + if (_node == null) { + const head = { + posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), + profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), + posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + profilePosMasterNo: Extension.ToThaiNumber(node.profileOrgShortName.toString()), + profilePosExecutive: "", + profilePositionName: "", + profilePosType: "", + profilePosLevel: "", + education: "", + salary: "", + reason: "", + }; + data.push(head); + const _head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), + profileFullname: + node.profileFullname == null + ? "" + : Extension.ToThaiNumber(node.profileFullname.toString()), + posExecutive: + node.posExecutive == null + ? "" + : Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: + node.positionName == null + ? "" + : Extension.ToThaiNumber(node.positionName.toString()), + posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), + posLevel: + node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), + profilePosMasterNo: Extension.ToThaiNumber(node.profilePosMasterNo.toString()), + profilePosExecutive: + node.profilePosExecutive == null + ? "" + : Extension.ToThaiNumber(node.profilePosExecutive.toString()), + profilePositionName: + node.profilePositionName == null + ? "" + : Extension.ToThaiNumber(node.profilePositionName.toString()), + profilePosType: + node.profilePosType == null + ? "" + : Extension.ToThaiNumber(node.profilePosType.toString()), + profilePosLevel: + node.profilePosLevel == null + ? "" + : Extension.ToThaiNumber(node.profilePosLevel.toString()), + education: + node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), + salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", + reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _node.orgTreeShortName || + node.orgTreeName != _node.orgTreeName || + node.profileOrgShortName != _node.profileOrgShortName || + node.profileOrgName != _node.profileOrgName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _node.orgTreeShortName ? "" : node.orgTreeShortName, + profileFullname: + node.profileOrgName == _node.profileOrgName ? "" : node.profileOrgName, + posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + profilePosMasterNo: + node.profileOrgShortName == _node.profileOrgShortName + ? "" + : node.profileOrgShortName, + profilePosExecutive: "", + profilePositionName: "", + profilePosType: "", + profilePosLevel: "", + education: "", + salary: "", + reason: "", + }; + data.push(head); + _node == null; + } + const head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null ? "" : Extension.ToThaiNumber(node.posMasterNo.toString()), + profileFullname: + node.profileFullname == null + ? "" + : Extension.ToThaiNumber(node.profileFullname.toString()), + posExecutive: + node.posExecutive == null + ? "" + : Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: + node.positionName == null + ? "" + : Extension.ToThaiNumber(node.positionName.toString()), + posType: node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), + posLevel: + node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), + profilePosMasterNo: Extension.ToThaiNumber(node.profilePosMasterNo.toString()), + profilePosExecutive: + node.profilePosExecutive == null + ? "" + : Extension.ToThaiNumber(node.profilePosExecutive.toString()), + profilePositionName: + node.profilePositionName == null + ? "" + : Extension.ToThaiNumber(node.profilePositionName.toString()), + profilePosType: + node.profilePosType == null + ? "" + : Extension.ToThaiNumber(node.profilePosType.toString()), + profilePosLevel: + node.profilePosLevel == null + ? "" + : Extension.ToThaiNumber(node.profilePosLevel.toString()), + education: + node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), + salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", + reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(head); + } + no += 1; + _node = node; + } + } + _node = null; + + for (let orgChild1 of orgChild1Data.filter( + (orgChild1) => orgChild1.orgRootId === orgRoot.id, + )) { + const posMastersFiltered1 = orgChild1.posMasters.sort( + (a, b) => a.posMasterOrder - b.posMasterOrder, + ); + for (let posMaster of posMastersFiltered1) { + if (posMaster.orgChild2Id == null) { + const positionName = [...new Set(posMaster.positions.map((x) => x.positionName))]; + const posType = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posType != null) + .map((x) => x.posType.posTypeName), + ), + ]; + const posLevel = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posLevel != null) + .map((x) => x.posLevel.posLevelName), + ), + ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x) => x.posExecutive.posExecutiveName), + ), + ]; + let positionMasterProfileOld: any = null; + if (posMaster.next_holder != null && posMaster.next_holder.current_holders) { + positionMasterProfileOld = posMaster.next_holder.current_holders.find( + (x) => x.orgRevisionId == orgRevisionActive.id, + ); + } + + let positionMasterOld: any = null; + let profilePositionName: any = []; + let profilePosType: any = []; + let profilePosLevel: any = []; + let profilePosExecutive: any = []; + if ( + posMaster.ancestorDNA != null && + posMaster.ancestorDNA != "" && + orgRevisionActive && + orgRevisionActive.posMasters + ) { + positionMasterOld = orgRevisionActive.posMasters.find( + (x: any) => + x.orgRevisionId == orgRevisionActive.id && x.ancestorDNA == posMaster.ancestorDNA, + ); + if (positionMasterOld && positionMasterOld.positions) { profilePositionName = [ ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), ]; @@ -3824,6 +4955,419 @@ export class ReportController extends Controller { ), ]; } + } + let education: any = ""; + if ( + posMaster.next_holder != null && + posMaster.next_holder.profileEducations != null && + posMaster.next_holder.profileEducations.length > 0 + ) { + let _education: any = posMaster.next_holder.profileEducations.sort( + (a, b) => + (b.finishDate == null ? 0 : b.finishDate.getTime()) - + (a.finishDate == null ? 0 : a.finishDate.getTime()), + ); + if (_education.length > 0) { + education = _education[0]; + } + } else { + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileEducations != null && + posMaster.current_holder.profileEducations.length > 0 + ) { + let _education: any = posMaster.current_holder.profileEducations.sort( + (a, b) => + (b.finishDate == null ? 0 : b.finishDate.getTime()) - + (a.finishDate == null ? 0 : a.finishDate.getTime()), + ); + if (_education.length > 0) { + education = _education[0]; + } + } + } + let salary: any = ""; + if ( + posMaster.next_holder != null && + posMaster.next_holder.profileSalary != null && + posMaster.next_holder.profileSalary.length > 0 + ) { + let _salary: any = posMaster.next_holder.profileSalary.sort( + (a, b) => + (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - + (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), + ); + if (_salary.length > 0) { + salary = _salary[0]; + } + } else { + if ( + posMaster.current_holder != null && + posMaster.current_holder.profileSalary != null && + posMaster.current_holder.profileSalary.length > 0 + ) { + let _salary: any = posMaster.current_holder.profileSalary.sort( + (a, b) => + (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - + (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), + ); + if (_salary.length > 0) { + salary = _salary[0]; + } + } + } + + let node = { + posMasterOrder: posMaster.posMasterOrder, // + isSit: posMaster.isSit, // + orgTreeName: orgChild1.orgChild1Name, + orgTreeShortName: orgChild1.orgChild1ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: + posMaster.next_holder == null + ? positionName.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.positionName + : posMaster.next_holder.position, + posType: + posMaster.next_holder == null + ? posType.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true)?.posType + ?.posTypeName + : posMaster.next_holder.posType == null + ? "-" + : posMaster.next_holder.posType.posTypeName, + posLevel: + posMaster.next_holder == null + ? posLevel.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true)?.posLevel + ?.posLevelName + : posMaster.next_holder.posLevel == null + ? "-" + : posMaster.next_holder.posLevel.posLevelName, + posExecutive: + posMaster.next_holder == null + ? posExecutive.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.posExecutive?.posExecutiveName + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posExecutive?.posExecutiveName, + + profileOrgName: + posMaster.next_holder == null + ? orgChild1.orgChild1Name + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.orgChild4 != null + ? positionMasterProfileOld.orgChild4.orgChild4Name + : positionMasterProfileOld.orgChild3 != null + ? positionMasterProfileOld.orgChild3.orgChild3Name + : positionMasterProfileOld.orgChild2 != null + ? positionMasterProfileOld.orgChild2.orgChild2Name + : positionMasterProfileOld.orgChild1 != null + ? positionMasterProfileOld.orgChild1.orgChild1Name + : positionMasterProfileOld.orgRoot != null + ? positionMasterProfileOld.orgRoot.orgRootName + : "-", + profileOrgShortName: + posMaster.next_holder == null + ? orgChild1.orgChild1ShortName + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.orgChild4 != null + ? positionMasterProfileOld.orgChild4.orgChild4ShortName + : positionMasterProfileOld.orgChild3 != null + ? positionMasterProfileOld.orgChild3.orgChild3ShortName + : positionMasterProfileOld.orgChild2 != null + ? positionMasterProfileOld.orgChild2.orgChild2ShortName + : positionMasterProfileOld.orgChild1 != null + ? positionMasterProfileOld.orgChild1.orgChild1ShortName + : positionMasterProfileOld.orgRoot != null + ? positionMasterProfileOld.orgRoot.orgRootShortName + : "-", + profileFullname: + posMaster.next_holder == null + ? posMaster.current_holder == null + ? "- ว่าง -" + : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}` + : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, + profilePosMasterNo: + posMaster.next_holder == null + ? positionMasterOld == null + ? "-" + : positionMasterOld.posMasterNo + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.posMasterNo, + profilePositionName: + posMaster.next_holder == null + ? positionMasterOld == null + ? positionName.join(" หรือ ") + : profilePositionName.join(" หรือ ") + : posMaster.next_holder.position, + profilePosType: + posMaster.next_holder == null + ? positionMasterOld == null + ? posType.join(" หรือ ") + : profilePosType.join(" หรือ ") + : posMaster.next_holder.posType == null + ? "-" + : posMaster.next_holder.posType.posTypeName, + profilePosLevel: + posMaster.next_holder == null + ? positionMasterOld == null + ? posLevel.join(" หรือ ") + : profilePosLevel.join(" หรือ ") + : posMaster.next_holder.posLevel == null + ? "-" + : posMaster.next_holder.posLevel.posLevelName, + profilePosExecutive: + posMaster.next_holder == null + ? positionMasterOld == null + ? posExecutive.join(" หรือ ") + : profilePosExecutive.join(" หรือ ") + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posExecutive?.posExecutiveName, + education: education == "" ? "" : education.degree, + salary: salary == "" ? "" : salary.amount, + reason: posMaster.reason, + }; + if (_node == null) { + const head = { + posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), + profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), + posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + profilePosMasterNo: Extension.ToThaiNumber(node.profileOrgShortName.toString()), + profilePosExecutive: "", + profilePositionName: "", + profilePosType: "", + profilePosLevel: "", + education: "", + salary: "", + reason: "", + }; + data.push(head); + const _head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + profileFullname: + node.profileFullname == null + ? "" + : Extension.ToThaiNumber(node.profileFullname.toString()), + posExecutive: + node.posExecutive == null + ? "" + : Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: + node.positionName == null + ? "" + : Extension.ToThaiNumber(node.positionName.toString()), + posType: + node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), + posLevel: + node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), + profilePosMasterNo: Extension.ToThaiNumber(node.profilePosMasterNo.toString()), + profilePosExecutive: + node.profilePosExecutive == null + ? "" + : Extension.ToThaiNumber(node.profilePosExecutive.toString()), + profilePositionName: + node.profilePositionName == null + ? "" + : Extension.ToThaiNumber(node.profilePositionName.toString()), + profilePosType: + node.profilePosType == null + ? "" + : Extension.ToThaiNumber(node.profilePosType.toString()), + profilePosLevel: + node.profilePosLevel == null + ? "" + : Extension.ToThaiNumber(node.profilePosLevel.toString()), + education: + node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), + salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", + reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _node.orgTreeShortName || + node.orgTreeName != _node.orgTreeName || + node.profileOrgShortName != _node.profileOrgShortName || + node.profileOrgName != _node.profileOrgName + ) { + const head = { + posMasterNo: + node.orgTreeShortName == _node.orgTreeShortName ? "" : node.orgTreeShortName, + profileFullname: + node.profileOrgName == _node.profileOrgName ? "" : node.profileOrgName, + posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, + positionName: "", + posType: "", + posLevel: "", + profilePosMasterNo: + node.profileOrgShortName == _node.profileOrgShortName + ? "" + : node.profileOrgShortName, + profilePosExecutive: "", + profilePositionName: "", + profilePosType: "", + profilePosLevel: "", + education: "", + salary: "", + reason: "", + }; + data.push(head); + _node == null; + } + const head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + profileFullname: + node.profileFullname == null + ? "" + : Extension.ToThaiNumber(node.profileFullname.toString()), + posExecutive: + node.posExecutive == null + ? "" + : Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: + node.positionName == null + ? "" + : Extension.ToThaiNumber(node.positionName.toString()), + posType: + node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), + posLevel: + node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), + profilePosMasterNo: Extension.ToThaiNumber(node.profilePosMasterNo.toString()), + profilePosExecutive: + node.profilePosExecutive == null + ? "" + : Extension.ToThaiNumber(node.profilePosExecutive.toString()), + profilePositionName: + node.profilePositionName == null + ? "" + : Extension.ToThaiNumber(node.profilePositionName.toString()), + profilePosType: + node.profilePosType == null + ? "" + : Extension.ToThaiNumber(node.profilePosType.toString()), + profilePosLevel: + node.profilePosLevel == null + ? "" + : Extension.ToThaiNumber(node.profilePosLevel.toString()), + education: + node.education == null ? "" : Extension.ToThaiNumber(node.education.toString()), + salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", + reason: node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(head); + } + no += 1; + _node = node; + } + } + _node = null; + + for (let orgChild2 of orgChild2Data.filter( + (orgChild2) => orgChild2.orgChild1Id === orgChild1.id, + )) { + const posMastersFiltered2 = orgChild2.posMasters.sort( + (a, b) => a.posMasterOrder - b.posMasterOrder, + ); + for (let posMaster of posMastersFiltered2) { + if (posMaster.orgChild3Id == null) { + const positionName = [...new Set(posMaster.positions.map((x) => x.positionName))]; + const posType = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posType != null) + .map((x) => x.posType.posTypeName), + ), + ]; + const posLevel = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posLevel != null) + .map((x) => x.posLevel.posLevelName), + ), + ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x) => x.posExecutive.posExecutiveName), + ), + ]; + let positionMasterProfileOld: any = null; + if (posMaster.next_holder != null && posMaster.next_holder.current_holders) { + positionMasterProfileOld = posMaster.next_holder.current_holders.find( + (x) => x.orgRevisionId == orgRevisionActive.id, + ); + } + + let positionMasterOld: any = null; + let profilePositionName: any = []; + let profilePosType: any = []; + let profilePosLevel: any = []; + let profilePosExecutive: any = []; + if ( + posMaster.ancestorDNA != null && + posMaster.ancestorDNA != "" && + orgRevisionActive && + orgRevisionActive.posMasters + ) { + positionMasterOld = orgRevisionActive.posMasters.find( + (x: any) => + x.orgRevisionId == orgRevisionActive.id && + x.ancestorDNA == posMaster.ancestorDNA, + ); + if (positionMasterOld && positionMasterOld.positions) { + profilePositionName = [ + ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), + ]; + profilePosType = [ + ...new Set( + positionMasterOld.positions + .filter((x: any) => x.posType != null) + .map((x: any) => x.posType.posTypeName), + ), + ]; + profilePosLevel = [ + ...new Set( + positionMasterOld.positions + .filter((x: any) => x.posLevel != null) + .map((x: any) => x.posLevel.posLevelName), + ), + ]; + profilePosExecutive = [ + ...new Set( + positionMasterOld.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => x.posExecutive.posExecutiveName), + ), + ]; + } + } let education: any = ""; if ( posMaster.next_holder != null && @@ -3838,8 +5382,7 @@ export class ReportController extends Controller { if (_education.length > 0) { education = _education[0]; } - } - else { + } else { if ( posMaster.current_holder != null && posMaster.current_holder.profileEducations != null && @@ -3869,8 +5412,7 @@ export class ReportController extends Controller { if (_salary.length > 0) { salary = _salary[0]; } - } - else { + } else { if ( posMaster.current_holder != null && posMaster.current_holder.profileSalary != null && @@ -3886,11 +5428,12 @@ export class ReportController extends Controller { } } } + let node = { posMasterOrder: posMaster.posMasterOrder, // isSit: posMaster.isSit, // - orgTreeName: orgRoot.orgRootName, - orgTreeShortName: orgRoot.orgRootShortName, + orgTreeName: orgChild2.orgChild2Name, + orgTreeShortName: orgChild2.orgChild2ShortName, posMasterNo: posMaster.posMasterNo, positionName: posMaster.next_holder == null @@ -3931,7 +5474,7 @@ export class ReportController extends Controller { profileOrgName: posMaster.next_holder == null - ? orgRoot.orgRootName + ? orgChild2.orgChild2Name : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.orgChild4 != null @@ -3947,7 +5490,7 @@ export class ReportController extends Controller { : "-", profileOrgShortName: posMaster.next_holder == null - ? orgRoot.orgRootShortName + ? orgChild2.orgChild2ShortName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.orgChild4 != null @@ -3963,7 +5506,7 @@ export class ReportController extends Controller { : "-", profileFullname: posMaster.next_holder == null - ? posMaster.current_holder == null + ? posMaster.current_holder == null ? "- ว่าง -" : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}` : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, @@ -4154,18 +5697,18 @@ export class ReportController extends Controller { no += 1; _node = node; } - }), - ); - _node = null; + } + _node = null; - for (let orgChild1 of orgChild1Data.filter( - (orgChild1) => orgChild1.orgRootId === orgRoot.id, - )) { - await Promise.all( - orgChild1.posMasters - .sort((a, b) => a.posMasterOrder - b.posMasterOrder) - .map(async (posMaster) => { - if (posMaster.orgChild2Id == null) { + for (let orgChild3 of orgChild3Data.filter( + (orgChild3) => orgChild3.orgChild2Id === orgChild2.id, + )) { + const posMastersFiltered3 = orgChild3.posMasters.sort( + (a, b) => a.posMasterOrder - b.posMasterOrder, + ); + for (let posMaster of posMastersFiltered3) { + if (posMaster.orgChild4Id == null) { + console.log(posMaster); const positionName = [...new Set(posMaster.positions.map((x) => x.positionName))]; const posType = [ ...new Set( @@ -4189,7 +5732,7 @@ export class ReportController extends Controller { ), ]; let positionMasterProfileOld: any = null; - if (posMaster.next_holder != null) { + if (posMaster.next_holder != null && posMaster.next_holder.current_holders) { positionMasterProfileOld = posMaster.next_holder.current_holders.find( (x) => x.orgRevisionId == orgRevisionActive.id, ); @@ -4200,36 +5743,43 @@ export class ReportController extends Controller { let profilePosType: any = []; let profilePosLevel: any = []; let profilePosExecutive: any = []; - if (posMaster.ancestorDNA != null && posMaster.ancestorDNA != "") { + if ( + posMaster.ancestorDNA != null && + posMaster.ancestorDNA != "" && + orgRevisionActive && + orgRevisionActive.posMasters + ) { positionMasterOld = orgRevisionActive.posMasters.find( (x: any) => x.orgRevisionId == orgRevisionActive.id && x.ancestorDNA == posMaster.ancestorDNA, ); - profilePositionName = [ - ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), - ]; - profilePosType = [ - ...new Set( - positionMasterOld.positions - .filter((x: any) => x.posType != null) - .map((x: any) => x.posType.posTypeName), - ), - ]; - profilePosLevel = [ - ...new Set( - positionMasterOld.positions - .filter((x: any) => x.posLevel != null) - .map((x: any) => x.posLevel.posLevelName), - ), - ]; - profilePosExecutive = [ - ...new Set( - positionMasterOld.positions - .filter((x: any) => x.posExecutive != null) - .map((x: any) => x.posExecutive.posExecutiveName), - ), - ]; + if (positionMasterOld && positionMasterOld.positions) { + profilePositionName = [ + ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), + ]; + profilePosType = [ + ...new Set( + positionMasterOld.positions + .filter((x: any) => x.posType != null) + .map((x: any) => x.posType.posTypeName), + ), + ]; + profilePosLevel = [ + ...new Set( + positionMasterOld.positions + .filter((x: any) => x.posLevel != null) + .map((x: any) => x.posLevel.posLevelName), + ), + ]; + profilePosExecutive = [ + ...new Set( + positionMasterOld.positions + .filter((x: any) => x.posExecutive != null) + .map((x: any) => x.posExecutive.posExecutiveName), + ), + ]; + } } let education: any = ""; if ( @@ -4245,8 +5795,7 @@ export class ReportController extends Controller { if (_education.length > 0) { education = _education[0]; } - } - else { + } else { if ( posMaster.current_holder != null && posMaster.current_holder.profileEducations != null && @@ -4276,8 +5825,7 @@ export class ReportController extends Controller { if (_salary.length > 0) { salary = _salary[0]; } - } - else { + } else { if ( posMaster.current_holder != null && posMaster.current_holder.profileSalary != null && @@ -4297,8 +5845,8 @@ export class ReportController extends Controller { let node = { posMasterOrder: posMaster.posMasterOrder, // isSit: posMaster.isSit, // - orgTreeName: orgChild1.orgChild1Name, - orgTreeShortName: orgChild1.orgChild1ShortName, + orgTreeName: orgChild3.orgChild3Name, + orgTreeShortName: orgChild3.orgChild3ShortName, posMasterNo: posMaster.posMasterNo, positionName: posMaster.next_holder == null @@ -4339,7 +5887,7 @@ export class ReportController extends Controller { profileOrgName: posMaster.next_holder == null - ? orgChild1.orgChild1Name + ? orgChild3.orgChild3Name : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.orgChild4 != null @@ -4355,7 +5903,7 @@ export class ReportController extends Controller { : "-", profileOrgShortName: posMaster.next_holder == null - ? orgChild1.orgChild1ShortName + ? orgChild3.orgChild3ShortName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.orgChild4 != null @@ -4371,7 +5919,7 @@ export class ReportController extends Controller { : "-", profileFullname: posMaster.next_holder == null - ? posMaster.current_holder == null + ? posMaster.current_holder == null ? "- ว่าง -" : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}` : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, @@ -4570,58 +6118,63 @@ export class ReportController extends Controller { no += 1; _node = node; } - }), - ); - _node = null; + } + console.log("Processing orgChild4"); + _node = null; - for (let orgChild2 of orgChild2Data.filter( - (orgChild2) => orgChild2.orgChild1Id === orgChild1.id, - )) { - await Promise.all( - orgChild2.posMasters - .sort((a, b) => a.posMasterOrder - b.posMasterOrder) - .map(async (posMaster) => { - if (posMaster.orgChild3Id == null) { - const positionName = [...new Set(posMaster.positions.map((x) => x.positionName))]; - const posType = [ - ...new Set( - posMaster.positions - .filter((x: any) => x.posType != null) - .map((x) => x.posType.posTypeName), - ), - ]; - const posLevel = [ - ...new Set( - posMaster.positions - .filter((x: any) => x.posLevel != null) - .map((x) => x.posLevel.posLevelName), - ), - ]; - const posExecutive = [ - ...new Set( - posMaster.positions - .filter((x: any) => x.posExecutive != null) - .map((x) => x.posExecutive.posExecutiveName), - ), - ]; - let positionMasterProfileOld: any = null; - if (posMaster.next_holder != null) { - positionMasterProfileOld = posMaster.next_holder.current_holders.find( - (x) => x.orgRevisionId == orgRevisionActive.id, - ); - } + for (let orgChild4 of orgChild4Data.filter( + (orgChild4) => orgChild4.orgChild3Id === orgChild3.id, + )) { + const posMastersFiltered4 = orgChild4.posMasters.sort( + (a, b) => a.posMasterOrder - b.posMasterOrder, + ); + for (let posMaster of posMastersFiltered4) { + const positionName = [...new Set(posMaster.positions.map((x) => x.positionName))]; + const posType = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posType != null) + .map((x) => x.posType.posTypeName), + ), + ]; + const posLevel = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posLevel != null) + .map((x) => x.posLevel.posLevelName), + ), + ]; + const posExecutive = [ + ...new Set( + posMaster.positions + .filter((x: any) => x.posExecutive != null) + .map((x) => x.posExecutive.posExecutiveName), + ), + ]; + let positionMasterProfileOld: any = null; + if (posMaster.next_holder != null && posMaster.next_holder.current_holders) { + positionMasterProfileOld = posMaster.next_holder.current_holders.find( + (x) => x.orgRevisionId == orgRevisionActive.id, + ); + } - let positionMasterOld: any = null; - let profilePositionName: any = []; - let profilePosType: any = []; - let profilePosLevel: any = []; - let profilePosExecutive: any = []; - if (posMaster.ancestorDNA != null && posMaster.ancestorDNA != "") { - positionMasterOld = orgRevisionActive.posMasters.find( - (x: any) => - x.orgRevisionId == orgRevisionActive.id && - x.ancestorDNA == posMaster.ancestorDNA, - ); + let positionMasterOld: any = null; + let profilePositionName: any = []; + let profilePosType: any = []; + let profilePosLevel: any = []; + let profilePosExecutive: any = []; + if ( + posMaster.ancestorDNA != null && + posMaster.ancestorDNA != "" && + orgRevisionActive && + orgRevisionActive.posMasters + ) { + positionMasterOld = orgRevisionActive.posMasters.find( + (x: any) => + x.orgRevisionId == orgRevisionActive.id && + x.ancestorDNA == posMaster.ancestorDNA, + ); + if (positionMasterOld && positionMasterOld.positions) { profilePositionName = [ ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), ]; @@ -4647,13 +6200,28 @@ export class ReportController extends Controller { ), ]; } - let education: any = ""; + } + let education: any = ""; + if ( + posMaster.next_holder != null && + posMaster.next_holder.profileEducations != null && + posMaster.next_holder.profileEducations.length > 0 + ) { + let _education: any = posMaster.next_holder.profileEducations.sort( + (a, b) => + (b.finishDate == null ? 0 : b.finishDate.getTime()) - + (a.finishDate == null ? 0 : a.finishDate.getTime()), + ); + if (_education.length > 0) { + education = _education[0]; + } + } else { if ( - posMaster.next_holder != null && - posMaster.next_holder.profileEducations != null && - posMaster.next_holder.profileEducations.length > 0 + posMaster.current_holder != null && + posMaster.current_holder.profileEducations != null && + posMaster.current_holder.profileEducations.length > 0 ) { - let _education: any = posMaster.next_holder.profileEducations.sort( + let _education: any = posMaster.current_holder.profileEducations.sort( (a, b) => (b.finishDate == null ? 0 : b.finishDate.getTime()) - (a.finishDate == null ? 0 : a.finishDate.getTime()), @@ -4662,29 +6230,28 @@ export class ReportController extends Controller { education = _education[0]; } } - else { - if ( - posMaster.current_holder != null && - posMaster.current_holder.profileEducations != null && - posMaster.current_holder.profileEducations.length > 0 - ) { - let _education: any = posMaster.current_holder.profileEducations.sort( - (a, b) => - (b.finishDate == null ? 0 : b.finishDate.getTime()) - - (a.finishDate == null ? 0 : a.finishDate.getTime()), - ); - if (_education.length > 0) { - education = _education[0]; - } - } + } + let salary: any = ""; + if ( + posMaster.next_holder != null && + posMaster.next_holder.profileSalary != null && + posMaster.next_holder.profileSalary.length > 0 + ) { + let _salary: any = posMaster.next_holder.profileSalary.sort( + (a, b) => + (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - + (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), + ); + if (_salary.length > 0) { + salary = _salary[0]; } - let salary: any = ""; + } else { if ( - posMaster.next_holder != null && - posMaster.next_holder.profileSalary != null && - posMaster.next_holder.profileSalary.length > 0 + posMaster.current_holder != null && + posMaster.current_holder.profileSalary != null && + posMaster.current_holder.profileSalary.length > 0 ) { - let _salary: any = posMaster.next_holder.profileSalary.sort( + let _salary: any = posMaster.current_holder.profileSalary.sort( (a, b) => (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), @@ -4693,159 +6260,221 @@ export class ReportController extends Controller { salary = _salary[0]; } } - else { - if ( - posMaster.current_holder != null && - posMaster.current_holder.profileSalary != null && - posMaster.current_holder.profileSalary.length > 0 - ) { - let _salary: any = posMaster.current_holder.profileSalary.sort( - (a, b) => - (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - - (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), - ); - if (_salary.length > 0) { - salary = _salary[0]; - } - } - } + } - let node = { - posMasterOrder: posMaster.posMasterOrder, // - isSit: posMaster.isSit, // - orgTreeName: orgChild2.orgChild2Name, - orgTreeShortName: orgChild2.orgChild2ShortName, - posMasterNo: posMaster.posMasterNo, - positionName: - posMaster.next_holder == null - ? positionName.join(" หรือ ") - : posMaster.isSit == false - ? posMaster.positions.find((x: any) => x.positionIsSelected == true) - ?.positionName - : posMaster.next_holder.position, - posType: - posMaster.next_holder == null - ? posType.join(" หรือ ") - : posMaster.isSit == false - ? posMaster.positions.find((x: any) => x.positionIsSelected == true) - ?.posType?.posTypeName - : posMaster.next_holder.posType == null - ? "-" - : posMaster.next_holder.posType.posTypeName, - posLevel: - posMaster.next_holder == null - ? posLevel.join(" หรือ ") - : posMaster.isSit == false - ? posMaster.positions.find((x: any) => x.positionIsSelected == true) - ?.posLevel?.posLevelName - : posMaster.next_holder.posLevel == null - ? "-" - : posMaster.next_holder.posLevel.posLevelName, - posExecutive: - posMaster.next_holder == null - ? posExecutive.join(" หรือ ") - : posMaster.isSit == false - ? posMaster.positions.find((x: any) => x.positionIsSelected == true) - ?.posExecutive?.posExecutiveName - : positionMasterProfileOld == null - ? "-" - : positionMasterProfileOld.positions.find( - (x: any) => x.positionIsSelected == true, - )?.posExecutive?.posExecutiveName, - - profileOrgName: - posMaster.next_holder == null - ? orgChild2.orgChild2Name - : positionMasterProfileOld == null - ? "-" - : positionMasterProfileOld.orgChild4 != null - ? positionMasterProfileOld.orgChild4.orgChild4Name - : positionMasterProfileOld.orgChild3 != null - ? positionMasterProfileOld.orgChild3.orgChild3Name - : positionMasterProfileOld.orgChild2 != null - ? positionMasterProfileOld.orgChild2.orgChild2Name - : positionMasterProfileOld.orgChild1 != null - ? positionMasterProfileOld.orgChild1.orgChild1Name - : positionMasterProfileOld.orgRoot != null - ? positionMasterProfileOld.orgRoot.orgRootName - : "-", - profileOrgShortName: - posMaster.next_holder == null - ? orgChild2.orgChild2ShortName - : positionMasterProfileOld == null - ? "-" - : positionMasterProfileOld.orgChild4 != null - ? positionMasterProfileOld.orgChild4.orgChild4ShortName - : positionMasterProfileOld.orgChild3 != null - ? positionMasterProfileOld.orgChild3.orgChild3ShortName - : positionMasterProfileOld.orgChild2 != null - ? positionMasterProfileOld.orgChild2.orgChild2ShortName - : positionMasterProfileOld.orgChild1 != null - ? positionMasterProfileOld.orgChild1.orgChild1ShortName - : positionMasterProfileOld.orgRoot != null - ? positionMasterProfileOld.orgRoot.orgRootShortName - : "-", - profileFullname: - posMaster.next_holder == null - ? posMaster.current_holder == null - ? "- ว่าง -" - : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}` - : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, - profilePosMasterNo: - posMaster.next_holder == null - ? positionMasterOld == null - ? "-" - : positionMasterOld.posMasterNo - : positionMasterProfileOld == null - ? "-" - : positionMasterProfileOld.posMasterNo, - profilePositionName: - posMaster.next_holder == null - ? positionMasterOld == null - ? positionName.join(" หรือ ") - : profilePositionName.join(" หรือ ") + let node = { + posMasterOrder: posMaster.posMasterOrder, // + isSit: posMaster.isSit, // + orgTreeName: orgChild4.orgChild4Name, + orgTreeShortName: orgChild4.orgChild4ShortName, + posMasterNo: posMaster.posMasterNo, + positionName: + posMaster.next_holder == null + ? positionName.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.positionName : posMaster.next_holder.position, - profilePosType: - posMaster.next_holder == null - ? positionMasterOld == null - ? posType.join(" หรือ ") - : profilePosType.join(" หรือ ") + posType: + posMaster.next_holder == null + ? posType.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.posType?.posTypeName : posMaster.next_holder.posType == null ? "-" : posMaster.next_holder.posType.posTypeName, - profilePosLevel: - posMaster.next_holder == null - ? positionMasterOld == null - ? posLevel.join(" หรือ ") - : profilePosLevel.join(" หรือ ") + posLevel: + posMaster.next_holder == null + ? posLevel.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.posLevel?.posLevelName : posMaster.next_holder.posLevel == null ? "-" : posMaster.next_holder.posLevel.posLevelName, - profilePosExecutive: - posMaster.next_holder == null - ? positionMasterOld == null - ? posExecutive.join(" หรือ ") - : profilePosExecutive.join(" หรือ ") + posExecutive: + posMaster.next_holder == null + ? posExecutive.join(" หรือ ") + : posMaster.isSit == false + ? posMaster.positions.find((x: any) => x.positionIsSelected == true) + ?.posExecutive?.posExecutiveName : positionMasterProfileOld == null ? "-" : positionMasterProfileOld.positions.find( (x: any) => x.positionIsSelected == true, )?.posExecutive?.posExecutiveName, - education: education == "" ? "" : education.degree, - salary: salary == "" ? "" : salary.amount, - reason: posMaster.reason, + + profileOrgName: + posMaster.next_holder == null + ? orgChild4.orgChild4Name + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.orgChild4 != null + ? positionMasterProfileOld.orgChild4.orgChild4Name + : positionMasterProfileOld.orgChild3 != null + ? positionMasterProfileOld.orgChild3.orgChild3Name + : positionMasterProfileOld.orgChild2 != null + ? positionMasterProfileOld.orgChild2.orgChild2Name + : positionMasterProfileOld.orgChild1 != null + ? positionMasterProfileOld.orgChild1.orgChild1Name + : positionMasterProfileOld.orgRoot != null + ? positionMasterProfileOld.orgRoot.orgRootName + : "-", + profileOrgShortName: + posMaster.next_holder == null + ? orgChild4.orgChild4ShortName + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.orgChild4 != null + ? positionMasterProfileOld.orgChild4.orgChild4ShortName + : positionMasterProfileOld.orgChild3 != null + ? positionMasterProfileOld.orgChild3.orgChild3ShortName + : positionMasterProfileOld.orgChild2 != null + ? positionMasterProfileOld.orgChild2.orgChild2ShortName + : positionMasterProfileOld.orgChild1 != null + ? positionMasterProfileOld.orgChild1.orgChild1ShortName + : positionMasterProfileOld.orgRoot != null + ? positionMasterProfileOld.orgRoot.orgRootShortName + : "-", + profileFullname: + posMaster.next_holder == null + ? posMaster.current_holder == null + ? "- ว่าง -" + : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}` + : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, + profilePosMasterNo: + posMaster.next_holder == null + ? positionMasterOld == null + ? "-" + : positionMasterOld.posMasterNo + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.posMasterNo, + profilePositionName: + posMaster.next_holder == null + ? positionMasterOld == null + ? positionName.join(" หรือ ") + : profilePositionName.join(" หรือ ") + : posMaster.next_holder.position, + profilePosType: + posMaster.next_holder == null + ? positionMasterOld == null + ? posType.join(" หรือ ") + : profilePosType.join(" หรือ ") + : posMaster.next_holder.posType == null + ? "-" + : posMaster.next_holder.posType.posTypeName, + profilePosLevel: + posMaster.next_holder == null + ? positionMasterOld == null + ? posLevel.join(" หรือ ") + : profilePosLevel.join(" หรือ ") + : posMaster.next_holder.posLevel == null + ? "-" + : posMaster.next_holder.posLevel.posLevelName, + profilePosExecutive: + posMaster.next_holder == null + ? positionMasterOld == null + ? posExecutive.join(" หรือ ") + : profilePosExecutive.join(" หรือ ") + : positionMasterProfileOld == null + ? "-" + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posExecutive?.posExecutiveName, + education: education == "" ? "" : education.degree, + salary: salary == "" ? "" : salary.amount, + reason: posMaster.reason, + }; + if (_node == null) { + const head = { + posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), + profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), + posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), + positionName: "", + posType: "", + posLevel: "", + profilePosMasterNo: Extension.ToThaiNumber(node.profileOrgShortName.toString()), + profilePosExecutive: "", + profilePositionName: "", + profilePosType: "", + profilePosLevel: "", + education: "", + salary: "", + reason: "", }; - if (_node == null) { + data.push(head); + const _head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + profileFullname: + node.profileFullname == null + ? "" + : Extension.ToThaiNumber(node.profileFullname.toString()), + posExecutive: + node.posExecutive == null + ? "" + : Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: + node.positionName == null + ? "" + : Extension.ToThaiNumber(node.positionName.toString()), + posType: + node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), + posLevel: + node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), + profilePosMasterNo: Extension.ToThaiNumber(node.profilePosMasterNo.toString()), + profilePosExecutive: + node.profilePosExecutive == null + ? "" + : Extension.ToThaiNumber(node.profilePosExecutive.toString()), + profilePositionName: + node.profilePositionName == null + ? "" + : Extension.ToThaiNumber(node.profilePositionName.toString()), + profilePosType: + node.profilePosType == null + ? "" + : Extension.ToThaiNumber(node.profilePosType.toString()), + profilePosLevel: + node.profilePosLevel == null + ? "" + : Extension.ToThaiNumber(node.profilePosLevel.toString()), + education: + node.education == null + ? "" + : Extension.ToThaiNumber(node.education.toString()), + salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", + reason: + node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(_head); + } else { + if ( + node.orgTreeShortName != _node.orgTreeShortName || + node.orgTreeName != _node.orgTreeName || + node.profileOrgShortName != _node.profileOrgShortName || + node.profileOrgName != _node.profileOrgName + ) { const head = { - posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), - profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), - posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), + posMasterNo: + node.orgTreeShortName == _node.orgTreeShortName + ? "" + : node.orgTreeShortName, + profileFullname: + node.profileOrgName == _node.profileOrgName ? "" : node.profileOrgName, + posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, positionName: "", posType: "", posLevel: "", - profilePosMasterNo: Extension.ToThaiNumber( - node.profileOrgShortName.toString(), - ), + profilePosMasterNo: + node.profileOrgShortName == _node.profileOrgShortName + ? "" + : node.profileOrgShortName, profilePosExecutive: "", profilePositionName: "", profilePosType: "", @@ -4855,1025 +6484,60 @@ export class ReportController extends Controller { reason: "", }; data.push(head); - const _head = { - no: Extension.ToThaiNumber(no.toString()), - posMasterNo: - node.posMasterNo == null - ? "" - : Extension.ToThaiNumber(node.posMasterNo.toString()), - profileFullname: - node.profileFullname == null - ? "" - : Extension.ToThaiNumber(node.profileFullname.toString()), - posExecutive: - node.posExecutive == null - ? "" - : Extension.ToThaiNumber(node.posExecutive.toString()), - positionName: - node.positionName == null - ? "" - : Extension.ToThaiNumber(node.positionName.toString()), - posType: - node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), - posLevel: - node.posLevel == null - ? "" - : Extension.ToThaiNumber(node.posLevel.toString()), - profilePosMasterNo: Extension.ToThaiNumber( - node.profilePosMasterNo.toString(), - ), - profilePosExecutive: - node.profilePosExecutive == null - ? "" - : Extension.ToThaiNumber(node.profilePosExecutive.toString()), - profilePositionName: - node.profilePositionName == null - ? "" - : Extension.ToThaiNumber(node.profilePositionName.toString()), - profilePosType: - node.profilePosType == null - ? "" - : Extension.ToThaiNumber(node.profilePosType.toString()), - profilePosLevel: - node.profilePosLevel == null - ? "" - : Extension.ToThaiNumber(node.profilePosLevel.toString()), - education: - node.education == null - ? "" - : Extension.ToThaiNumber(node.education.toString()), - salary: node.salary - ? Extension.ToThaiNumber(node.salary.toLocaleString()) - : "", - reason: - node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), - }; - data.push(_head); - } else { - if ( - node.orgTreeShortName != _node.orgTreeShortName || - node.orgTreeName != _node.orgTreeName || - node.profileOrgShortName != _node.profileOrgShortName || - node.profileOrgName != _node.profileOrgName - ) { - const head = { - posMasterNo: - node.orgTreeShortName == _node.orgTreeShortName - ? "" - : node.orgTreeShortName, - profileFullname: - node.profileOrgName == _node.profileOrgName ? "" : node.profileOrgName, - posExecutive: node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, - positionName: "", - posType: "", - posLevel: "", - profilePosMasterNo: - node.profileOrgShortName == _node.profileOrgShortName - ? "" - : node.profileOrgShortName, - profilePosExecutive: "", - profilePositionName: "", - profilePosType: "", - profilePosLevel: "", - education: "", - salary: "", - reason: "", - }; - data.push(head); - _node == null; - } - const head = { - no: Extension.ToThaiNumber(no.toString()), - posMasterNo: - node.posMasterNo == null - ? "" - : Extension.ToThaiNumber(node.posMasterNo.toString()), - profileFullname: - node.profileFullname == null - ? "" - : Extension.ToThaiNumber(node.profileFullname.toString()), - posExecutive: - node.posExecutive == null - ? "" - : Extension.ToThaiNumber(node.posExecutive.toString()), - positionName: - node.positionName == null - ? "" - : Extension.ToThaiNumber(node.positionName.toString()), - posType: - node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), - posLevel: - node.posLevel == null - ? "" - : Extension.ToThaiNumber(node.posLevel.toString()), - profilePosMasterNo: Extension.ToThaiNumber( - node.profilePosMasterNo.toString(), - ), - profilePosExecutive: - node.profilePosExecutive == null - ? "" - : Extension.ToThaiNumber(node.profilePosExecutive.toString()), - profilePositionName: - node.profilePositionName == null - ? "" - : Extension.ToThaiNumber(node.profilePositionName.toString()), - profilePosType: - node.profilePosType == null - ? "" - : Extension.ToThaiNumber(node.profilePosType.toString()), - profilePosLevel: - node.profilePosLevel == null - ? "" - : Extension.ToThaiNumber(node.profilePosLevel.toString()), - education: - node.education == null - ? "" - : Extension.ToThaiNumber(node.education.toString()), - salary: node.salary - ? Extension.ToThaiNumber(node.salary.toLocaleString()) - : "", - reason: - node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), - }; - data.push(head); + _node == null; } - no += 1; - _node = node; + const head = { + no: Extension.ToThaiNumber(no.toString()), + posMasterNo: + node.posMasterNo == null + ? "" + : Extension.ToThaiNumber(node.posMasterNo.toString()), + profileFullname: + node.profileFullname == null + ? "" + : Extension.ToThaiNumber(node.profileFullname.toString()), + posExecutive: + node.posExecutive == null + ? "" + : Extension.ToThaiNumber(node.posExecutive.toString()), + positionName: + node.positionName == null + ? "" + : Extension.ToThaiNumber(node.positionName.toString()), + posType: + node.posType == null ? "" : Extension.ToThaiNumber(node.posType.toString()), + posLevel: + node.posLevel == null ? "" : Extension.ToThaiNumber(node.posLevel.toString()), + profilePosMasterNo: Extension.ToThaiNumber(node.profilePosMasterNo.toString()), + profilePosExecutive: + node.profilePosExecutive == null + ? "" + : Extension.ToThaiNumber(node.profilePosExecutive.toString()), + profilePositionName: + node.profilePositionName == null + ? "" + : Extension.ToThaiNumber(node.profilePositionName.toString()), + profilePosType: + node.profilePosType == null + ? "" + : Extension.ToThaiNumber(node.profilePosType.toString()), + profilePosLevel: + node.profilePosLevel == null + ? "" + : Extension.ToThaiNumber(node.profilePosLevel.toString()), + education: + node.education == null + ? "" + : Extension.ToThaiNumber(node.education.toString()), + salary: node.salary ? Extension.ToThaiNumber(node.salary.toLocaleString()) : "", + reason: + node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), + }; + data.push(head); } - }), - ); - _node = null; - - for (let orgChild3 of orgChild3Data.filter( - (orgChild3) => orgChild3.orgChild2Id === orgChild2.id, - )) { - await Promise.all( - orgChild3.posMasters - .sort((a, b) => a.posMasterOrder - b.posMasterOrder) - .map(async (posMaster) => { - if (posMaster.orgChild4Id == null) { - const positionName = [ - ...new Set(posMaster.positions.map((x) => x.positionName)), - ]; - const posType = [ - ...new Set( - posMaster.positions - .filter((x: any) => x.posType != null) - .map((x) => x.posType.posTypeName), - ), - ]; - const posLevel = [ - ...new Set( - posMaster.positions - .filter((x: any) => x.posLevel != null) - .map((x) => x.posLevel.posLevelName), - ), - ]; - const posExecutive = [ - ...new Set( - posMaster.positions - .filter((x: any) => x.posExecutive != null) - .map((x) => x.posExecutive.posExecutiveName), - ), - ]; - let positionMasterProfileOld: any = null; - if (posMaster.next_holder != null) { - positionMasterProfileOld = posMaster.next_holder.current_holders.find( - (x) => x.orgRevisionId == orgRevisionActive.id, - ); - } - - let positionMasterOld: any = null; - let profilePositionName: any = []; - let profilePosType: any = []; - let profilePosLevel: any = []; - let profilePosExecutive: any = []; - if (posMaster.ancestorDNA != null && posMaster.ancestorDNA != "") { - positionMasterOld = orgRevisionActive.posMasters.find( - (x: any) => - x.orgRevisionId == orgRevisionActive.id && - x.ancestorDNA == posMaster.ancestorDNA, - ); - profilePositionName = [ - ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), - ]; - profilePosType = [ - ...new Set( - positionMasterOld.positions - .filter((x: any) => x.posType != null) - .map((x: any) => x.posType.posTypeName), - ), - ]; - profilePosLevel = [ - ...new Set( - positionMasterOld.positions - .filter((x: any) => x.posLevel != null) - .map((x: any) => x.posLevel.posLevelName), - ), - ]; - profilePosExecutive = [ - ...new Set( - positionMasterOld.positions - .filter((x: any) => x.posExecutive != null) - .map((x: any) => x.posExecutive.posExecutiveName), - ), - ]; - } - let education: any = ""; - if ( - posMaster.next_holder != null && - posMaster.next_holder.profileEducations != null && - posMaster.next_holder.profileEducations.length > 0 - ) { - let _education: any = posMaster.next_holder.profileEducations.sort( - (a, b) => - (b.finishDate == null ? 0 : b.finishDate.getTime()) - - (a.finishDate == null ? 0 : a.finishDate.getTime()), - ); - if (_education.length > 0) { - education = _education[0]; - } - } - else { - if ( - posMaster.current_holder != null && - posMaster.current_holder.profileEducations != null && - posMaster.current_holder.profileEducations.length > 0 - ) { - let _education: any = posMaster.current_holder.profileEducations.sort( - (a, b) => - (b.finishDate == null ? 0 : b.finishDate.getTime()) - - (a.finishDate == null ? 0 : a.finishDate.getTime()), - ); - if (_education.length > 0) { - education = _education[0]; - } - } - } - let salary: any = ""; - if ( - posMaster.next_holder != null && - posMaster.next_holder.profileSalary != null && - posMaster.next_holder.profileSalary.length > 0 - ) { - let _salary: any = posMaster.next_holder.profileSalary.sort( - (a, b) => - (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - - (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), - ); - if (_salary.length > 0) { - salary = _salary[0]; - } - } - else { - if ( - posMaster.current_holder != null && - posMaster.current_holder.profileSalary != null && - posMaster.current_holder.profileSalary.length > 0 - ) { - let _salary: any = posMaster.current_holder.profileSalary.sort( - (a, b) => - (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - - (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), - ); - if (_salary.length > 0) { - salary = _salary[0]; - } - } - } - - let node = { - posMasterOrder: posMaster.posMasterOrder, // - isSit: posMaster.isSit, // - orgTreeName: orgChild3.orgChild3Name, - orgTreeShortName: orgChild3.orgChild3ShortName, - posMasterNo: posMaster.posMasterNo, - positionName: - posMaster.next_holder == null - ? positionName.join(" หรือ ") - : posMaster.isSit == false - ? posMaster.positions.find((x: any) => x.positionIsSelected == true) - ?.positionName - : posMaster.next_holder.position, - posType: - posMaster.next_holder == null - ? posType.join(" หรือ ") - : posMaster.isSit == false - ? posMaster.positions.find((x: any) => x.positionIsSelected == true) - ?.posType?.posTypeName - : posMaster.next_holder.posType == null - ? "-" - : posMaster.next_holder.posType.posTypeName, - posLevel: - posMaster.next_holder == null - ? posLevel.join(" หรือ ") - : posMaster.isSit == false - ? posMaster.positions.find((x: any) => x.positionIsSelected == true) - ?.posLevel?.posLevelName - : posMaster.next_holder.posLevel == null - ? "-" - : posMaster.next_holder.posLevel.posLevelName, - posExecutive: - posMaster.next_holder == null - ? posExecutive.join(" หรือ ") - : posMaster.isSit == false - ? posMaster.positions.find((x: any) => x.positionIsSelected == true) - ?.posExecutive?.posExecutiveName - : positionMasterProfileOld == null - ? "-" - : positionMasterProfileOld.positions.find( - (x: any) => x.positionIsSelected == true, - )?.posExecutive?.posExecutiveName, - - profileOrgName: - posMaster.next_holder == null - ? orgChild3.orgChild3Name - : positionMasterProfileOld == null - ? "-" - : positionMasterProfileOld.orgChild4 != null - ? positionMasterProfileOld.orgChild4.orgChild4Name - : positionMasterProfileOld.orgChild3 != null - ? positionMasterProfileOld.orgChild3.orgChild3Name - : positionMasterProfileOld.orgChild2 != null - ? positionMasterProfileOld.orgChild2.orgChild2Name - : positionMasterProfileOld.orgChild1 != null - ? positionMasterProfileOld.orgChild1.orgChild1Name - : positionMasterProfileOld.orgRoot != null - ? positionMasterProfileOld.orgRoot.orgRootName - : "-", - profileOrgShortName: - posMaster.next_holder == null - ? orgChild3.orgChild3ShortName - : positionMasterProfileOld == null - ? "-" - : positionMasterProfileOld.orgChild4 != null - ? positionMasterProfileOld.orgChild4.orgChild4ShortName - : positionMasterProfileOld.orgChild3 != null - ? positionMasterProfileOld.orgChild3.orgChild3ShortName - : positionMasterProfileOld.orgChild2 != null - ? positionMasterProfileOld.orgChild2.orgChild2ShortName - : positionMasterProfileOld.orgChild1 != null - ? positionMasterProfileOld.orgChild1.orgChild1ShortName - : positionMasterProfileOld.orgRoot != null - ? positionMasterProfileOld.orgRoot.orgRootShortName - : "-", - profileFullname: - posMaster.next_holder == null - ? posMaster.current_holder == null - ? "- ว่าง -" - : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}` - : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, - profilePosMasterNo: - posMaster.next_holder == null - ? positionMasterOld == null - ? "-" - : positionMasterOld.posMasterNo - : positionMasterProfileOld == null - ? "-" - : positionMasterProfileOld.posMasterNo, - profilePositionName: - posMaster.next_holder == null - ? positionMasterOld == null - ? positionName.join(" หรือ ") - : profilePositionName.join(" หรือ ") - : posMaster.next_holder.position, - profilePosType: - posMaster.next_holder == null - ? positionMasterOld == null - ? posType.join(" หรือ ") - : profilePosType.join(" หรือ ") - : posMaster.next_holder.posType == null - ? "-" - : posMaster.next_holder.posType.posTypeName, - profilePosLevel: - posMaster.next_holder == null - ? positionMasterOld == null - ? posLevel.join(" หรือ ") - : profilePosLevel.join(" หรือ ") - : posMaster.next_holder.posLevel == null - ? "-" - : posMaster.next_holder.posLevel.posLevelName, - profilePosExecutive: - posMaster.next_holder == null - ? positionMasterOld == null - ? posExecutive.join(" หรือ ") - : profilePosExecutive.join(" หรือ ") - : positionMasterProfileOld == null - ? "-" - : positionMasterProfileOld.positions.find( - (x: any) => x.positionIsSelected == true, - )?.posExecutive?.posExecutiveName, - education: education == "" ? "" : education.degree, - salary: salary == "" ? "" : salary.amount, - reason: posMaster.reason, - }; - if (_node == null) { - const head = { - posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), - profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), - posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), - positionName: "", - posType: "", - posLevel: "", - profilePosMasterNo: Extension.ToThaiNumber( - node.profileOrgShortName.toString(), - ), - profilePosExecutive: "", - profilePositionName: "", - profilePosType: "", - profilePosLevel: "", - education: "", - salary: "", - reason: "", - }; - data.push(head); - const _head = { - no: Extension.ToThaiNumber(no.toString()), - posMasterNo: - node.posMasterNo == null - ? "" - : Extension.ToThaiNumber(node.posMasterNo.toString()), - profileFullname: - node.profileFullname == null - ? "" - : Extension.ToThaiNumber(node.profileFullname.toString()), - posExecutive: - node.posExecutive == null - ? "" - : Extension.ToThaiNumber(node.posExecutive.toString()), - positionName: - node.positionName == null - ? "" - : Extension.ToThaiNumber(node.positionName.toString()), - posType: - node.posType == null - ? "" - : Extension.ToThaiNumber(node.posType.toString()), - posLevel: - node.posLevel == null - ? "" - : Extension.ToThaiNumber(node.posLevel.toString()), - profilePosMasterNo: Extension.ToThaiNumber( - node.profilePosMasterNo.toString(), - ), - profilePosExecutive: - node.profilePosExecutive == null - ? "" - : Extension.ToThaiNumber(node.profilePosExecutive.toString()), - profilePositionName: - node.profilePositionName == null - ? "" - : Extension.ToThaiNumber(node.profilePositionName.toString()), - profilePosType: - node.profilePosType == null - ? "" - : Extension.ToThaiNumber(node.profilePosType.toString()), - profilePosLevel: - node.profilePosLevel == null - ? "" - : Extension.ToThaiNumber(node.profilePosLevel.toString()), - education: - node.education == null - ? "" - : Extension.ToThaiNumber(node.education.toString()), - salary: node.salary - ? Extension.ToThaiNumber(node.salary.toLocaleString()) - : "", - reason: - node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), - }; - data.push(_head); - } else { - if ( - node.orgTreeShortName != _node.orgTreeShortName || - node.orgTreeName != _node.orgTreeName || - node.profileOrgShortName != _node.profileOrgShortName || - node.profileOrgName != _node.profileOrgName - ) { - const head = { - posMasterNo: - node.orgTreeShortName == _node.orgTreeShortName - ? "" - : node.orgTreeShortName, - profileFullname: - node.profileOrgName == _node.profileOrgName ? "" : node.profileOrgName, - posExecutive: - node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, - positionName: "", - posType: "", - posLevel: "", - profilePosMasterNo: - node.profileOrgShortName == _node.profileOrgShortName - ? "" - : node.profileOrgShortName, - profilePosExecutive: "", - profilePositionName: "", - profilePosType: "", - profilePosLevel: "", - education: "", - salary: "", - reason: "", - }; - data.push(head); - _node == null; - } - const head = { - no: Extension.ToThaiNumber(no.toString()), - posMasterNo: - node.posMasterNo == null - ? "" - : Extension.ToThaiNumber(node.posMasterNo.toString()), - profileFullname: - node.profileFullname == null - ? "" - : Extension.ToThaiNumber(node.profileFullname.toString()), - posExecutive: - node.posExecutive == null - ? "" - : Extension.ToThaiNumber(node.posExecutive.toString()), - positionName: - node.positionName == null - ? "" - : Extension.ToThaiNumber(node.positionName.toString()), - posType: - node.posType == null - ? "" - : Extension.ToThaiNumber(node.posType.toString()), - posLevel: - node.posLevel == null - ? "" - : Extension.ToThaiNumber(node.posLevel.toString()), - profilePosMasterNo: Extension.ToThaiNumber( - node.profilePosMasterNo.toString(), - ), - profilePosExecutive: - node.profilePosExecutive == null - ? "" - : Extension.ToThaiNumber(node.profilePosExecutive.toString()), - profilePositionName: - node.profilePositionName == null - ? "" - : Extension.ToThaiNumber(node.profilePositionName.toString()), - profilePosType: - node.profilePosType == null - ? "" - : Extension.ToThaiNumber(node.profilePosType.toString()), - profilePosLevel: - node.profilePosLevel == null - ? "" - : Extension.ToThaiNumber(node.profilePosLevel.toString()), - education: - node.education == null - ? "" - : Extension.ToThaiNumber(node.education.toString()), - salary: node.salary - ? Extension.ToThaiNumber(node.salary.toLocaleString()) - : "", - reason: - node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), - }; - data.push(head); - } - no += 1; - _node = node; - } - }), - ); - _node = null; - - for (let orgChild4 of orgChild4Data.filter( - (orgChild4) => orgChild4.orgChild3Id === orgChild3.id, - )) { - await Promise.all( - orgChild4.posMasters - .sort((a, b) => a.posMasterOrder - b.posMasterOrder) - .map(async (posMaster) => { - const positionName = [ - ...new Set(posMaster.positions.map((x) => x.positionName)), - ]; - const posType = [ - ...new Set( - posMaster.positions - .filter((x: any) => x.posType != null) - .map((x) => x.posType.posTypeName), - ), - ]; - const posLevel = [ - ...new Set( - posMaster.positions - .filter((x: any) => x.posLevel != null) - .map((x) => x.posLevel.posLevelName), - ), - ]; - const posExecutive = [ - ...new Set( - posMaster.positions - .filter((x: any) => x.posExecutive != null) - .map((x) => x.posExecutive.posExecutiveName), - ), - ]; - let positionMasterProfileOld: any = null; - if (posMaster.next_holder != null) { - positionMasterProfileOld = posMaster.next_holder.current_holders.find( - (x) => x.orgRevisionId == orgRevisionActive.id, - ); - } - - let positionMasterOld: any = null; - let profilePositionName: any = []; - let profilePosType: any = []; - let profilePosLevel: any = []; - let profilePosExecutive: any = []; - if (posMaster.ancestorDNA != null && posMaster.ancestorDNA != "") { - positionMasterOld = orgRevisionActive.posMasters.find( - (x: any) => - x.orgRevisionId == orgRevisionActive.id && - x.ancestorDNA == posMaster.ancestorDNA, - ); - profilePositionName = [ - ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), - ]; - profilePosType = [ - ...new Set( - positionMasterOld.positions - .filter((x: any) => x.posType != null) - .map((x: any) => x.posType.posTypeName), - ), - ]; - profilePosLevel = [ - ...new Set( - positionMasterOld.positions - .filter((x: any) => x.posLevel != null) - .map((x: any) => x.posLevel.posLevelName), - ), - ]; - profilePosExecutive = [ - ...new Set( - positionMasterOld.positions - .filter((x: any) => x.posExecutive != null) - .map((x: any) => x.posExecutive.posExecutiveName), - ), - ]; - } - let education: any = ""; - if ( - posMaster.next_holder != null && - posMaster.next_holder.profileEducations != null && - posMaster.next_holder.profileEducations.length > 0 - ) { - let _education: any = posMaster.next_holder.profileEducations.sort( - (a, b) => - (b.finishDate == null ? 0 : b.finishDate.getTime()) - - (a.finishDate == null ? 0 : a.finishDate.getTime()), - ); - if (_education.length > 0) { - education = _education[0]; - } - } - else { - if ( - posMaster.current_holder != null && - posMaster.current_holder.profileEducations != null && - posMaster.current_holder.profileEducations.length > 0 - ) { - let _education: any = posMaster.current_holder.profileEducations.sort( - (a, b) => - (b.finishDate == null ? 0 : b.finishDate.getTime()) - - (a.finishDate == null ? 0 : a.finishDate.getTime()), - ); - if (_education.length > 0) { - education = _education[0]; - } - } - } - let salary: any = ""; - if ( - posMaster.next_holder != null && - posMaster.next_holder.profileSalary != null && - posMaster.next_holder.profileSalary.length > 0 - ) { - let _salary: any = posMaster.next_holder.profileSalary.sort( - (a, b) => - (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - - (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), - ); - if (_salary.length > 0) { - salary = _salary[0]; - } - } - else { - if ( - posMaster.current_holder != null && - posMaster.current_holder.profileSalary != null && - posMaster.current_holder.profileSalary.length > 0 - ) { - let _salary: any = posMaster.current_holder.profileSalary.sort( - (a, b) => - (b.commandDateAffect == null ? 0 : b.commandDateAffect.getTime()) - - (a.commandDateAffect == null ? 0 : a.commandDateAffect.getTime()), - ); - if (_salary.length > 0) { - salary = _salary[0]; - } - } - } - - let node = { - posMasterOrder: posMaster.posMasterOrder, // - isSit: posMaster.isSit, // - orgTreeName: orgChild4.orgChild4Name, - orgTreeShortName: orgChild4.orgChild4ShortName, - posMasterNo: posMaster.posMasterNo, - positionName: - posMaster.next_holder == null - ? positionName.join(" หรือ ") - : posMaster.isSit == false - ? posMaster.positions.find((x: any) => x.positionIsSelected == true) - ?.positionName - : posMaster.next_holder.position, - posType: - posMaster.next_holder == null - ? posType.join(" หรือ ") - : posMaster.isSit == false - ? posMaster.positions.find((x: any) => x.positionIsSelected == true) - ?.posType?.posTypeName - : posMaster.next_holder.posType == null - ? "-" - : posMaster.next_holder.posType.posTypeName, - posLevel: - posMaster.next_holder == null - ? posLevel.join(" หรือ ") - : posMaster.isSit == false - ? posMaster.positions.find((x: any) => x.positionIsSelected == true) - ?.posLevel?.posLevelName - : posMaster.next_holder.posLevel == null - ? "-" - : posMaster.next_holder.posLevel.posLevelName, - posExecutive: - posMaster.next_holder == null - ? posExecutive.join(" หรือ ") - : posMaster.isSit == false - ? posMaster.positions.find((x: any) => x.positionIsSelected == true) - ?.posExecutive?.posExecutiveName - : positionMasterProfileOld == null - ? "-" - : positionMasterProfileOld.positions.find( - (x: any) => x.positionIsSelected == true, - )?.posExecutive?.posExecutiveName, - - profileOrgName: - posMaster.next_holder == null - ? orgChild4.orgChild4Name - : positionMasterProfileOld == null - ? "-" - : positionMasterProfileOld.orgChild4 != null - ? positionMasterProfileOld.orgChild4.orgChild4Name - : positionMasterProfileOld.orgChild3 != null - ? positionMasterProfileOld.orgChild3.orgChild3Name - : positionMasterProfileOld.orgChild2 != null - ? positionMasterProfileOld.orgChild2.orgChild2Name - : positionMasterProfileOld.orgChild1 != null - ? positionMasterProfileOld.orgChild1.orgChild1Name - : positionMasterProfileOld.orgRoot != null - ? positionMasterProfileOld.orgRoot.orgRootName - : "-", - profileOrgShortName: - posMaster.next_holder == null - ? orgChild4.orgChild4ShortName - : positionMasterProfileOld == null - ? "-" - : positionMasterProfileOld.orgChild4 != null - ? positionMasterProfileOld.orgChild4.orgChild4ShortName - : positionMasterProfileOld.orgChild3 != null - ? positionMasterProfileOld.orgChild3.orgChild3ShortName - : positionMasterProfileOld.orgChild2 != null - ? positionMasterProfileOld.orgChild2.orgChild2ShortName - : positionMasterProfileOld.orgChild1 != null - ? positionMasterProfileOld.orgChild1.orgChild1ShortName - : positionMasterProfileOld.orgRoot != null - ? positionMasterProfileOld.orgRoot.orgRootShortName - : "-", - profileFullname: - posMaster.next_holder == null - ? posMaster.current_holder == null - ? "- ว่าง -" - : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}` - : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, - profilePosMasterNo: - posMaster.next_holder == null - ? positionMasterOld == null - ? "-" - : positionMasterOld.posMasterNo - : positionMasterProfileOld == null - ? "-" - : positionMasterProfileOld.posMasterNo, - profilePositionName: - posMaster.next_holder == null - ? positionMasterOld == null - ? positionName.join(" หรือ ") - : profilePositionName.join(" หรือ ") - : posMaster.next_holder.position, - profilePosType: - posMaster.next_holder == null - ? positionMasterOld == null - ? posType.join(" หรือ ") - : profilePosType.join(" หรือ ") - : posMaster.next_holder.posType == null - ? "-" - : posMaster.next_holder.posType.posTypeName, - profilePosLevel: - posMaster.next_holder == null - ? positionMasterOld == null - ? posLevel.join(" หรือ ") - : profilePosLevel.join(" หรือ ") - : posMaster.next_holder.posLevel == null - ? "-" - : posMaster.next_holder.posLevel.posLevelName, - profilePosExecutive: - posMaster.next_holder == null - ? positionMasterOld == null - ? posExecutive.join(" หรือ ") - : profilePosExecutive.join(" หรือ ") - : positionMasterProfileOld == null - ? "-" - : positionMasterProfileOld.positions.find( - (x: any) => x.positionIsSelected == true, - )?.posExecutive?.posExecutiveName, - education: education == "" ? "" : education.degree, - salary: salary == "" ? "" : salary.amount, - reason: posMaster.reason, - }; - if (_node == null) { - const head = { - posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), - profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), - posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), - positionName: "", - posType: "", - posLevel: "", - profilePosMasterNo: Extension.ToThaiNumber( - node.profileOrgShortName.toString(), - ), - profilePosExecutive: "", - profilePositionName: "", - profilePosType: "", - profilePosLevel: "", - education: "", - salary: "", - reason: "", - }; - data.push(head); - const _head = { - no: Extension.ToThaiNumber(no.toString()), - posMasterNo: - node.posMasterNo == null - ? "" - : Extension.ToThaiNumber(node.posMasterNo.toString()), - profileFullname: - node.profileFullname == null - ? "" - : Extension.ToThaiNumber(node.profileFullname.toString()), - posExecutive: - node.posExecutive == null - ? "" - : Extension.ToThaiNumber(node.posExecutive.toString()), - positionName: - node.positionName == null - ? "" - : Extension.ToThaiNumber(node.positionName.toString()), - posType: - node.posType == null - ? "" - : Extension.ToThaiNumber(node.posType.toString()), - posLevel: - node.posLevel == null - ? "" - : Extension.ToThaiNumber(node.posLevel.toString()), - profilePosMasterNo: Extension.ToThaiNumber( - node.profilePosMasterNo.toString(), - ), - profilePosExecutive: - node.profilePosExecutive == null - ? "" - : Extension.ToThaiNumber(node.profilePosExecutive.toString()), - profilePositionName: - node.profilePositionName == null - ? "" - : Extension.ToThaiNumber(node.profilePositionName.toString()), - profilePosType: - node.profilePosType == null - ? "" - : Extension.ToThaiNumber(node.profilePosType.toString()), - profilePosLevel: - node.profilePosLevel == null - ? "" - : Extension.ToThaiNumber(node.profilePosLevel.toString()), - education: - node.education == null - ? "" - : Extension.ToThaiNumber(node.education.toString()), - salary: node.salary - ? Extension.ToThaiNumber(node.salary.toLocaleString()) - : "", - reason: - node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), - }; - data.push(_head); - } else { - if ( - node.orgTreeShortName != _node.orgTreeShortName || - node.orgTreeName != _node.orgTreeName || - node.profileOrgShortName != _node.profileOrgShortName || - node.profileOrgName != _node.profileOrgName - ) { - const head = { - posMasterNo: - node.orgTreeShortName == _node.orgTreeShortName - ? "" - : node.orgTreeShortName, - profileFullname: - node.profileOrgName == _node.profileOrgName ? "" : node.profileOrgName, - posExecutive: - node.orgTreeName == _node.orgTreeName ? "" : node.orgTreeName, - positionName: "", - posType: "", - posLevel: "", - profilePosMasterNo: - node.profileOrgShortName == _node.profileOrgShortName - ? "" - : node.profileOrgShortName, - profilePosExecutive: "", - profilePositionName: "", - profilePosType: "", - profilePosLevel: "", - education: "", - salary: "", - reason: "", - }; - data.push(head); - _node == null; - } - const head = { - no: Extension.ToThaiNumber(no.toString()), - posMasterNo: - node.posMasterNo == null - ? "" - : Extension.ToThaiNumber(node.posMasterNo.toString()), - profileFullname: - node.profileFullname == null - ? "" - : Extension.ToThaiNumber(node.profileFullname.toString()), - posExecutive: - node.posExecutive == null - ? "" - : Extension.ToThaiNumber(node.posExecutive.toString()), - positionName: - node.positionName == null - ? "" - : Extension.ToThaiNumber(node.positionName.toString()), - posType: - node.posType == null - ? "" - : Extension.ToThaiNumber(node.posType.toString()), - posLevel: - node.posLevel == null - ? "" - : Extension.ToThaiNumber(node.posLevel.toString()), - profilePosMasterNo: Extension.ToThaiNumber( - node.profilePosMasterNo.toString(), - ), - profilePosExecutive: - node.profilePosExecutive == null - ? "" - : Extension.ToThaiNumber(node.profilePosExecutive.toString()), - profilePositionName: - node.profilePositionName == null - ? "" - : Extension.ToThaiNumber(node.profilePositionName.toString()), - profilePosType: - node.profilePosType == null - ? "" - : Extension.ToThaiNumber(node.profilePosType.toString()), - profilePosLevel: - node.profilePosLevel == null - ? "" - : Extension.ToThaiNumber(node.profilePosLevel.toString()), - education: - node.education == null - ? "" - : Extension.ToThaiNumber(node.education.toString()), - salary: node.salary - ? Extension.ToThaiNumber(node.salary.toLocaleString()) - : "", - reason: - node.reason == null ? "" : Extension.ToThaiNumber(node.reason.toString()), - }; - data.push(head); - } - no += 1; - _node = node; - }), - ); + no += 1; + _node = node; + } _node = null; } // End for orgChild4 } // End for orgChild3 @@ -5940,27 +6604,10 @@ export class ReportController extends Controller { order: { orgChild1Order: "ASC" }, relations: [ "posMasters", - "posMasters.orgRoot", - "posMasters.orgChild1", - "posMasters.orgChild2", - "posMasters.orgChild3", - "posMasters.orgChild4", "posMasters.current_holder", - "posMasters.current_holder.posLevel", - "posMasters.current_holder.posType", "posMasters.current_holder.profileSalary", "posMasters.current_holder.profileEducations", - "posMasters.current_holder.current_holders", - "posMasters.current_holder.current_holders.positions", - "posMasters.current_holder.current_holders.orgRoot", - "posMasters.current_holder.current_holders.orgChild1", - "posMasters.current_holder.current_holders.orgChild2", - "posMasters.current_holder.current_holders.orgChild3", - "posMasters.current_holder.current_holders.orgChild4", "posMasters.positions", - "posMasters.positions.posLevel", - "posMasters.positions.posType", - "posMasters.positions.posExecutive", ], }); @@ -5972,27 +6619,10 @@ export class ReportController extends Controller { order: { orgChild2Order: "ASC" }, relations: [ "posMasters", - "posMasters.orgRoot", - "posMasters.orgChild1", - "posMasters.orgChild2", - "posMasters.orgChild3", - "posMasters.orgChild4", "posMasters.current_holder", - "posMasters.current_holder.posLevel", - "posMasters.current_holder.posType", "posMasters.current_holder.profileSalary", "posMasters.current_holder.profileEducations", - "posMasters.current_holder.current_holders", - "posMasters.current_holder.current_holders.positions", - "posMasters.current_holder.current_holders.orgRoot", - "posMasters.current_holder.current_holders.orgChild1", - "posMasters.current_holder.current_holders.orgChild2", - "posMasters.current_holder.current_holders.orgChild3", - "posMasters.current_holder.current_holders.orgChild4", "posMasters.positions", - "posMasters.positions.posLevel", - "posMasters.positions.posType", - "posMasters.positions.posExecutive", ], }); @@ -6004,27 +6634,10 @@ export class ReportController extends Controller { order: { orgChild3Order: "ASC" }, relations: [ "posMasters", - "posMasters.orgRoot", - "posMasters.orgChild1", - "posMasters.orgChild2", - "posMasters.orgChild3", - "posMasters.orgChild4", "posMasters.current_holder", - "posMasters.current_holder.posLevel", - "posMasters.current_holder.posType", "posMasters.current_holder.profileSalary", "posMasters.current_holder.profileEducations", - "posMasters.current_holder.current_holders", - "posMasters.current_holder.current_holders.positions", - "posMasters.current_holder.current_holders.orgRoot", - "posMasters.current_holder.current_holders.orgChild1", - "posMasters.current_holder.current_holders.orgChild2", - "posMasters.current_holder.current_holders.orgChild3", - "posMasters.current_holder.current_holders.orgChild4", "posMasters.positions", - "posMasters.positions.posLevel", - "posMasters.positions.posType", - "posMasters.positions.posExecutive", ], }); @@ -6036,27 +6649,10 @@ export class ReportController extends Controller { order: { orgChild4Order: "ASC" }, relations: [ "posMasters", - "posMasters.orgRoot", - "posMasters.orgChild1", - "posMasters.orgChild2", - "posMasters.orgChild3", - "posMasters.orgChild4", "posMasters.current_holder", - "posMasters.current_holder.posLevel", - "posMasters.current_holder.posType", "posMasters.current_holder.profileSalary", "posMasters.current_holder.profileEducations", - "posMasters.current_holder.current_holders", - "posMasters.current_holder.current_holders.positions", - "posMasters.current_holder.current_holders.orgRoot", - "posMasters.current_holder.current_holders.orgChild1", - "posMasters.current_holder.current_holders.orgChild2", - "posMasters.current_holder.current_holders.orgChild3", - "posMasters.current_holder.current_holders.orgChild4", "posMasters.positions", - "posMasters.positions.posLevel", - "posMasters.positions.posType", - "posMasters.positions.posExecutive", ], }); @@ -8080,10 +8676,10 @@ export class ReportController extends Controller { reqBody: { node: number; nodeId: string; - } + }, ) { - // @Get("report3/{rootId}") - // async findReport3(@Path() rootId: string) { + // @Get("report3/{rootId}") + // async findReport3(@Path() rootId: string) { // const orgRevision = await this.orgRevisionRepository.findOne({ // where: { orgRevisionIsDraft: true, orgRevisionIsCurrent: false }, // relations: ["orgRoots"], @@ -8091,9 +8687,9 @@ export class ReportController extends Controller { // if (!orgRevision) { // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); // } - let nodeId:string = reqBody.nodeId ? reqBody.nodeId : ""; - let node:number = reqBody.node ? reqBody.node : 0; - if(nodeId === "" || node < 0 || node > 4) { + let nodeId: string = reqBody.nodeId ? reqBody.nodeId : ""; + let node: number = reqBody.node ? reqBody.node : 0; + if (nodeId === "" || node < 0 || node > 4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } // ดึงแค่ rootId ก่อน ถ้าลูกค้า confirm ค่อยปรับ @@ -8101,50 +8697,45 @@ export class ReportController extends Controller { case 0: { const orgRoot = await this.orgRootRepository.findOne({ select: ["id"], - where: { id: nodeId } + where: { id: nodeId }, }); - if (!orgRoot) - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + if (!orgRoot) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); nodeId = orgRoot.id; break; } case 1: { const orgChild1 = await this.child1Repository.findOne({ select: ["id", "orgRootId"], - where: { id: nodeId } + where: { id: nodeId }, }); - if (!orgChild1) - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + if (!orgChild1) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); nodeId = orgChild1.orgRootId; break; } case 2: { const orgChild2 = await this.child2Repository.findOne({ select: ["id", "orgRootId"], - where: { id: nodeId } + where: { id: nodeId }, }); - if (!orgChild2) - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + if (!orgChild2) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); nodeId = orgChild2.orgRootId; break; } case 3: { const orgChild3 = await this.child3Repository.findOne({ select: ["id", "orgRootId"], - where: { id: nodeId } + where: { id: nodeId }, }); - if (!orgChild3) - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + if (!orgChild3) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); nodeId = orgChild3.orgRootId; break; } case 4: { const orgChild4 = await this.child4Repository.findOne({ select: ["id", "orgRootId"], - where: { id: nodeId } + where: { id: nodeId }, }); - if (!orgChild4) - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + if (!orgChild4) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); nodeId = orgChild4.orgRootId; break; } @@ -9389,28 +9980,27 @@ export class ReportController extends Controller { reqBody: { node: number; nodeId: string; - } + }, ) { - // @Get("report4/{rootId}") - // async findReport4(@Path() rootId: string) { - - let nodeId:string = reqBody.nodeId ? reqBody.nodeId : ""; - let node:number = reqBody.node ? reqBody.node : 0; - let rootName:string = ""; + // @Get("report4/{rootId}") + // async findReport4(@Path() rootId: string) { + + let nodeId: string = reqBody.nodeId ? reqBody.nodeId : ""; + let node: number = reqBody.node ? reqBody.node : 0; + let rootName: string = ""; let whereCase: string = ""; let params: any = {}; - if(nodeId === "" || node < 0 || node > 4) { + if (nodeId === "" || node < 0 || node > 4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } switch (node) { case 0: { const orgRoot = await this.orgRootRepository.findOne({ select: ["id", "orgRootName"], - where: { id: nodeId } + where: { id: nodeId }, }); - if (!orgRoot) - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + if (!orgRoot) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); rootName = orgRoot.orgRootName; whereCase = "posMaster.orgRootId = :nodeId"; params = { nodeId }; @@ -9419,10 +10009,9 @@ export class ReportController extends Controller { case 1: { const orgChild1 = await this.child1Repository.findOne({ select: ["id", "orgChild1Name"], - where: { id: nodeId } + where: { id: nodeId }, }); - if (!orgChild1) - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + if (!orgChild1) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); rootName = orgChild1.orgChild1Name; whereCase = "posMaster.orgChild1Id = :nodeId"; params = { nodeId }; @@ -9431,10 +10020,9 @@ export class ReportController extends Controller { case 2: { const orgChild2 = await this.child2Repository.findOne({ select: ["id", "orgChild2Name"], - where: { id: nodeId } + where: { id: nodeId }, }); - if (!orgChild2) - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + if (!orgChild2) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); rootName = orgChild2.orgChild2Name; whereCase = "posMaster.orgChild2Id = :nodeId"; params = { nodeId }; @@ -9443,10 +10031,9 @@ export class ReportController extends Controller { case 3: { const orgChild3 = await this.child3Repository.findOne({ select: ["id", "orgChild3Name"], - where: { id: nodeId } + where: { id: nodeId }, }); - if (!orgChild3) - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + if (!orgChild3) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); rootName = orgChild3.orgChild3Name; whereCase = "posMaster.orgChild3Id = :nodeId"; params = { nodeId }; @@ -9455,10 +10042,9 @@ export class ReportController extends Controller { case 4: { const orgChild4 = await this.child4Repository.findOne({ select: ["id", "orgChild4Name"], - where: { id: nodeId } + where: { id: nodeId }, }); - if (!orgChild4) - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + if (!orgChild4) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); rootName = orgChild4.orgChild4Name; whereCase = "posMaster.orgChild4Id = :nodeId"; params = { nodeId }; From 27f3bc0875db20d1bbb698ca6f2918719d905b73 Mon Sep 17 00:00:00 2001 From: mamoss <> Date: Fri, 19 Sep 2025 01:19:25 +0700 Subject: [PATCH 18/39] open noti build --- .github/workflows/release.yaml | 72 +++++++++++++++++----------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 21dc9ba3..c4b02bcc 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -67,40 +67,40 @@ jobs: docker compose pull docker compose up -d echo "${{ steps.gen_ver.outputs.image_ver }}"> success - # - name: Notify Discord Success - # if: success() - # run: | - # curl -H "Content-Type: application/json" \ - # -X POST \ - # -d '{ - # "embeds": [{ - # "title": "✅ Deployment Success!", - # "description": "**Details:**\n- Image: `${{env.IMAGE_NAME}}`\n- Version: `${{ steps.gen_ver.outputs.image_ver }}`\n- Deployed by: `${{github.actor}}`", - # "color": 3066993, - # "footer": { - # "text": "Release Notification", - # "icon_url": "https://example.com/success-icon.png" - # }, - # "timestamp": "'$(date -u +%Y-%m-%dT%H:%M:%SZ)'" - # }] - # }' \ - # ${{ secrets.DISCORD_WEBHOOK }} + - name: Notify Discord Success + if: success() + run: | + curl -H "Content-Type: application/json" \ + -X POST \ + -d '{ + "embeds": [{ + "title": "✅ Deployment Success!", + "description": "**Details:**\n- Image: `${{env.IMAGE_NAME}}`\n- Version: `${{ steps.gen_ver.outputs.image_ver }}`\n- Deployed by: `${{github.actor}}`", + "color": 3066993, + "footer": { + "text": "Release Notification", + "icon_url": "https://example.com/success-icon.png" + }, + "timestamp": "'$(date -u +%Y-%m-%dT%H:%M:%SZ)'" + }] + }' \ + ${{ secrets.DISCORD_WEBHOOK }} - # - name: Notify Discord Failure - # if: failure() - # run: | - # curl -H "Content-Type: application/json" \ - # -X POST \ - # -d '{ - # "embeds": [{ - # "title": "❌ Deployment Failed!", - # "description": "**Details:**\n- Image: `${{env.IMAGE_NAME}}`\n- Version: `${{ steps.gen_ver.outputs.image_ver }}`\n- Attempted by: `${{github.actor}}`", - # "color": 15158332, - # "footer": { - # "text": "Release Notification", - # "icon_url": "https://example.com/failure-icon.png" - # }, - # "timestamp": "'$(date -u +%Y-%m-%dT%H:%M:%SZ)'" - # }] - # }' \ - # ${{ secrets.DISCORD_WEBHOOK }} + - name: Notify Discord Failure + if: failure() + run: | + curl -H "Content-Type: application/json" \ + -X POST \ + -d '{ + "embeds": [{ + "title": "❌ Deployment Failed!", + "description": "**Details:**\n- Image: `${{env.IMAGE_NAME}}`\n- Version: `${{ steps.gen_ver.outputs.image_ver }}`\n- Attempted by: `${{github.actor}}`", + "color": 15158332, + "footer": { + "text": "Release Notification", + "icon_url": "https://example.com/failure-icon.png" + }, + "timestamp": "'$(date -u +%Y-%m-%dT%H:%M:%SZ)'" + }] + }' \ + ${{ secrets.DISCORD_WEBHOOK }} From d8ec3b220132f47a60fda8aa92c00aff67b722c4 Mon Sep 17 00:00:00 2001 From: mamoss <> Date: Fri, 19 Sep 2025 02:28:36 +0700 Subject: [PATCH 19/39] add edu report2 --- src/controllers/ReportController.ts | 234 ++++++++++++++++++---------- 1 file changed, 150 insertions(+), 84 deletions(-) diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index 4b993247..13d3e8a2 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -3658,17 +3658,26 @@ export class ReportController extends Controller { if (profileIds.length > 0) { educationsData = await this.profileEducationRepository .createQueryBuilder("pe") - .select([ - "pe.profileId", - "pe.finishDate", - "pe.degree", - "ROW_NUMBER() OVER (PARTITION BY pe.profileId ORDER BY pe.finishDate DESC) as rn", - ]) + .select(["pe.profileId", "pe.finishDate", "pe.degree", "pe.level"]) .where("pe.profileId IN (:...profileIds)", { profileIds }) - .getRawMany(); - - // Filter เฉพาะ latest education - educationsData = educationsData.filter((ed: any) => ed.rn === 1); + .andWhere( + `(pe.profileId, COALESCE(pe.finishDate, '1900-01-01'), pe.level) IN ( + SELECT pe2.profileId, + COALESCE(MAX(pe2.finishDate), '1900-01-01'), + CASE + WHEN MAX(pe2.finishDate) IS NOT NULL THEN + (SELECT pe3.level FROM profileEducation pe3 + WHERE pe3.profileId = pe2.profileId + AND pe3.finishDate = MAX(pe2.finishDate) LIMIT 1) + ELSE MAX(pe2.level) + END + FROM profileEducation pe2 + WHERE pe2.profileId IN (:...profileIds) + GROUP BY pe2.profileId + )`, + { profileIds }, + ) + .getMany(); } // Step 5: ดึงข้อมูล Salary ล่าสุดของแต่ละ Profile @@ -3676,17 +3685,18 @@ export class ReportController extends Controller { if (profileIds.length > 0) { salariesData = await this.profileSalaryRepository .createQueryBuilder("ps") - .select([ - "ps.profileId", - "ps.commandDateAffect", - "ps.amount", - "ROW_NUMBER() OVER (PARTITION BY ps.profileId ORDER BY ps.commandDateAffect DESC) as rn", - ]) + .select(["ps.profileId", "ps.commandDateAffect", "ps.amount"]) .where("ps.profileId IN (:...profileIds)", { profileIds }) - .getRawMany(); - - // Filter เฉพาะ latest salary - salariesData = salariesData.filter((sal: any) => sal.rn === 1); + .andWhere( + `(ps.profileId, ps.commandDateAffect) IN ( + SELECT ps2.profileId, MAX(ps2.commandDateAffect) + FROM profileSalary ps2 + WHERE ps2.profileId IN (:...profileIds) + GROUP BY ps2.profileId + )`, + { profileIds }, + ) + .getMany(); } // Step 6: ดึงข้อมูล Positions ที่เกี่ยวข้อง @@ -3832,27 +3842,41 @@ export class ReportController extends Controller { this.profileEducationRepository .createQueryBuilder("pe") - .select([ - "pe.profileId", - "pe.finishDate", - "pe.degree", - "ROW_NUMBER() OVER (PARTITION BY pe.profileId ORDER BY pe.finishDate DESC) as rn", - ]) + .select(["pe.profileId", "pe.finishDate", "pe.degree", "pe.level"]) .where("pe.profileId IN (:...child1ProfileIds)", { child1ProfileIds }) - .getRawMany() - .then((data: any[]) => data.filter((ed: any) => ed.rn === 1)), + .andWhere( + `(pe.profileId, COALESCE(pe.finishDate, '1900-01-01'), pe.level) IN ( + SELECT pe2.profileId, + COALESCE(MAX(pe2.finishDate), '1900-01-01'), + CASE + WHEN MAX(pe2.finishDate) IS NOT NULL THEN + (SELECT pe3.level FROM profileEducation pe3 + WHERE pe3.profileId = pe2.profileId + AND pe3.finishDate = MAX(pe2.finishDate) LIMIT 1) + ELSE MAX(pe2.level) + END + FROM profileEducation pe2 + WHERE pe2.profileId IN (:...child1ProfileIds) + GROUP BY pe2.profileId + )`, + { child1ProfileIds }, + ) + .getMany(), this.profileSalaryRepository .createQueryBuilder("ps") - .select([ - "ps.profileId", - "ps.commandDateAffect", - "ps.amount", - "ROW_NUMBER() OVER (PARTITION BY ps.profileId ORDER BY ps.commandDateAffect DESC) as rn", - ]) + .select(["ps.profileId", "ps.commandDateAffect", "ps.amount"]) .where("ps.profileId IN (:...child1ProfileIds)", { child1ProfileIds }) - .getRawMany() - .then((data: any[]) => data.filter((sal: any) => sal.rn === 1)), + .andWhere( + `(ps.profileId, ps.commandDateAffect) IN ( + SELECT ps2.profileId, MAX(ps2.commandDateAffect) + FROM profileSalary ps2 + WHERE ps2.profileId IN (:...child1ProfileIds) + GROUP BY ps2.profileId + )`, + { child1ProfileIds }, + ) + .getMany(), ]); } @@ -3998,27 +4022,41 @@ export class ReportController extends Controller { this.profileEducationRepository .createQueryBuilder("pe") - .select([ - "pe.profileId", - "pe.finishDate", - "pe.degree", - "ROW_NUMBER() OVER (PARTITION BY pe.profileId ORDER BY pe.finishDate DESC) as rn", - ]) + .select(["pe.profileId", "pe.finishDate", "pe.degree", "pe.level"]) .where("pe.profileId IN (:...child2ProfileIds)", { child2ProfileIds }) - .getRawMany() - .then((data: any[]) => data.filter((ed: any) => ed.rn === 1)), + .andWhere( + `(pe.profileId, COALESCE(pe.finishDate, '1900-01-01'), pe.level) IN ( + SELECT pe2.profileId, + COALESCE(MAX(pe2.finishDate), '1900-01-01'), + CASE + WHEN MAX(pe2.finishDate) IS NOT NULL THEN + (SELECT pe3.level FROM profileEducation pe3 + WHERE pe3.profileId = pe2.profileId + AND pe3.finishDate = MAX(pe2.finishDate) LIMIT 1) + ELSE MAX(pe2.level) + END + FROM profileEducation pe2 + WHERE pe2.profileId IN (:...child2ProfileIds) + GROUP BY pe2.profileId + )`, + { child2ProfileIds }, + ) + .getMany(), this.profileSalaryRepository .createQueryBuilder("ps") - .select([ - "ps.profileId", - "ps.commandDateAffect", - "ps.amount", - "ROW_NUMBER() OVER (PARTITION BY ps.profileId ORDER BY ps.commandDateAffect DESC) as rn", - ]) + .select(["ps.profileId", "ps.commandDateAffect", "ps.amount"]) .where("ps.profileId IN (:...child2ProfileIds)", { child2ProfileIds }) - .getRawMany() - .then((data: any[]) => data.filter((sal: any) => sal.rn === 1)), + .andWhere( + `(ps.profileId, ps.commandDateAffect) IN ( + SELECT ps2.profileId, MAX(ps2.commandDateAffect) + FROM profileSalary ps2 + WHERE ps2.profileId IN (:...child2ProfileIds) + GROUP BY ps2.profileId + )`, + { child2ProfileIds }, + ) + .getMany(), ]); } @@ -4164,27 +4202,41 @@ export class ReportController extends Controller { this.profileEducationRepository .createQueryBuilder("pe") - .select([ - "pe.profileId", - "pe.finishDate", - "pe.degree", - "ROW_NUMBER() OVER (PARTITION BY pe.profileId ORDER BY pe.finishDate DESC) as rn", - ]) + .select(["pe.profileId", "pe.finishDate", "pe.degree", "pe.level"]) .where("pe.profileId IN (:...child3ProfileIds)", { child3ProfileIds }) - .getRawMany() - .then((data: any[]) => data.filter((ed: any) => ed.rn === 1)), + .andWhere( + `(pe.profileId, COALESCE(pe.finishDate, '1900-01-01'), pe.level) IN ( + SELECT pe2.profileId, + COALESCE(MAX(pe2.finishDate), '1900-01-01'), + CASE + WHEN MAX(pe2.finishDate) IS NOT NULL THEN + (SELECT pe3.level FROM profileEducation pe3 + WHERE pe3.profileId = pe2.profileId + AND pe3.finishDate = MAX(pe2.finishDate) LIMIT 1) + ELSE MAX(pe2.level) + END + FROM profileEducation pe2 + WHERE pe2.profileId IN (:...child3ProfileIds) + GROUP BY pe2.profileId + )`, + { child3ProfileIds }, + ) + .getMany(), this.profileSalaryRepository .createQueryBuilder("ps") - .select([ - "ps.profileId", - "ps.commandDateAffect", - "ps.amount", - "ROW_NUMBER() OVER (PARTITION BY ps.profileId ORDER BY ps.commandDateAffect DESC) as rn", - ]) + .select(["ps.profileId", "ps.commandDateAffect", "ps.amount"]) .where("ps.profileId IN (:...child3ProfileIds)", { child3ProfileIds }) - .getRawMany() - .then((data: any[]) => data.filter((sal: any) => sal.rn === 1)), + .andWhere( + `(ps.profileId, ps.commandDateAffect) IN ( + SELECT ps2.profileId, MAX(ps2.commandDateAffect) + FROM profileSalary ps2 + WHERE ps2.profileId IN (:...child3ProfileIds) + GROUP BY ps2.profileId + )`, + { child3ProfileIds }, + ) + .getMany(), ]); } @@ -4328,27 +4380,41 @@ export class ReportController extends Controller { this.profileEducationRepository .createQueryBuilder("pe") - .select([ - "pe.profileId", - "pe.finishDate", - "pe.degree", - "ROW_NUMBER() OVER (PARTITION BY pe.profileId ORDER BY pe.finishDate DESC) as rn", - ]) + .select(["pe.profileId", "pe.finishDate", "pe.degree", "pe.level"]) .where("pe.profileId IN (:...child4ProfileIds)", { child4ProfileIds }) - .getRawMany() - .then((data: any[]) => data.filter((ed: any) => ed.rn === 1)), + .andWhere( + `(pe.profileId, COALESCE(pe.finishDate, '1900-01-01'), pe.level) IN ( + SELECT pe2.profileId, + COALESCE(MAX(pe2.finishDate), '1900-01-01'), + CASE + WHEN MAX(pe2.finishDate) IS NOT NULL THEN + (SELECT pe3.level FROM profileEducation pe3 + WHERE pe3.profileId = pe2.profileId + AND pe3.finishDate = MAX(pe2.finishDate) LIMIT 1) + ELSE MAX(pe2.level) + END + FROM profileEducation pe2 + WHERE pe2.profileId IN (:...child4ProfileIds) + GROUP BY pe2.profileId + )`, + { child4ProfileIds }, + ) + .getMany(), this.profileSalaryRepository .createQueryBuilder("ps") - .select([ - "ps.profileId", - "ps.commandDateAffect", - "ps.amount", - "ROW_NUMBER() OVER (PARTITION BY ps.profileId ORDER BY ps.commandDateAffect DESC) as rn", - ]) + .select(["ps.profileId", "ps.commandDateAffect", "ps.amount"]) .where("ps.profileId IN (:...child4ProfileIds)", { child4ProfileIds }) - .getRawMany() - .then((data: any[]) => data.filter((sal: any) => sal.rn === 1)), + .andWhere( + `(ps.profileId, ps.commandDateAffect) IN ( + SELECT ps2.profileId, MAX(ps2.commandDateAffect) + FROM profileSalary ps2 + WHERE ps2.profileId IN (:...child4ProfileIds) + GROUP BY ps2.profileId + )`, + { child4ProfileIds }, + ) + .getMany(), ]); } From 72bd408d7ea462c5e2eb866c6fa27efc702108c1 Mon Sep 17 00:00:00 2001 From: mamoss <> Date: Fri, 19 Sep 2025 12:05:24 +0700 Subject: [PATCH 20/39] test report 3 --- src/controllers/ReportController.ts | 1178 +++++++++++++++++++++------ 1 file changed, 937 insertions(+), 241 deletions(-) diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index 13d3e8a2..f78ba007 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -8808,168 +8808,924 @@ export class ReportController extends Controller { default: throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } + // Step 1: ดึงข้อมูล OrgRoot พื้นฐาน + const orgRootData = await this.orgRootRepository + .createQueryBuilder("orgRoot") + .select([ + "orgRoot.id", + "orgRoot.orgRootName", + "orgRoot.orgRootShortName", + "orgRoot.orgRootOrder", + ]) + .where("orgRoot.id = :nodeId", { nodeId }) + .orderBy("orgRoot.orgRootOrder", "ASC") + .getMany(); + + if (!orgRootData || orgRootData.length === 0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลองค์กร"); + } + + // Step 2: ดึงข้อมูล PosMasters ที่เกี่ยวข้องเท่านั้น + const posMastersData = await this.posMasterRepository + .createQueryBuilder("posMaster") + .select([ + "posMaster.id", + "posMaster.posMasterOrder", + "posMaster.isSit", + "posMaster.posMasterNo", + "posMaster.ancestorDNA", + "posMaster.reason", + "posMaster.orgRootId", + "posMaster.orgChild1Id", + "posMaster.current_holderId", + "posMaster.next_holderId", + ]) + .where("posMaster.orgRootId = :nodeId", { nodeId }) + .andWhere("posMaster.orgChild1Id IS NULL") // เฉพาะ root level + .orderBy("posMaster.posMasterOrder", "ASC") + .getMany(); + + // Step 3: ดึงข้อมูล Profiles ที่ใช้จริงเท่านั้น + const profileIds = [ + ...new Set([ + ...posMastersData.map((pm: any) => pm.current_holderId).filter(Boolean), + ...posMastersData.map((pm: any) => pm.next_holderId).filter(Boolean), + ]), + ]; + + let profilesData: any[] = []; + if (profileIds.length > 0) { + profilesData = await this.profileRepository + .createQueryBuilder("profile") + .select([ + "profile.id", + "profile.prefix", + "profile.firstName", + "profile.lastName", + "profile.position", + ]) + .where("profile.id IN (:...profileIds)", { profileIds }) + .getMany(); + } + + // Step 4: ดึงข้อมูล Education ล่าสุดของแต่ละ Profile + let educationsData: any[] = []; + if (profileIds.length > 0) { + educationsData = await this.profileEducationRepository + .createQueryBuilder("pe") + .select(["pe.profileId", "pe.finishDate", "pe.degree", "pe.level"]) + .where("pe.profileId IN (:...profileIds)", { profileIds }) + .andWhere( + `(pe.profileId, COALESCE(pe.finishDate, '1900-01-01'), pe.level) IN ( + SELECT pe2.profileId, + COALESCE(MAX(pe2.finishDate), '1900-01-01'), + CASE + WHEN MAX(pe2.finishDate) IS NOT NULL THEN + (SELECT pe3.level FROM profileEducation pe3 + WHERE pe3.profileId = pe2.profileId + AND pe3.finishDate = MAX(pe2.finishDate) LIMIT 1) + ELSE MIN(pe2.level) + END + FROM profileEducation pe2 + WHERE pe2.profileId IN (:...profileIds) + GROUP BY pe2.profileId + )`, + { profileIds }, + ) + .getMany(); + } + + // Step 5: ดึงข้อมูล Salary ล่าสุดของแต่ละ Profile + let salariesData: any[] = []; + if (profileIds.length > 0) { + salariesData = await this.profileSalaryRepository + .createQueryBuilder("ps") + .select([ + "ps.profileId", + "ps.commandDateAffect", + "ps.amount", + "ps.positionSalaryAmount", + "ps.mouthSalaryAmount", + ]) + .where("ps.profileId IN (:...profileIds)", { profileIds }) + .andWhere( + `(ps.profileId, ps.commandDateAffect) IN ( + SELECT ps2.profileId, MAX(ps2.commandDateAffect) + FROM profileSalary ps2 + WHERE ps2.profileId IN (:...profileIds) + GROUP BY ps2.profileId + )`, + { profileIds }, + ) + .getMany(); + } + + // Step 6: ดึงข้อมูล Positions ที่เกี่ยวข้อง + const posMasterIds = posMastersData.map((pm: any) => pm.id); + let positionsData: any[] = []; + if (posMasterIds.length > 0) { + positionsData = await this.positionRepository + .createQueryBuilder("position") + .leftJoinAndSelect("position.posType", "posType") + .leftJoinAndSelect("position.posLevel", "posLevel") + .leftJoinAndSelect("position.posExecutive", "posExecutive") + .select([ + "position.id", + "position.positionName", + "position.positionIsSelected", + "position.posMasterId", + "posType.id", + "posType.posTypeName", + "posLevel.id", + "posLevel.posLevelName", + "posExecutive.id", + "posExecutive.posExecutiveName", + ]) + .where("position.posMasterId IN (:...posMasterIds)", { posMasterIds }) + .getMany(); + } + + // Step 7: Combine ข้อมูลกลับเหมือนเดิม + for (let orgRoot of orgRootData) { + orgRoot.posMasters = posMastersData.filter((pm: any) => pm.orgRootId === orgRoot.id); + + for (let posMaster of orgRoot.posMasters) { + // Attach current_holder + if (posMaster.current_holderId) { + posMaster.current_holder = profilesData.find( + (p: any) => p.id === posMaster.current_holderId, + ); + if (posMaster.current_holder) { + posMaster.current_holder.profileEducations = educationsData.filter( + (ed: any) => ed.profileId === posMaster.current_holderId, + ); + posMaster.current_holder.profileSalary = salariesData.filter( + (sal: any) => sal.profileId === posMaster.current_holderId, + ); + } + } + + // Attach next_holder + if (posMaster.next_holderId) { + posMaster.next_holder = profilesData.find((p: any) => p.id === posMaster.next_holderId); + if (posMaster.next_holder) { + posMaster.next_holder.profileEducations = educationsData.filter( + (ed: any) => ed.profileId === posMaster.next_holderId, + ); + posMaster.next_holder.profileSalary = salariesData.filter( + (sal: any) => sal.profileId === posMaster.next_holderId, + ); + } + } + + // Attach positions + posMaster.positions = positionsData.filter((pos: any) => pos.posMasterId === posMaster.id); + } + } + + if (!orgRootData || orgRootData.length === 0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลองค์กร"); + } - const orgRootData = await this.orgRootRepository.find({ - where: { - id: nodeId, - // orgRevisionId: orgRevision.id, - }, - order: { orgRootOrder: "ASC" }, - relations: [ - "posMasters", - "posMasters.orgRoot", - "posMasters.orgChild1", - "posMasters.orgChild2", - "posMasters.orgChild3", - "posMasters.orgChild4", - "posMasters.next_holder", - "posMasters.next_holder", - "posMasters.next_holder", - "posMasters.next_holder.posLevel", - "posMasters.next_holder.posType", - "posMasters.next_holder.profileSalary", - "posMasters.next_holder.profileEducations", - "posMasters.next_holder.current_holders", - "posMasters.next_holder.current_holders.positions", - "posMasters.next_holder.current_holders.orgRoot", - "posMasters.next_holder.current_holders.orgChild1", - "posMasters.next_holder.current_holders.orgChild2", - "posMasters.next_holder.current_holders.orgChild3", - "posMasters.next_holder.current_holders.orgChild4", - "posMasters.positions", - "posMasters.positions.posLevel", - "posMasters.positions.posType", - "posMasters.positions.posExecutive", - ], - }); const orgName = orgRootData[0].orgRootName ?? ""; const orgRootIds = orgRootData.map((orgRoot) => orgRoot.id) || null; - const orgChild1Data = await this.child1Repository.find({ - where: { - orgRootId: In(orgRootIds), - }, - order: { orgChild1Order: "ASC" }, - relations: [ - "posMasters", - "posMasters.orgRoot", - "posMasters.orgChild1", - "posMasters.orgChild2", - "posMasters.orgChild3", - "posMasters.orgChild4", - "posMasters.next_holder", - "posMasters.next_holder.posLevel", - "posMasters.next_holder.posType", - "posMasters.next_holder.profileSalary", - "posMasters.next_holder.profileEducations", - "posMasters.next_holder.current_holders", - "posMasters.next_holder.current_holders.positions", - "posMasters.next_holder.current_holders.orgRoot", - "posMasters.next_holder.current_holders.orgChild1", - "posMasters.next_holder.current_holders.orgChild2", - "posMasters.next_holder.current_holders.orgChild3", - "posMasters.next_holder.current_holders.orgChild4", - "posMasters.positions", - "posMasters.positions.posLevel", - "posMasters.positions.posType", - "posMasters.positions.posExecutive", - ], - }); - const orgChild1Ids = orgChild1Data.map((orgChild1) => orgChild1.id) || null; - const orgChild2Data = await this.child2Repository.find({ - where: { - orgChild1: In(orgChild1Ids), - }, - order: { orgChild2Order: "ASC" }, - relations: [ - "posMasters", - "posMasters.orgRoot", - "posMasters.orgChild1", - "posMasters.orgChild2", - "posMasters.orgChild3", - "posMasters.orgChild4", - "posMasters.next_holder", - "posMasters.next_holder.posLevel", - "posMasters.next_holder.posType", - "posMasters.next_holder.profileSalary", - "posMasters.next_holder.profileEducations", - "posMasters.next_holder.current_holders", - "posMasters.next_holder.current_holders.positions", - "posMasters.next_holder.current_holders.orgRoot", - "posMasters.next_holder.current_holders.orgChild1", - "posMasters.next_holder.current_holders.orgChild2", - "posMasters.next_holder.current_holders.orgChild3", - "posMasters.next_holder.current_holders.orgChild4", - "posMasters.positions", - "posMasters.positions.posLevel", - "posMasters.positions.posType", - "posMasters.positions.posExecutive", - ], - }); + // Step 8: ปรับปรุง orgChild1Data - ใช้ optimized approach + const orgChild1Data = await this.child1Repository + .createQueryBuilder("orgChild1") + .select([ + "orgChild1.id", + "orgChild1.orgRootId", + "orgChild1.orgChild1Name", + "orgChild1.orgChild1ShortName", + "orgChild1.orgChild1Order", + ]) + .where("orgChild1.orgRootId IN (:...orgRootIds)", { orgRootIds }) + .orderBy("orgChild1.orgChild1Order", "ASC") + .getMany(); - const orgChild2Ids = orgChild2Data.map((orgChild2) => orgChild2.id) || null; - const orgChild3Data = await this.child3Repository.find({ - where: { - orgChild2: In(orgChild2Ids), - }, - order: { orgChild3Order: "ASC" }, - relations: [ - "posMasters", - "posMasters.orgRoot", - "posMasters.orgChild1", - "posMasters.orgChild2", - "posMasters.orgChild3", - "posMasters.orgChild4", - "posMasters.next_holder", - "posMasters.next_holder.posLevel", - "posMasters.next_holder.posType", - "posMasters.next_holder.profileSalary", - "posMasters.next_holder.profileEducations", - "posMasters.next_holder.current_holders", - "posMasters.next_holder.current_holders.positions", - "posMasters.next_holder.current_holders.orgRoot", - "posMasters.next_holder.current_holders.orgChild1", - "posMasters.next_holder.current_holders.orgChild2", - "posMasters.next_holder.current_holders.orgChild3", - "posMasters.next_holder.current_holders.orgChild4", - "posMasters.positions", - "posMasters.positions.posLevel", - "posMasters.positions.posType", - "posMasters.positions.posExecutive", - ], - }); + // ดึงข้อมูล PosMasters สำหรับ orgChild1 + const orgChild1Ids = orgChild1Data.map((orgChild1) => orgChild1.id); + let child1PosMasters: any[] = []; + if (orgChild1Ids.length > 0) { + child1PosMasters = await this.posMasterRepository + .createQueryBuilder("posMaster") + .select([ + "posMaster.id", + "posMaster.posMasterOrder", + "posMaster.isSit", + "posMaster.posMasterNo", + "posMaster.ancestorDNA", + "posMaster.reason", + "posMaster.orgChild1Id", + "posMaster.orgChild2Id", + "posMaster.current_holderId", + "posMaster.next_holderId", + ]) + .where("posMaster.orgChild1Id IN (:...orgChild1Ids)", { orgChild1Ids }) + .andWhere("posMaster.orgChild2Id IS NULL") // เฉพาะ child1 level + .orderBy("posMaster.posMasterOrder", "ASC") + .getMany(); + } - const orgChild3Ids = orgChild3Data.map((orgChild3) => orgChild3.id) || null; - const orgChild4Data = await this.child4Repository.find({ - where: { - orgChild3: In(orgChild3Ids), - }, - order: { orgChild4Order: "ASC" }, - relations: [ - "posMasters", - "posMasters.orgRoot", - "posMasters.orgChild1", - "posMasters.orgChild2", - "posMasters.orgChild3", - "posMasters.orgChild4", - "posMasters.next_holder", - "posMasters.next_holder.posLevel", - "posMasters.next_holder.posType", - "posMasters.next_holder.profileSalary", - "posMasters.next_holder.profileEducations", - "posMasters.next_holder.current_holders", - "posMasters.next_holder.current_holders.positions", - "posMasters.next_holder.current_holders.orgRoot", - "posMasters.next_holder.current_holders.orgChild1", - "posMasters.next_holder.current_holders.orgChild2", - "posMasters.next_holder.current_holders.orgChild3", - "posMasters.next_holder.current_holders.orgChild4", - "posMasters.positions", - "posMasters.positions.posLevel", - "posMasters.positions.posType", - "posMasters.positions.posExecutive", - ], - }); + // รวบรวม Profile IDs สำหรับ child1 + const child1ProfileIds = + child1PosMasters.length > 0 + ? [ + ...new Set([ + ...child1PosMasters.map((pm: any) => pm.current_holderId).filter(Boolean), + ...child1PosMasters.map((pm: any) => pm.next_holderId).filter(Boolean), + ]), + ] + : []; + + // ดึงข้อมูล Profiles, Education, Salary, Positions สำหรับ child1 + let child1Profiles: any[] = []; + let child1Educations: any[] = []; + let child1Salaries: any[] = []; + let child1Positions: any[] = []; + + if (child1ProfileIds.length > 0) { + [child1Profiles, child1Educations, child1Salaries] = await Promise.all([ + this.profileRepository + .createQueryBuilder("profile") + .select([ + "profile.id", + "profile.prefix", + "profile.firstName", + "profile.lastName", + "profile.position", + "profile.posTypeId", + "profile.posLevelId", + ]) + .where("profile.id IN (:...child1ProfileIds)", { child1ProfileIds }) + .getMany(), + + this.profileEducationRepository + .createQueryBuilder("pe") + .select(["pe.profileId", "pe.finishDate", "pe.degree", "pe.level"]) + .where("pe.profileId IN (:...child1ProfileIds)", { child1ProfileIds }) + .andWhere( + `(pe.profileId, COALESCE(pe.finishDate, '1900-01-01'), pe.level) IN ( + SELECT pe2.profileId, + COALESCE(MAX(pe2.finishDate), '1900-01-01'), + CASE + WHEN MAX(pe2.finishDate) IS NOT NULL THEN + (SELECT pe3.level FROM profileEducation pe3 + WHERE pe3.profileId = pe2.profileId + AND pe3.finishDate = MAX(pe2.finishDate) LIMIT 1) + ELSE MIN(pe2.level) + END + FROM profileEducation pe2 + WHERE pe2.profileId IN (:...child1ProfileIds) + GROUP BY pe2.profileId + )`, + { child1ProfileIds }, + ) + .getMany(), + + this.profileSalaryRepository + .createQueryBuilder("ps") + .select([ + "ps.profileId", + "ps.commandDateAffect", + "ps.amount", + "ps.positionSalaryAmount", + "ps.mouthSalaryAmount", + ]) + .where("ps.profileId IN (:...child1ProfileIds)", { child1ProfileIds }) + .andWhere( + `(ps.profileId, ps.commandDateAffect) IN ( + SELECT ps2.profileId, MAX(ps2.commandDateAffect) + FROM profileSalary ps2 + WHERE ps2.profileId IN (:...child1ProfileIds) + GROUP BY ps2.profileId + )`, + { child1ProfileIds }, + ) + .getMany(), + ]); + } + + if (child1PosMasters.length > 0) { + const child1PosMasterIds = child1PosMasters.map((pm: any) => pm.id); + child1Positions = await this.positionRepository + .createQueryBuilder("position") + .leftJoinAndSelect("position.posType", "posType") + .leftJoinAndSelect("position.posLevel", "posLevel") + .leftJoinAndSelect("position.posExecutive", "posExecutive") + .select([ + "position.id", + "position.positionName", + "position.positionIsSelected", + "position.posMasterId", + "posType.id", + "posType.posTypeName", + "posLevel.id", + "posLevel.posLevelName", + "posExecutive.id", + "posExecutive.posExecutiveName", + ]) + .where("position.posMasterId IN (:...child1PosMasterIds)", { child1PosMasterIds }) + .getMany(); + } + + // Combine ข้อมูล orgChild1 + for (let orgChild1 of orgChild1Data) { + orgChild1.posMasters = child1PosMasters.filter((pm: any) => pm.orgChild1Id === orgChild1.id); + + for (let posMaster of orgChild1.posMasters) { + if (posMaster.current_holderId) { + posMaster.current_holder = child1Profiles.find( + (p: any) => p.id === posMaster.current_holderId, + ); + if (posMaster.current_holder) { + posMaster.current_holder.profileEducations = child1Educations.filter( + (ed: any) => ed.profileId === posMaster.current_holderId, + ); + posMaster.current_holder.profileSalary = child1Salaries.filter( + (sal: any) => sal.profileId === posMaster.current_holderId, + ); + } + } + + if (posMaster.next_holderId) { + posMaster.next_holder = child1Profiles.find((p: any) => p.id === posMaster.next_holderId); + if (posMaster.next_holder) { + posMaster.next_holder.profileEducations = child1Educations.filter( + (ed: any) => ed.profileId === posMaster.next_holderId, + ); + posMaster.next_holder.profileSalary = child1Salaries.filter( + (sal: any) => sal.profileId === posMaster.next_holderId, + ); + } + } + + posMaster.positions = child1Positions.filter( + (pos: any) => pos.posMasterId === posMaster.id, + ); + } + } + + // Step 9: ปรับปรุง orgChild2Data + const originalOrgChild1Ids = + orgChild1Data.length > 0 ? orgChild1Data.map((orgChild1) => orgChild1.id) : []; + const orgChild2Data = await this.child2Repository + .createQueryBuilder("orgChild2") + .select([ + "orgChild2.id", + "orgChild2.orgChild1Id", + "orgChild2.orgChild2Name", + "orgChild2.orgChild2ShortName", + "orgChild2.orgChild2Order", + ]) + .where( + originalOrgChild1Ids.length > 0 + ? "orgChild2.orgChild1Id IN (:...originalOrgChild1Ids)" + : "1=0", + { + originalOrgChild1Ids, + }, + ) + .orderBy("orgChild2.orgChild2Order", "ASC") + .getMany(); + + // ดึงข้อมูล PosMasters สำหรับ orgChild2 + const orgChild2Ids = orgChild2Data.map((orgChild2) => orgChild2.id); + let child2PosMasters: any[] = []; + if (orgChild2Ids.length > 0) { + child2PosMasters = await this.posMasterRepository + .createQueryBuilder("posMaster") + .select([ + "posMaster.id", + "posMaster.posMasterOrder", + "posMaster.isSit", + "posMaster.posMasterNo", + "posMaster.ancestorDNA", + "posMaster.reason", + "posMaster.orgChild2Id", + "posMaster.orgChild3Id", + "posMaster.current_holderId", + "posMaster.next_holderId", + ]) + .where("posMaster.orgChild2Id IN (:...orgChild2Ids)", { orgChild2Ids }) + .andWhere("posMaster.orgChild3Id IS NULL") // เฉพาะ child2 level + .orderBy("posMaster.posMasterOrder", "ASC") + .getMany(); + } + + // รวบรวม Profile IDs สำหรับ child2 + const child2ProfileIds = + child2PosMasters.length > 0 + ? [ + ...new Set([ + ...child2PosMasters.map((pm: any) => pm.current_holderId).filter(Boolean), + ...child2PosMasters.map((pm: any) => pm.next_holderId).filter(Boolean), + ]), + ] + : []; + + // ดึงข้อมูล Profiles, Education, Salary, Positions สำหรับ child2 + let child2Profiles: any[] = []; + let child2Educations: any[] = []; + let child2Salaries: any[] = []; + let child2Positions: any[] = []; + + console.log("4"); + if (child2ProfileIds.length > 0) { + [child2Profiles, child2Educations, child2Salaries] = await Promise.all([ + this.profileRepository + .createQueryBuilder("profile") + .select([ + "profile.id", + "profile.prefix", + "profile.firstName", + "profile.lastName", + "profile.position", + "profile.posTypeId", + "profile.posLevelId", + ]) + .where("profile.id IN (:...child2ProfileIds)", { child2ProfileIds }) + .getMany(), + + this.profileEducationRepository + .createQueryBuilder("pe") + .select(["pe.profileId", "pe.finishDate", "pe.degree", "pe.level"]) + .where("pe.profileId IN (:...child2ProfileIds)", { child2ProfileIds }) + .andWhere( + `(pe.profileId, COALESCE(pe.finishDate, '1900-01-01'), pe.level) IN ( + SELECT pe2.profileId, + COALESCE(MAX(pe2.finishDate), '1900-01-01'), + CASE + WHEN MAX(pe2.finishDate) IS NOT NULL THEN + (SELECT pe3.level FROM profileEducation pe3 + WHERE pe3.profileId = pe2.profileId + AND pe3.finishDate = MAX(pe2.finishDate) LIMIT 1) + ELSE MIN(pe2.level) + END + FROM profileEducation pe2 + WHERE pe2.profileId IN (:...child2ProfileIds) + GROUP BY pe2.profileId + )`, + { child2ProfileIds }, + ) + .getMany(), + + this.profileSalaryRepository + .createQueryBuilder("ps") + .select([ + "ps.profileId", + "ps.commandDateAffect", + "ps.amount", + "ps.positionSalaryAmount", + "ps.mouthSalaryAmount", + ]) + .where("ps.profileId IN (:...child2ProfileIds)", { child2ProfileIds }) + .andWhere( + `(ps.profileId, ps.commandDateAffect) IN ( + SELECT ps2.profileId, MAX(ps2.commandDateAffect) + FROM profileSalary ps2 + WHERE ps2.profileId IN (:...child2ProfileIds) + GROUP BY ps2.profileId + )`, + { child2ProfileIds }, + ) + .getMany(), + ]); + } + + if (child2PosMasters.length > 0) { + const child2PosMasterIds = child2PosMasters.map((pm: any) => pm.id); + child2Positions = await this.positionRepository + .createQueryBuilder("position") + .leftJoinAndSelect("position.posType", "posType") + .leftJoinAndSelect("position.posLevel", "posLevel") + .leftJoinAndSelect("position.posExecutive", "posExecutive") + .select([ + "position.id", + "position.positionName", + "position.positionIsSelected", + "position.posMasterId", + "posType.id", + "posType.posTypeName", + "posLevel.id", + "posLevel.posLevelName", + "posExecutive.id", + "posExecutive.posExecutiveName", + ]) + .where("position.posMasterId IN (:...child2PosMasterIds)", { child2PosMasterIds }) + .getMany(); + } + + // Combine ข้อมูล orgChild2 + for (let orgChild2 of orgChild2Data) { + orgChild2.posMasters = child2PosMasters.filter((pm: any) => pm.orgChild2Id === orgChild2.id); + + for (let posMaster of orgChild2.posMasters) { + if (posMaster.current_holderId) { + posMaster.current_holder = child2Profiles.find( + (p: any) => p.id === posMaster.current_holderId, + ); + if (posMaster.current_holder) { + posMaster.current_holder.profileEducations = child2Educations.filter( + (ed: any) => ed.profileId === posMaster.current_holderId, + ); + posMaster.current_holder.profileSalary = child2Salaries.filter( + (sal: any) => sal.profileId === posMaster.current_holderId, + ); + } + } + + if (posMaster.next_holderId) { + posMaster.next_holder = child2Profiles.find((p: any) => p.id === posMaster.next_holderId); + if (posMaster.next_holder) { + posMaster.next_holder.profileEducations = child2Educations.filter( + (ed: any) => ed.profileId === posMaster.next_holderId, + ); + posMaster.next_holder.profileSalary = child2Salaries.filter( + (sal: any) => sal.profileId === posMaster.next_holderId, + ); + } + } + + posMaster.positions = child2Positions.filter( + (pos: any) => pos.posMasterId === posMaster.id, + ); + } + } + + // Step 10: ปรับปรุง orgChild3Data + const originalOrgChild2Ids = + orgChild2Data.length > 0 ? orgChild2Data.map((orgChild2) => orgChild2.id) : []; + const orgChild3Data = await this.child3Repository + .createQueryBuilder("orgChild3") + .select([ + "orgChild3.id", + "orgChild3.orgChild2Id", + "orgChild3.orgChild3Name", + "orgChild3.orgChild3ShortName", + "orgChild3.orgChild3Order", + ]) + .where( + originalOrgChild2Ids.length > 0 + ? "orgChild3.orgChild2Id IN (:...originalOrgChild2Ids)" + : "1=0", + { + originalOrgChild2Ids, + }, + ) + .orderBy("orgChild3.orgChild3Order", "ASC") + .getMany(); + + // ดึงข้อมูล PosMasters สำหรับ orgChild3 + const orgChild3Ids = orgChild3Data.map((orgChild3) => orgChild3.id); + let child3PosMasters: any[] = []; + if (orgChild3Ids.length > 0) { + child3PosMasters = await this.posMasterRepository + .createQueryBuilder("posMaster") + .select([ + "posMaster.id", + "posMaster.posMasterOrder", + "posMaster.isSit", + "posMaster.posMasterNo", + "posMaster.ancestorDNA", + "posMaster.reason", + "posMaster.orgChild2Id", + "posMaster.orgChild3Id", + "posMaster.orgChild4Id", + "posMaster.current_holderId", + "posMaster.next_holderId", + ]) + .where("posMaster.orgChild3Id IN (:...orgChild3Ids)", { orgChild3Ids }) + .andWhere("posMaster.orgChild4Id IS NULL") // เฉพาะ child3 level + .orderBy("posMaster.posMasterOrder", "ASC") + .getMany(); + } + + // รวบรวม Profile IDs สำหรับ child3 + const child3ProfileIds = + child3PosMasters.length > 0 + ? [ + ...new Set([ + ...child3PosMasters.map((pm: any) => pm.current_holderId).filter(Boolean), + ...child3PosMasters.map((pm: any) => pm.next_holderId).filter(Boolean), + ]), + ] + : []; + + // ดึงข้อมูล Profiles, Education, Salary, Positions สำหรับ child3 + let child3Profiles: any[] = []; + let child3Educations: any[] = []; + let child3Salaries: any[] = []; + let child3Positions: any[] = []; + + if (child3ProfileIds.length > 0) { + [child3Profiles, child3Educations, child3Salaries] = await Promise.all([ + this.profileRepository + .createQueryBuilder("profile") + .select([ + "profile.id", + "profile.prefix", + "profile.firstName", + "profile.lastName", + "profile.position", + "profile.posTypeId", + "profile.posLevelId", + ]) + .where("profile.id IN (:...child3ProfileIds)", { child3ProfileIds }) + .getMany(), + + this.profileEducationRepository + .createQueryBuilder("pe") + .select(["pe.profileId", "pe.finishDate", "pe.degree", "pe.level"]) + .where("pe.profileId IN (:...child3ProfileIds)", { child3ProfileIds }) + .andWhere( + `(pe.profileId, COALESCE(pe.finishDate, '1900-01-01'), pe.level) IN ( + SELECT pe2.profileId, + COALESCE(MAX(pe2.finishDate), '1900-01-01'), + CASE + WHEN MAX(pe2.finishDate) IS NOT NULL THEN + (SELECT pe3.level FROM profileEducation pe3 + WHERE pe3.profileId = pe2.profileId + AND pe3.finishDate = MAX(pe2.finishDate) LIMIT 1) + ELSE MIN(pe2.level) + END + FROM profileEducation pe2 + WHERE pe2.profileId IN (:...child3ProfileIds) + GROUP BY pe2.profileId + )`, + { child3ProfileIds }, + ) + .getMany(), + + this.profileSalaryRepository + .createQueryBuilder("ps") + .select([ + "ps.profileId", + "ps.commandDateAffect", + "ps.amount", + "ps.positionSalaryAmount", + "ps.mouthSalaryAmount", + ]) + .where("ps.profileId IN (:...child3ProfileIds)", { child3ProfileIds }) + .andWhere( + `(ps.profileId, ps.commandDateAffect) IN ( + SELECT ps2.profileId, MAX(ps2.commandDateAffect) + FROM profileSalary ps2 + WHERE ps2.profileId IN (:...child3ProfileIds) + GROUP BY ps2.profileId + )`, + { child3ProfileIds }, + ) + .getMany(), + ]); + } + console.log("2"); + + if (child3PosMasters.length > 0) { + const child3PosMasterIds = child3PosMasters.map((pm: any) => pm.id); + child3Positions = await this.positionRepository + .createQueryBuilder("position") + .leftJoinAndSelect("position.posType", "posType") + .leftJoinAndSelect("position.posLevel", "posLevel") + .leftJoinAndSelect("position.posExecutive", "posExecutive") + .select([ + "position.id", + "position.positionName", + "position.positionIsSelected", + "position.posMasterId", + "posType.id", + "posType.posTypeName", + "posLevel.id", + "posLevel.posLevelName", + "posExecutive.id", + "posExecutive.posExecutiveName", + ]) + .where("position.posMasterId IN (:...child3PosMasterIds)", { child3PosMasterIds }) + .getMany(); + } + + // Combine ข้อมูล orgChild3 + for (let orgChild3 of orgChild3Data) { + orgChild3.posMasters = child3PosMasters.filter((pm: any) => pm.orgChild3Id === orgChild3.id); + + for (let posMaster of orgChild3.posMasters) { + if (posMaster.current_holderId) { + posMaster.current_holder = child3Profiles.find( + (p: any) => p.id === posMaster.current_holderId, + ); + if (posMaster.current_holder) { + posMaster.current_holder.profileEducations = child3Educations.filter( + (ed: any) => ed.profileId === posMaster.current_holderId, + ); + posMaster.current_holder.profileSalary = child3Salaries.filter( + (sal: any) => sal.profileId === posMaster.current_holderId, + ); + } + } + + if (posMaster.next_holderId) { + posMaster.next_holder = child3Profiles.find((p: any) => p.id === posMaster.next_holderId); + if (posMaster.next_holder) { + posMaster.next_holder.profileEducations = child3Educations.filter( + (ed: any) => ed.profileId === posMaster.next_holderId, + ); + posMaster.next_holder.profileSalary = child3Salaries.filter( + (sal: any) => sal.profileId === posMaster.next_holderId, + ); + } + } + + posMaster.positions = child3Positions.filter( + (pos: any) => pos.posMasterId === posMaster.id, + ); + } + } + + // Step 11: ปรับปรุง orgChild4Data + const originalOrgChild3Ids = + orgChild3Data.length > 0 ? orgChild3Data.map((orgChild3) => orgChild3.id) : []; + const orgChild4Data = await this.child4Repository + .createQueryBuilder("orgChild4") + .select([ + "orgChild4.id", + "orgChild4.orgChild3Id", + "orgChild4.orgChild4Name", + "orgChild4.orgChild4ShortName", + "orgChild4.orgChild4Order", + ]) + .where( + originalOrgChild3Ids.length > 0 + ? "orgChild4.orgChild3Id IN (:...originalOrgChild3Ids)" + : "1=0", + { + originalOrgChild3Ids, + }, + ) + .orderBy("orgChild4.orgChild4Order", "ASC") + .getMany(); + + // ดึงข้อมูล PosMasters สำหรับ orgChild4 + const orgChild4Ids = orgChild4Data.map((orgChild4) => orgChild4.id); + let child4PosMasters: any[] = []; + if (orgChild4Ids.length > 0) { + child4PosMasters = await this.posMasterRepository + .createQueryBuilder("posMaster") + .select([ + "posMaster.id", + "posMaster.posMasterOrder", + "posMaster.isSit", + "posMaster.posMasterNo", + "posMaster.ancestorDNA", + "posMaster.reason", + "posMaster.orgChild3Id", + "posMaster.orgChild4Id", + "posMaster.current_holderId", + "posMaster.next_holderId", + ]) + .where("posMaster.orgChild4Id IN (:...orgChild4Ids)", { orgChild4Ids }) + .orderBy("posMaster.posMasterOrder", "ASC") + .getMany(); + } + + // รวบรวม Profile IDs สำหรับ child4 + const child4ProfileIds = + child4PosMasters.length > 0 + ? [ + ...new Set([ + ...child4PosMasters.map((pm: any) => pm.current_holderId).filter(Boolean), + ...child4PosMasters.map((pm: any) => pm.next_holderId).filter(Boolean), + ]), + ] + : []; + + // ดึงข้อมูล Profiles, Education, Salary, Positions สำหรับ child4 + let child4Profiles: any[] = []; + let child4Educations: any[] = []; + let child4Salaries: any[] = []; + let child4Positions: any[] = []; + + if (child4ProfileIds.length > 0) { + [child4Profiles, child4Educations, child4Salaries] = await Promise.all([ + this.profileRepository + .createQueryBuilder("profile") + .select([ + "profile.id", + "profile.prefix", + "profile.firstName", + "profile.lastName", + "profile.position", + "profile.posTypeId", + "profile.posLevelId", + ]) + .where("profile.id IN (:...child4ProfileIds)", { child4ProfileIds }) + .getMany(), + + this.profileEducationRepository + .createQueryBuilder("pe") + .select(["pe.profileId", "pe.finishDate", "pe.degree", "pe.level"]) + .where("pe.profileId IN (:...child4ProfileIds)", { child4ProfileIds }) + .andWhere( + `(pe.profileId, COALESCE(pe.finishDate, '1900-01-01'), pe.level) IN ( + SELECT pe2.profileId, + COALESCE(MAX(pe2.finishDate), '1900-01-01'), + CASE + WHEN MAX(pe2.finishDate) IS NOT NULL THEN + (SELECT pe3.level FROM profileEducation pe3 + WHERE pe3.profileId = pe2.profileId + AND pe3.finishDate = MAX(pe2.finishDate) LIMIT 1) + ELSE MIN(pe2.level) + END + FROM profileEducation pe2 + WHERE pe2.profileId IN (:...child4ProfileIds) + GROUP BY pe2.profileId + )`, + { child4ProfileIds }, + ) + .getMany(), + + this.profileSalaryRepository + .createQueryBuilder("ps") + .select([ + "ps.profileId", + "ps.commandDateAffect", + "ps.amount", + "ps.positionSalaryAmount", + "ps.mouthSalaryAmount", + ]) + .where("ps.profileId IN (:...child4ProfileIds)", { child4ProfileIds }) + .andWhere( + `(ps.profileId, ps.commandDateAffect) IN ( + SELECT ps2.profileId, MAX(ps2.commandDateAffect) + FROM profileSalary ps2 + WHERE ps2.profileId IN (:...child4ProfileIds) + GROUP BY ps2.profileId + )`, + { child4ProfileIds }, + ) + .getMany(), + ]); + } + + if (child4PosMasters.length > 0) { + const child4PosMasterIds = child4PosMasters.map((pm: any) => pm.id); + child4Positions = await this.positionRepository + .createQueryBuilder("position") + .leftJoinAndSelect("position.posType", "posType") + .leftJoinAndSelect("position.posLevel", "posLevel") + .leftJoinAndSelect("position.posExecutive", "posExecutive") + .select([ + "position.id", + "position.positionName", + "position.positionIsSelected", + "position.posMasterId", + "posType.id", + "posType.posTypeName", + "posLevel.id", + "posLevel.posLevelName", + "posExecutive.id", + "posExecutive.posExecutiveName", + ]) + .where("position.posMasterId IN (:...child4PosMasterIds)", { child4PosMasterIds }) + .getMany(); + } + + // Combine ข้อมูล orgChild4 + for (let orgChild4 of orgChild4Data) { + orgChild4.posMasters = child4PosMasters.filter((pm: any) => pm.orgChild4Id === orgChild4.id); + + for (let posMaster of orgChild4.posMasters) { + if (posMaster.current_holderId) { + posMaster.current_holder = child4Profiles.find( + (p: any) => p.id === posMaster.current_holderId, + ); + if (posMaster.current_holder) { + posMaster.current_holder.profileEducations = child4Educations.filter( + (ed: any) => ed.profileId === posMaster.current_holderId, + ); + posMaster.current_holder.profileSalary = child4Salaries.filter( + (sal: any) => sal.profileId === posMaster.current_holderId, + ); + } + } + + if (posMaster.next_holderId) { + posMaster.next_holder = child4Profiles.find((p: any) => p.id === posMaster.next_holderId); + if (posMaster.next_holder) { + posMaster.next_holder.profileEducations = child4Educations.filter( + (ed: any) => ed.profileId === posMaster.next_holderId, + ); + posMaster.next_holder.profileSalary = child4Salaries.filter( + (sal: any) => sal.profileId === posMaster.next_holderId, + ); + } + } + + posMaster.positions = child4Positions.filter( + (pos: any) => pos.posMasterId === posMaster.id, + ); + } + } let orgRevisionActive: any = await this.orgRevisionRepository.findOne({ where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, @@ -9005,12 +9761,6 @@ export class ReportController extends Controller { .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { if (posMaster.orgChild1Id == null && posMaster.next_holder != null) { - let positionMasterProfileOld: any = null; - if (posMaster.next_holder != null) { - positionMasterProfileOld = posMaster.next_holder.current_holders.find( - (x) => x.orgRevisionId == orgRevisionActive.id, - ); - } let education: any = ""; if ( posMaster.next_holder != null && @@ -9055,25 +9805,17 @@ export class ReportController extends Controller { posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true)?.posType ?.posTypeName - : posMaster.next_holder.posType == null - ? "-" - : posMaster.next_holder.posType.posTypeName, + : "-", posLevel: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true)?.posLevel ?.posLevelName - : posMaster.next_holder.posLevel == null - ? "-" - : posMaster.next_holder.posLevel.posLevelName, + : "-", posExecutive: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posExecutive?.posExecutiveName - : positionMasterProfileOld == null - ? "-" - : positionMasterProfileOld.positions.find( - (x: any) => x.positionIsSelected == true, - )?.posExecutive?.posExecutiveName, + : "-", profileFullname: `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, education: education == "" ? "" : education.degree, @@ -9189,6 +9931,7 @@ export class ReportController extends Controller { ); _node = null; + console.log("13"); for (let orgChild1 of orgChild1Data.filter( (orgChild1) => orgChild1.orgRootId === orgRoot.id, )) { @@ -9197,12 +9940,6 @@ export class ReportController extends Controller { .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { if (posMaster.orgChild2Id == null && posMaster.next_holder != null) { - let positionMasterProfileOld: any = null; - if (posMaster.next_holder != null) { - positionMasterProfileOld = posMaster.next_holder.current_holders.find( - (x) => x.orgRevisionId == orgRevisionActive.id, - ); - } let education: any = ""; if ( posMaster.next_holder != null && @@ -9234,6 +9971,7 @@ export class ReportController extends Controller { } } + console.log("14"); let node = { orgTreeName: orgChild1.orgChild1Name, orgTreeShortName: orgChild1.orgChild1ShortName, @@ -9247,25 +9985,17 @@ export class ReportController extends Controller { posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true)?.posType ?.posTypeName - : posMaster.next_holder.posType == null - ? "-" - : posMaster.next_holder.posType.posTypeName, + : "-", posLevel: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true)?.posLevel ?.posLevelName - : posMaster.next_holder.posLevel == null - ? "-" - : posMaster.next_holder.posLevel.posLevelName, + : "-", posExecutive: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posExecutive?.posExecutiveName - : positionMasterProfileOld == null - ? "-" - : positionMasterProfileOld.positions.find( - (x: any) => x.positionIsSelected == true, - )?.posExecutive?.posExecutiveName, + : "-", profileFullname: `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, education: education == "" ? "" : education.degree, @@ -9389,20 +10119,18 @@ export class ReportController extends Controller { ); _node = null; + console.log("15"); for (let orgChild2 of orgChild2Data.filter( (orgChild2) => orgChild2.orgChild1Id === orgChild1.id, )) { + console.log("111"); await Promise.all( orgChild2.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { + console.log("112"); if (posMaster.orgChild3Id == null && posMaster.next_holder != null) { - let positionMasterProfileOld: any = null; - if (posMaster.next_holder != null) { - positionMasterProfileOld = posMaster.next_holder.current_holders.find( - (x) => x.orgRevisionId == orgRevisionActive.id, - ); - } + console.log("114"); let education: any = ""; if ( posMaster.next_holder != null && @@ -9418,6 +10146,7 @@ export class ReportController extends Controller { education = _education[0]; } } + console.log("115"); let salary: any = ""; if ( posMaster.next_holder != null && @@ -9433,6 +10162,7 @@ export class ReportController extends Controller { salary = _salary[0]; } } + console.log("116"); let node = { orgTreeName: orgChild2.orgChild2Name, @@ -9447,25 +10177,17 @@ export class ReportController extends Controller { posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posType?.posTypeName - : posMaster.next_holder.posType == null - ? "-" - : posMaster.next_holder.posType.posTypeName, + : "-", posLevel: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posLevel?.posLevelName - : posMaster.next_holder.posLevel == null - ? "-" - : posMaster.next_holder.posLevel.posLevelName, + : "-", posExecutive: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posExecutive?.posExecutiveName - : positionMasterProfileOld == null - ? "-" - : positionMasterProfileOld.positions.find( - (x: any) => x.positionIsSelected == true, - )?.posExecutive?.posExecutiveName, + : "-", profileFullname: `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, education: education == "" ? "" : education.degree, salary: salary == "" ? "" : salary.amount, @@ -9597,6 +10319,7 @@ export class ReportController extends Controller { ); _node = null; + console.log("16"); for (let orgChild3 of orgChild3Data.filter( (orgChild3) => orgChild3.orgChild2Id === orgChild2.id, )) { @@ -9605,12 +10328,6 @@ export class ReportController extends Controller { .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { if (posMaster.orgChild4Id == null && posMaster.next_holder != null) { - let positionMasterProfileOld: any = null; - if (posMaster.next_holder != null) { - positionMasterProfileOld = posMaster.next_holder.current_holders.find( - (x) => x.orgRevisionId == orgRevisionActive.id, - ); - } let education: any = ""; if ( posMaster.next_holder != null && @@ -9655,25 +10372,17 @@ export class ReportController extends Controller { posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posType?.posTypeName - : posMaster.next_holder.posType == null - ? "-" - : posMaster.next_holder.posType.posTypeName, + : "-", posLevel: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posLevel?.posLevelName - : posMaster.next_holder.posLevel == null - ? "-" - : posMaster.next_holder.posLevel.posLevelName, + : "-", posExecutive: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posExecutive?.posExecutiveName - : positionMasterProfileOld == null - ? "-" - : positionMasterProfileOld.positions.find( - (x: any) => x.positionIsSelected == true, - )?.posExecutive?.posExecutiveName, + : "-", profileFullname: `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, education: education == "" ? "" : education.degree, salary: salary == "" ? "" : salary.amount, @@ -9810,6 +10519,7 @@ export class ReportController extends Controller { ); _node = null; + console.log("17"); for (let orgChild4 of orgChild4Data.filter( (orgChild4) => orgChild4.orgChild3Id === orgChild3.id, )) { @@ -9818,12 +10528,6 @@ export class ReportController extends Controller { .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { if (posMaster.next_holder != null) { - let positionMasterProfileOld: any = null; - if (posMaster.next_holder != null) { - positionMasterProfileOld = posMaster.next_holder.current_holders.find( - (x) => x.orgRevisionId == orgRevisionActive.id, - ); - } let education: any = ""; if ( posMaster.next_holder != null && @@ -9870,25 +10574,17 @@ export class ReportController extends Controller { posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posType?.posTypeName - : posMaster.next_holder.posType == null - ? "-" - : posMaster.next_holder.posType.posTypeName, + : "-", posLevel: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posLevel?.posLevelName - : posMaster.next_holder.posLevel == null - ? "-" - : posMaster.next_holder.posLevel.posLevelName, + : "-", posExecutive: posMaster.isSit == false ? posMaster.positions.find((x: any) => x.positionIsSelected == true) ?.posExecutive?.posExecutiveName - : positionMasterProfileOld == null - ? "-" - : positionMasterProfileOld.positions.find( - (x: any) => x.positionIsSelected == true, - )?.posExecutive?.posExecutiveName, + : "-", profileFullname: `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, education: education == "" ? "" : education.degree, salary: salary == "" ? "" : salary.amount, From cc1e591bfa3d95d4ad2ebbf4083fcda3601e59cc Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Fri, 19 Sep 2025 16:08:55 +0700 Subject: [PATCH 21/39] #1812 --- src/controllers/ReportController.ts | 45 +++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index 13d3e8a2..3199a5fc 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -3626,6 +3626,7 @@ export class ReportController extends Controller { .where("posMaster.orgRootId = :nodeId", { nodeId }) .andWhere("posMaster.orgChild1Id IS NULL") // เฉพาะ root level .orderBy("posMaster.posMasterOrder", "ASC") + .addOrderBy("posMaster.posMasterCreatedAt", "ASC") .getMany(); // Step 3: ดึงข้อมูล Profiles ที่ใช้จริงเท่านั้น @@ -3804,6 +3805,7 @@ export class ReportController extends Controller { .where("posMaster.orgChild1Id IN (:...orgChild1Ids)", { orgChild1Ids }) .andWhere("posMaster.orgChild2Id IS NULL") // เฉพาะ child1 level .orderBy("posMaster.posMasterOrder", "ASC") + .addOrderBy("posMaster.posMasterCreatedAt", "ASC") .getMany(); } @@ -3984,6 +3986,7 @@ export class ReportController extends Controller { .where("posMaster.orgChild2Id IN (:...orgChild2Ids)", { orgChild2Ids }) .andWhere("posMaster.orgChild3Id IS NULL") // เฉพาะ child2 level .orderBy("posMaster.posMasterOrder", "ASC") + .addOrderBy("posMaster.posMasterCreatedAt", "ASC") .getMany(); } @@ -4164,6 +4167,7 @@ export class ReportController extends Controller { .where("posMaster.orgChild3Id IN (:...orgChild3Ids)", { orgChild3Ids }) .andWhere("posMaster.orgChild4Id IS NULL") // เฉพาะ child3 level .orderBy("posMaster.posMasterOrder", "ASC") + .addOrderBy("posMaster.posMasterCreatedAt", "ASC") .getMany(); } @@ -4342,6 +4346,7 @@ export class ReportController extends Controller { ]) .where("posMaster.orgChild4Id IN (:...orgChild4Ids)", { orgChild4Ids }) .orderBy("posMaster.posMasterOrder", "ASC") + .addOrderBy("posMaster.posMasterCreatedAt", "ASC") .getMany(); } @@ -8814,7 +8819,13 @@ export class ReportController extends Controller { id: nodeId, // orgRevisionId: orgRevision.id, }, - order: { orgRootOrder: "ASC" }, + order: { + orgRootOrder: "ASC", + posMasters:{ + posMasterOrder: "ASC", + posMasterCreatedAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.orgRoot", @@ -8848,7 +8859,13 @@ export class ReportController extends Controller { where: { orgRootId: In(orgRootIds), }, - order: { orgChild1Order: "ASC" }, + order: { + orgChild1Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + posMasterCreatedAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.orgRoot", @@ -8880,7 +8897,13 @@ export class ReportController extends Controller { where: { orgChild1: In(orgChild1Ids), }, - order: { orgChild2Order: "ASC" }, + order: { + orgChild2Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + posMasterCreatedAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.orgRoot", @@ -8912,7 +8935,13 @@ export class ReportController extends Controller { where: { orgChild2: In(orgChild2Ids), }, - order: { orgChild3Order: "ASC" }, + order: { + orgChild3Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + posMasterCreatedAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.orgRoot", @@ -8944,7 +8973,13 @@ export class ReportController extends Controller { where: { orgChild3: In(orgChild3Ids), }, - order: { orgChild4Order: "ASC" }, + order: { + orgChild4Order: "ASC", + posMasters:{ + posMasterOrder: "ASC", + posMasterCreatedAt: "ASC" + } + }, relations: [ "posMasters", "posMasters.orgRoot", From 8fa3b89b14ae92f0e9c8f3babca4f2b1cbc7c11b Mon Sep 17 00:00:00 2001 From: mamoss <> Date: Fri, 19 Sep 2025 23:43:59 +0700 Subject: [PATCH 22/39] create cornjob retire --- src/controllers/CommandController.ts | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index a888eeff..8046056a 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -1559,6 +1559,12 @@ export class CommandController extends Controller { return new HttpSuccess(); } + @Get("cornjob/cronjobUpdateRetirementStatus") + async runCronjobUpdateRetirementStatus() { + await this.cronjobUpdateRetirementStatus(); + return new HttpSuccess(); + } + /** * API รายละเอียดรายการคำสั่ง tab4 คำสั่ง * @@ -3541,19 +3547,19 @@ export class CommandController extends Controller { profile.lastUpdateFullName = req.user.name; profile.lastUpdatedAt = new Date(); const clearProfile = await checkCommandType(String(item.commandId)); - + //ปั๊มประวัติก่อนลบตำแหน่ง const curRevision = await this.orgRevisionRepo.findOne({ - where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false } - }) + where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, + }); - if(curRevision){ + if (curRevision) { const curPosMaster = await this.posMasterRepository.findOne({ - where:{ + where: { current_holderId: profile.id, - orgRevisionId: curRevision.id - } - }) + orgRevisionId: curRevision.id, + }, + }); if (curPosMaster && clearProfile.LeaveType != "RETIRE_OUT_EMP") { await CreatePosMasterHistoryOfficer(curPosMaster.id, req, "DELETE"); } From 02f19d66ad75a20b073be74f2a54bc48adc2c380 Mon Sep 17 00:00:00 2001 From: Bright Date: Mon, 22 Sep 2025 12:05:45 +0700 Subject: [PATCH 23/39] =?UTF-8?q?=E0=B8=9A=E0=B8=B1=E0=B8=8D=E0=B8=8A?= =?UTF-8?q?=E0=B8=B5=202=20=E0=B9=81=E0=B8=9A=E0=B8=9A=E0=B8=A3=E0=B9=88?= =?UTF-8?q?=E0=B8=B2=E0=B8=87=20=E0=B8=82=E0=B9=89=E0=B8=AD=E0=B8=A1?= =?UTF-8?q?=E0=B8=B9=E0=B8=A5=E0=B9=81=E0=B8=AA=E0=B8=94=E0=B8=87=E0=B9=84?= =?UTF-8?q?=E0=B8=A1=E0=B9=88=E0=B8=84=E0=B8=A3=E0=B8=9A=20#164?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/ReportController.ts | 109 ++++++++++++++++++++-------- 1 file changed, 80 insertions(+), 29 deletions(-) diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index 811a2c8b..ac0b0cbb 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -4592,11 +4592,11 @@ export class ReportController extends Controller { (x: any) => x.orgRevisionId == orgRevisionActive.id && x.ancestorDNA == posMaster.ancestorDNA, ); - if (positionMasterOld && positionMasterOld.positions) { - profilePositionName = [ - ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), - ]; - } + // if (positionMasterOld && positionMasterOld.positions) { + // profilePositionName = [ + // ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), + // ]; + // } if (positionMasterOld && positionMasterOld.positions) { profilePositionName = [ ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), @@ -4684,6 +4684,11 @@ export class ReportController extends Controller { } } } + if (positionMasterProfileOld == null && posMaster.next_holder != null + && posMaster.next_holder.current_holders == null + ) { + positionMasterProfileOld = positionMasterOld + } let node = { posMasterOrder: posMaster.posMasterOrder, // isSit: posMaster.isSit, // @@ -4783,17 +4788,23 @@ export class ReportController extends Controller { ? positionMasterOld == null ? posType.join(" หรือ ") : profilePosType.join(" หรือ ") - : posMaster.next_holder.posType == null + : positionMasterProfileOld == null ? "-" - : posMaster.next_holder.posType.posTypeName, + // : posMaster.next_holder.posType.posTypeName, + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posType?.posTypeName, profilePosLevel: posMaster.next_holder == null ? positionMasterOld == null ? posLevel.join(" หรือ ") : profilePosLevel.join(" หรือ ") - : posMaster.next_holder.posLevel == null + : positionMasterProfileOld == null ? "-" - : posMaster.next_holder.posLevel.posLevelName, + // : posMaster.next_holder.posLevel.posLevelName, + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posLevel?.posLevelName, profilePosExecutive: posMaster.next_holder == null ? positionMasterOld == null @@ -5087,7 +5098,11 @@ export class ReportController extends Controller { } } } - + if (positionMasterProfileOld == null && posMaster.next_holder != null + && posMaster.next_holder.current_holders == null + ) { + positionMasterProfileOld = positionMasterOld + } let node = { posMasterOrder: posMaster.posMasterOrder, // isSit: posMaster.isSit, // @@ -5188,17 +5203,23 @@ export class ReportController extends Controller { ? positionMasterOld == null ? posType.join(" หรือ ") : profilePosType.join(" หรือ ") - : posMaster.next_holder.posType == null + : positionMasterProfileOld == null ? "-" - : posMaster.next_holder.posType.posTypeName, + // : posMaster.next_holder.posType.posTypeName, + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posType?.posTypeName, profilePosLevel: posMaster.next_holder == null ? positionMasterOld == null ? posLevel.join(" หรือ ") : profilePosLevel.join(" หรือ ") - : posMaster.next_holder.posLevel == null + : positionMasterProfileOld == null ? "-" - : posMaster.next_holder.posLevel.posLevelName, + // : posMaster.next_holder.posLevel.posLevelName, + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posLevel?.posLevelName, profilePosExecutive: posMaster.next_holder == null ? positionMasterOld == null @@ -5499,7 +5520,11 @@ export class ReportController extends Controller { } } } - + if (positionMasterProfileOld == null && posMaster.next_holder != null + && posMaster.next_holder.current_holders == null + ) { + positionMasterProfileOld = positionMasterOld + } let node = { posMasterOrder: posMaster.posMasterOrder, // isSit: posMaster.isSit, // @@ -5600,17 +5625,23 @@ export class ReportController extends Controller { ? positionMasterOld == null ? posType.join(" หรือ ") : profilePosType.join(" หรือ ") - : posMaster.next_holder.posType == null + : positionMasterProfileOld == null ? "-" - : posMaster.next_holder.posType.posTypeName, + // : posMaster.next_holder.posType.posTypeName, + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posType?.posTypeName, profilePosLevel: posMaster.next_holder == null ? positionMasterOld == null ? posLevel.join(" หรือ ") : profilePosLevel.join(" หรือ ") - : posMaster.next_holder.posLevel == null + : positionMasterProfileOld == null ? "-" - : posMaster.next_holder.posLevel.posLevelName, + // : posMaster.next_holder.posLevel.posLevelName, + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posLevel?.posLevelName, profilePosExecutive: posMaster.next_holder == null ? positionMasterOld == null @@ -5912,7 +5943,11 @@ export class ReportController extends Controller { } } } - + if (positionMasterProfileOld == null && posMaster.next_holder != null + && posMaster.next_holder.current_holders == null + ) { + positionMasterProfileOld = positionMasterOld + } let node = { posMasterOrder: posMaster.posMasterOrder, // isSit: posMaster.isSit, // @@ -6013,17 +6048,23 @@ export class ReportController extends Controller { ? positionMasterOld == null ? posType.join(" หรือ ") : profilePosType.join(" หรือ ") - : posMaster.next_holder.posType == null + : positionMasterProfileOld == null ? "-" - : posMaster.next_holder.posType.posTypeName, + // : posMaster.next_holder.posType.posTypeName, + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posType?.posTypeName, profilePosLevel: posMaster.next_holder == null ? positionMasterOld == null ? posLevel.join(" หรือ ") : profilePosLevel.join(" หรือ ") - : posMaster.next_holder.posLevel == null + : positionMasterProfileOld == null ? "-" - : posMaster.next_holder.posLevel.posLevelName, + // : posMaster.next_holder.posLevel.posLevelName, + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posLevel?.posLevelName, profilePosExecutive: posMaster.next_holder == null ? positionMasterOld == null @@ -6332,7 +6373,11 @@ export class ReportController extends Controller { } } } - + if (positionMasterProfileOld == null && posMaster.next_holder != null + && posMaster.next_holder.current_holders == null + ) { + positionMasterProfileOld = positionMasterOld + } let node = { posMasterOrder: posMaster.posMasterOrder, // isSit: posMaster.isSit, // @@ -6433,17 +6478,23 @@ export class ReportController extends Controller { ? positionMasterOld == null ? posType.join(" หรือ ") : profilePosType.join(" หรือ ") - : posMaster.next_holder.posType == null + : positionMasterProfileOld == null ? "-" - : posMaster.next_holder.posType.posTypeName, + // : posMaster.next_holder.posType.posTypeName, + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posType?.posTypeName, profilePosLevel: posMaster.next_holder == null ? positionMasterOld == null ? posLevel.join(" หรือ ") : profilePosLevel.join(" หรือ ") - : posMaster.next_holder.posLevel == null + : positionMasterProfileOld == null ? "-" - : posMaster.next_holder.posLevel.posLevelName, + // : posMaster.next_holder.posLevel.posLevelName, + : positionMasterProfileOld.positions.find( + (x: any) => x.positionIsSelected == true, + )?.posLevel?.posLevelName, profilePosExecutive: posMaster.next_holder == null ? positionMasterOld == null From 57c9a84c40a1a0c6c5e90c85da89ff409a03a08f Mon Sep 17 00:00:00 2001 From: Bright Date: Mon, 22 Sep 2025 15:59:21 +0700 Subject: [PATCH 24/39] no message --- src/controllers/ReportController.ts | 55 +++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index ac0b0cbb..d98800b0 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -4592,11 +4592,6 @@ export class ReportController extends Controller { (x: any) => x.orgRevisionId == orgRevisionActive.id && x.ancestorDNA == posMaster.ancestorDNA, ); - // if (positionMasterOld && positionMasterOld.positions) { - // profilePositionName = [ - // ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), - // ]; - // } if (positionMasterOld && positionMasterOld.positions) { profilePositionName = [ ...new Set(positionMasterOld.positions.map((x: any) => x.positionName)), @@ -4822,12 +4817,18 @@ export class ReportController extends Controller { if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), - profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), + profileFullname: + posMaster.next_holder == null + ? Extension.ToThaiNumber(node.profileOrgName.toString()) + : Extension.ToThaiNumber(node.orgTreeName.toString()), posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", - profilePosMasterNo: Extension.ToThaiNumber(node.profileOrgShortName.toString()), + profilePosMasterNo: + posMaster.next_holder == null + ? Extension.ToThaiNumber(node.profileOrgShortName.toString()) + : Extension.ToThaiNumber(node.orgTreeShortName.toString()), profilePosExecutive: "", profilePositionName: "", profilePosType: "", @@ -5237,12 +5238,18 @@ export class ReportController extends Controller { if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), - profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), + profileFullname: + posMaster.next_holder == null + ? Extension.ToThaiNumber(node.profileOrgName.toString()) + : Extension.ToThaiNumber(node.orgTreeName.toString()), posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", - profilePosMasterNo: Extension.ToThaiNumber(node.profileOrgShortName.toString()), + profilePosMasterNo: + posMaster.next_holder == null + ? Extension.ToThaiNumber(node.profileOrgShortName.toString()) + : Extension.ToThaiNumber(node.orgTreeShortName.toString()), profilePosExecutive: "", profilePositionName: "", profilePosType: "", @@ -5659,12 +5666,18 @@ export class ReportController extends Controller { if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), - profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), + profileFullname: + posMaster.next_holder == null + ? Extension.ToThaiNumber(node.profileOrgName.toString()) + : Extension.ToThaiNumber(node.orgTreeName.toString()), posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", - profilePosMasterNo: Extension.ToThaiNumber(node.profileOrgShortName.toString()), + profilePosMasterNo: + posMaster.next_holder == null + ? Extension.ToThaiNumber(node.profileOrgShortName.toString()) + : Extension.ToThaiNumber(node.orgTreeShortName.toString()), profilePosExecutive: "", profilePositionName: "", profilePosType: "", @@ -6082,12 +6095,18 @@ export class ReportController extends Controller { if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), - profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), + profileFullname: + posMaster.next_holder == null + ? Extension.ToThaiNumber(node.profileOrgName.toString()) + : Extension.ToThaiNumber(node.orgTreeName.toString()), posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", - profilePosMasterNo: Extension.ToThaiNumber(node.profileOrgShortName.toString()), + profilePosMasterNo: + posMaster.next_holder == null + ? Extension.ToThaiNumber(node.profileOrgShortName.toString()) + : Extension.ToThaiNumber(node.orgTreeShortName.toString()), profilePosExecutive: "", profilePositionName: "", profilePosType: "", @@ -6512,12 +6531,18 @@ export class ReportController extends Controller { if (_node == null) { const head = { posMasterNo: Extension.ToThaiNumber(node.orgTreeShortName.toString()), - profileFullname: Extension.ToThaiNumber(node.profileOrgName.toString()), + profileFullname: + posMaster.next_holder == null + ? Extension.ToThaiNumber(node.profileOrgName.toString()) + : Extension.ToThaiNumber(node.orgTreeName.toString()), posExecutive: Extension.ToThaiNumber(node.orgTreeName.toString()), positionName: "", posType: "", posLevel: "", - profilePosMasterNo: Extension.ToThaiNumber(node.profileOrgShortName.toString()), + profilePosMasterNo: + posMaster.next_holder == null + ? Extension.ToThaiNumber(node.profileOrgShortName.toString()) + : Extension.ToThaiNumber(node.orgTreeShortName.toString()), profilePosExecutive: "", profilePositionName: "", profilePosType: "", From d999241536a7f3dda5033d965a3bc482f6826294 Mon Sep 17 00:00:00 2001 From: adisak Date: Tue, 23 Sep 2025 11:54:17 +0700 Subject: [PATCH 25/39] sort admin --- .../DevelopmentRequestController.ts | 21 +++++++++++--- .../ProfileDevelopmentController.ts | 14 ++++++++- src/controllers/ProfileEditController.ts | 29 +++++++++++++++---- 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/src/controllers/DevelopmentRequestController.ts b/src/controllers/DevelopmentRequestController.ts index 752ae293..7c646230 100644 --- a/src/controllers/DevelopmentRequestController.ts +++ b/src/controllers/DevelopmentRequestController.ts @@ -127,6 +127,8 @@ export class DevelopmentRequestController extends Controller { @Query("keyword") keyword: string = "", @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, + @Query("sortBy") sortBy?: string, + @Query("descending") descending?: boolean, ) { let data = await new permission().PermissionOrgList(request, "SYS_REGISTRY_OFFICER"); const orgRevisionPublish = await this.orgRevisionRepository @@ -134,7 +136,7 @@ export class DevelopmentRequestController extends Controller { .where("orgRevision.orgRevisionIsDraft = false") .andWhere("orgRevision.orgRevisionIsCurrent = true") .getOne(); - const [lists, total] = await AppDataSource.getRepository(DevelopmentRequest) + let query = await AppDataSource.getRepository(DevelopmentRequest) .createQueryBuilder("developmentRequest") .leftJoinAndSelect("developmentRequest.profile", "profile") .leftJoinAndSelect("profile.current_holders", "current_holders") @@ -249,9 +251,20 @@ export class DevelopmentRequestController extends Controller { }), ) .orderBy("developmentRequest.createdAt", "DESC") - .skip((page - 1) * pageSize) - .take(pageSize) - .getManyAndCount(); + + + if (sortBy) { + query = query.orderBy( + `developmentRequest.${sortBy}`, + descending ? "DESC" : "ASC" + ); + } + + const [lists, total] = await query + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); + const _data = lists.map((item) => ({ ...item, profile: null })); return new HttpSuccess({ data: _data, total }); } diff --git a/src/controllers/ProfileDevelopmentController.ts b/src/controllers/ProfileDevelopmentController.ts index 7982fe7d..3a615a5a 100644 --- a/src/controllers/ProfileDevelopmentController.ts +++ b/src/controllers/ProfileDevelopmentController.ts @@ -58,11 +58,13 @@ export class ProfileDevelopmentController extends Controller { @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query() searchKeyword: string = "", + @Query("sortBy") sortBy?: string, + @Query("descending") descending?: boolean, ) { let _workflow = await new permission().Workflow(req, profileId, "SYS_REGISTRY_OFFICER"); if (_workflow == false) await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", profileId); - const [profileDevelopment, total] = await AppDataSource.getRepository(ProfileDevelopment) + let query = await AppDataSource.getRepository(ProfileDevelopment) .createQueryBuilder("profileDevelopment") .where({ profileId: profileId }) .andWhere( @@ -102,9 +104,19 @@ export class ProfileDevelopmentController extends Controller { }), ) .orderBy("profileDevelopment.createdAt", "ASC") + + if (sortBy) { + query = query.orderBy( + `profileDevelopment.${sortBy}`, + descending ? "DESC" : "ASC" + ); + } + + const [profileDevelopment, total] = await query .skip((page - 1) * pageSize) .take(pageSize) .getManyAndCount(); + return new HttpSuccess({ data: profileDevelopment, total }); } diff --git a/src/controllers/ProfileEditController.ts b/src/controllers/ProfileEditController.ts index 9e90d294..c7d66ee0 100644 --- a/src/controllers/ProfileEditController.ts +++ b/src/controllers/ProfileEditController.ts @@ -116,6 +116,8 @@ export class ProfileEditController extends Controller { @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword: string = "", @Query("status") status: string = "", + @Query("sortBy") sortBy?: string, + @Query("descending") descending?: boolean, ) { let data = await new permission().PermissionOrgList(request, "SYS_REGISTRY_OFFICER"); const orgRevisionPublish = await this.orgRevisionRepository @@ -123,7 +125,7 @@ export class ProfileEditController extends Controller { .where("orgRevision.orgRevisionIsDraft = false") .andWhere("orgRevision.orgRevisionIsCurrent = true") .getOne(); - let [getProfileEdit, total] = await AppDataSource.getRepository(ProfileEdit) + let query = await AppDataSource.getRepository(ProfileEdit) .createQueryBuilder("ProfileEdit") .leftJoinAndSelect("ProfileEdit.profile", "profile") .leftJoinAndSelect("profile.current_holders", "current_holders") @@ -214,10 +216,27 @@ export class ProfileEditController extends Controller { ); }), ) - .orderBy("ProfileEdit.createdAt", "DESC") - .skip((page - 1) * pageSize) - .take(pageSize) - .getManyAndCount(); + .orderBy("ProfileEdit.createdAt", "DESC") + + if (sortBy) { + if(sortBy == "fullname"){ + query = query.orderBy( + `profile.firstName`, + descending ? "DESC" : "ASC" + ); + }else{ + query = query.orderBy( + `ProfileEdit.${sortBy}`, + descending ? "DESC" : "ASC" + ); + } + } + + const [getProfileEdit, total] = await query + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); + const _data = getProfileEdit.map((item) => ({ id: item.id, idcard: item.profile.citizenId, From 8b40eb0ad55b75f47a1cd5b3ff18417722a3a425 Mon Sep 17 00:00:00 2001 From: adisak Date: Tue, 23 Sep 2025 14:56:38 +0700 Subject: [PATCH 26/39] sort admin --- .../ProfileDevelopmentEmployeeController.ts | 15 ++++++++- .../ProfileEditEmployeeController.ts | 27 ++++++++++++--- .../ProfileEmployeeTempController.ts | 33 ++++++++++++++++--- 3 files changed, 66 insertions(+), 9 deletions(-) diff --git a/src/controllers/ProfileDevelopmentEmployeeController.ts b/src/controllers/ProfileDevelopmentEmployeeController.ts index c7099378..76260a1b 100644 --- a/src/controllers/ProfileDevelopmentEmployeeController.ts +++ b/src/controllers/ProfileDevelopmentEmployeeController.ts @@ -56,11 +56,14 @@ export class ProfileDevelopmentEmployeeController extends Controller { @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query() searchKeyword: string = "", + @Query("sortBy") sortBy?: string, + @Query("descending") descending?: boolean, ) { let _workflow = await new permission().Workflow(req, profileId, "SYS_REGISTRY_EMP"); if (_workflow == false) await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_EMP", profileId); - const [profileDevelopment, total] = await AppDataSource.getRepository(ProfileDevelopment) + + let query = await AppDataSource.getRepository(ProfileDevelopment) .createQueryBuilder("profileDevelopment") .where({ profileEmployeeId: profileId }) .andWhere( @@ -100,9 +103,19 @@ export class ProfileDevelopmentEmployeeController extends Controller { }), ) .orderBy("profileDevelopment.createdAt", "ASC") + + if (sortBy) { + query = query.orderBy( + `profileDevelopment.${sortBy}`, + descending ? "DESC" : "ASC" + ); + } + + const [profileDevelopment, total] = await query .skip((page - 1) * pageSize) .take(pageSize) .getManyAndCount(); + return new HttpSuccess({ data: profileDevelopment, total }); } diff --git a/src/controllers/ProfileEditEmployeeController.ts b/src/controllers/ProfileEditEmployeeController.ts index 03e56021..75fa1672 100644 --- a/src/controllers/ProfileEditEmployeeController.ts +++ b/src/controllers/ProfileEditEmployeeController.ts @@ -110,6 +110,8 @@ export class ProfileEditEmployeeController extends Controller { @Query("pageSize") pageSize: number = 10, @Query("keyword") keyword: string = "", @Query("status") status: string = "", + @Query("sortBy") sortBy?: string, + @Query("descending") descending?: boolean, ) { let data = await new permission().PermissionOrgList(request, "SYS_REGISTRY_EMP"); const orgRevisionPublish = await this.orgRevisionRepository @@ -117,7 +119,8 @@ export class ProfileEditEmployeeController extends Controller { .where("orgRevision.orgRevisionIsDraft = false") .andWhere("orgRevision.orgRevisionIsCurrent = true") .getOne(); - let [getProfileEdit, total] = await AppDataSource.getRepository(ProfileEdit) + + let query = await AppDataSource.getRepository(ProfileEdit) .createQueryBuilder("ProfileEdit") .leftJoinAndSelect("ProfileEdit.profileEmployee", "profileEmployee") .leftJoinAndSelect("profileEmployee.current_holders", "current_holders") @@ -209,9 +212,25 @@ export class ProfileEditEmployeeController extends Controller { }), ) .orderBy("ProfileEdit.createdAt", "DESC") - .skip((page - 1) * pageSize) - .take(pageSize) - .getManyAndCount(); + + if (sortBy) { + if(sortBy == "fullname"){ + query = query.orderBy( + `profileEmployee.firstName`, + descending ? "DESC" : "ASC" + ); + }else{ + query = query.orderBy( + `ProfileEdit.${sortBy}`, + descending ? "DESC" : "ASC" + ); + } + } + + let [getProfileEdit, total] = await query + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); const _data = getProfileEdit.map((item) => ({ id: item.id, diff --git a/src/controllers/ProfileEmployeeTempController.ts b/src/controllers/ProfileEmployeeTempController.ts index b00197a5..e900a99f 100644 --- a/src/controllers/ProfileEmployeeTempController.ts +++ b/src/controllers/ProfileEmployeeTempController.ts @@ -1504,6 +1504,8 @@ export class ProfileEmployeeTempController extends Controller { @Query() isProbation?: boolean, @Query() isRetire?: boolean, @Query() type?: string, + @Query("sortBy") sortBy?: string, + @Query("descending") descending?: boolean, ) { let _data = await new permission().PermissionOrgList(request, "SYS_REGISTRY_TEMP"); let queryLike = @@ -1525,7 +1527,8 @@ export class ProfileEmployeeTempController extends Controller { if (!findRevision) { throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); } - const [record, total] = await this.profileRepo + + let query = await this.profileRepo .createQueryBuilder("profileEmployee") .leftJoinAndSelect("profileEmployee.posLevel", "posLevel") .leftJoinAndSelect("profileEmployee.posType", "posType") @@ -1638,9 +1641,31 @@ export class ProfileEmployeeTempController extends Controller { } }), ) - .skip((page - 1) * pageSize) - .take(pageSize) - .getManyAndCount(); + + if (sortBy) { + if(sortBy == "posLevel"){ + query = query.orderBy( + `posLevel.posLevelName`, + descending ? "DESC" : "ASC" + ); + }else if(sortBy == "posType"){ + query = query.orderBy( + `posType.posTypeName`, + descending ? "DESC" : "ASC" + ); + }else{ + query = query.orderBy( + `profileEmployee.${sortBy}`, + descending ? "DESC" : "ASC" + ); + } + } + + const [record, total] = await query + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); + const data = await Promise.all( record.map((_data) => { const shortName = From f8ba8004b3c32948e0e0d84d66b9c13eb14e9acc Mon Sep 17 00:00:00 2001 From: adisak Date: Tue, 23 Sep 2025 16:22:13 +0700 Subject: [PATCH 27/39] fix --- src/controllers/ProfileEmployeeTempController.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/controllers/ProfileEmployeeTempController.ts b/src/controllers/ProfileEmployeeTempController.ts index e900a99f..e5d03e4a 100644 --- a/src/controllers/ProfileEmployeeTempController.ts +++ b/src/controllers/ProfileEmployeeTempController.ts @@ -1643,7 +1643,7 @@ export class ProfileEmployeeTempController extends Controller { ) if (sortBy) { - if(sortBy == "posLevel"){ + if(sortBy == "posLevel"){ query = query.orderBy( `posLevel.posLevelName`, descending ? "DESC" : "ASC" @@ -1653,6 +1653,11 @@ export class ProfileEmployeeTempController extends Controller { `posType.posTypeName`, descending ? "DESC" : "ASC" ); + }else if(sortBy == "govAge"){ + query = query.orderBy( + `profileEmployee.dateAppoint`, + descending ? "DESC" : "ASC" + ); }else{ query = query.orderBy( `profileEmployee.${sortBy}`, From 191ca586c587a796f5a93468a4a7090ce9e71f56 Mon Sep 17 00:00:00 2001 From: Bright Date: Thu, 25 Sep 2025 18:07:18 +0700 Subject: [PATCH 28/39] =?UTF-8?q?API=20=E0=B8=AD=E0=B8=B1=E0=B8=9E?= =?UTF-8?q?=E0=B9=80=E0=B8=94=E0=B8=97=E0=B8=AA=E0=B8=96=E0=B8=B2=E0=B8=99?= =?UTF-8?q?=E0=B8=B0=20CANCEL=20=E0=B8=84=E0=B8=B3=E0=B8=AA=E0=B8=B1?= =?UTF-8?q?=E0=B9=88=E0=B8=87=E0=B8=A2=E0=B8=81=E0=B9=80=E0=B8=A5=E0=B8=B4?= =?UTF-8?q?=E0=B8=81=E0=B8=81=E0=B8=B2=E0=B8=A3=E0=B8=A5=E0=B8=B2=E0=B8=AD?= =?UTF-8?q?=E0=B8=AD=E0=B8=81,=20=E0=B8=84=E0=B8=B3=E0=B8=AA=E0=B8=B1?= =?UTF-8?q?=E0=B9=88=E0=B8=87=E0=B8=A2=E0=B8=81=E0=B9=80=E0=B8=A5=E0=B8=B4?= =?UTF-8?q?=E0=B8=81=E0=B8=81=E0=B8=B2=E0=B8=A3=E0=B8=A5=E0=B8=B2=E0=B8=AD?= =?UTF-8?q?=E0=B8=AD=E0=B8=81=E0=B8=A5=E0=B8=B9=E0=B8=81=E0=B8=88=E0=B9=89?= =?UTF-8?q?=E0=B8=B2=E0=B8=87=20#1409?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/CommandController.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 8046056a..04de1aef 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -3012,6 +3012,29 @@ export class CommandController extends Controller { return new HttpSuccess(_data); } + /** + * API อัพเดทสถานะ CANCEL คำสั่งยกเลิกการลาออก, คำสั่งยกเลิกการลาออกลูกจ้าง + * + * @summary API อัพเดทสถานะ CANCEL คำสั่งยกเลิกการลาออก, คำสั่งยกเลิกการลาออกลูกจ้าง + * + */ + @Post("cancel-resign") + public async command41Excecute( + @Request() req: RequestWithUser, + @Body() + body: { + commandId: string[]; + }, + ) { + const commands = await this.commandRepository.find({ where: { id: In(body.commandId) } }); + const data = commands.map((_data) => ({ + ..._data, + status: "CANCEL", + })); + await this.commandRepository.save(data); + return new HttpSuccess(); + } + @Post("excexute/salary-current") public async newSalaryAndUpdateCurrent( @Request() req: RequestWithUser, From 91281ba995b4719b2dda57225fa29a90e939da34 Mon Sep 17 00:00:00 2001 From: mamoss <> Date: Fri, 26 Sep 2025 23:42:26 +0700 Subject: [PATCH 29/39] add-role-staff/user --- src/controllers/UserController.ts | 105 +++++++++++++++++++++++++++--- 1 file changed, 97 insertions(+), 8 deletions(-) diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index e808a2be..8d77b1ec 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -852,10 +852,15 @@ export class KeycloakController extends Controller { // _item.birthDate.toISOString().slice(5, 7) + // gregorianYear; // password = formattedDate; - const _date = new Date(_item.birthDate.toDateString()).getDate().toString().padStart(2, "0"); - const _month = (new Date(_item.birthDate.toDateString()).getMonth()+1).toString().padStart(2, "0"); - const _year = (new Date(_item.birthDate.toDateString()).getFullYear()+543); - password = `${_date}${_month}${_year}` + const _date = new Date(_item.birthDate.toDateString()) + .getDate() + .toString() + .padStart(2, "0"); + const _month = (new Date(_item.birthDate.toDateString()).getMonth() + 1) + .toString() + .padStart(2, "0"); + const _year = new Date(_item.birthDate.toDateString()).getFullYear() + 543; + password = `${_date}${_month}${_year}`; } const checkUser = await getUserByUsername(_item.citizenId); let userId: any = ""; @@ -918,10 +923,15 @@ export class KeycloakController extends Controller { // _item.birthDate.toISOString().slice(5, 7) + // gregorianYear; // password = formattedDate; - const _date = new Date(_item.birthDate.toDateString()).getDate().toString().padStart(2, "0"); - const _month = (new Date(_item.birthDate.toDateString()).getMonth()+1).toString().padStart(2, "0"); - const _year = (new Date(_item.birthDate.toDateString()).getFullYear()+543); - password = `${_date}${_month}${_year}` + const _date = new Date(_item.birthDate.toDateString()) + .getDate() + .toString() + .padStart(2, "0"); + const _month = (new Date(_item.birthDate.toDateString()).getMonth() + 1) + .toString() + .padStart(2, "0"); + const _year = new Date(_item.birthDate.toDateString()).getFullYear() + 543; + password = `${_date}${_month}${_year}`; } const checkUser = await getUserByUsername(_item.citizenId); let userId: any = ""; @@ -990,4 +1000,83 @@ export class KeycloakController extends Controller { } return; } + + @Post("add-role-staff/user/{child1Id}") + @Security("bearerAuth", ["system", "admin"]) + async addroleStaffToUser( + @Path() child1Id: string, + @Request() request: { user: { sub: string; preferred_username: string } }, + ) { + const profiles = await this.profileRepo.find({ + where: { + keycloak: Not(IsNull()), + current_holders: { + orgChild1Id: child1Id, + }, + }, + relations: ["roleKeycloaks"], + }); + // return profiles.length; + + for await (const _item of profiles) { + let password = _item.citizenId; + if (_item.birthDate != null) { + const _date = new Date(_item.birthDate.toDateString()) + .getDate() + .toString() + .padStart(2, "0"); + const _month = (new Date(_item.birthDate.toDateString()).getMonth() + 1) + .toString() + .padStart(2, "0"); + const _year = new Date(_item.birthDate.toDateString()).getFullYear() + 543; + password = `${_date}${_month}${_year}`; + } + const checkUser = await getUserByUsername(_item.citizenId); + let userId: any = ""; + if (checkUser.length == 0) { + userId = await createUser(_item.citizenId, password, { + firstName: _item.firstName, + lastName: _item.lastName, + }); + if (typeof userId !== "string") { + throw new Error(userId.errorMessage); + } + } else { + userId = checkUser[0].id; + } + + const list = await getRoles(); + if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server."); + const resultUser = await addUserRoles( + userId, + list.filter((v) => v.id == "8a1a0dc9-304c-4e5b-a90a-65f841048212"), + ); + const resultStaff = await addUserRoles( + userId, + list.filter((v) => v.id == "f1fff8db-0795-47c1-9952-f3c18d5b6172"), + ); + + // if (!resultUser) { + // throw new Error("Failed. Cannot set user's role."); + // } + + // if (!resultStaff) { + // throw new Error("Failed. Cannot set staff's role."); + // } + if (typeof userId === "string") { + _item.keycloak = userId; + } + const roleKeycloakUser = await this.roleKeycloakRepo.find({ + where: { id: "8a1a0dc9-304c-4e5b-a90a-65f841048212" }, + }); + const roleKeycloakStaff = await this.roleKeycloakRepo.find({ + where: { id: "f1fff8db-0795-47c1-9952-f3c18d5b6172" }, + }); + if (_item) { + _item.roleKeycloaks = Array.from(new Set([...roleKeycloakUser, ...roleKeycloakStaff])); + this.profileRepo.save(_item); + } + } + return ""; + } } From c5b54d55dee13afb6e2e5aaa931bb1131918f0f6 Mon Sep 17 00:00:00 2001 From: mamoss <> Date: Sun, 28 Sep 2025 14:24:46 +0700 Subject: [PATCH 30/39] api add permission org --- src/controllers/OrganizationController.ts | 46 +++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index 20511811..5ad7c11c 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -30,6 +30,7 @@ import { checkQueueInProgress, setLogDataDiff } from "../interfaces/utils"; import { sendToQueueOrg, sendToQueueOrgDraft } from "../services/rabbitmq"; import { PosType } from "../entities/PosType"; import { PosLevel } from "../entities/PosLevel"; +import { PermissionOrg } from "../entities/PermissionOrg"; @Route("api/v1/org") @Tags("Organization") @@ -49,6 +50,7 @@ export class OrganizationController extends Controller { private profileRepo = AppDataSource.getRepository(Profile); private posTypeRepository = AppDataSource.getRepository(PosType); private posLevelRepository = AppDataSource.getRepository(PosLevel); + private permissionOrgRepository = AppDataSource.getRepository(PermissionOrg); /** * API ล้างข้อมูล @@ -7869,4 +7871,48 @@ export class OrganizationController extends Controller { posLevelNameOrder: posLevel.map((x) => x.posLevelName), }); } + + /** + * API เพิ่มสิทธิ์โครงสร้าง + * + * @summary - เพิ่มสิทธิ์โครงสร้าง (ADMIN) + * + */ + @Get("root/add/permission/{child1Id}") + async addRootPermission(@Path() child1Id: string, @Request() request: RequestWithUser) { + const profiles = await this.profileRepo.find({ + where: { + keycloak: Not(IsNull()), + current_holders: { + orgChild1Id: child1Id, + }, + }, + }); + const orgRoots = await this.orgRootRepository.find({ + where: { + orgRevision: { + orgRevisionIsDraft: true, + orgRevisionIsCurrent: false, + }, + }, + }); + + for await (const root of orgRoots) { + const _permissionOrg = profiles.map((profile) => { + const permission = new PermissionOrg(); + permission.orgRootId = root.id; + permission.profileId = profile.id; + permission.createdUserId = request.user.sub; + permission.createdFullName = request.user.name; + permission.lastUpdateUserId = request.user.sub; + permission.lastUpdateFullName = request.user.name; + permission.createdAt = new Date(); + permission.lastUpdatedAt = new Date(); + return permission; + }); + await this.permissionOrgRepository.save(_permissionOrg); + } + + return new HttpSuccess(); + } } From 3961488b917ca77c7e9bad16b62a15e631df7d15 Mon Sep 17 00:00:00 2001 From: Bright Date: Mon, 29 Sep 2025 11:46:19 +0700 Subject: [PATCH 31/39] =?UTF-8?q?#1409=20=E0=B9=81=E0=B8=81=E0=B9=89?= =?UTF-8?q?=E0=B9=80=E0=B8=9B=E0=B9=87=E0=B8=99=E0=B8=AA=E0=B9=88=E0=B8=87?= =?UTF-8?q?=20refId=20=E0=B8=82=E0=B8=AD=E0=B8=87=E0=B8=84=E0=B8=B3?= =?UTF-8?q?=E0=B8=AA=E0=B8=B1=E0=B9=88=E0=B8=87=E0=B8=82=E0=B8=AD=E0=B8=A5?= =?UTF-8?q?=E0=B8=B2=E0=B8=AD=E0=B8=AD=E0=B8=81=E0=B9=84=E0=B8=9B=E0=B8=84?= =?UTF-8?q?=E0=B9=89=E0=B8=99=E0=B8=97=E0=B8=B5=E0=B9=88=20command=20?= =?UTF-8?q?=E0=B8=81=E0=B9=88=E0=B8=AD=E0=B8=99=E0=B8=AD=E0=B8=B1=E0=B8=9E?= =?UTF-8?q?=20status=20=3D=20CANCEL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/CommandController.ts | 29 ++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 04de1aef..a9feeeb0 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -3466,6 +3466,7 @@ export class CommandController extends Controller { orgChild2New?: string | null; orgChild3New?: string | null; orgChild4New?: string | null; + resignId?: string | null; }[]; }, ) { @@ -3530,6 +3531,19 @@ export class CommandController extends Controller { if (code && ["C-PM-08", "C-PM-17", "C-PM-18"].includes(code)) { removePostMasterAct(profile.id); } + //ออกคำสั่งยกเลิกลาออกต้องอัพเดทสถานะคำสั่งลาออกเป็น CANCEL + else if (item.resignId && code && ["C-PM-41"].includes(code)) { + const commandRecive = await this.commandReciveRepository.findOne({ + select: ["commandId"], + where: { refId: item.resignId }, + }); + if (commandRecive && commandRecive.commandId) { + await this.commandRepository.update( + { id: commandRecive?.commandId }, + { status: "CANCEL" } + ); + } + } let _commandYear = item.commandYear; if (item.commandYear) { _commandYear = item.commandYear > 2500 ? item.commandYear : item.commandYear + 543; @@ -3796,6 +3810,7 @@ export class CommandController extends Controller { commandCode?: string | null; commandName?: string | null; remark: string | null; + resignId: string | null; }[]; }, ) { @@ -3851,6 +3866,20 @@ export class CommandController extends Controller { if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } + const code = _command?.commandType?.code; + //ออกคำสั่งยกเลิกลาออกต้องอัพเดทสถานะคำสั่งลาออกเป็น CANCEL + if (item.resignId && code && ["C-PM-42"].includes(code)) { + const commandRecive = await this.commandReciveRepository.findOne({ + select: ["commandId"], + where: { refId: item.resignId }, + }); + if (commandRecive && commandRecive.commandId) { + await this.commandRepository.update( + { id: commandRecive?.commandId }, + { status: "CANCEL" } + ); + } + } let _commandYear = item.commandYear; if (item.commandYear) { _commandYear = item.commandYear > 2500 ? item.commandYear : item.commandYear + 543; From f2ea301d443e0866b53aa1fda54f59bdb71cad02 Mon Sep 17 00:00:00 2001 From: Adisak Date: Mon, 29 Sep 2025 13:59:02 +0700 Subject: [PATCH 32/39] =?UTF-8?q?sort=20=E0=B9=81=E0=B8=81=E0=B9=89?= =?UTF-8?q?=E0=B9=84=E0=B8=82=E0=B8=97=E0=B8=B0=E0=B9=80=E0=B8=9A=E0=B8=B5?= =?UTF-8?q?=E0=B8=A2=E0=B8=99=E0=B8=9B=E0=B8=A3=E0=B8=B0=E0=B8=A7=E0=B8=B1?= =?UTF-8?q?=E0=B8=95=E0=B8=B4=20=E0=B8=95=E0=B8=B3=E0=B9=81=E0=B8=AB?= =?UTF-8?q?=E0=B8=99=E0=B9=88=E0=B8=87/=E0=B9=80=E0=B8=87=E0=B8=B4?= =?UTF-8?q?=E0=B8=99=E0=B9=80=E0=B8=94=E0=B8=B7=E0=B8=AD=E0=B8=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit @Get: api/v1/org/profile/salaryTemp --- .../ProfileSalaryTempController.ts | 80 ++++++++++++++++--- 1 file changed, 70 insertions(+), 10 deletions(-) diff --git a/src/controllers/ProfileSalaryTempController.ts b/src/controllers/ProfileSalaryTempController.ts index 81cc1751..3583d09b 100644 --- a/src/controllers/ProfileSalaryTempController.ts +++ b/src/controllers/ProfileSalaryTempController.ts @@ -64,6 +64,8 @@ export class ProfileSalaryTempController extends Controller { @Query() searchKeyword: string = "", @Query() statusCheckEdit?: string | null, @Query() rootId?: string, + @Query("sortBy") sortBy?: string, + @Query("descending") descending?: boolean, ) { if (type.trim().toUpperCase() == "OFFICER") { let _data = await new permission().PermissionOrgList(request, "SYS_REGISTRY_OFFICER"); @@ -78,7 +80,7 @@ export class ProfileSalaryTempController extends Controller { if (!findRevision) { throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); } - const [record, total] = await this.profileRepo + let query = await this.profileRepo .createQueryBuilder("profile") .leftJoinAndSelect("profile.posLevel", "posLevel") .leftJoinAndSelect("profile.posType", "posType") @@ -321,17 +323,49 @@ export class ProfileSalaryTempController extends Controller { rootId: rootId, }) .addSelect("CASE WHEN current_holders.posMasterNo IS NULL THEN 1 ELSE 0 END", "sort_order") - .orderBy("sort_order", "ASC") + // .orderBy(`${sortBy}`, sort) + + if (sortBy) { + if(sortBy == "posLevel"){ + query = query.orderBy( + `posLevel.posLevelName`, + descending ? "DESC" : "ASC" + ); + }else if(sortBy == "posType"){ + query = query.orderBy( + `posType.posTypeName`, + descending ? "DESC" : "ASC" + ); + }else if(sortBy == "posExecutive"){ + query = query.orderBy( + `posExecutive.posExecutiveName`, + descending ? "DESC" : "ASC" + ); + }else if(sortBy == "posNo"){ + query = query.orderBy( + `orgRoot.orgRootShortName`, + descending ? "DESC" : "ASC" + ); + }else{ + query = query.orderBy( + `profile.${sortBy}`, + descending ? "DESC" : "ASC" + ); + } + }else{ + query = query.orderBy("sort_order", "ASC") .addOrderBy("orgRoot.orgRootOrder", "ASC") .addOrderBy("orgChild1.orgChild1Order", "ASC") .addOrderBy("orgChild2.orgChild2Order", "ASC") .addOrderBy("orgChild3.orgChild3Order", "ASC") .addOrderBy("orgChild4.orgChild4Order", "ASC") .addOrderBy("current_holders.posMasterNo", "ASC") - // .orderBy(`${sortBy}`, sort) - .skip((page - 1) * pageSize) - .take(pageSize) - .getManyAndCount(); + } + + const [record, total] = await query + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); const data = await Promise.all( record.map((_data) => { @@ -465,7 +499,7 @@ export class ProfileSalaryTempController extends Controller { if (!findRevision) { throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); } - const [record, total] = await this.profileEmployeeRepo + let query = await this.profileEmployeeRepo .createQueryBuilder("profileEmployee") .leftJoinAndSelect("profileEmployee.posLevel", "posLevel") .leftJoinAndSelect("profileEmployee.posType", "posType") @@ -721,9 +755,35 @@ export class ProfileSalaryTempController extends Controller { }) .orderBy("current_holders.posMasterNo", "ASC") // .orderBy(`${sortBy}`, sort) - .skip((page - 1) * pageSize) - .take(pageSize) - .getManyAndCount(); + + if (sortBy) { + if(sortBy == "posLevel"){ + query = query.orderBy( + `posLevel.posLevelName`, + descending ? "DESC" : "ASC" + ); + }else if(sortBy == "posType"){ + query = query.orderBy( + `posType.posTypeName`, + descending ? "DESC" : "ASC" + ); + }else if(sortBy == "posNo"){ + query = query.orderBy( + `orgRoot.orgRootShortName`, + descending ? "DESC" : "ASC" + ); + }else{ + query = query.orderBy( + `profileEmployee.${sortBy}`, + descending ? "DESC" : "ASC" + ); + } + } + const [record, total] = await query + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); + const data = await Promise.all( record.map((_data) => { const shortName = From a7ada567454c3097345b44555008cc70d7990051 Mon Sep 17 00:00:00 2001 From: Adisak Date: Mon, 29 Sep 2025 14:15:49 +0700 Subject: [PATCH 33/39] fix --- src/controllers/ProfileSalaryTempController.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/controllers/ProfileSalaryTempController.ts b/src/controllers/ProfileSalaryTempController.ts index 3583d09b..b160f98d 100644 --- a/src/controllers/ProfileSalaryTempController.ts +++ b/src/controllers/ProfileSalaryTempController.ts @@ -342,10 +342,12 @@ export class ProfileSalaryTempController extends Controller { descending ? "DESC" : "ASC" ); }else if(sortBy == "posNo"){ - query = query.orderBy( - `orgRoot.orgRootShortName`, - descending ? "DESC" : "ASC" - ); + query = query.orderBy("orgChild4.orgChild4ShortName",descending ? "DESC" : "ASC") + .addOrderBy("orgChild3.orgChild3ShortName",descending ? "DESC" : "ASC") + .addOrderBy("orgChild2.orgChild2ShortName",descending ? "DESC" : "ASC") + .addOrderBy("orgChild1.orgChild1ShortName",descending ? "DESC" : "ASC") + .addOrderBy("orgRoot.orgRootShortName",descending ? "DESC" : "ASC") + .addOrderBy("current_holders.posMasterNo",descending ? "DESC" : "ASC") }else{ query = query.orderBy( `profile.${sortBy}`, From e63799eb00e09fbfcc4e895996866726c12c6737 Mon Sep 17 00:00:00 2001 From: Bright Date: Mon, 29 Sep 2025 15:01:01 +0700 Subject: [PATCH 34/39] no message --- src/controllers/CommandController.ts | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index a9feeeb0..6a4a2e3f 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -3023,15 +3023,24 @@ export class CommandController extends Controller { @Request() req: RequestWithUser, @Body() body: { - commandId: string[]; + resignId: string[]; }, ) { - const commands = await this.commandRepository.find({ where: { id: In(body.commandId) } }); - const data = commands.map((_data) => ({ - ..._data, - status: "CANCEL", - })); - await this.commandRepository.save(data); + const _refId = Array.from(new Set(body.resignId)); + // 1. ดึง commandRecive ที่ refId ตรงกับ resignId + const commandRecives = await this.commandReciveRepository.find({ + select: ["commandId"], + where: { refId: In(_refId) }, + }); + // 2. ดึง commandId ที่ไม่ซ้ำ + const commandIds = Array.from(new Set(commandRecives.map(x => x.commandId).filter(Boolean))); + // 3. อัปเดต status ของ command + if (commandIds.length > 0) { + await this.commandRepository.update( + { id: In(commandIds) }, + { status: "CANCEL" } + ); + } return new HttpSuccess(); } From 5be36351986530443db647c9fc6879afe22b69a7 Mon Sep 17 00:00:00 2001 From: Bright Date: Mon, 29 Sep 2025 17:33:22 +0700 Subject: [PATCH 35/39] no message --- src/controllers/CommandController.ts | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 6a4a2e3f..4e5f898e 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -160,6 +160,8 @@ export class CommandController extends Controller { @Get("list") async GetResult( @Request() request: RequestWithUser, + @Query("sortBy") sortBy?: string, + @Query("descending") descending?: boolean, @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query() keyword: string = "", @@ -262,14 +264,9 @@ export class CommandController extends Controller { yearKeyword = match[1].trim(); } let yearInBC = yearKeyword ? parseInt(yearKeyword) - 543 : null; - // - - // console.log("k>>",keyword); - // console.log("bk>>",baseKeyword); - // console.log("yk>>",yearKeyword); - // console.log("yi>>",yearInBC); - - const [commands, total] = await this.commandRepository + + // const [commands, total] = await this.commandRepository + let query = await this.commandRepository .createQueryBuilder("command") .leftJoinAndSelect("command.commandType", "commandType") .andWhere( @@ -339,6 +336,15 @@ export class CommandController extends Controller { }), ) .orderBy("command.createdAt", "DESC") + + if (sortBy) { + query = query.orderBy( + `command.${sortBy}`, + descending ? "DESC" : "ASC" + ); + } + + let [commands, total] = await query .skip((page - 1) * pageSize) .take(pageSize) .getManyAndCount(); From 8af006bae3538d88b99867ee1d34282f235abb9e Mon Sep 17 00:00:00 2001 From: Adisak Date: Tue, 30 Sep 2025 12:02:44 +0700 Subject: [PATCH 36/39] sort probation --- src/controllers/ProfileController.ts | 33 ++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 81781adf..800dc72d 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -9103,6 +9103,8 @@ export class ProfileController extends Controller { page: number; pageSize: number; keyword?: string; + sortBy?: string; + descending?: boolean; }, ) { let _data: any = { @@ -9116,7 +9118,7 @@ export class ProfileController extends Controller { if (!request.user.role.includes("SUPER_ADMIN")) { _data = await new permission().PermissionOrgCreate(request, "SYS_PROBATION"); } - const [findProfile, total] = await AppDataSource.getRepository(Profile) + let query = await AppDataSource.getRepository(Profile) .createQueryBuilder("profile") .leftJoinAndSelect("profile.profileSalary", "profileSalary") .leftJoinAndSelect("profile.posLevel", "posLevel") @@ -9230,7 +9232,34 @@ export class ProfileController extends Controller { ); }), ) - .orderBy("profile.citizenId", "ASC") + + if (body.sortBy) { + if(body.sortBy === "posLevelName"){ + query = query.orderBy( + `posLevel.posLevelName`, + body.descending ? "DESC" : "ASC" + ); + }else if(body.sortBy === "posTypeName"){ + query = query.orderBy( + `posType.posTypeName`, + body.descending ? "DESC" : "ASC" + ); + }else if(body.sortBy === "commandNo"){ + query = query.orderBy( + `profileSalary.commandNo`, + body.descending ? "DESC" : "ASC" + ); + }else{ + query = query.orderBy( + `profile.${body.sortBy}`, + body.descending ? "DESC" : "ASC" + ); + } + }else{ + query = query.orderBy("profile.citizenId", "ASC") + } + + const [findProfile, total] = await query .skip((body.page - 1) * body.pageSize) .take(body.pageSize) .getManyAndCount(); From 77b455ea8f4b1b56c153f5ca11885c9923684cf1 Mon Sep 17 00:00:00 2001 From: Adisak Date: Tue, 30 Sep 2025 12:16:48 +0700 Subject: [PATCH 37/39] fix --- src/controllers/ProfileController.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 800dc72d..e9ead8e5 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -9249,6 +9249,11 @@ export class ProfileController extends Controller { `profileSalary.commandNo`, body.descending ? "DESC" : "ASC" ); + }else if(body.sortBy === "orgRootName"){ + query = query.orderBy( + `orgRoot.orgRootName`, + body.descending ? "DESC" : "ASC" + ); }else{ query = query.orderBy( `profile.${body.sortBy}`, From 58e44de196abaed7b3a63e69870032c497805449 Mon Sep 17 00:00:00 2001 From: Adisak Date: Tue, 30 Sep 2025 13:52:48 +0700 Subject: [PATCH 38/39] sort org command --- src/controllers/ProfileController.ts | 32 +++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index e9ead8e5..c8a0e3c3 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -2585,6 +2585,8 @@ export class ProfileController extends Controller { keyword: string; page: number; pageSize: number; + sortBy?: string; + descending?: boolean; }, ) { let posMaster = await this.posMasterRepo.findOne({ @@ -2672,7 +2674,7 @@ export class ProfileController extends Controller { // condition.isDirector = true; // // conditionNow.isDirector = true; // } - const [lists, total] = await AppDataSource.getRepository(viewDirectorActing) + let query = await AppDataSource.getRepository(viewDirectorActing) .createQueryBuilder("viewDirectorActing") // .andWhere(condition) .andWhere( @@ -2724,9 +2726,19 @@ export class ProfileController extends Controller { ); }), ) - .skip((body.page - 1) * body.pageSize) - .take(body.pageSize) - .getManyAndCount(); + + if (body.sortBy) { + query = query.orderBy( + `viewDirectorActing.${body.sortBy}`, + body.descending ? "DESC" : "ASC" + ); + } + + const [lists, total] = await query + .skip((body.page - 1) * body.pageSize) + .take(body.pageSize) + .getManyAndCount(); + return new HttpSuccess({ data: lists, total }); } else { // const posMaster = await this.posMasterRepo.findOne({ @@ -2764,7 +2776,7 @@ export class ProfileController extends Controller { // condition.isDirector = true; // // conditionNow.isDirector = true; // } - const [lists, total] = await AppDataSource.getRepository(viewDirector) + let query = await AppDataSource.getRepository(viewDirector) .createQueryBuilder("viewDirector") // .andWhere(condition) .andWhere( @@ -2816,9 +2828,19 @@ export class ProfileController extends Controller { ); }), ) + + if (body.sortBy) { + query = query.orderBy( + `viewDirector.${body.sortBy}`, + body.descending ? "DESC" : "ASC" + ); + } + + const [lists, total] = await query .skip((body.page - 1) * body.pageSize) .take(body.pageSize) .getManyAndCount(); + return new HttpSuccess({ data: lists, total }); } } From 2a9f69f736b95a084a0aaaf4518a395970033d19 Mon Sep 17 00:00:00 2001 From: Adisak Date: Tue, 30 Sep 2025 15:15:57 +0700 Subject: [PATCH 39/39] sort --- src/controllers/ProfileController.ts | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index c8a0e3c3..33d33227 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -8567,6 +8567,8 @@ export class ProfileController extends Controller { keyword?: string; system?: string; }, + @Query("sortBy") sortBy?: string, + @Query("descending") descending?: boolean ) { // ค้นหารายชื่อถ้าไม่ส่ง system มาให้ default ตามทะเบียนประวัติ let _system: string = "SYS_REGISTRY_OFFICER"; @@ -8638,7 +8640,7 @@ export class ProfileController extends Controller { break; } - const [findProfile, total] = await this.profileRepo + let query = await this.profileRepo .createQueryBuilder("profile") .leftJoinAndSelect("profile.posType", "posType") .leftJoinAndSelect("profile.posLevel", "posLevel") @@ -8694,6 +8696,27 @@ export class ProfileController extends Controller { qb.orWhere(body.keyword ? queryLike : "1=1", { keyword: `%${body.keyword}%` }); }), ) + + if (sortBy) { + if(sortBy === "name"){ + query = query + .orderBy(`profile.prefix`,descending ? "DESC" : "ASC") + .addOrderBy(`profile.firstName`,descending ? "DESC" : "ASC") + .addOrderBy(`profile.lastName`,descending ? "DESC" : "ASC") + }else if(sortBy === "organization"){ + query = query.orderBy( + `orgRoot.orgRootName`, + descending ? "DESC" : "ASC" + ); + }else{ + query = query.orderBy( + `profile.${sortBy}`, + descending ? "DESC" : "ASC" + ); + } + } + + const [findProfile, total] = await query .skip((page - 1) * pageSize) .take(pageSize) .getManyAndCount();