From 44f1fc9e54caacb47fa862888bce278929a90188 Mon Sep 17 00:00:00 2001 From: waruneeauy Date: Fri, 14 Mar 2025 15:10:05 +0700 Subject: [PATCH 1/4] fixing error redis --- src/interfaces/permission.ts | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/interfaces/permission.ts b/src/interfaces/permission.ts index b55d8e72..cfe76e6d 100644 --- a/src/interfaces/permission.ts +++ b/src/interfaces/permission.ts @@ -184,12 +184,34 @@ class CheckAuth { }); } public async checkOrg(token: any, keycloakId: string) { - const redisClient = await this.redis.createClient({ - host: process.env.REDIS_HOST, - port: process.env.REDIS_PORT, - }); - const getAsync = promisify(redisClient.get).bind(redisClient); try { + // Validate required environment variables + const REDIS_HOST = process.env.REDIS_HOST; + const REDIS_PORT = process.env.REDIS_PORT ? Number(process.env.REDIS_PORT) : 6379; + + if (!REDIS_HOST) { + throw new Error("REDIS_HOST is not set in environment variables"); + } + + console.log(`[REDIS] Connecting to Redis at ${REDIS_HOST}:${REDIS_PORT}`); + + // Create Redis client + const redisClient = this.redis.createClient({ + socket: { + host: REDIS_HOST, + port: REDIS_PORT, + }, + }); + + redisClient.on("error", (err: any) => { + console.error("[REDIS] Connection error:", err.message); + }); + + await redisClient.connect(); + console.log("[REDIS] Connected successfully!"); + + const getAsync = promisify(redisClient.get).bind(redisClient); + let reply = await getAsync("org_" + keycloakId); if (reply != null) { reply = JSON.parse(reply); From dbf0952b6b73d8eb62297ef5216fd10699ca6483 Mon Sep 17 00:00:00 2001 From: kittapath Date: Fri, 14 Mar 2025 15:17:32 +0700 Subject: [PATCH 2/4] =?UTF-8?q?=E0=B8=AD=E0=B8=B1=E0=B8=95=E0=B8=A3?= =?UTF-8?q?=E0=B8=B2=E0=B8=81=E0=B8=B3=E0=B8=A5=E0=B8=B1=E0=B8=87=E0=B8=A5?= =?UTF-8?q?=E0=B8=B9=E0=B8=81=E0=B8=88=E0=B9=89=E0=B8=B2=E0=B8=87=E0=B8=8A?= =?UTF-8?q?=E0=B8=B1=E0=B9=88=E0=B8=A7=E0=B8=84=E0=B8=A3=E0=B8=B2=E0=B8=A7?= =?UTF-8?q?=20=E0=B8=95=E0=B8=B3=E0=B9=81=E0=B8=AB=E0=B8=99=E0=B9=88?= =?UTF-8?q?=E0=B8=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EmployeeTempPositionController.ts | 310 +++++++++--------- src/entities/EmployeeTempPosMaster.ts | 6 +- 2 files changed, 159 insertions(+), 157 deletions(-) diff --git a/src/controllers/EmployeeTempPositionController.ts b/src/controllers/EmployeeTempPositionController.ts index deb32067..a4975673 100644 --- a/src/controllers/EmployeeTempPositionController.ts +++ b/src/controllers/EmployeeTempPositionController.ts @@ -531,84 +531,84 @@ export class EmployeeTempPositionController extends Controller { SName = orgRoot.orgRootShortName; } - const chk_SName0 = await this.employeeTempPosMasterRepository.findOne({ - where: { - orgRevisionId: posMaster.orgRevisionId, - orgRoot: { orgRootShortName: SName }, - orgChild1Id: IsNull(), - posMasterNo: requestBody.posMasterNo, - }, - relations: ["orgRoot"], - }); - // if (chk_SName0 != null) { - // throw new HttpError( - // HttpStatusCode.INTERNAL_SERVER_ERROR, - // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // ); - // } + // const chk_SName0 = await this.employeeTempPosMasterRepository.findOne({ + // where: { + // orgRevisionId: posMaster.orgRevisionId, + // orgRoot: { orgRootShortName: SName }, + // orgChild1Id: IsNull(), + // posMasterNo: requestBody.posMasterNo, + // }, + // relations: ["orgRoot"], + // }); + // // if (chk_SName0 != null) { + // // throw new HttpError( + // // HttpStatusCode.INTERNAL_SERVER_ERROR, + // // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + // // ); + // // } - const chk_SName1 = await this.employeeTempPosMasterRepository.findOne({ - where: { - orgRevisionId: posMaster.orgRevisionId, - orgChild1: { orgChild1ShortName: SName }, - orgChild2Id: IsNull(), - posMasterNo: requestBody.posMasterNo, - }, - relations: ["orgChild1"], - }); - // if (chk_SName1 != null) { - // throw new HttpError( - // HttpStatusCode.INTERNAL_SERVER_ERROR, - // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // ); - // } + // const chk_SName1 = await this.employeeTempPosMasterRepository.findOne({ + // where: { + // orgRevisionId: posMaster.orgRevisionId, + // orgChild1: { orgChild1ShortName: SName }, + // orgChild2Id: IsNull(), + // posMasterNo: requestBody.posMasterNo, + // }, + // relations: ["orgChild1"], + // }); + // // if (chk_SName1 != null) { + // // throw new HttpError( + // // HttpStatusCode.INTERNAL_SERVER_ERROR, + // // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + // // ); + // // } - const chk_SName2 = await this.employeeTempPosMasterRepository.findOne({ - where: { - orgRevisionId: posMaster.orgRevisionId, - orgChild2: { orgChild2ShortName: SName }, - orgChild3Id: IsNull(), - posMasterNo: requestBody.posMasterNo, - }, - relations: ["orgChild2"], - }); - // if (chk_SName2 != null) { - // throw new HttpError( - // HttpStatusCode.INTERNAL_SERVER_ERROR, - // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // ); - // } + // const chk_SName2 = await this.employeeTempPosMasterRepository.findOne({ + // where: { + // orgRevisionId: posMaster.orgRevisionId, + // orgChild2: { orgChild2ShortName: SName }, + // orgChild3Id: IsNull(), + // posMasterNo: requestBody.posMasterNo, + // }, + // relations: ["orgChild2"], + // }); + // // if (chk_SName2 != null) { + // // throw new HttpError( + // // HttpStatusCode.INTERNAL_SERVER_ERROR, + // // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + // // ); + // // } - const chk_SName3 = await this.employeeTempPosMasterRepository.findOne({ - where: { - orgRevisionId: posMaster.orgRevisionId, - orgChild3: { orgChild3ShortName: SName }, - orgChild4Id: IsNull(), - posMasterNo: requestBody.posMasterNo, - }, - relations: ["orgChild3"], - }); - // if (chk_SName3 != null) { - // throw new HttpError( - // HttpStatusCode.INTERNAL_SERVER_ERROR, - // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // ); - // } + // const chk_SName3 = await this.employeeTempPosMasterRepository.findOne({ + // where: { + // orgRevisionId: posMaster.orgRevisionId, + // orgChild3: { orgChild3ShortName: SName }, + // orgChild4Id: IsNull(), + // posMasterNo: requestBody.posMasterNo, + // }, + // relations: ["orgChild3"], + // }); + // // if (chk_SName3 != null) { + // // throw new HttpError( + // // HttpStatusCode.INTERNAL_SERVER_ERROR, + // // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + // // ); + // // } - const chk_SName4 = await this.employeeTempPosMasterRepository.findOne({ - where: { - orgRevisionId: posMaster.orgRevisionId, - orgChild4: { orgChild4ShortName: SName }, - posMasterNo: requestBody.posMasterNo, - }, - relations: ["orgChild4"], - }); - // if (chk_SName4 != null) { - // throw new HttpError( - // HttpStatusCode.INTERNAL_SERVER_ERROR, - // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // ); - // } + // const chk_SName4 = await this.employeeTempPosMasterRepository.findOne({ + // where: { + // orgRevisionId: posMaster.orgRevisionId, + // orgChild4: { orgChild4ShortName: SName }, + // posMasterNo: requestBody.posMasterNo, + // }, + // relations: ["orgChild4"], + // }); + // // if (chk_SName4 != null) { + // // throw new HttpError( + // // HttpStatusCode.INTERNAL_SERVER_ERROR, + // // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + // // ); + // // } const before = null; posMaster.createdUserId = request.user.sub; posMaster.createdFullName = request.user.name; @@ -652,7 +652,9 @@ export class EmployeeTempPositionController extends Controller { @Request() request: RequestWithUser, ) { await new permission().PermissionUpdate(request, "SYS_ORG_TEMP"); - const posMaster = await this.employeeTempPosMasterRepository.findOne({ where: { id: id } }); + const posMaster: any = await this.employeeTempPosMasterRepository.findOne({ + where: { id: id }, + }); if (!posMaster) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลอัตรากำลัง"); } @@ -739,89 +741,89 @@ export class EmployeeTempPositionController extends Controller { SName = orgRoot.orgRootShortName; } - const chk_SName0 = await this.employeeTempPosMasterRepository.findOne({ - where: { - orgRevisionId: posMaster.orgRevisionId, - orgRoot: { orgRootShortName: SName }, - orgChild1Id: IsNull(), - posMasterNo: requestBody.posMasterNo, - id: Not(posMaster.id), - }, - relations: ["orgRoot"], - }); - // if (chk_SName0 != null) { - // throw new HttpError( - // HttpStatusCode.INTERNAL_SERVER_ERROR, - // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // ); - // } + // const chk_SName0 = await this.employeeTempPosMasterRepository.findOne({ + // where: { + // orgRevisionId: posMaster.orgRevisionId, + // orgRoot: { orgRootShortName: SName }, + // orgChild1Id: IsNull(), + // posMasterNo: requestBody.posMasterNo, + // id: Not(posMaster.id), + // }, + // relations: ["orgRoot"], + // }); + // // if (chk_SName0 != null) { + // // throw new HttpError( + // // HttpStatusCode.INTERNAL_SERVER_ERROR, + // // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + // // ); + // // } - const chk_SName1 = await this.employeeTempPosMasterRepository.findOne({ - where: { - orgRevisionId: posMaster.orgRevisionId, - orgChild1: { orgChild1ShortName: SName }, - orgChild2Id: IsNull(), - posMasterNo: requestBody.posMasterNo, - id: Not(posMaster.id), - }, - relations: ["orgChild1"], - }); - // if (chk_SName1 != null) { - // throw new HttpError( - // HttpStatusCode.INTERNAL_SERVER_ERROR, - // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // ); - // } + // const chk_SName1 = await this.employeeTempPosMasterRepository.findOne({ + // where: { + // orgRevisionId: posMaster.orgRevisionId, + // orgChild1: { orgChild1ShortName: SName }, + // orgChild2Id: IsNull(), + // posMasterNo: requestBody.posMasterNo, + // id: Not(posMaster.id), + // }, + // relations: ["orgChild1"], + // }); + // // if (chk_SName1 != null) { + // // throw new HttpError( + // // HttpStatusCode.INTERNAL_SERVER_ERROR, + // // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + // // ); + // // } - const chk_SName2 = await this.employeeTempPosMasterRepository.findOne({ - where: { - orgRevisionId: posMaster.orgRevisionId, - orgChild2: { orgChild2ShortName: SName }, - orgChild3Id: IsNull(), - posMasterNo: requestBody.posMasterNo, - id: Not(posMaster.id), - }, - relations: ["orgChild2"], - }); - // if (chk_SName2 != null) { - // throw new HttpError( - // HttpStatusCode.INTERNAL_SERVER_ERROR, - // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // ); - // } + // const chk_SName2 = await this.employeeTempPosMasterRepository.findOne({ + // where: { + // orgRevisionId: posMaster.orgRevisionId, + // orgChild2: { orgChild2ShortName: SName }, + // orgChild3Id: IsNull(), + // posMasterNo: requestBody.posMasterNo, + // id: Not(posMaster.id), + // }, + // relations: ["orgChild2"], + // }); + // // if (chk_SName2 != null) { + // // throw new HttpError( + // // HttpStatusCode.INTERNAL_SERVER_ERROR, + // // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + // // ); + // // } - const chk_SName3 = await this.employeeTempPosMasterRepository.findOne({ - where: { - orgRevisionId: posMaster.orgRevisionId, - orgChild3: { orgChild3ShortName: SName }, - orgChild4Id: IsNull(), - posMasterNo: requestBody.posMasterNo, - id: Not(posMaster.id), - }, - relations: ["orgChild3"], - }); - // if (chk_SName3 != null) { - // throw new HttpError( - // HttpStatusCode.INTERNAL_SERVER_ERROR, - // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // ); - // } + // const chk_SName3 = await this.employeeTempPosMasterRepository.findOne({ + // where: { + // orgRevisionId: posMaster.orgRevisionId, + // orgChild3: { orgChild3ShortName: SName }, + // orgChild4Id: IsNull(), + // posMasterNo: requestBody.posMasterNo, + // id: Not(posMaster.id), + // }, + // relations: ["orgChild3"], + // }); + // // if (chk_SName3 != null) { + // // throw new HttpError( + // // HttpStatusCode.INTERNAL_SERVER_ERROR, + // // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + // // ); + // // } - const chk_SName4 = await this.employeeTempPosMasterRepository.findOne({ - where: { - orgRevisionId: posMaster.orgRevisionId, - orgChild4: { orgChild4ShortName: SName }, - posMasterNo: requestBody.posMasterNo, - id: Not(posMaster.id), - }, - relations: ["orgChild4"], - }); - // if (chk_SName4 != null) { - // throw new HttpError( - // HttpStatusCode.INTERNAL_SERVER_ERROR, - // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // ); - // } + // const chk_SName4 = await this.employeeTempPosMasterRepository.findOne({ + // where: { + // orgRevisionId: posMaster.orgRevisionId, + // orgChild4: { orgChild4ShortName: SName }, + // posMasterNo: requestBody.posMasterNo, + // id: Not(posMaster.id), + // }, + // relations: ["orgChild4"], + // }); + // // if (chk_SName4 != null) { + // // throw new HttpError( + // // HttpStatusCode.INTERNAL_SERVER_ERROR, + // // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + // // ); + // // } const before = structuredClone(posMaster); posMaster.createdUserId = request.user.sub; //สงสัยว่าทำให้ bug แก้ไขไม่ได้ posMaster.createdFullName = request.user.name; diff --git a/src/entities/EmployeeTempPosMaster.ts b/src/entities/EmployeeTempPosMaster.ts index 82112df1..7486f15b 100644 --- a/src/entities/EmployeeTempPosMaster.ts +++ b/src/entities/EmployeeTempPosMaster.ts @@ -235,13 +235,13 @@ export class EmployeeTempPosMaster extends EntityBase { export class CreateEmployeeTempPosMaster { @Column() - posMasterNoPrefix: string; + posMasterNoPrefix?: string | null; @Column() - posMasterNo: number; + posMasterNo?: number | null; @Column() - posMasterNoSuffix: string; + posMasterNoSuffix?: string | null; @Column("uuid") positions: CreateEmployeePosDict[]; From c778b52e8b8317fadd3b0c2be21d26b5ce9b2ead Mon Sep 17 00:00:00 2001 From: kittapath Date: Fri, 14 Mar 2025 15:48:52 +0700 Subject: [PATCH 3/4] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89=E0=B9=84?= =?UTF-8?q?=E0=B8=82=E0=B8=95=E0=B8=B3=E0=B9=81=E0=B8=AB=E0=B8=99=E0=B9=88?= =?UTF-8?q?=E0=B8=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EmployeeTempPositionController.ts | 240 ------------------ 1 file changed, 240 deletions(-) diff --git a/src/controllers/EmployeeTempPositionController.ts b/src/controllers/EmployeeTempPositionController.ts index a4975673..b2e2da56 100644 --- a/src/controllers/EmployeeTempPositionController.ts +++ b/src/controllers/EmployeeTempPositionController.ts @@ -530,85 +530,6 @@ export class EmployeeTempPositionController extends Controller { posMaster.orgRevisionId = orgRoot.orgRevisionId; SName = orgRoot.orgRootShortName; } - - // const chk_SName0 = await this.employeeTempPosMasterRepository.findOne({ - // where: { - // orgRevisionId: posMaster.orgRevisionId, - // orgRoot: { orgRootShortName: SName }, - // orgChild1Id: IsNull(), - // posMasterNo: requestBody.posMasterNo, - // }, - // relations: ["orgRoot"], - // }); - // // if (chk_SName0 != null) { - // // throw new HttpError( - // // HttpStatusCode.INTERNAL_SERVER_ERROR, - // // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // // ); - // // } - - // const chk_SName1 = await this.employeeTempPosMasterRepository.findOne({ - // where: { - // orgRevisionId: posMaster.orgRevisionId, - // orgChild1: { orgChild1ShortName: SName }, - // orgChild2Id: IsNull(), - // posMasterNo: requestBody.posMasterNo, - // }, - // relations: ["orgChild1"], - // }); - // // if (chk_SName1 != null) { - // // throw new HttpError( - // // HttpStatusCode.INTERNAL_SERVER_ERROR, - // // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // // ); - // // } - - // const chk_SName2 = await this.employeeTempPosMasterRepository.findOne({ - // where: { - // orgRevisionId: posMaster.orgRevisionId, - // orgChild2: { orgChild2ShortName: SName }, - // orgChild3Id: IsNull(), - // posMasterNo: requestBody.posMasterNo, - // }, - // relations: ["orgChild2"], - // }); - // // if (chk_SName2 != null) { - // // throw new HttpError( - // // HttpStatusCode.INTERNAL_SERVER_ERROR, - // // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // // ); - // // } - - // const chk_SName3 = await this.employeeTempPosMasterRepository.findOne({ - // where: { - // orgRevisionId: posMaster.orgRevisionId, - // orgChild3: { orgChild3ShortName: SName }, - // orgChild4Id: IsNull(), - // posMasterNo: requestBody.posMasterNo, - // }, - // relations: ["orgChild3"], - // }); - // // if (chk_SName3 != null) { - // // throw new HttpError( - // // HttpStatusCode.INTERNAL_SERVER_ERROR, - // // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // // ); - // // } - - // const chk_SName4 = await this.employeeTempPosMasterRepository.findOne({ - // where: { - // orgRevisionId: posMaster.orgRevisionId, - // orgChild4: { orgChild4ShortName: SName }, - // posMasterNo: requestBody.posMasterNo, - // }, - // relations: ["orgChild4"], - // }); - // // if (chk_SName4 != null) { - // // throw new HttpError( - // // HttpStatusCode.INTERNAL_SERVER_ERROR, - // // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // // ); - // // } const before = null; posMaster.createdUserId = request.user.sub; posMaster.createdFullName = request.user.name; @@ -740,90 +661,6 @@ export class EmployeeTempPositionController extends Controller { posMaster.orgRevisionId = orgRoot.orgRevisionId; SName = orgRoot.orgRootShortName; } - - // const chk_SName0 = await this.employeeTempPosMasterRepository.findOne({ - // where: { - // orgRevisionId: posMaster.orgRevisionId, - // orgRoot: { orgRootShortName: SName }, - // orgChild1Id: IsNull(), - // posMasterNo: requestBody.posMasterNo, - // id: Not(posMaster.id), - // }, - // relations: ["orgRoot"], - // }); - // // if (chk_SName0 != null) { - // // throw new HttpError( - // // HttpStatusCode.INTERNAL_SERVER_ERROR, - // // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // // ); - // // } - - // const chk_SName1 = await this.employeeTempPosMasterRepository.findOne({ - // where: { - // orgRevisionId: posMaster.orgRevisionId, - // orgChild1: { orgChild1ShortName: SName }, - // orgChild2Id: IsNull(), - // posMasterNo: requestBody.posMasterNo, - // id: Not(posMaster.id), - // }, - // relations: ["orgChild1"], - // }); - // // if (chk_SName1 != null) { - // // throw new HttpError( - // // HttpStatusCode.INTERNAL_SERVER_ERROR, - // // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // // ); - // // } - - // const chk_SName2 = await this.employeeTempPosMasterRepository.findOne({ - // where: { - // orgRevisionId: posMaster.orgRevisionId, - // orgChild2: { orgChild2ShortName: SName }, - // orgChild3Id: IsNull(), - // posMasterNo: requestBody.posMasterNo, - // id: Not(posMaster.id), - // }, - // relations: ["orgChild2"], - // }); - // // if (chk_SName2 != null) { - // // throw new HttpError( - // // HttpStatusCode.INTERNAL_SERVER_ERROR, - // // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // // ); - // // } - - // const chk_SName3 = await this.employeeTempPosMasterRepository.findOne({ - // where: { - // orgRevisionId: posMaster.orgRevisionId, - // orgChild3: { orgChild3ShortName: SName }, - // orgChild4Id: IsNull(), - // posMasterNo: requestBody.posMasterNo, - // id: Not(posMaster.id), - // }, - // relations: ["orgChild3"], - // }); - // // if (chk_SName3 != null) { - // // throw new HttpError( - // // HttpStatusCode.INTERNAL_SERVER_ERROR, - // // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // // ); - // // } - - // const chk_SName4 = await this.employeeTempPosMasterRepository.findOne({ - // where: { - // orgRevisionId: posMaster.orgRevisionId, - // orgChild4: { orgChild4ShortName: SName }, - // posMasterNo: requestBody.posMasterNo, - // id: Not(posMaster.id), - // }, - // relations: ["orgChild4"], - // }); - // // if (chk_SName4 != null) { - // // throw new HttpError( - // // HttpStatusCode.INTERNAL_SERVER_ERROR, - // // "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", - // // ); - // // } const before = structuredClone(posMaster); posMaster.createdUserId = request.user.sub; //สงสัยว่าทำให้ bug แก้ไขไม่ได้ posMaster.createdFullName = request.user.name; @@ -855,43 +692,6 @@ export class EmployeeTempPositionController extends Controller { return new HttpSuccess(posMaster.id); } - /** - * API รายละเอียดอัตรากำลัง - * - * @summary ORG_ - รายละเอียดอัตรากำลัง (ADMIN) - * - */ - // @Get("position/{id}") - // async detailEmpPosition(@Path() id: string) { - // const posMaster = await this.employeeTempPosMasterRepository.findOne({ - // where: { id }, - // }); - // if (!posMaster) { - // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); - // } - // const positions = await this.employeePositionRepository.find({ - // where: { posMasterTempId: posMaster.id }, - // relations: ["posType", "posLevel"], - // order: { lastUpdatedAt: "ASC" }, - // }); - // const formattedData = { - // id: posMaster.id, - // posMasterNoPrefix: posMaster.posMasterNoPrefix, - // posMasterNo: posMaster.posMasterNo, - // posMasterNoSuffix: posMaster.posMasterNoSuffix, - // positions: positions.map((position) => ({ - // id: position.id, - // positionName: position.positionName, - // posTypeId: position.posTypeId, - // posTypeName: position.posType == null ? null : position.posType.posTypeName, - // posLevelId: position.posLevelId, - // posLevelName: position.posLevel == null ? null : position.posLevel.posLevelName, - // positionIsSelected: position.positionIsSelected, - // })), - // }; - // return new HttpSuccess(formattedData); - // } - /** * API ลบอัตรากำลัง * @@ -1553,14 +1353,6 @@ export class EmployeeTempPositionController extends Controller { where: { id: requestBody.id }, }); if (org != null) { - const _posMaster = await this.employeeTempPosMasterRepository.findOne({ - where: { orgRootId: org.id, posMasterNo: posMaster.posMasterNo }, - }); - if (_posMaster != null) - throw new HttpError( - HttpStatusCode.NOT_FOUND, - `เลขที่ตำแหน่ง ${org.orgRootShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, - ); if ( posMaster.orgRootId == org.id && posMaster.orgChild1Id == null && @@ -1582,14 +1374,6 @@ export class EmployeeTempPositionController extends Controller { where: { id: requestBody.id }, }); if (org != null) { - const _posMaster = await this.employeeTempPosMasterRepository.findOne({ - where: { orgChild1Id: org.id, posMasterNo: posMaster.posMasterNo }, - }); - if (_posMaster != null) - throw new HttpError( - HttpStatusCode.NOT_FOUND, - `เลขที่ตำแหน่ง ${org.orgChild1ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, - ); if ( posMaster.orgChild1Id == org.id && posMaster.orgChild2Id == null && @@ -1610,14 +1394,6 @@ export class EmployeeTempPositionController extends Controller { where: { id: requestBody.id }, }); if (org != null) { - const _posMaster = await this.employeeTempPosMasterRepository.findOne({ - where: { orgChild2Id: org.id, posMasterNo: posMaster.posMasterNo }, - }); - if (_posMaster != null) - throw new HttpError( - HttpStatusCode.NOT_FOUND, - `เลขที่ตำแหน่ง ${org.orgChild2ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, - ); if ( posMaster.orgChild2Id == org.id && posMaster.orgChild3Id == null && @@ -1637,14 +1413,6 @@ export class EmployeeTempPositionController extends Controller { where: { id: requestBody.id }, }); if (org != null) { - const _posMaster = await this.employeeTempPosMasterRepository.findOne({ - where: { orgChild3Id: org.id, posMasterNo: posMaster.posMasterNo }, - }); - if (_posMaster != null) - throw new HttpError( - HttpStatusCode.NOT_FOUND, - `เลขที่ตำแหน่ง ${org.orgChild3ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, - ); if (posMaster.orgChild3Id == org.id && posMaster.orgChild4Id == null) change = false; posMaster.orgRootId = org.orgRootId; posMaster.orgChild1Id = org.orgChild1Id; @@ -1659,14 +1427,6 @@ export class EmployeeTempPositionController extends Controller { where: { id: requestBody.id }, }); if (org != null) { - const _posMaster = await this.employeeTempPosMasterRepository.findOne({ - where: { orgChild4Id: org.id, posMasterNo: posMaster.posMasterNo }, - }); - if (_posMaster != null) - throw new HttpError( - HttpStatusCode.NOT_FOUND, - `เลขที่ตำแหน่ง ${org.orgChild4ShortName}${posMaster.posMasterNo} มีอยู่ในระบบอยู่แล้ว`, - ); if (posMaster.orgChild4Id == org.id) change = false; posMaster.orgRootId = org.orgRootId; posMaster.orgChild1Id = org.orgChild1Id; From e09317203e395e04f3d4a46a79e88878ca01d566 Mon Sep 17 00:00:00 2001 From: Bright Date: Fri, 14 Mar 2025 16:05:39 +0700 Subject: [PATCH 4/4] =?UTF-8?q?=E0=B8=A3=E0=B8=B0=E0=B8=A2=E0=B8=B0?= =?UTF-8?q?=E0=B9=80=E0=B8=A7=E0=B8=A5=E0=B8=B2=E0=B8=94=E0=B8=B3=E0=B8=A3?= =?UTF-8?q?=E0=B8=87=E0=B8=95=E0=B8=B3=E0=B9=81=E0=B8=AB=E0=B8=99=E0=B9=88?= =?UTF-8?q?=E0=B8=87=E0=B8=A5=E0=B8=B9=E0=B8=81=E0=B8=88=E0=B9=89=E0=B8=B2?= =?UTF-8?q?=E0=B8=87=20#1245?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProfileSalaryEmployeeController.ts | 138 ++++++++++++++---- 1 file changed, 112 insertions(+), 26 deletions(-) diff --git a/src/controllers/ProfileSalaryEmployeeController.ts b/src/controllers/ProfileSalaryEmployeeController.ts index 59e97ef1..d5b301c7 100644 --- a/src/controllers/ProfileSalaryEmployeeController.ts +++ b/src/controllers/ProfileSalaryEmployeeController.ts @@ -100,24 +100,67 @@ export class ProfileSalaryEmployeeController extends Controller { const mapPosition = _position.length > 1 ? _position.slice(1).map((curr: any, index: number) => ({ - year: curr.Years ? Math.floor(Number(curr.Years)) : 0, - month: curr.Months ? Math.floor(Number(curr.Months)) : 0, - day: curr.Days ? Math.floor(Number(curr.Days)) : 0, + days: curr.days_diff ? Number(curr.days_diff) : 0, name: _position[index]?.positionName, })) : []; - const posLevel: any = [ - // { - // year: 3, - // month: 0, - // day: 0, - // name: "ส 1", - // } - ]; + const groupMapPosition = mapPosition.reduce( + (acc: any, curr: any) => { + let existing = acc.find((item: any) => item.name === curr.name); + + if (existing) { + existing.days += curr.days; + } else { + existing = { name: curr.name, days: curr.days }; + acc.push(existing); + } + + existing.year = Math.floor(existing.days / 365.2524); + existing.month = Math.floor((existing.days / 30.4375) % 12); + existing.day = Math.floor(existing.days % 30.4375); + + return acc; + }, + [] as { name: string; days: number; year: number; month: number; day: number }[], + ); + + const posLevel = await AppDataSource.query("CALL GetProfileEmployeeSalaryLevel(?)", [profile.id]); + const _posLevel = posLevel.length > 0 ? posLevel[0] : []; + const mapPosLevel = + _posLevel.length > 1 + ? _posLevel.slice(1).map((curr: any, index: number) => ({ + days: curr.days_diff ? Number(curr.days_diff) : 0, + name: + !_posLevel[index]?.positionType && _posLevel[index]?.positionCee + ? `ระดับ ${_posLevel[index]?.positionCee.trim()}` + : _posLevel[index]?.positionLevel, + })) + : []; + + const groupMapPosLevel = mapPosLevel.reduce( + (acc: any, curr: any) => { + let existing = acc.find((item: any) => item.name === curr.name); + + if (existing) { + existing.days += curr.days; + } else { + existing = { name: curr.name, days: curr.days }; + acc.push(existing); + } + + existing.year = Math.floor(existing.days / 365.2524); + existing.month = Math.floor((existing.days / 30.4375) % 12); + existing.day = Math.floor(existing.days % 30.4375); + + return acc; + }, + [] as { name: string; days: number; year: number; month: number; day: number }[], + ); + return new HttpSuccess({ - position: mapPosition, - posLevel: posLevel, + position: groupMapPosition, + posLevel: groupMapPosLevel, }); } @@ -130,24 +173,67 @@ export class ProfileSalaryEmployeeController extends Controller { const mapPosition = _position.length > 1 ? _position.slice(1).map((curr: any, index: number) => ({ - year: curr.Years ? Math.floor(Number(curr.Years)) : 0, - month: curr.Months ? Math.floor(Number(curr.Months)) : 0, - day: curr.Days ? Math.floor(Number(curr.Days)) : 0, + days: curr.days_diff ? Number(curr.days_diff) : 0, name: _position[index]?.positionName, })) : []; - const posLevel: any = [ - // { - // year: 3, - // month: 0, - // day: 0, - // name: "ส 1", - // } - ]; + const groupMapPosition = mapPosition.reduce( + (acc: any, curr: any) => { + let existing = acc.find((item: any) => item.name === curr.name); + + if (existing) { + existing.days += curr.days; + } else { + existing = { name: curr.name, days: curr.days }; + acc.push(existing); + } + + existing.year = Math.floor(existing.days / 365.2524); + existing.month = Math.floor((existing.days / 30.4375) % 12); + existing.day = Math.floor(existing.days % 30.4375); + + return acc; + }, + [] as { name: string; days: number; year: number; month: number; day: number }[], + ); + + const posLevel = await AppDataSource.query("CALL GetProfileEmployeeSalaryLevel(?)", [profileId]); + const _posLevel = posLevel.length > 0 ? posLevel[0] : []; + const mapPosLevel = + _posLevel.length > 1 + ? _posLevel.slice(1).map((curr: any, index: number) => ({ + days: curr.days_diff ? Number(curr.days_diff) : 0, + name: + !_posLevel[index]?.positionType && _posLevel[index]?.positionCee + ? `ระดับ ${_posLevel[index]?.positionCee.trim()}` + : _posLevel[index]?.positionLevel, + })) + : []; + + const groupMapPosLevel = mapPosLevel.reduce( + (acc: any, curr: any) => { + let existing = acc.find((item: any) => item.name === curr.name); + + if (existing) { + existing.days += curr.days; + } else { + existing = { name: curr.name, days: curr.days }; + acc.push(existing); + } + + existing.year = Math.floor(existing.days / 365.2524); + existing.month = Math.floor((existing.days / 30.4375) % 12); + existing.day = Math.floor(existing.days % 30.4375); + + return acc; + }, + [] as { name: string; days: number; year: number; month: number; day: number }[], + ); + return new HttpSuccess({ - position: mapPosition, - posLevel: posLevel, + position: groupMapPosition, + posLevel: groupMapPosLevel, }); }