diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 38131ed8..f539e683 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -2795,4 +2795,200 @@ export class ProfileController extends Controller { }; return new HttpSuccess(_profile); } + + /** + * API ข้อมูลบุลคลที่มีอายุเกษียณราชการตามปี + * + * @summary ข้อมูลบุลคลที่มีอายุเกษียณราชการตามปี + * + */ + @Get("profileid/retire/{year}") + async getProfileByRetireYear(@Path() year: number) { + const profiles = await this.profileRepo + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.posLevel", "posLevel") + .leftJoinAndSelect("profile.posType", "posType") + .leftJoinAndSelect("profile.current_holders", "current_holders") + .leftJoinAndSelect("current_holders.orgRoot", "orgRoot") + .leftJoinAndSelect("current_holders.orgChild1", "orgChild1") + .leftJoinAndSelect("current_holders.orgChild2", "orgChild2") + .leftJoinAndSelect("current_holders.orgChild3", "orgChild3") + .leftJoinAndSelect("current_holders.orgChild4", "orgChild4") + .leftJoinAndSelect("current_holders.positions", "positions") + .leftJoinAndSelect("positions.posExecutive", "posExecutive") + .where("YEAR(profile.dateRetire) = :year", { year }) + .getMany(); + + if (!profiles || profiles.length === 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลที่มีอายุเกษียณราชการในปีนี้"); + } + + // const orgRevisionActive = await this.orgRevisionRepo.findOne({ + // where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, + // }); + + const findRevision = await this.orgRevisionRepo.findOne({ + where: { orgRevisionIsCurrent: true }, + }); + if (!findRevision) { + throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); + } + const formattedData = profiles.map((item) => { + const posMaster = + item.current_holders == null || + item.current_holders.length == 0 || + item.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null + ? null + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id); + const position = + posMaster == null || + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.positions == null || + item.current_holders?.find((x) => x.orgRevisionId == findRevision.id)?.positions.length == + 0 || + item.current_holders + .find((x) => x.orgRevisionId == findRevision.id) + ?.positions?.find((position) => position.positionIsSelected == true) == null + ? null + : item.current_holders + .find((x) => x.orgRevisionId == findRevision.id) + ?.positions?.find((position) => position.positionIsSelected == true); + const posExecutive = + position == null || + item.current_holders + .find((x) => x.orgRevisionId == findRevision.id) + ?.positions?.find((position) => position.positionIsSelected == true)?.posExecutive == + null || + item.current_holders + .find((x) => x.orgRevisionId == findRevision.id) + ?.positions?.find((position) => position.positionIsSelected == true)?.posExecutive + ?.posExecutiveName == null + ? null + : item.current_holders + .find((x) => x.orgRevisionId == findRevision.id) + ?.positions?.find((position) => position.positionIsSelected == true)?.posExecutive + .posExecutiveName; + const posExecutiveId = + position == null || position.posExecutive == null ? null : position.posExecutive.id; + + const shortName = + item.current_holders.length == 0 + ? null + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4 != + null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4.orgChild4ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3 != + null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3.orgChild3ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2 != + null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2.orgChild2ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgChild1 != null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild1.orgChild1ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgRoot != null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot.orgRootShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : null; + const root = + item.current_holders == null || + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot == null + ? null + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot; + const child1 = + item.current_holders == null || + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild1 == null + ? null + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild1; + const child2 = + item.current_holders == null || + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2 == null + ? null + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2; + const child3 = + item.current_holders == null || + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3 == null + ? null + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3; + const child4 = + item.current_holders == null || + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4 == null + ? null + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4; + + let node = null; + let nodeId = null; + let nodeShortName = null; + + if (root) { + node = 0; + nodeId = root.id; + nodeShortName = root.orgRootShortName; + } + if (child1) { + node = 1; + nodeId = child1.id; + nodeShortName = child1.orgChild1ShortName; + } + if (child2) { + node = 2; + nodeId = child2.id; + nodeShortName = child2.orgChild2ShortName; + } + if (child3) { + node = 3; + nodeId = child3.id; + nodeShortName = child3.orgChild3ShortName; + } + if (child4) { + node = 4; + nodeId = child4.id; + nodeShortName = child4.orgChild4ShortName; + } + + return { + profileId: item.id, + prefix: item.prefix, + rank: item.rank, + firstName: item.firstName, + lastName: item.lastName, + citizenId: item.citizenId, + root: root == null ? null : root.orgRootName, + rootId: root == null ? null : root.id, + rootShortName: root == null ? null : root.orgRootShortName, + child1: child1 == null ? null : child1.orgChild1Name, + child1Id: child1 == null ? null : child1.id, + child1ShortName: child1 == null ? null : child1.orgChild1ShortName, + child2: child2 == null ? null : child2.orgChild2Name, + child2Id: child2 == null ? null : child2.id, + child2ShortName: child2 == null ? null : child2.orgChild2ShortName, + child3: child3 == null ? null : child3.orgChild3Name, + child3Id: child3 == null ? null : child3.id, + child3ShortName: child3 == null ? null : child3.orgChild3ShortName, + child4: child4 == null ? null : child4.orgChild4Name, + child4Id: child4 == null ? null : child4.id, + child4ShortName: child4 == null ? null : child4.orgChild4ShortName, + dateRetire: item.dateRetire, + posLevelId: item.posLevel == null ? null : item.posLevel.id, + posLevelName: item.posLevel == null ? null : item.posLevel.posLevelName, + posLevelRank: item.posLevel == null ? null : item.posLevel.posLevelRank, + posTypeId: item.posType == null ? null : item.posType.id, + posTypeName: item.posType == null ? null : item.posType.posTypeName, + posTypeRank: item.posType == null ? null : item.posType.posTypeRank, + posNo: `${posMaster == null ? null : posMaster.posMasterNo}${shortName}`, + posMasterNo: posMaster == null ? null : posMaster.posMasterNo, + position: item.position, + posExecutiveId: posExecutiveId, + posExecutiveName: posExecutive, + node: node, + nodeId: nodeId, + nodeShortName: nodeShortName, + }; + }); + return new HttpSuccess(formattedData); + } } diff --git a/src/controllers/ProfileEmployeeController.ts b/src/controllers/ProfileEmployeeController.ts index 5e82667e..8e6b1e18 100644 --- a/src/controllers/ProfileEmployeeController.ts +++ b/src/controllers/ProfileEmployeeController.ts @@ -1991,4 +1991,196 @@ export class ProfileEmployeeController extends Controller { }; return new HttpSuccess(_profile); } + + /** + * API ข้อมูลบุลคลที่มีอายุเกษียณราชการตามปี + * + * @summary ข้อมูลบุลคลที่มีอายุเกษียณราชการตามปี + * + */ + @Get("profileid/retire/{year}") + async getProfileByRetireYear(@Path() year: number) { + const profiles = await this.profileRepo + .createQueryBuilder("profileEmployee") + .leftJoinAndSelect("profileEmployee.posLevel", "posLevel") + .leftJoinAndSelect("profileEmployee.posType", "posType") + .leftJoinAndSelect("profileEmployee.current_holders", "current_holders") + .leftJoinAndSelect("current_holders.orgRoot", "orgRoot") + .leftJoinAndSelect("current_holders.orgChild1", "orgChild1") + .leftJoinAndSelect("current_holders.orgChild2", "orgChild2") + .leftJoinAndSelect("current_holders.orgChild3", "orgChild3") + .leftJoinAndSelect("current_holders.orgChild4", "orgChild4") + .leftJoinAndSelect("current_holders.positions", "positions") + // .leftJoinAndSelect("positions.posExecutive", "posExecutive") + .where("YEAR(profileEmployee.dateRetire) = :year", { year }) + .getMany(); + + if (!profiles || profiles.length === 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลที่มีอายุเกษียณราชการในปีนี้"); + } + + const findRevision = await this.orgRevisionRepo.findOne({ + where: { orgRevisionIsCurrent: true }, + }); + if (!findRevision) { + throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); + } + const formattedData = profiles.map((item) => { + const posMaster = + item.current_holders == null || + item.current_holders.length == 0 || + item.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null + ? null + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id); + const position = + posMaster == null || + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.positions == null || + item.current_holders?.find((x) => x.orgRevisionId == findRevision.id)?.positions.length == + 0 || + item.current_holders + .find((x) => x.orgRevisionId == findRevision.id) + ?.positions?.find((position) => position.positionIsSelected == true) == null + ? null + : item.current_holders + .find((x) => x.orgRevisionId == findRevision.id) + ?.positions?.find((position) => position.positionIsSelected == true); + // const posExecutive = + // position == null || + // item.current_holders + // .find((x) => x.orgRevisionId == findRevision.id) + // ?.positions?.find((position) => position.positionIsSelected == true)?.posExecutive == + // null || + // item.current_holders + // .find((x) => x.orgRevisionId == findRevision.id) + // ?.positions?.find((position) => position.positionIsSelected == true)?.posExecutive + // ?.posExecutiveName == null + // ? null + // : item.current_holders + // .find((x) => x.orgRevisionId == findRevision.id) + // ?.positions?.find((position) => position.positionIsSelected == true)?.posExecutive + // .posExecutiveName; + // const posExecutiveId = + // position == null || position.posExecutive == null ? null : position.posExecutive.id; + + const shortName = + item.current_holders.length == 0 + ? null + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4 != + null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4.orgChild4ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3 != + null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3.orgChild3ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2 != + null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2.orgChild2ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgChild1 != null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild1.orgChild1ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgRoot != null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot.orgRootShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : null; + const root = + item.current_holders == null || + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot == null + ? null + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot; + const child1 = + item.current_holders == null || + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild1 == null + ? null + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild1; + const child2 = + item.current_holders == null || + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2 == null + ? null + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2; + const child3 = + item.current_holders == null || + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3 == null + ? null + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3; + const child4 = + item.current_holders == null || + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4 == null + ? null + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4; + + let node = null; + let nodeId = null; + let nodeShortName = null; + + if (root) { + node = 0; + nodeId = root.id; + nodeShortName = root.orgRootShortName; + } + if (child1) { + node = 1; + nodeId = child1.id; + nodeShortName = child1.orgChild1ShortName; + } + if (child2) { + node = 2; + nodeId = child2.id; + nodeShortName = child2.orgChild2ShortName; + } + if (child3) { + node = 3; + nodeId = child3.id; + nodeShortName = child3.orgChild3ShortName; + } + if (child4) { + node = 4; + nodeId = child4.id; + nodeShortName = child4.orgChild4ShortName; + } + + return { + profileId: item.id, + prefix: item.prefix, + rank: item.rank, + firstName: item.firstName, + lastName: item.lastName, + citizenId: item.citizenId, + root: root == null ? null : root.orgRootName, + rootId: root == null ? null : root.id, + rootShortName: root == null ? null : root.orgRootShortName, + child1: child1 == null ? null : child1.orgChild1Name, + child1Id: child1 == null ? null : child1.id, + child1ShortName: child1 == null ? null : child1.orgChild1ShortName, + child2: child2 == null ? null : child2.orgChild2Name, + child2Id: child2 == null ? null : child2.id, + child2ShortName: child2 == null ? null : child2.orgChild2ShortName, + child3: child3 == null ? null : child3.orgChild3Name, + child3Id: child3 == null ? null : child3.id, + child3ShortName: child3 == null ? null : child3.orgChild3ShortName, + child4: child4 == null ? null : child4.orgChild4Name, + child4Id: child4 == null ? null : child4.id, + child4ShortName: child4 == null ? null : child4.orgChild4ShortName, + dateRetire: item.dateRetire, + posLevelId: item.posLevel == null ? null : item.posLevel.id, + posLevelName: item.posLevel == null ? null : item.posLevel.posLevelName, + posLevelRank: item.posLevel == null ? null : item.posLevel.posLevelRank, + posTypeId: item.posType == null ? null : item.posType.id, + posTypeName: item.posType == null ? null : item.posType.posTypeName, + posTypeRank: item.posType == null ? null : item.posType.posTypeRank, + posNo: `${posMaster == null ? null : posMaster.posMasterNo}${shortName}`, + posMasterNo: posMaster == null ? null : posMaster.posMasterNo, + position: item.position, + // posExecutiveId: posExecutiveId, + // posExecutiveName: posExecutive, + node: node, + nodeId: nodeId, + nodeShortName: nodeShortName, + }; + }); + return new HttpSuccess(formattedData); + } }