From b413fbe1939866e1d121bcf18f9447fccbaaca5e Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Tue, 20 Feb 2024 11:55:11 +0700 Subject: [PATCH] =?UTF-8?q?org=5F069=20=E0=B8=84=E0=B9=89=E0=B8=99?= =?UTF-8?q?=E0=B8=AB=E0=B8=B2=E0=B8=9C=E0=B8=B9=E0=B9=89=E0=B8=9A=E0=B8=B1?= =?UTF-8?q?=E0=B8=87=E0=B8=84=E0=B8=B1=E0=B8=9A=E0=B8=9A=E0=B8=B1=E0=B8=8D?= =?UTF-8?q?=E0=B8=8A=E0=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/ProfileController.ts | 164 +++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 30d86c80..7462474a 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -486,4 +486,168 @@ export class ProfileController extends Controller { throw new Error(error); } } + + /** + * API ค้นหาผู้บังคับบัญชา + * + * @summary ORG_069 - ค้นหาผู้บังคับบัญชา (ADMIN) #75 + * + */ + @Get("search/commander") + async searchCommander(@Request() request: { user: Record }) { + let fullName_: any = {}; + let position_: any = {}; + let commanderAboveFullname_: any = {}; + let commanderAbovePosition_: any = {}; + let commanderFullname_: any = {}; + let commanderPosition_: any = {}; + + const findProfile = await this.profileRepository.findOne({ + where: { keycloak: request.user.sub }, + }); + + const findRevision = await this.orgRevisionRepository.findOne({ + where: { + orgRevisionIsCurrent: true, + }, + }); + + const findPosMaster = await this.posMasterRepository.findOne({ + where: { + current_holderId: findProfile?.id, + orgRevisionId: findRevision?.id, + }, + }); + + let node = 4; + let childId = findPosMaster?.orgChild4Id; + let condition: any = { orgChild4Id: childId }; + + if (findPosMaster?.orgChild4Id == null && findPosMaster?.orgChild3Id != null) { + node = 3; + childId = findPosMaster?.orgChild3Id; + condition = { orgChild3Id: childId, orgChild4Id: IsNull() }; + } else if (findPosMaster?.orgChild3Id == null && findPosMaster?.orgChild2Id != null) { + node = 2; + childId = findPosMaster?.orgChild2Id; + condition = { orgChild2Id: childId, orgChild3Id: IsNull() }; + } else if (findPosMaster?.orgChild2Id == null && findPosMaster?.orgChild1Id != null) { + node = 1; + childId = findPosMaster?.orgChild1Id; + condition = { orgChild1Id: childId, orgChild2Id: IsNull() }; + } else if (findPosMaster?.orgChild1Id == null) { + node = 0; + childId = findPosMaster?.orgRootId; + condition = { orgRootId: childId, orgChild1Id: IsNull() }; + } + + const findCmd = await this.posMasterRepository.findOne({ + where: { + current_holderId: Not(IsNull()) || Not(""), + orgRevisionId: findRevision?.id, + ...condition, + }, + relations: ["current_holder"], + order: { posMasterOrder: "ASC" }, + }); + let findOSAB: PosMaster | null = null; + let findTSAB: PosMaster | null = null; + //หาผู้บังคับบัญชาที่เหนือขึ้นไปอีก 1 ขั้น + if (node !== 0) { + findOSAB = await AppDataSource.getRepository(PosMaster) + .createQueryBuilder("posMaster") + .leftJoinAndSelect("posMaster.current_holder", "current_holder") + .where("posMaster.current_holderId IS NOT NULL") + .andWhere("posMaster.orgRevisionId = :revisionId", { revisionId: findRevision?.id }) + .andWhere( + new Brackets((qb) => { + if (node === 4) { + qb.andWhere("posMaster.orgChild4Id IS NULL"); + qb.andWhere("posMaster.orgChild3Id = :childId", { + childId: findPosMaster?.orgChild3Id, + }); + } else if (node === 3) { + qb.andWhere("posMaster.orgChild3Id IS NULL"); + qb.andWhere("posMaster.orgChild2Id = :childId", { + childId: findPosMaster?.orgChild2Id, + }); + } else if (node === 2) { + qb.andWhere("posMaster.orgChild2Id IS NULL"); + qb.andWhere("posMaster.orgChild1Id = :childId", { + childId: findPosMaster?.orgChild1Id, + }); + } else if (node === 1) { + qb.andWhere("posMaster.orgChild1Id IS NULL"); + qb.andWhere("posMaster.orgRootId = :childId", { childId: findPosMaster?.orgRootId }); + } + }), + ) + .orderBy("posMaster.posMasterOrder", "ASC") + .getOne(); + } + + //หาผู้บังคับบัญชาที่เหนือขึ้นไปอีก 2 ขั้น + if (node !== 0 && node !== 1) { + findTSAB = await AppDataSource.getRepository(PosMaster) + .createQueryBuilder("posMaster") + .leftJoinAndSelect("posMaster.current_holder", "current_holder") + .where("posMaster.current_holderId IS NOT NULL") + .andWhere("posMaster.orgRevisionId = :revisionId", { revisionId: findRevision?.id }) + .andWhere( + new Brackets((qb) => { + if (node === 4) { + qb.andWhere("posMaster.orgChild3Id IS NULL"); + qb.andWhere("posMaster.orgChild2Id = :childId", { + childId: findPosMaster?.orgChild2Id, + }); + } else if (node === 3) { + qb.andWhere("posMaster.orgChild2Id IS NULL"); + qb.andWhere("posMaster.orgChild1Id = :childId", { + childId: findPosMaster?.orgChild1Id, + }); + } else if (node === 2) { + qb.andWhere("posMaster.orgChild1Id IS NULL"); + qb.andWhere("posMaster.orgRootId = :childId", { + childId: findPosMaster?.orgRootId, + }); + } + }), + ) + .orderBy("posMaster.posMasterOrder", "ASC") + .getOne(); + } + fullName_ = (findProfile?.prefix ?? "") + (findProfile?.firstName ?? "") + " " + (findProfile?.lastName ?? ""); + position_ = findProfile?.position ?? ""; + commanderFullname_ = (findCmd?.current_holder?.prefix ?? "") + (findCmd?.current_holder?.firstName ?? "") + " " + (findCmd?.current_holder?.lastName ?? ""); + commanderPosition_ = findCmd?.current_holder?.position ?? ""; + commanderAboveFullname_ = (findOSAB?.current_holder?.prefix ?? "") + (findOSAB?.current_holder?.firstName ?? "") + " " + (findOSAB?.current_holder?.lastName ?? ""); + commanderAbovePosition_ = findOSAB?.current_holder?.position ?? ""; + + if (findCmd?.current_holderId == findProfile?.id) { + commanderFullname_ = (findOSAB?.current_holder?.prefix ?? "") + (findOSAB?.current_holder?.firstName ?? "") + " " + (findOSAB?.current_holder?.lastName ?? ""); + commanderPosition_ = findOSAB?.current_holder?.position ?? ""; + commanderAboveFullname_ = (findTSAB?.current_holder?.prefix ?? "") + (findTSAB?.current_holder?.firstName ?? "") + " " + (findTSAB?.current_holder?.lastName ?? ""); + commanderAbovePosition_ = findTSAB?.current_holder?.position ?? ""; + + const formattedDataTSAB = { + fullname: fullName_, + position: position_, + commanderAboveFullname: commanderAboveFullname_, + commanderAbovePosition: commanderAbovePosition_, + commanderFullname: commanderFullname_, + commanderPosition: commanderPosition_, + }; + return new HttpSuccess(formattedDataTSAB); + } + + const formattedData = { + fullname: fullName_, + position: position_, + commanderAboveFullname: commanderAboveFullname_, + commanderAbovePosition: commanderAbovePosition_, + commanderFullname: commanderFullname_, + commanderPosition: commanderPosition_, + }; + return new HttpSuccess(formattedData); + } }