From 5d2ab9269db01e3cafa76c8e82fd5518e4979aa9 Mon Sep 17 00:00:00 2001 From: Kittapath Date: Tue, 30 Apr 2024 00:09:32 +0700 Subject: [PATCH] search placment --- src/controllers/PositionController.ts | 163 ++++++++++++++++++++++++-- 1 file changed, 151 insertions(+), 12 deletions(-) diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index b34b8390..c4070b81 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -24,7 +24,7 @@ import { PosType } from "../entities/PosType"; import { PosLevel } from "../entities/PosLevel"; import { CreatePosDict, CreatePosDictExe, PosDict, UpdatePosDict } from "../entities/PosDict"; import HttpError from "../interfaces/http-error"; -import { Equal, ILike, In, IsNull, Like, Not, Brackets,MoreThan } from "typeorm"; +import { Equal, ILike, In, IsNull, Like, Not, Brackets, MoreThan } from "typeorm"; import { CreatePosMaster, PosMaster } from "../entities/PosMaster"; import { OrgRevision } from "../entities/OrgRevision"; import { OrgRoot } from "../entities/OrgRoot"; @@ -828,7 +828,7 @@ export class PositionController extends Controller { orgChild4: { orgChild4ShortName: SName }, posMasterNo: requestBody.posMasterNo, id: Not(posMaster.id), - orgRevisionId: posMaster.orgRevisionId + orgRevisionId: posMaster.orgRevisionId, }, }); if (chk_SName4 != null) { @@ -844,7 +844,7 @@ export class PositionController extends Controller { posMasterNo: requestBody.posMasterNo, orgChild4Id: IsNull(), id: Not(posMaster.id), - orgRevisionId: posMaster.orgRevisionId + orgRevisionId: posMaster.orgRevisionId, }, }); if (chk_SName3 != null) { @@ -860,7 +860,7 @@ export class PositionController extends Controller { posMasterNo: requestBody.posMasterNo, orgChild3Id: IsNull(), id: Not(posMaster.id), - orgRevisionId: posMaster.orgRevisionId + orgRevisionId: posMaster.orgRevisionId, }, }); if (chk_SName2 != null) { @@ -876,7 +876,7 @@ export class PositionController extends Controller { posMasterNo: requestBody.posMasterNo, orgChild2Id: IsNull(), id: Not(posMaster.id), - orgRevisionId: posMaster.orgRevisionId + orgRevisionId: posMaster.orgRevisionId, }, }); if (chk_SName1 != null) { @@ -892,7 +892,7 @@ export class PositionController extends Controller { posMasterNo: requestBody.posMasterNo, orgChild1Id: IsNull(), id: Not(posMaster.id), - orgRevisionId: posMaster.orgRevisionId + orgRevisionId: posMaster.orgRevisionId, }, }); if (chk_SName0 != null) { @@ -2662,22 +2662,30 @@ export class PositionController extends Controller { ) { let typeCondition: any = {}; let conditionA: any = {}; - let posTypeRank: any = {}; - posTypeRank = await this.posTypeRepository.findOne({ - where: { id: String(posTypeRank?.posTypeId) }, + let posType = await this.posTypeRepository.findOne({ + where: { id: String(body.posType) }, }); + let posLevel = await this.posLevelRepository.findOne({ + where: { id: String(body.posLevel) }, + }); + if (body.typeCommand == "APPOINTED" || body.typeCommand == "MOVE") { conditionA = { posTypeId: body.posType, posLevelId: body.posLevel, }; } else if (body.typeCommand == "APPOINT") { + conditionA = { + posType: { + posTypeRank: MoreThan(posType == null ? 0 : posType.posTypeRank), + }, + }; + } else if (body.typeCommand == "SLIP") { conditionA = { posTypeId: body.posType, - posLevel: { - posTypeRank: MoreThan(posTypeRank), + posLevelRank: MoreThan(posLevel == null ? 0 : posLevel.posLevelRank), }, }; } @@ -2723,5 +2731,136 @@ export class PositionController extends Controller { }, }; } -} + 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, + isSpecial: position.isSpecial, + })), + }; + }), + ); + return new HttpSuccess({ data: formattedData, total }); + } +}