diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index e2bd35ba..d799681e 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -4136,14 +4136,9 @@ export class ProfileController extends Controller { .leftJoinAndSelect("current_holders.orgChild2", "orgChild2") .leftJoinAndSelect("current_holders.orgChild3", "orgChild3") .leftJoinAndSelect("current_holders.orgChild4", "orgChild4") - .where( - node && nodeId - ? "current_holders.orgRevisionId = :orgRevisionId" - : "1=1", - { - orgRevisionId: node && nodeId ? findRevision.id : undefined, - } - ) + .where(node && nodeId ? "current_holders.orgRevisionId = :orgRevisionId" : "1=1", { + orgRevisionId: node && nodeId ? findRevision.id : undefined, + }) .andWhere( _data.root != undefined && _data.root != null ? _data.root[0] != null @@ -4483,6 +4478,74 @@ export class ProfileController extends Controller { return new HttpSuccess({ data: data, total }); } + // /** + // * API ค้นหาประวัติการครองตำแหน่ง ข้าราชการ + // * + // * @summary ค้นหาประวัติการครองตำแหน่ง ข้าราชการ + // * + // */ + // @Post("search/history/oc") + // async searchHistoryOC( + // @Body() + // requestBody: { + // posNo?: string; + // position?: string; + // }, + // ) { + // const profiles = await this.profileRepo + // .createQueryBuilder("profile") + // .leftJoinAndSelect("profile.profileSalary", "profileSalary") + // .select([ + // "profile.id", + // "profile.prefix", + // "profile.firstName", + // "profile.lastName", + // "profile.citizenId", + // "profileSalary.position", + // "profileSalary.posNo", + // "profileSalary.date", + // ]) + // .andWhere( + // requestBody.position != null && requestBody.position != "" && requestBody.posNo == undefined + // ? "profileSalary.position LIKE :position" + // : "1=2", + // { + // position: `%${requestBody.position}%`, + // }, + // ) + // .orWhere( + // requestBody.posNo != null && requestBody.posNo != "" && requestBody.position == undefined + // ? "profileSalary.posNo LIKE :posNo" + // : "1=2", + // { + // posNo: `%${requestBody.posNo}%`, + // }, + // ) + // .getMany(); + + // const mapData = profiles.map((profile) => { + // let profileSalary; + // if (profile.profileSalary && profile.profileSalary.length > 0) { + // profileSalary = profile.profileSalary.reduce((latest, current) => { + // return new Date(current.date) > new Date(latest.date) ? current : latest; + // }); + // } + // return { + // id: profile.id, + // // prefix: profile.prefix, + // // firstName: profile.firstName, + // // lastName: profile.lastName, + // fullName: `${profile.prefix}${profile.firstName} ${profile.lastName}`, + // citizenId: profile.citizenId, + // position: profileSalary ? profileSalary.position : null, + // posNo: profileSalary ? profileSalary.posNo : null, + // date: profileSalary ? profileSalary.date : null, + // }; + // }); + + // return new HttpSuccess(mapData); + // } + /** * API ค้นหาประวัติการครองตำแหน่ง ข้าราชการ * @@ -4490,63 +4553,80 @@ export class ProfileController extends Controller { * */ @Post("search/history/oc") - async searchHistoryOC( + async searchOC( @Body() requestBody: { posNo?: string; position?: string; }, ) { + let queryLike = ` + CASE + WHEN current_holders.orgChild4Id IS NOT NULL THEN CONCAT(orgChild4.orgChild4ShortName, current_holders.posMasterNo) + WHEN current_holders.orgChild3Id IS NOT NULL THEN CONCAT(orgChild3.orgChild3ShortName, current_holders.posMasterNo) + WHEN current_holders.orgChild2Id IS NOT NULL THEN CONCAT(orgChild2.orgChild2ShortName, current_holders.posMasterNo) + WHEN current_holders.orgChild1Id IS NOT NULL THEN CONCAT(orgChild1.orgChild1ShortName, current_holders.posMasterNo) + ELSE CONCAT(orgRoot.orgRootShortName, current_holders.posMasterNo) + END LIKE :keyword + `; + const profiles = await this.profileRepo .createQueryBuilder("profile") - .leftJoinAndSelect("profile.profileSalary", "profileSalary") - .select([ - "profile.id", - "profile.prefix", - "profile.firstName", - "profile.lastName", - "profile.citizenId", - "profileSalary.position", - "profileSalary.posNo", - "profileSalary.date", - ]) + .leftJoinAndSelect("profile.current_holders", "current_holders") + .leftJoinAndSelect("current_holders.positions", "positions") + .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") + .where("positions.positionIsSelected = :posIsSelected", { posIsSelected: true }) .andWhere( - requestBody.position != null && requestBody.position != "" && requestBody.posNo == undefined - ? "profileSalary.position LIKE :position" - : "1=2", - { - position: `%${requestBody.position}%`, - }, + new Brackets((qb) => { + qb.orWhere( + requestBody.posNo != undefined && requestBody.posNo != null && requestBody.posNo != "" + ? queryLike + : "1=1", + { + keyword: `%${requestBody.posNo}%`, + }, + ); + }), ) - .orWhere( - requestBody.posNo != null && requestBody.posNo != "" && requestBody.position == undefined - ? "profileSalary.posNo LIKE :posNo" - : "1=2", - { - posNo: `%${requestBody.posNo}%`, - }, + .andWhere( + requestBody.position != null && requestBody.position !== "" + ? "positions.positionName LIKE :positionName" + : "1=1", + { positionName: `${requestBody.position}` }, ) .getMany(); - const mapData = profiles.map((profile) => { - let profileSalary; - if (profile.profileSalary && profile.profileSalary.length > 0) { - profileSalary = profile.profileSalary.reduce((latest, current) => { - return new Date(current.date) > new Date(latest.date) ? current : latest; + const mapData = profiles + .map((profile) => { + const shortNames = profile.current_holders.map((holder) => { + const shortName = + holder.orgChild4?.orgChild4ShortName || + holder.orgChild3?.orgChild3ShortName || + holder.orgChild2?.orgChild2ShortName || + holder.orgChild1?.orgChild1ShortName || + holder.orgRoot?.orgRootShortName; + return `${shortName || ""}${holder.posMasterNo || ""}`; }); - } - return { - id: profile.id, - // prefix: profile.prefix, - // firstName: profile.firstName, - // lastName: profile.lastName, - fullName: `${profile.prefix}${profile.firstName} ${profile.lastName}`, - citizenId: profile.citizenId, - position: profileSalary ? profileSalary.position : null, - posNo: profileSalary ? profileSalary.posNo : null, - date: profileSalary ? profileSalary.date : null, - }; - }); + return profile.current_holders.map((holder, index) => { + const position = holder.positions.find((position) => position.posMasterId === holder.id); + const positionName = position ? position.positionName : null; + + return { + id: profile.id, + posMasterId: holder.id, + fullName: `${profile.prefix}${profile.firstName} ${profile.lastName}`, + citizenId: profile.citizenId, + posNo: shortNames[index], + positionName: positionName, + date: holder.createdAt + }; + }); + }) + .flat(); return new HttpSuccess(mapData); } diff --git a/src/controllers/ProfileEmployeeController.ts b/src/controllers/ProfileEmployeeController.ts index 760c0c1f..8c290b3f 100644 --- a/src/controllers/ProfileEmployeeController.ts +++ b/src/controllers/ProfileEmployeeController.ts @@ -1855,70 +1855,155 @@ export class ProfileEmployeeController extends Controller { return new HttpSuccess({ data: data, total }); } - /** - * API ค้นหาประวัติการครองตำแหน่ง ลูกจ้าง + // /** + // * API ค้นหาประวัติการครองตำแหน่ง ลูกจ้าง + // * + // * @summary ค้นหาประวัติการครองตำแหน่ง ลูกจ้าง + // * + // */ + // @Post("search/history/oc") + // async searchHistoryOC( + // @Body() + // requestBody: { + // posNo?: string; + // position?: string; + // }, + // ) { + // const profiles = await this.profileRepo + // .createQueryBuilder("profileEmployee") + // .leftJoinAndSelect("profileEmployee.profileSalary", "profileSalary") + // .select([ + // "profileEmployee.id", + // "profileEmployee.prefix", + // "profileEmployee.firstName", + // "profileEmployee.lastName", + // "profileEmployee.citizenId", + // "profileSalary.position", + // "profileSalary.posNo", + // "profileSalary.date", + // ]) + // .andWhere( + // requestBody.position != null && requestBody.position != "" && requestBody.posNo == undefined + // ? "profileSalary.position LIKE :position" + // : "1=2", + // { + // position: `%${requestBody.position}%`, + // }, + // ) + // .orWhere( + // requestBody.posNo != null && requestBody.posNo != "" && requestBody.position == undefined + // ? "profileSalars.posNo LIKE :posNo" + // : "1=2", + // { + // posNo: `%${requestBody.posNo}%`, + // }, + // ) + // .getMany(); + + // const mapData = profiles.map((profile) => { + // let profileSalary; + // if (profile.profileSalary && profile.profileSalary.length > 0) { + // profileSalary = profile.profileSalary.reduce((latest, current) => { + // return new Date(current.date) > new Date(latest.date) ? current : latest; + // }); + // } + // return { + // id: profile.id, + // fullName: `${profile.prefix}${profile.firstName} ${profile.lastName}`, + // citizenId: profile.citizenId, + // position: profileSalary ? profileSalary.position : null, + // posNo: profileSalary ? profileSalary.posNo : null, + // date: profileSalary ? profileSalary.date : null, + // }; + // }); + + // return new HttpSuccess(mapData); + // } + + /** + * API ค้นหาประวัติการครองตำแหน่ง ข้าราชการ * - * @summary ค้นหาประวัติการครองตำแหน่ง ลูกจ้าง + * @summary ค้นหาประวัติการครองตำแหน่ง ข้าราชการ * */ - @Post("search/history/oc") - async searchHistoryOC( - @Body() - requestBody: { - posNo?: string; - position?: string; - }, - ) { - const profiles = await this.profileRepo - .createQueryBuilder("profileEmployee") - .leftJoinAndSelect("profileEmployee.profileSalary", "profileSalary") - .select([ - "profileEmployee.id", - "profileEmployee.prefix", - "profileEmployee.firstName", - "profileEmployee.lastName", - "profileEmployee.citizenId", - "profileSalary.position", - "profileSalary.posNo", - "profileSalary.date", - ]) - .andWhere( - requestBody.position != null && requestBody.position != "" && requestBody.posNo == undefined - ? "profileSalary.position LIKE :position" - : "1=2", - { - position: `%${requestBody.position}%`, - }, - ) - .orWhere( - requestBody.posNo != null && requestBody.posNo != "" && requestBody.position == undefined - ? "profileSalars.posNo LIKE :posNo" - : "1=2", - { - posNo: `%${requestBody.posNo}%`, - }, - ) - .getMany(); - - const mapData = profiles.map((profile) => { - let profileSalary; - if (profile.profileSalary && profile.profileSalary.length > 0) { - profileSalary = profile.profileSalary.reduce((latest, current) => { - return new Date(current.date) > new Date(latest.date) ? current : latest; - }); - } - return { - id: profile.id, - fullName: `${profile.prefix}${profile.firstName} ${profile.lastName}`, - citizenId: profile.citizenId, - position: profileSalary ? profileSalary.position : null, - posNo: profileSalary ? profileSalary.posNo : null, - date: profileSalary ? profileSalary.date : null, - }; - }); - - return new HttpSuccess(mapData); - } + @Post("search/history/oc") + async searchOC( + @Body() + requestBody: { + posNo?: string; + position?: string; + }, + ) { + let queryLike = ` + CASE + WHEN current_holders.orgChild4Id IS NOT NULL THEN CONCAT(orgChild4.orgChild4ShortName, current_holders.posMasterNo) + WHEN current_holders.orgChild3Id IS NOT NULL THEN CONCAT(orgChild3.orgChild3ShortName, current_holders.posMasterNo) + WHEN current_holders.orgChild2Id IS NOT NULL THEN CONCAT(orgChild2.orgChild2ShortName, current_holders.posMasterNo) + WHEN current_holders.orgChild1Id IS NOT NULL THEN CONCAT(orgChild1.orgChild1ShortName, current_holders.posMasterNo) + ELSE CONCAT(orgRoot.orgRootShortName, current_holders.posMasterNo) + END LIKE :keyword + `; + + const profiles = await this.profileRepo + .createQueryBuilder("profileEmployee") + .leftJoinAndSelect("profileEmployee.current_holders", "current_holders") + .leftJoinAndSelect("current_holders.positions", "positions") + .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") + .where("positions.positionIsSelected = :posIsSelected", { posIsSelected: true }) + .andWhere( + new Brackets((qb) => { + qb.orWhere( + requestBody.posNo != undefined && requestBody.posNo != null && requestBody.posNo != "" + ? queryLike + : "1=1", + { + keyword: `%${requestBody.posNo}%`, + }, + ); + }), + ) + .andWhere( + requestBody.position != null && requestBody.position !== "" + ? "positions.positionName LIKE :positionName" + : "1=1", + { positionName: `${requestBody.position}` }, + ) + .getMany(); + + const mapData = profiles + .map((profile) => { + const shortNames = profile.current_holders.map((holder) => { + const shortName = + holder.orgChild4?.orgChild4ShortName || + holder.orgChild3?.orgChild3ShortName || + holder.orgChild2?.orgChild2ShortName || + holder.orgChild1?.orgChild1ShortName || + holder.orgRoot?.orgRootShortName; + return `${shortName || ""}${holder.posMasterNo || ""}`; + }); + return profile.current_holders.map((holder, index) => { + const position = holder.positions.find((position) => position.posMasterId === holder.id); + const positionName = position ? position.positionName : null; + + return { + id: profile.id, + posMasterId: holder.id, + fullName: `${profile.prefix}${profile.firstName} ${profile.lastName}`, + citizenId: profile.citizenId, + posNo: shortNames[index], + positionName: positionName, + date: holder.createdAt + }; + }); + }) + .flat(); + + return new HttpSuccess(mapData); + } /** * API ข้อมูลทะเบียนประวัติตาม keycloak