diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index da744888..b54d8feb 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -712,4 +712,115 @@ export class ProfileController extends Controller { return error; } } + + /** + * API ค้นหาข้อมูลทะเบียนประวัติ ทดลองปฏิบัติหน้าที่ราชการ + * + * @summary ค้นหาข้อมูลทะเบียนประวัติ ทดลองปฏิบัติหน้าที่ราชการ (ADMIN) + * + */ + @Post("probation") + async getProfileBySearchKeywordProbation( + @Body() + body: { + page: number; + pageSize: number; + keyword?: string; + }, + ) { + try { + const [findProfile, total] = await AppDataSource.getRepository(Profile) + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.posLevel", "posLevel") + .leftJoinAndSelect("profile.current_holders", "current_holders") + .leftJoinAndSelect("current_holders.orgRevision", "orgRevision") + .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("profile.prefix LIKE :keyword", { keyword: `%${body.keyword}%` }) + .orWhere("profile.firstName LIKE :keyword", { keyword: `%${body.keyword}%` }) + .orWhere("profile.lastName LIKE :keyword", { keyword: `%${body.keyword}%` }) + .orWhere("profile.position LIKE :keyword", { keyword: `%${body.keyword}%` }) + .orWhere("posLevel.posLevelName LIKE :keyword", { keyword: `%${body.keyword}%` }) + .orderBy("profile.citizenId", "ASC") + .skip((body.page - 1) * body.pageSize) + .take(body.pageSize) + .getManyAndCount(); + + const orgRevisionActive = await this.orgRevisionRepository.findOne({ + where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, + }); + + const mapDataProfile = await Promise.all( + findProfile.map(async (item: Profile) => { + return { + id: item.id, + prefix: item.prefix, + firstName: item.firstName, + lastName: item.lastName, + position: item.position, + idcard: item.citizenId, + posLevelName: item.posLevel == null ? null : item.posLevel.posLevelName, + isProbation: item.isProbation, + orgRootName: + item.current_holders == null || + item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) == null || + item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgRoot == + null || + item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgRoot + ?.orgRootName == null + ? null + : item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) + ?.orgRoot?.orgRootName, + orgChild1Name: + item.current_holders == null || + item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) == null || + item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) + ?.orgChild1 == null || + item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgChild1 + ?.orgChild1Name == null + ? null + : item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) + ?.orgChild1?.orgChild1Name, + orgChild2Name: + item.current_holders == null || + item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) == null || + item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) + ?.orgChild2 == null || + item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgChild2 + ?.orgChild2Name == null + ? null + : item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) + ?.orgChild2?.orgChild2Name, + orgChild3Name: + item.current_holders == null || + item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) == null || + item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) + ?.orgChild3 == null || + item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgChild3 + ?.orgChild3Name == null + ? null + : item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) + ?.orgChild3?.orgChild3Name, + orgChild4Name: + item.current_holders == null || + item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) == null || + item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) + ?.orgChild4 == null || + item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgChild4 + ?.orgChild4Name == null + ? null + : item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) + ?.orgChild4?.orgChild4Name, + }; + }), + ); + + return new HttpSuccess({ data: mapDataProfile, total }); + } catch (error: any) { + throw new Error(error); + } + } } diff --git a/src/entities/Profile.ts b/src/entities/Profile.ts index cb1d4be1..0ac7040a 100644 --- a/src/entities/Profile.ts +++ b/src/entities/Profile.ts @@ -101,6 +101,12 @@ export class Profile extends EntityBase { }) keycloak: string; + @Column({ + comment: "ทดลองปฏิบัติหน้าที่", + default: false, + }) + isProbation: boolean; + @OneToMany(() => PosMaster, (posMaster) => posMaster.current_holder) current_holders: PosMaster[]; diff --git a/src/migration/1708590016918-update_table_profile_add_probation.ts b/src/migration/1708590016918-update_table_profile_add_probation.ts new file mode 100644 index 00000000..6c645a36 --- /dev/null +++ b/src/migration/1708590016918-update_table_profile_add_probation.ts @@ -0,0 +1,14 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateTableProfileAddProbation1708590016918 implements MigrationInterface { + name = 'UpdateTableProfileAddProbation1708590016918' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`profile\` ADD \`isProbation\` tinyint NOT NULL COMMENT 'ทดลองปฏิบัติหน้าที่' DEFAULT 0`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`isProbation\``); + } + +}