diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 05160272..0c452608 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -34,6 +34,7 @@ import { OrgChild3 } from "../entities/OrgChild3"; import { OrgChild4 } from "../entities/OrgChild4"; import { Position } from "../entities/Position"; import { Profile } from "../entities/Profile"; +import { EmployeePosMaster } from "../entities/EmployeePosMaster"; @Route("api/v1/org/pos") @Tags("Position") @Security("bearerAuth") @@ -2511,9 +2512,9 @@ export class PositionController extends Controller { } /** - * API ค้นหาตำแหน่งในระบบสมัครสอบ + * API ค้นหาตำแหน่งในระบบสมัครสอบ ขรก. * - * @summary ค้นหาตำแหน่งในระบบสมัครสอบ + * @summary ค้นหาตำแหน่งในระบบสมัครสอบ ขรก. * */ @Post("placement/search") @@ -2734,4 +2735,229 @@ export class PositionController extends Controller { ); return new HttpSuccess({ data: formattedData, total }); } + + /** + * API ค้นหาตำแหน่งในระบบสมัครสอบ ลูกจ้าง + * + * @summary ค้นหาตำแหน่งในระบบสมัครสอบ ลูกจ้าง + * + */ + @Post("placementemp/search") + async searchPlacementEmp( + @Body() + body: { + node: number; + nodeId: string; + position: string; + typeCommand: string | null; + posType: string; + posLevel: string; + isAll: boolean; + isBlank: boolean; + }, + ) { + let typeCondition: any = {}; + let conditionA: any = null; + + 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 = "positions.posTypeId LIKE :posType AND positions.posLevelId LIKE :posLevel"; + } else if (body.typeCommand == "APPOINT") { + conditionA = "posType.posTypeRank > :posTypeRank"; + } else if (body.typeCommand == "SLIP") { + conditionA = "positions.posTypeId LIKE :posType AND posLevel.posLevelRank > :posLevelRank"; + } + + if (body.isAll == false) { + if (body.node === 0) { + typeCondition = { + orgRootId: body.nodeId, + orgChild1Id: IsNull(), + current_holderId: body.isBlank == true ? IsNull() : Not(IsNull()), + }; + } else if (body.node === 1) { + typeCondition = { + orgChild1Id: body.nodeId, + orgChild2Id: IsNull(), + current_holderId: body.isBlank == true ? IsNull() : Not(IsNull()), + }; + } else if (body.node === 2) { + typeCondition = { + orgChild2Id: body.nodeId, + orgChild3Id: IsNull(), + current_holderId: body.isBlank == true ? IsNull() : Not(IsNull()), + }; + } else if (body.node === 3) { + typeCondition = { + orgChild3Id: body.nodeId, + orgChild4Id: IsNull(), + current_holderId: body.isBlank == true ? IsNull() : Not(IsNull()), + }; + } else if (body.node === 4) { + typeCondition = { + orgChild4Id: body.nodeId, + current_holderId: body.isBlank == true ? IsNull() : Not(IsNull()), + }; + } + } else { + if (body.node === 0) { + typeCondition = { + orgRootId: body.nodeId, + current_holderId: body.isBlank == true ? IsNull() : Not(IsNull()), + }; + } else if (body.node === 1) { + typeCondition = { + orgChild1Id: body.nodeId, + current_holderId: body.isBlank == true ? IsNull() : Not(IsNull()), + }; + } else if (body.node === 2) { + typeCondition = { + orgChild2Id: body.nodeId, + current_holderId: body.isBlank == true ? IsNull() : Not(IsNull()), + }; + } else if (body.node === 3) { + typeCondition = { + orgChild3Id: body.nodeId, + current_holderId: body.isBlank == true ? IsNull() : Not(IsNull()), + }; + } else if (body.node === 4) { + typeCondition = { + orgChild4Id: body.nodeId, + current_holderId: body.isBlank == true ? IsNull() : Not(IsNull()), + }; + } + } + + const [posMaster, total] = await AppDataSource.getRepository(EmployeePosMaster) + .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") + // .leftJoinAndSelect("positions.posExecutive", "posExecutive") + .andWhere( + new Brackets((qb) => { + qb.andWhere(typeCondition).andWhere(conditionA == null ? "1=1" : conditionA, { + posType: `${body.posType}`, + posLevel: `${body.posLevel}`, + posTypeRank: posType == null ? 0 : posType.posTypeRank, + posLevelRank: posLevel == null ? 0 : posLevel.posLevelRank, + }); + }), + ) + .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; + } + + 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, + isPosition: posMaster.positions.filter((x) => x.positionName == body.position).length > 0, + positions: posMaster.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 }); + } }