From 5fc7aa09385c6c179594ba43854832df667f2f83 Mon Sep 17 00:00:00 2001 From: Kittapath Date: Fri, 16 Feb 2024 12:07:37 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B8=84=E0=B9=89=E0=B8=99=E0=B8=AB=E0=B8=B2?= =?UTF-8?q?=E0=B8=97=E0=B8=B0=E0=B9=80=E0=B8=9A=E0=B8=B5=E0=B8=A2=E0=B8=99?= =?UTF-8?q?=E0=B8=9B=E0=B8=A3=E0=B8=B0=E0=B8=A7=E0=B8=B1=E0=B8=95=E0=B8=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/PositionController.ts | 103 ++++++++---------- src/controllers/ProfileController.ts | 102 +++++++++++++++++ src/entities/Profile.ts | 21 ++++ ...69956-update_table_profile_add_keycloak.ts | 20 ++++ ...58097765-update_table_profile_add_email.ts | 16 +++ 5 files changed, 207 insertions(+), 55 deletions(-) create mode 100644 src/migration/1708054669956-update_table_profile_add_keycloak.ts create mode 100644 src/migration/1708058097765-update_table_profile_add_email.ts diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 5938f93c..2a69b0fb 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -176,9 +176,9 @@ export class PositionController extends Controller { where: { posDictName: Like(`%${keyword}%`) }, relations: ["posType", "posLevel", "posExecutive"], }); - if (!findPosDict) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); - } + // if (!findPosDict) { + // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); + // } break; case "positionField": @@ -186,9 +186,9 @@ export class PositionController extends Controller { where: { posDictField: Like(`%${keyword}%`) }, relations: ["posType", "posLevel", "posExecutive"], }); - if (!findPosDict) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); - } + // if (!findPosDict) { + // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); + // } break; case "positionType": @@ -229,9 +229,9 @@ export class PositionController extends Controller { where: { posDictExecutiveField: Like(`%${keyword}%`) }, relations: ["posType", "posLevel", "posExecutive"], }); - if (!findPosDict) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); - } + // if (!findPosDict) { + // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); + // } break; case "positionArea": @@ -239,18 +239,18 @@ export class PositionController extends Controller { where: { posDictArea: Like(`%${keyword}%`) }, relations: ["posType", "posLevel", "posExecutive"], }); - if (!findPosDict) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); - } + // if (!findPosDict) { + // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); + // } break; default: findPosDict = await this.posDictRepository.find({ relations: ["posType", "posLevel", "posExecutive"], }); - if (!findPosDict) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); - } + // if (!findPosDict) { + // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + // } break; } @@ -812,37 +812,33 @@ export class PositionController extends Controller { // }); 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') - .where(conditions) - .orWhere( - new Brackets((qb) => { - qb.where( - body.keyword != null && body.keyword != "" - ? "CONCAT(current_holder.prefix, current_holder.firstName, current_holder.lastName) LIKE :keyword OR :keyword LIKE current_holder.prefix OR :keyword LIKE current_holder.firstName OR :keyword LIKE current_holder.lastName " - : "1=1", - { - keyword: `%${body.keyword}%`, - }, - ) - .andWhere( - checkChildConditions - ) - .andWhere( - typeCondition - ); - }) - ) - .orderBy('posMaster.posMasterOrder', 'ASC') - .skip((body.page - 1) * body.pageSize) - .take(body.pageSize) - .getManyAndCount(); + .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") + .where(conditions) + .orWhere( + new Brackets((qb) => { + qb.where( + body.keyword != null && body.keyword != "" + ? "CONCAT(current_holder.prefix, current_holder.firstName, current_holder.lastName) LIKE :keyword OR :keyword LIKE current_holder.prefix OR :keyword LIKE current_holder.firstName OR :keyword LIKE current_holder.lastName " + : "1=1", + { + keyword: `%${body.keyword}%`, + }, + ) + .andWhere(checkChildConditions) + .andWhere(typeCondition); + }), + ) + .orderBy("posMaster.posMasterOrder", "ASC") + .skip((body.page - 1) * body.pageSize) + .take(body.pageSize) + .getManyAndCount(); const formattedData = await Promise.all( posMaster.map(async (posMaster) => { @@ -855,14 +851,13 @@ export class PositionController extends Controller { let profile: any; const chkRevision = await this.orgRevisionRepository.findOne({ - where: { id: posMaster.orgRevisionId } - }) - if(chkRevision?.orgRevisionIsCurrent && !chkRevision?.orgRevisionIsDraft){ + where: { id: posMaster.orgRevisionId }, + }); + if (chkRevision?.orgRevisionIsCurrent && !chkRevision?.orgRevisionIsDraft) { profile = await this.profileRepository.findOne({ where: { id: String(posMaster.current_holderId) }, }); - } - else if(!chkRevision?.orgRevisionIsCurrent && chkRevision?.orgRevisionIsDraft){ + } else if (!chkRevision?.orgRevisionIsCurrent && chkRevision?.orgRevisionIsDraft) { profile = await this.profileRepository.findOne({ where: { id: String(posMaster.next_holderId) }, }); @@ -940,10 +935,8 @@ export class PositionController extends Controller { : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, orgShortname: shortName, isSit: posMaster.isSit, - profilePosition: - profile == null || profile.position == null ? null : profile.position, - profilePostype: - type == null || type.posTypeName == null ? null : type.posTypeName, + profilePosition: profile == null || profile.position == null ? null : profile.position, + profilePostype: type == null || type.posTypeName == null ? null : type.posTypeName, profilePoslevel: level == null || level.posLevelName == null ? null : level.posLevelName, positions: positions.map((position) => ({ diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 94fb4e53..67a2112e 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -384,4 +384,106 @@ export class ProfileController extends Controller { return error; } } + + /** + * API ข้อมูลทะเบียนประวัติตาม keycloak + * + * @summary ORG_065 - ข้อมูลทะเบียนประวัติตาม keycloak (ADMIN) #70 + * + */ + @Get("keycloak/position") + async getProfileByKeycloak(@Request() request: { user: Record }) { + const profile = await this.profileRepository.findOne({ + where: { keycloak: request.user.sub }, + relations: ["posLevel", "posType"], + }); + if (!profile) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบแบบร่างโครงสร้าง"); + } + const _profile = { + profileId: profile.id, + prefix: profile.prefix, + firstName: profile.firstName, + lastName: profile.lastName, + citizenId: profile.citizenId, + position: profile.position, + posLevelName: profile.posLevel == null ? null : profile.posLevel.posLevelName, + posLevelRank: profile.posLevel == null ? null : profile.posLevel.posLevelRank, + posLevelId: profile.posLevel == null ? null : profile.posLevel.id, + posTypeName: profile.posType == null ? null : profile.posType.posTypeName, + posTypeRank: profile.posType == null ? null : profile.posType.posTypeRank, + posTypeId: profile.posType == null ? null : profile.posType.id, + }; + try { + return new HttpSuccess(_profile); + } catch (error: any) { + throw new Error(error); + } + } + + /** + * API ค้นหาข้อมูลทะเบียนประวัติ + * + * @summary ORG_065 - ค้นหาข้อมูลทะเบียนประวัติ (ADMIN) #70 + * + */ + @Post("search-personal") + async getProfileBySearchKeyword( + @Body() + body: { + fieldName: string; + keyword?: number; + }, + ) { + try { + let findProfile: any; + switch (body.fieldName) { + case "idcard": + findProfile = await this.profileRepository.find({ + where: { citizenId: Like(`%${body.keyword}%`) }, + relations: ["posType", "posLevel"], + }); + break; + + case "firstname": + findProfile = await this.profileRepository.find({ + where: { firstName: Like(`%${body.keyword}%`) }, + relations: ["posType", "posLevel"], + }); + break; + + case "lastname": + findProfile = await this.profileRepository.find({ + where: { lastName: Like(`%${body.keyword}%`) }, + relations: ["posType", "posLevel"], + }); + break; + + default: + findProfile = await this.profileRepository.find({ + relations: ["posType", "posLevel"], + }); + break; + } + + 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, + email: item.email, + phone: item.phone, + }; + }), + ); + + return new HttpSuccess(mapDataProfile); + } catch (error: any) { + throw new Error(error); + } + } } diff --git a/src/entities/Profile.ts b/src/entities/Profile.ts index fb21455b..cb1d4be1 100644 --- a/src/entities/Profile.ts +++ b/src/entities/Profile.ts @@ -60,6 +60,20 @@ export class Profile extends EntityBase { }) posTypeId: string | null; + @Column({ + nullable: true, + length: 255, + comment: "อีเมล", + }) + email: string; + + @Column({ + nullable: true, + length: 20, + comment: "เบอร์โทร", + }) + phone: string; + // @Column({ // nullable: true, // length: 40, @@ -79,6 +93,13 @@ export class Profile extends EntityBase { // unique: false, // }) // next_holderId: string; + @Column({ + nullable: true, + comment: "id keycloak", + length: 40, + default: null, + }) + keycloak: string; @OneToMany(() => PosMaster, (posMaster) => posMaster.current_holder) current_holders: PosMaster[]; diff --git a/src/migration/1708054669956-update_table_profile_add_keycloak.ts b/src/migration/1708054669956-update_table_profile_add_keycloak.ts new file mode 100644 index 00000000..83310236 --- /dev/null +++ b/src/migration/1708054669956-update_table_profile_add_keycloak.ts @@ -0,0 +1,20 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateTableProfileAddKeycloak1708054669956 implements MigrationInterface { + name = 'UpdateTableProfileAddKeycloak1708054669956' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`profile\` ADD \`keycloak\` varchar(40) NULL COMMENT 'id keycloak'`); + await queryRunner.query(`ALTER TABLE \`profile\` DROP FOREIGN KEY \`FK_de774e32853add3313ff44bd793\``); + await queryRunner.query(`ALTER TABLE \`profile\` CHANGE \`posTypeId\` \`posTypeId\` varchar(40) NULL COMMENT 'ไอดีประเภทตำแหน่ง'`); + await queryRunner.query(`ALTER TABLE \`profile\` ADD CONSTRAINT \`FK_de774e32853add3313ff44bd793\` FOREIGN KEY (\`posTypeId\`) REFERENCES \`posType\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`profile\` DROP FOREIGN KEY \`FK_de774e32853add3313ff44bd793\``); + await queryRunner.query(`ALTER TABLE \`profile\` CHANGE \`posTypeId\` \`posTypeId\` varchar(40) NULL COMMENT 'ไอดีประเภทตำแหน่'`); + await queryRunner.query(`ALTER TABLE \`profile\` ADD CONSTRAINT \`FK_de774e32853add3313ff44bd793\` FOREIGN KEY (\`posTypeId\`) REFERENCES \`posType\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`keycloak\``); + } + +} diff --git a/src/migration/1708058097765-update_table_profile_add_email.ts b/src/migration/1708058097765-update_table_profile_add_email.ts new file mode 100644 index 00000000..1cd472f1 --- /dev/null +++ b/src/migration/1708058097765-update_table_profile_add_email.ts @@ -0,0 +1,16 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class UpdateTableProfileAddEmail1708058097765 implements MigrationInterface { + name = 'UpdateTableProfileAddEmail1708058097765' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`profile\` ADD \`email\` varchar(255) NULL COMMENT 'อีเมล'`); + await queryRunner.query(`ALTER TABLE \`profile\` ADD \`phone\` varchar(20) NULL COMMENT 'เบอร์โทร'`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`phone\``); + await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`email\``); + } + +}