diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index d2ccadf7..476e434b 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -2961,179 +2961,184 @@ export class PositionController extends Controller { return new HttpSuccess({ data: formattedData, total }); } - /** - * API ค้นหาคนตามโครงสร้าง + /** + * API ค้นหาคนตามโครงสร้าง * * @summary ค้นหาคนตามโครงสร้าง * */ - @Post("profile/search") - async searchProfile( - @Body() - body: { - isAll: boolean; - node: number; - nodeId: string; - }, - ) { - let typeCondition: any = {}; - - if (body.isAll == false) { - if (body.node === 0) { - typeCondition = { - orgRootId: body.nodeId, - orgChild1Id: IsNull(), - }; - } else if (body.node === 1) { - typeCondition = { - orgChild1Id: body.nodeId, - orgChild2Id: IsNull(), - }; - } else if (body.node === 2) { - typeCondition = { - orgChild2Id: body.nodeId, - orgChild3Id: IsNull(), - }; - } else if (body.node === 3) { - typeCondition = { - orgChild3Id: body.nodeId, - orgChild4Id: IsNull(), - }; - } else if (body.node === 4) { - typeCondition = { - orgChild4Id: body.nodeId, - }; - } - } else { - if (body.node === 0) { - typeCondition = { - orgRootId: body.nodeId, - }; - } else if (body.node === 1) { - typeCondition = { - orgChild1Id: body.nodeId, - }; - } else if (body.node === 2) { - typeCondition = { - orgChild2Id: body.nodeId, - }; - } else if (body.node === 3) { - typeCondition = { - orgChild3Id: body.nodeId, - }; - } else if (body.node === 4) { - typeCondition = { - orgChild4Id: body.nodeId, - }; - } - } - - 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") - .leftJoinAndSelect("posMaster.positions", "positions") - .leftJoinAndSelect("positions.posType", "posType") - .leftJoinAndSelect("positions.posLevel", "posLevel") - .where("posMaster.current_holderId IS NOT NULL") - .andWhere( + @Post("profile/search") + async searchProfile( + @Body() + body: { + isAll: boolean; + node: number; + nodeId: string; + }, + ) { + let typeCondition: any = {}; + + if (body.isAll == false) { + if (body.node === 0) { + typeCondition = { + orgRootId: body.nodeId, + orgChild1Id: IsNull(), + }; + } else if (body.node === 1) { + typeCondition = { + orgChild1Id: body.nodeId, + orgChild2Id: IsNull(), + }; + } else if (body.node === 2) { + typeCondition = { + orgChild2Id: body.nodeId, + orgChild3Id: IsNull(), + }; + } else if (body.node === 3) { + typeCondition = { + orgChild3Id: body.nodeId, + orgChild4Id: IsNull(), + }; + } else if (body.node === 4) { + typeCondition = { + orgChild4Id: body.nodeId, + }; + } + } else { + if (body.node === 0) { + typeCondition = { + orgRootId: body.nodeId, + }; + } else if (body.node === 1) { + typeCondition = { + orgChild1Id: body.nodeId, + }; + } else if (body.node === 2) { + typeCondition = { + orgChild2Id: body.nodeId, + }; + } else if (body.node === 3) { + typeCondition = { + orgChild3Id: body.nodeId, + }; + } else if (body.node === 4) { + typeCondition = { + orgChild4Id: body.nodeId, + }; + } + } + + 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") + .leftJoinAndSelect("posMaster.positions", "positions") + .leftJoinAndSelect("positions.posType", "posType") + .leftJoinAndSelect("positions.posLevel", "posLevel") + .where("posMaster.current_holderId IS NOT NULL") + .andWhere( new Brackets((qb) => { qb.orWhere(typeCondition); }), ) - .orderBy("posMaster.posMasterOrder", "ASC") - .getManyAndCount(); - + .orderBy("posMaster.posMasterOrder", "ASC") + .getManyAndCount(); - const formattedData = await Promise.all( - posMaster.map(async (posMaster) => { - 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; - } - - let node: any = null; - let nodeId: any = null; - if (posMaster.orgChild4Id != null) { - node = 4; - nodeId = posMaster.orgChild4Id; - } else if (posMaster.orgChild3Id != null) { - node = 3; - nodeId = posMaster.orgChild3Id; - } else if (posMaster.orgChild2Id != null) { - node = 2; - nodeId = posMaster.orgChild2Id; - } else if (posMaster.orgChild1Id != null) { - node = 1; - nodeId = posMaster.orgChild1Id; - } else if (posMaster.orgRootId != null) { - node = 0; - nodeId = posMaster.orgRootId; - } - const fullname = posMaster.current_holder.prefix + " " + posMaster.current_holder.firstName + " " + posMaster.current_holder.lastName; - return { - id: posMaster.id, - node: node, - nodeId: nodeId, - orgRootId: posMaster.orgRootId, - orgChild1Id: posMaster.orgChild1Id, - orgChild2Id: posMaster.orgChild2Id, - orgChild3Id: posMaster.orgChild3Id, - orgChild4Id: posMaster.orgChild4Id, - posMasterNoPrefix: posMaster.posMasterNoPrefix, - posMasterNo: posMaster.posMasterNo, - posMasterNoSuffix: posMaster.posMasterNoSuffix, - orgShortname: shortName, - isSit: posMaster.isSit, - name: fullname, - citizenId: posMaster.current_holder.citizenId, - position: posMaster.current_holder.position, - }; - }), - ); - return new HttpSuccess({ data: formattedData, total }); - } + const formattedData = await Promise.all( + posMaster.map(async (posMaster) => { + 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; + } + + let node: any = null; + let nodeId: any = null; + if (posMaster.orgChild4Id != null) { + node = 4; + nodeId = posMaster.orgChild4Id; + } else if (posMaster.orgChild3Id != null) { + node = 3; + nodeId = posMaster.orgChild3Id; + } else if (posMaster.orgChild2Id != null) { + node = 2; + nodeId = posMaster.orgChild2Id; + } else if (posMaster.orgChild1Id != null) { + node = 1; + nodeId = posMaster.orgChild1Id; + } else if (posMaster.orgRootId != null) { + node = 0; + nodeId = posMaster.orgRootId; + } + const fullname = + posMaster.current_holder.prefix + + " " + + posMaster.current_holder.firstName + + " " + + posMaster.current_holder.lastName; + return { + id: posMaster.id, + node: node, + nodeId: nodeId, + orgRootId: posMaster.orgRootId, + orgChild1Id: posMaster.orgChild1Id, + orgChild2Id: posMaster.orgChild2Id, + orgChild3Id: posMaster.orgChild3Id, + orgChild4Id: posMaster.orgChild4Id, + posMasterNoPrefix: posMaster.posMasterNoPrefix, + posMasterNo: posMaster.posMasterNo, + posMasterNoSuffix: posMaster.posMasterNoSuffix, + orgShortname: shortName, + isSit: posMaster.isSit, + name: fullname, + profileId: posMaster.current_holder.id, + citizenId: posMaster.current_holder.citizenId, + position: posMaster.current_holder.position, + }; + }), + ); + return new HttpSuccess({ data: formattedData, total }); + } }