From 5df57fc4d69b5a29c62b48db3d583d91cb0fe150 Mon Sep 17 00:00:00 2001 From: Kittapath Date: Tue, 20 Feb 2024 13:35:11 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=80=E0=B8=8A=E0=B9=87=E0=B8=84=E0=B8=95?= =?UTF-8?q?=E0=B8=B3=E0=B9=81=E0=B8=AB=E0=B8=99=E0=B9=88=E0=B8=87=E0=B8=8B?= =?UTF-8?q?=E0=B9=89=E0=B8=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/OrganizationController.ts | 69 +++++ src/controllers/PositionController.ts | 326 ++++++++++++++++++++++ src/controllers/ReportController.ts | 22 +- 3 files changed, 408 insertions(+), 9 deletions(-) diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index 4f2ecdfb..dea9aacc 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -2762,4 +2762,73 @@ export class OrganizationController extends Controller { return error; } } + + /** + * API เช็ค node + * + * @summary เช็ค node (ADMIN) + * + */ + @Post("find/node") + async findNodeAll(@Body() requestBody: { node: number; nodeId: string }) { + try { + switch (requestBody.node) { + case 0: { + const data = await this.orgRootRepository.findOne({ + where: { id: requestBody.nodeId }, + }); + if (data == null) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found rootId."); + } + return new HttpSuccess([data.id]); + } + case 1: { + const data = await this.child1Repository.findOne({ + where: { id: requestBody.nodeId }, + }); + if (data == null) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child1."); + } + return new HttpSuccess([data.orgRootId, data.id]); + } + case 2: { + const data = await this.child2Repository.findOne({ + where: { id: requestBody.nodeId }, + }); + if (data == null) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child2."); + } + return new HttpSuccess([data.orgRootId, data.orgChild1Id, data.id]); + } + case 3: { + const data = await this.child3Repository.findOne({ + where: { id: requestBody.nodeId }, + }); + if (data == null) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child3."); + } + return new HttpSuccess([data.orgRootId, data.orgChild1Id, data.orgChild2Id, data.id]); + } + case 4: { + const data = await this.child4Repository.findOne({ + where: { id: requestBody.nodeId }, + }); + if (data == null) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child4."); + } + return new HttpSuccess([ + data.orgRootId, + data.orgChild1Id, + data.orgChild2Id, + data.orgChild3Id, + data.id, + ]); + } + default: + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found type: " + requestBody.node); + } + } catch (error) { + return error; + } + } } diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 70a53c93..2cafa7d1 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -356,6 +356,19 @@ export class PositionController extends Controller { }, order: { posMasterOrder: "DESC" }, }); + const dataDup: any = await this.posMasterRepository.findOne({ + where: { + orgChild4Id: orgChild4.id, + posMasterNoPrefix: requestBody.posMasterNoPrefix, + posMasterNo: requestBody.posMasterNo, + posMasterNoSuffix: requestBody.posMasterNoSuffix, + }, + }); + if (dataDup != null) + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + ); posMaster.posMasterOrder = order !== null && order !== undefined && order.posMasterOrder ? order.posMasterOrder + 1 @@ -375,6 +388,20 @@ export class PositionController extends Controller { }, order: { posMasterOrder: "DESC" }, }); + const dataDup: any = await this.posMasterRepository.findOne({ + where: { + orgChild3Id: orgChild3.id, + orgChild4Id: IsNull() || "", + posMasterNoPrefix: requestBody.posMasterNoPrefix, + posMasterNo: requestBody.posMasterNo, + posMasterNoSuffix: requestBody.posMasterNoSuffix, + }, + }); + if (dataDup != null) + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + ); posMaster.posMasterOrder = order !== null && order !== undefined && order.posMasterOrder ? order.posMasterOrder + 1 @@ -394,6 +421,21 @@ export class PositionController extends Controller { }, order: { posMasterOrder: "DESC" }, }); + const dataDup: any = await this.posMasterRepository.findOne({ + where: { + orgChild2Id: orgChild2.id, + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + posMasterNoPrefix: requestBody.posMasterNoPrefix, + posMasterNo: requestBody.posMasterNo, + posMasterNoSuffix: requestBody.posMasterNoSuffix, + }, + }); + if (dataDup != null) + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + ); posMaster.posMasterOrder = order !== null && order !== undefined && order.posMasterOrder ? order.posMasterOrder + 1 @@ -413,6 +455,22 @@ export class PositionController extends Controller { }, order: { posMasterOrder: "DESC" }, }); + const dataDup: any = await this.posMasterRepository.findOne({ + where: { + orgChild1Id: orgChild1.id, + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + posMasterNoPrefix: requestBody.posMasterNoPrefix, + posMasterNo: requestBody.posMasterNo, + posMasterNoSuffix: requestBody.posMasterNoSuffix, + }, + }); + if (dataDup != null) + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + ); posMaster.posMasterOrder = order !== null && order !== undefined && order.posMasterOrder ? order.posMasterOrder + 1 @@ -432,6 +490,23 @@ export class PositionController extends Controller { }, order: { posMasterOrder: "DESC" }, }); + const dataDup: any = await this.posMasterRepository.findOne({ + where: { + orgRootId: orgRoot.id, + orgChild1Id: IsNull() || "", + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + posMasterNoPrefix: requestBody.posMasterNoPrefix, + posMasterNo: requestBody.posMasterNo, + posMasterNoSuffix: requestBody.posMasterNoSuffix, + }, + }); + if (dataDup != null) + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + ); posMaster.posMasterOrder = order !== null && order !== undefined && order.posMasterOrder ? order.posMasterOrder + 1 @@ -546,6 +621,20 @@ export class PositionController extends Controller { if (!orgChild4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงสร้าง"); } else { + const dataDup: any = await this.posMasterRepository.findOne({ + where: { + orgChild3Id: orgChild3.id, + orgChild4Id: IsNull() || "", + posMasterNoPrefix: requestBody.posMasterNoPrefix, + posMasterNo: requestBody.posMasterNo, + posMasterNoSuffix: requestBody.posMasterNoSuffix, + }, + }); + if (dataDup != null) + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + ); posMaster.orgRootId = orgChild4.orgRootId; posMaster.orgChild1Id = orgChild4.orgChild1Id; posMaster.orgChild2Id = orgChild4.orgChild2Id; @@ -554,6 +643,21 @@ export class PositionController extends Controller { posMaster.orgRevisionId = orgChild4.orgRevisionId; } } else { + const dataDup: any = await this.posMasterRepository.findOne({ + where: { + orgChild2Id: orgChild2.id, + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + posMasterNoPrefix: requestBody.posMasterNoPrefix, + posMasterNo: requestBody.posMasterNo, + posMasterNoSuffix: requestBody.posMasterNoSuffix, + }, + }); + if (dataDup != null) + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + ); posMaster.orgRootId = orgChild3.orgRootId; posMaster.orgChild1Id = orgChild3.orgChild1Id; posMaster.orgChild2Id = orgChild3.orgChild2Id; @@ -561,17 +665,67 @@ export class PositionController extends Controller { posMaster.orgRevisionId = orgChild3.orgRevisionId; } } else { + const dataDup: any = await this.posMasterRepository.findOne({ + where: { + orgChild1Id: orgChild1.id, + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + posMasterNoPrefix: requestBody.posMasterNoPrefix, + posMasterNo: requestBody.posMasterNo, + posMasterNoSuffix: requestBody.posMasterNoSuffix, + }, + }); + if (dataDup != null) + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + ); posMaster.orgRootId = orgChild2.orgRootId; posMaster.orgChild1Id = orgChild2.orgChild1Id; posMaster.orgChild2Id = orgChild2.id; posMaster.orgRevisionId = orgChild2.orgRevisionId; } } else { + const dataDup: any = await this.posMasterRepository.findOne({ + where: { + orgRootId: orgRoot.id, + orgChild1Id: IsNull() || "", + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + posMasterNoPrefix: requestBody.posMasterNoPrefix, + posMasterNo: requestBody.posMasterNo, + posMasterNoSuffix: requestBody.posMasterNoSuffix, + }, + }); + if (dataDup != null) + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + ); posMaster.orgRootId = orgChild1.orgRootId; posMaster.orgChild1Id = orgChild1.id; posMaster.orgRevisionId = orgChild1.orgRevisionId; } } else { + const dataDup: any = await this.posMasterRepository.findOne({ + where: { + orgRootId: orgRoot.id, + orgChild1Id: IsNull() || "", + orgChild2Id: IsNull() || "", + orgChild3Id: IsNull() || "", + orgChild4Id: IsNull() || "", + posMasterNoPrefix: requestBody.posMasterNoPrefix, + posMasterNo: requestBody.posMasterNo, + posMasterNoSuffix: requestBody.posMasterNoSuffix, + }, + }); + if (dataDup != null) + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", + ); posMaster.orgRootId = orgRoot.id; posMaster.orgRevisionId = orgRoot.orgRevisionId; } @@ -1933,4 +2087,176 @@ export class PositionController extends Controller { return error; } } + + /** + * API ค้นหาตำแหน่งในระบบสมัครสอบ + * + * @summary ค้นหาตำแหน่งในระบบสมัครสอบ + * + */ + @Post("placement/search") + async searchPlacement(@Body() body: { node: number; nodeId: string; position: string }) { + let typeCondition: any = {}; + + if (body.node === 0) { + typeCondition = { + orgRootId: body.nodeId, + orgChild1Id: IsNull(), + next_holderId: IsNull(), + }; + } else if (body.node === 1) { + typeCondition = { + orgChild1Id: body.nodeId, + orgChild2Id: IsNull(), + next_holderId: IsNull(), + }; + } else if (body.node === 2) { + typeCondition = { + orgChild2Id: body.nodeId, + orgChild3Id: IsNull(), + next_holderId: IsNull(), + }; + } else if (body.node === 3) { + typeCondition = { + orgChild3Id: body.nodeId, + orgChild4Id: IsNull(), + next_holderId: IsNull(), + }; + } else if (body.node === 4) { + typeCondition = { + orgChild4Id: body.nodeId, + next_holderId: IsNull(), + }; + } + + const [posMaster, total] = await AppDataSource.getRepository(PosMaster) + .createQueryBuilder("posMaster") + .leftJoinAndSelect("posMaster.orgRoot", "orgRoot") + .leftJoinAndSelect("posMaster.orgChild1", "orgChild1") + .leftJoinAndSelect("posMaster.orgChild2", "orgChild2") + .leftJoinAndSelect("posMaster.orgChild3", "orgChild3") + .leftJoinAndSelect("posMaster.orgChild4", "orgChild4") + .leftJoinAndSelect("posMaster.current_holder", "current_holder") + .leftJoinAndSelect("posMaster.next_holder", "next_holder") + .where(typeCondition) + .orderBy("posMaster.posMasterOrder", "ASC") + .getManyAndCount(); + + const formattedData = await Promise.all( + posMaster.map(async (posMaster) => { + const positions = await this.positionRepository.find({ + where: { + posMasterId: posMaster.id, + }, + relations: ["posLevel", "posType", "posExecutive"], + }); + + let profile: any; + const chkRevision = await this.orgRevisionRepository.findOne({ + where: { id: posMaster.orgRevisionId }, + }); + if (chkRevision?.orgRevisionIsCurrent && !chkRevision?.orgRevisionIsDraft) { + profile = await this.profileRepository.findOne({ + where: { id: String(posMaster.current_holderId) }, + }); + } else if (!chkRevision?.orgRevisionIsCurrent && chkRevision?.orgRevisionIsDraft) { + profile = await this.profileRepository.findOne({ + where: { id: String(posMaster.next_holderId) }, + }); + } + const type = await this.posTypeRepository.findOne({ + where: { id: String(profile?.posTypeId) }, + }); + const level = await this.posLevelRepository.findOne({ + where: { id: String(profile?.posLevelId) }, + }); + + let shortName = ""; + + if ( + posMaster.orgRootId !== null && + posMaster.orgChild1Id == null && + posMaster.orgChild2Id == null && + posMaster.orgChild3Id == null + ) { + body.node = 0; + shortName = posMaster.orgRoot.orgRootShortName; + } else if ( + posMaster.orgRootId !== null && + posMaster.orgChild1Id !== null && + posMaster.orgChild2Id == null && + posMaster.orgChild3Id == null + ) { + body.node = 1; + shortName = posMaster.orgChild1.orgChild1ShortName; + } else if ( + posMaster.orgRootId !== null && + posMaster.orgChild1Id !== null && + posMaster.orgChild2Id !== null && + posMaster.orgChild3Id == null + ) { + body.node = 2; + shortName = posMaster.orgChild2.orgChild2ShortName; + } else if ( + posMaster.orgRootId !== null && + posMaster.orgChild1Id !== null && + posMaster.orgChild2Id !== null && + posMaster.orgChild3Id !== null + ) { + body.node = 3; + shortName = posMaster.orgChild3.orgChild3ShortName; + } else if ( + posMaster.orgRootId !== null && + posMaster.orgChild1Id !== null && + posMaster.orgChild2Id !== null && + posMaster.orgChild3Id !== null + ) { + body.node = 4; + shortName = posMaster.orgChild4.orgChild4ShortName; + } + + return { + id: posMaster.id, + orgRootId: posMaster.orgRootId, + orgChild1Id: posMaster.orgChild1Id, + orgChild2Id: posMaster.orgChild2Id, + orgChild3Id: posMaster.orgChild3Id, + orgChild4Id: posMaster.orgChild4Id, + posMasterNoPrefix: posMaster.posMasterNoPrefix, + posMasterNo: posMaster.posMasterNo, + posMasterNoSuffix: posMaster.posMasterNoSuffix, + fullNameCurrentHolder: + posMaster.current_holder == null + ? null + : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}`, + fullNameNextHolder: + posMaster.next_holder == null + ? null + : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, + orgShortname: shortName, + isSit: posMaster.isSit, + profilePosition: profile == null || profile.position == null ? null : profile.position, + profilePostype: type == null || type.posTypeName == null ? null : type.posTypeName, + profilePoslevel: level == null || level.posLevelName == null ? null : level.posLevelName, + isPosition: positions.filter((x) => x.positionName == body.position).length > 0, + positions: positions.map((position) => ({ + id: position.id, + positionName: position.positionName, + positionField: position.positionField, + posTypeId: position.posTypeId, + posTypeName: position.posType == null ? null : position.posType.posTypeName, + posLevelId: position.posLevelId, + posLevelName: position.posLevel == null ? null : position.posLevel.posLevelName, + posExecutiveId: position.posExecutiveId, + posExecutiveName: + position.posExecutive == null ? null : position.posExecutive.posExecutiveName, + positionExecutiveField: position.positionExecutiveField, + positionArea: position.positionArea, + positionIsSelected: position.positionIsSelected, + })), + }; + }), + ); + return new HttpSuccess({ data: formattedData, total }); + } } diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index c29dac89..2c2f7226 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -140,9 +140,10 @@ export class ReportController extends Controller { 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.orgChild1 == null) { + if (posMaster.orgChild1Id == null) { const positionName = [...new Set(posMaster.positions.map((x) => x.positionName))]; const posType = [ ...new Set( @@ -237,6 +238,7 @@ export class ReportController extends Controller { )) { await Promise.all( orgChild1.posMasters + .filter((x) => x.orgChild2Id == null) .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { if (posMaster.orgChild2Id == null) { @@ -336,9 +338,10 @@ export class ReportController extends Controller { )) { await Promise.all( orgChild2.posMasters + .filter((x) => x.orgChild3Id == null) .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { - if (posMaster.orgChild3 == null) { + if (posMaster.orgChild3Id == null) { const positionName = [ ...new Set(posMaster.positions.map((x) => x.positionName)), ]; @@ -438,9 +441,10 @@ export class ReportController extends Controller { )) { await Promise.all( orgChild3.posMasters + .filter((x) => x.orgChild4Id == null) .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { - if (posMaster.orgChild3 == null) { + if (posMaster.orgChild4Id == null) { const positionName = [ ...new Set(posMaster.positions.map((x) => x.positionName)), ]; @@ -846,7 +850,7 @@ export class ReportController extends Controller { orgRoot.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { - if (posMaster.orgChild1 == null) { + if (posMaster.orgChild1Id == null) { const positionName = [...new Set(posMaster.positions.map((x) => x.positionName))]; const posType = [ ...new Set( @@ -1427,7 +1431,7 @@ export class ReportController extends Controller { orgChild2.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { - if (posMaster.orgChild3 == null) { + if (posMaster.orgChild3Id == null) { const positionName = [ ...new Set(posMaster.positions.map((x) => x.positionName)), ]; @@ -1721,7 +1725,7 @@ export class ReportController extends Controller { orgChild3.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { - if (posMaster.orgChild3 == null) { + if (posMaster.orgChild4Id == null) { const positionName = [ ...new Set(posMaster.positions.map((x) => x.positionName)), ]; @@ -2515,7 +2519,7 @@ export class ReportController extends Controller { orgRoot.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { - if (posMaster.orgChild1 == null && posMaster.next_holder != null) { + if (posMaster.orgChild1Id == null && posMaster.next_holder != null) { let positionMasterProfileOld: any = null; if (posMaster.next_holder != null) { positionMasterProfileOld = posMaster.next_holder.current_holders.find( @@ -2780,7 +2784,7 @@ export class ReportController extends Controller { orgChild2.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { - if (posMaster.orgChild3 == null && posMaster.next_holder != null) { + if (posMaster.orgChild3Id == null && posMaster.next_holder != null) { let positionMasterProfileOld: any = null; if (posMaster.next_holder != null) { positionMasterProfileOld = posMaster.next_holder.current_holders.find( @@ -2913,7 +2917,7 @@ export class ReportController extends Controller { orgChild3.posMasters .sort((a, b) => a.posMasterOrder - b.posMasterOrder) .map(async (posMaster) => { - if (posMaster.orgChild3 == null && posMaster.next_holder != null) { + if (posMaster.orgChild4Id == null && posMaster.next_holder != null) { let positionMasterProfileOld: any = null; if (posMaster.next_holder != null) { positionMasterProfileOld = posMaster.next_holder.current_holders.find(