From 900fb8deb5666f247e4824c071341242341389b2 Mon Sep 17 00:00:00 2001 From: Bright Date: Wed, 30 Jul 2025 17:03:24 +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=A3=E0=B8=B2=E0=B8=A2=E0=B8=8A=E0=B8=B7=E0=B9=88=E0=B8=AD?= =?UTF-8?q?=E0=B8=96=E0=B9=89=E0=B8=B2=E0=B9=84=E0=B8=A1=E0=B9=88=E0=B8=AA?= =?UTF-8?q?=E0=B9=88=E0=B8=87=20system=20=E0=B8=A1=E0=B8=B2=E0=B9=83?= =?UTF-8?q?=E0=B8=AB=E0=B9=89=20default=20=E0=B8=95=E0=B8=B2=E0=B8=A1?= =?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?= =?UTF-8?q?=20#1689?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/ProfileController.ts | 353 +++++++----------- src/controllers/ProfileEmployeeController.ts | 336 ++++++----------- .../ProfileEmployeeTempController.ts | 181 +++++---- 3 files changed, 342 insertions(+), 528 deletions(-) diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index fd43d0b1..002cedc2 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -8264,237 +8264,150 @@ export class ProfileController extends Controller { */ @Post("search-personal") async getProfileBySearchKeyword( + @Request() request: RequestWithUser, @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Body() body: { fieldName?: string | null; keyword?: string; + system?: string; }, ) { - let findProfile: any; - let total: any; - let revision = await this.orgRevisionRepo.findOne({ where: { orgRevisionIsCurrent: true } }); - const skip = (page - 1) * pageSize; - const take = pageSize; - let queryLike = `CONCAT( - IFNULL(orgChild4.orgChild4ShortName, '')," ", - IFNULL(current_holders.posMasterNo , '') - ) LIKE :keyword OR CONCAT( - IFNULL(orgChild3.orgChild3ShortName, '')," ", - IFNULL(current_holders.posMasterNo , '') - ) LIKE :keyword OR CONCAT( - IFNULL(orgChild2.orgChild2ShortName, '')," ", - IFNULL(current_holders.posMasterNo , '') - ) LIKE :keyword OR CONCAT( - IFNULL(orgChild1.orgChild1ShortName, '')," ", - IFNULL(current_holders.posMasterNo , '') - ) LIKE :keyword OR CONCAT( - IFNULL(orgRoot.orgRootShortName, '')," ", - IFNULL(current_holders.posMasterNo , '') - ) LIKE :keyword`; - - switch (body.fieldName) { - case "citizenId": - [findProfile, total] = await this.profileRepo.findAndCount({ - where: { - citizenId: Like(`%${body.keyword}%`), - current_holders: { - orgRevisionId: revision?.id, - }, - }, - relations: [ - "posType", - "posLevel", - "current_holders", - "current_holders.orgRoot", - "current_holders.orgChild1", - "current_holders.orgChild2", - "current_holders.orgChild3", - "current_holders.orgChild4", - ], - skip, - take, - }); - break; - - case "fullName": - [findProfile, total] = await this.profileRepo - .createQueryBuilder("profile") - .leftJoinAndSelect("profile.posType", "posType") - .leftJoinAndSelect("profile.posLevel", "posLevel") - .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") - .where("current_holders.orgRevision = :revisionId", { revisionId: revision?.id }) - .andWhere( - "CONCAT(profile.prefix, profile.firstName, ' ', profile.lastName) LIKE :keyword", - { keyword: `%${body.keyword}%` }, - ) - .skip(skip) - .take(take) - .getManyAndCount(); - break; - - case "position": - [findProfile, total] = await this.profileRepo.findAndCount({ - where: { - position: Like(`%${body.keyword}%`), - current_holders: { - orgRevisionId: revision?.id, - }, - }, - relations: [ - "posType", - "posLevel", - "current_holders", - "current_holders.orgRoot", - "current_holders.orgChild1", - "current_holders.orgChild2", - "current_holders.orgChild3", - "current_holders.orgChild4", - ], - skip, - take, - }); - break; - - case "posNo": - [findProfile, total] = await this.profileRepo - .createQueryBuilder("profile") - .leftJoinAndSelect("profile.posType", "posType") - .leftJoinAndSelect("profile.posLevel", "posLevel") - .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") - .where("current_holders.orgRevision = :revisionId", { revisionId: revision?.id }) - .andWhere( - new Brackets((qb) => { - qb.orWhere( - body.keyword != undefined && body.keyword != null && body.keyword != "" - ? queryLike - : "1=1", - { - keyword: `%${body.keyword}%`, - }, - ); - }), - ) - .skip(skip) - .take(take) - .getManyAndCount(); - break; - - case "posType": - [findProfile, total] = await this.profileRepo.findAndCount({ - where: { - posType: { - posTypeName: Like(`%${body.keyword}%`), - }, - current_holders: { - orgRevisionId: revision?.id, - }, - }, - relations: [ - "posType", - "posLevel", - "current_holders", - "current_holders.orgRoot", - "current_holders.orgChild1", - "current_holders.orgChild2", - "current_holders.orgChild3", - "current_holders.orgChild4", - ], - skip, - take, - }); - break; - - case "posLevel": - [findProfile, total] = await this.profileRepo.findAndCount({ - where: { - posLevel: { - posLevelName: Like(`%${body.keyword}%`), - }, - current_holders: { - orgRevisionId: revision?.id, - }, - }, - relations: [ - "posType", - "posLevel", - "current_holders", - "current_holders.orgRoot", - "current_holders.orgChild1", - "current_holders.orgChild2", - "current_holders.orgChild3", - "current_holders.orgChild4", - ], - skip, - take, - }); - break; - - case "organization": - [findProfile, total] = await this.profileRepo.findAndCount({ - where: { - current_holders: { - orgRevisionId: revision?.id, - orgRoot: { - orgRootName: Like(`%${body.keyword}%`), - }, - }, - }, - relations: [ - "posType", - "posLevel", - "current_holders", - "current_holders.orgRoot", - "current_holders.orgChild1", - "current_holders.orgChild2", - "current_holders.orgChild3", - "current_holders.orgChild4", - ], - skip, - take, - }); - break; - - default: - [findProfile, total] = await this.profileRepo.findAndCount({ - where: { - current_holders: { - orgRevisionId: revision?.id, - }, - }, - relations: [ - "posType", - "posLevel", - "current_holders", - "current_holders.orgRoot", - "current_holders.orgChild1", - "current_holders.orgChild2", - "current_holders.orgChild3", - "current_holders.orgChild4", - ], - skip, - take, - }); - break; - } - - const findRevision = await this.orgRevisionRepo.findOne({ - where: { orgRevisionIsCurrent: true }, - }); + // ค้นหารายชื่อถ้าไม่ส่ง system มาให้ default ตามทะเบียนประวัติ + let _system:string ="SYS_REGISTRY_OFFICER"; + if(body.system) + _system = body.system; + let _data = await new permission().PermissionOrgList(request, _system); + const findRevision = await this.orgRevisionRepo.findOne({ where: { orgRevisionIsCurrent: true } }); if (!findRevision) { throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); } + // let queryLike = `CONCAT( + // IFNULL(orgChild4.orgChild4ShortName, '')," ", + // IFNULL(current_holders.posMasterNo , '') + // ) LIKE :keyword OR CONCAT( + // IFNULL(orgChild3.orgChild3ShortName, '')," ", + // IFNULL(current_holders.posMasterNo , '') + // ) LIKE :keyword OR CONCAT( + // IFNULL(orgChild2.orgChild2ShortName, '')," ", + // IFNULL(current_holders.posMasterNo , '') + // ) LIKE :keyword OR CONCAT( + // IFNULL(orgChild1.orgChild1ShortName, '')," ", + // IFNULL(current_holders.posMasterNo , '') + // ) LIKE :keyword OR CONCAT( + // IFNULL(orgRoot.orgRootShortName, '')," ", + // IFNULL(current_holders.posMasterNo , '') + // ) LIKE :keyword`; + let queryLike = "1=1" + switch (body.fieldName) { + case "citizenId": + queryLike = "profile.citizenId LIKE :keyword"; + break; + + case "fullName": + queryLike = "CONCAT(profile.prefix, profile.firstName, ' ', profile.lastName) LIKE :keyword"; + break; + + case "position": + queryLike = "profile.position LIKE :keyword"; + break; + + case "posNo": + 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 + `; + break; + + case "posType": + queryLike = "posType.posTypeName LIKE :keyword"; + break; + + case "posLevel": + queryLike = "posLevel.posLevelName LIKE :keyword"; + break; + + case "organization": + queryLike = "orgRoot.orgRootName LIKE :keyword"; + break; + + default: + queryLike = "1=1" + break; + } + + const [findProfile, total] = await this.profileRepo + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.posType", "posType") + .leftJoinAndSelect("profile.posLevel", "posLevel") + .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") + .where("current_holders.orgRevision = :revisionId", { revisionId: findRevision?.id }) + .andWhere( + _data.root != undefined && _data.root != null + ? _data.root[0] != null + ? `current_holders.orgRootId IN (:...root)` + : `current_holders.orgRootId is null` + : "1=1", + { root: _data.root }, + ) + .andWhere( + _data.child1 != undefined && _data.child1 != null + ? _data.child1[0] != null + ? `current_holders.orgChild1Id IN (:...child1)` + : `current_holders.orgChild1Id is null` + : "1=1", + { child1: _data.child1 }, + ) + .andWhere( + _data.child2 != undefined && _data.child2 != null + ? _data.child2[0] != null + ? `current_holders.orgChild2Id IN (:...child2)` + : `current_holders.orgChild2Id is null` + : "1=1", + { child2: _data.child2 }, + ) + .andWhere( + _data.child3 != undefined && _data.child3 != null + ? _data.child3[0] != null + ? `current_holders.orgChild3Id IN (:...child3)` + : `current_holders.orgChild3Id is null` + : "1=1", + { child3: _data.child3 }, + ) + .andWhere( + _data.child4 != undefined && _data.child4 != null + ? _data.child4[0] != null + ? `current_holders.orgChild4Id IN (:...child4)` + : `current_holders.orgChild4Id is null` + : "1=1", + { child4: _data.child4 }, + ) + .andWhere( + new Brackets((qb) => { + qb.orWhere(body.keyword ? queryLike : "1=1", { keyword: `%${body.keyword}%` }); + }), + ) + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); + + // const findRevision = await this.orgRevisionRepo.findOne({ + // where: { orgRevisionIsCurrent: true }, + // }); + // if (!findRevision) { + // throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); + // } const mapDataProfile = await Promise.all( findProfile.map(async (item: Profile) => { diff --git a/src/controllers/ProfileEmployeeController.ts b/src/controllers/ProfileEmployeeController.ts index a905c452..c0db0ae8 100644 --- a/src/controllers/ProfileEmployeeController.ts +++ b/src/controllers/ProfileEmployeeController.ts @@ -4010,243 +4010,127 @@ export class ProfileEmployeeController extends Controller { */ @Post("search-personal") async getProfileBySearchKeyword( + @Request() request: RequestWithUser, @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Body() body: { fieldName: string; keyword?: string; + system?: string; }, ) { - let findProfile: any; - let total: any; - let revision = await this.orgRevisionRepo.findOne({ where: { orgRevisionIsCurrent: true } }); - const skip = (page - 1) * pageSize; - const take = pageSize; - let queryLike = `CONCAT( - IFNULL(orgChild4.orgChild4ShortName, ''), " ", - IFNULL(current_holders.posMasterNo , '') - ) LIKE :keyword OR CONCAT( - IFNULL(orgChild3.orgChild3ShortName, ''), " ", - IFNULL(current_holders.posMasterNo , '') - ) LIKE :keyword OR CONCAT( - IFNULL(orgChild2.orgChild2ShortName, ''), " ", - IFNULL(current_holders.posMasterNo , '') - ) LIKE :keyword OR CONCAT( - IFNULL(orgChild1.orgChild1ShortName, ''), " ", - IFNULL(current_holders.posMasterNo , '') - ) LIKE :keyword OR CONCAT( - IFNULL(orgRoot.orgRootShortName, ''), " ", - IFNULL(current_holders.posMasterNo , '') - ) LIKE :keyword`; - - switch (body.fieldName) { - case "citizenId": - [findProfile, total] = await this.profileRepo.findAndCount({ - where: { - citizenId: Like(`%${body.keyword}%`), - current_holders: { - orgRevisionId: revision?.id, - }, - }, - relations: [ - "posType", - "posLevel", - "current_holders", - "profileSalary", - "current_holders.orgRoot", - "current_holders.orgChild1", - "current_holders.orgChild2", - "current_holders.orgChild3", - "current_holders.orgChild4", - ], - skip, - take, - }); - break; - - case "fullName": - [findProfile, total] = await this.profileRepo - .createQueryBuilder("profile") - .leftJoinAndSelect("profile.posType", "posType") - .leftJoinAndSelect("profile.posLevel", "posLevel") - .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") - .where("current_holders.orgRevision = :revisionId", { revisionId: revision?.id }) - .andWhere( - "CONCAT(profile.prefix, profile.firstName, ' ', profile.lastName) LIKE :keyword", - { keyword: `%${body.keyword}%` }, - ) - .skip(skip) - .take(take) - .getManyAndCount(); - break; - - case "position": - [findProfile, total] = await this.profileRepo.findAndCount({ - where: { - position: Like(`%${body.keyword}%`), - current_holders: { - orgRevisionId: revision?.id, - }, - }, - relations: [ - "posType", - "posLevel", - "current_holders", - "profileSalary", - "current_holders.orgRoot", - "current_holders.orgChild1", - "current_holders.orgChild2", - "current_holders.orgChild3", - "current_holders.orgChild4", - ], - skip, - take, - }); - break; - - case "posNo": - [findProfile, total] = await this.profileRepo - .createQueryBuilder("profile") - .leftJoinAndSelect("profile.posType", "posType") - .leftJoinAndSelect("profile.posLevel", "posLevel") - .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") - .where("current_holders.orgRevision = :revisionId", { revisionId: revision?.id }) - .andWhere( - new Brackets((qb) => { - qb.orWhere( - body.keyword != undefined && body.keyword != null && body.keyword != "" - ? queryLike - : "1=1", - { - keyword: `%${body.keyword}%`, - }, - ); - }), - ) - .skip(skip) - .take(take) - .getManyAndCount(); - break; - - case "posType": - [findProfile, total] = await this.profileRepo.findAndCount({ - where: { - posType: { - posTypeName: Like(`%${body.keyword}%`), - }, - current_holders: { - orgRevisionId: revision?.id, - }, - }, - relations: [ - "posType", - "posLevel", - "current_holders", - "profileSalary", - "current_holders.orgRoot", - "current_holders.orgChild1", - "current_holders.orgChild2", - "current_holders.orgChild3", - "current_holders.orgChild4", - ], - skip, - take, - }); - break; - - // case "posLevel": - // [findProfile, total] = await this.profileRepo.findAndCount({ - // where: { - // posLevel: { - // posLevelName: Like(`%${body.keyword}%`), - // }, - // current_holders: { - // orgRevisionId: revision?.id, - // }, - // }, - // relations: [ - // "posType", - // "posLevel", - // "current_holders", - // "profileSalary", - // "current_holders.orgRoot", - // "current_holders.orgChild1", - // "current_holders.orgChild2", - // "current_holders.orgChild3", - // "current_holders.orgChild4", - // ], - // skip, - // take, - // }); - // break; - - case "organization": - [findProfile, total] = await this.profileRepo.findAndCount({ - where: { - current_holders: { - orgRevisionId: revision?.id, - orgRoot: { - orgRootName: Like(`%${body.keyword}%`), - }, - }, - }, - relations: [ - "posType", - "posLevel", - "current_holders", - "profileSalary", - "current_holders.orgRoot", - "current_holders.orgChild1", - "current_holders.orgChild2", - "current_holders.orgChild3", - "current_holders.orgChild4", - ], - skip, - take, - }); - break; - - default: - [findProfile, total] = await this.profileRepo.findAndCount({ - where: { - current_holders: { - orgRevisionId: revision?.id, - }, - }, - relations: [ - "posType", - "posLevel", - "current_holders", - "profileSalary", - "current_holders.orgRoot", - "current_holders.orgChild1", - "current_holders.orgChild2", - "current_holders.orgChild3", - "current_holders.orgChild4", - ], - skip, - take, - }); - break; - } - - const findRevision = await this.orgRevisionRepo.findOne({ - where: { orgRevisionIsCurrent: true }, - }); + // ค้นหารายชื่อถ้าไม่ส่ง system มาให้ default ตามทะเบียนประวัติ + let _system:string ="SYS_REGISTRY_EMP"; + if(body.system) + _system = body.system; + let _data = await new permission().PermissionOrgList(request, _system); + const findRevision = await this.orgRevisionRepo.findOne({ where: { orgRevisionIsCurrent: true } }); if (!findRevision) { throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); } + let queryLike = "1=1" + switch (body.fieldName) { + case "citizenId": + queryLike = "profile.citizenId LIKE :keyword"; + break; + + case "fullName": + queryLike = "CONCAT(profile.prefix, profile.firstName, ' ', profile.lastName) LIKE :keyword"; + break; + + case "position": + queryLike = "profile.position LIKE :keyword"; + break; + + case "posNo": + 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 + `; + break; + + case "posType": + queryLike = "posType.posTypeName LIKE :keyword"; + break; + + case "posLevel": + queryLike = "posLevel.posLevelName LIKE :keyword"; + break; + + case "organization": + queryLike = "orgRoot.orgRootName LIKE :keyword"; + break; + + default: + queryLike = "1=1" + break; + } + + const [findProfile, total] = await this.profileRepo + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.posType", "posType") + .leftJoinAndSelect("profile.posLevel", "posLevel") + .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") + .where("current_holders.orgRevision = :revisionId", { revisionId: findRevision?.id }) + .andWhere( + _data.root != undefined && _data.root != null + ? _data.root[0] != null + ? `current_holders.orgRootId IN (:...root)` + : `current_holders.orgRootId is null` + : "1=1", + { root: _data.root }, + ) + .andWhere( + _data.child1 != undefined && _data.child1 != null + ? _data.child1[0] != null + ? `current_holders.orgChild1Id IN (:...child1)` + : `current_holders.orgChild1Id is null` + : "1=1", + { child1: _data.child1 }, + ) + .andWhere( + _data.child2 != undefined && _data.child2 != null + ? _data.child2[0] != null + ? `current_holders.orgChild2Id IN (:...child2)` + : `current_holders.orgChild2Id is null` + : "1=1", + { child2: _data.child2 }, + ) + .andWhere( + _data.child3 != undefined && _data.child3 != null + ? _data.child3[0] != null + ? `current_holders.orgChild3Id IN (:...child3)` + : `current_holders.orgChild3Id is null` + : "1=1", + { child3: _data.child3 }, + ) + .andWhere( + _data.child4 != undefined && _data.child4 != null + ? _data.child4[0] != null + ? `current_holders.orgChild4Id IN (:...child4)` + : `current_holders.orgChild4Id is null` + : "1=1", + { child4: _data.child4 }, + ) + .andWhere( + new Brackets((qb) => { + qb.orWhere(body.keyword ? queryLike : "1=1", { keyword: `%${body.keyword}%` }); + }), + ) + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); const mapDataProfile = await Promise.all( findProfile.map(async (item: ProfileEmployee) => { diff --git a/src/controllers/ProfileEmployeeTempController.ts b/src/controllers/ProfileEmployeeTempController.ts index bcb3bad5..3cfe9d38 100644 --- a/src/controllers/ProfileEmployeeTempController.ts +++ b/src/controllers/ProfileEmployeeTempController.ts @@ -2174,97 +2174,108 @@ export class ProfileEmployeeTempController extends Controller { */ @Post("search-personal") async getProfileBySearchKeyword( + @Request() request: RequestWithUser, @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Body() body: { fieldName: string; keyword?: string; + system?: string; }, ) { - let findProfile: any; - let total: any; - const skip = (page - 1) * pageSize; - const take = pageSize; - switch (body.fieldName) { - case "citizenId": - [findProfile, total] = await this.profileRepo.findAndCount({ - where: { citizenId: Like(`%${body.keyword}%`) }, - relations: [ - "posType", - "posLevel", - "current_holders", - "current_holders.orgRoot", - "current_holders.orgChild1", - "current_holders.orgChild2", - "current_holders.orgChild3", - "current_holders.orgChild4", - ], - skip, - take, - }); - break; - - case "firstname": - [findProfile, total] = await this.profileRepo.findAndCount({ - where: { firstName: Like(`%${body.keyword}%`) }, - relations: [ - "posType", - "posLevel", - "current_holders", - "current_holders.orgRoot", - "current_holders.orgChild1", - "current_holders.orgChild2", - "current_holders.orgChild3", - "current_holders.orgChild4", - ], - skip, - take, - }); - break; - - case "lastname": - [findProfile, total] = await this.profileRepo.findAndCount({ - where: { lastName: Like(`%${body.keyword}%`) }, - relations: [ - "posType", - "posLevel", - "current_holders", - "current_holders.orgRoot", - "current_holders.orgChild1", - "current_holders.orgChild2", - "current_holders.orgChild3", - "current_holders.orgChild4", - ], - skip, - take, - }); - break; - - default: - [findProfile, total] = await this.profileRepo.findAndCount({ - relations: [ - "posType", - "posLevel", - "current_holders", - "current_holders.orgRoot", - "current_holders.orgChild1", - "current_holders.orgChild2", - "current_holders.orgChild3", - "current_holders.orgChild4", - ], - skip, - take, - }); - break; - } - - const findRevision = await this.orgRevisionRepo.findOne({ - where: { orgRevisionIsCurrent: true }, - }); + // ค้นหารายชื่อถ้าไม่ส่ง system มาให้ default ตามทะเบียนประวัติ + let _system:string ="SYS_REGISTRY_TEMP"; + if(body.system) + _system = body.system; + let _data = await new permission().PermissionOrgList(request, _system); + const findRevision = await this.orgRevisionRepo.findOne({ where: { orgRevisionIsCurrent: true } }); if (!findRevision) { throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); } + let queryLike = "1=1" + switch (body.fieldName) { + case "citizenId": + queryLike = "profile.citizenId LIKE :keyword"; + break; + + case "firstname": + queryLike = "profile.firstName LIKE :keyword"; + break; + + case "lastname": + queryLike = "profile.lastName LIKE :keyword"; + break; + + case "fullName": + queryLike = "CONCAT(profile.prefix, profile.firstName, ' ', profile.lastName) LIKE :keyword"; + break; + + default: + queryLike = "1=1" + break; + } + + const [findProfile, total] = await this.profileRepo + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.posType", "posType") + .leftJoinAndSelect("profile.posLevel", "posLevel") + .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") + .where("current_holders.orgRevision = :revisionId", { revisionId: findRevision?.id }) + .andWhere("profile.employeeClass = :employeeClass", { employeeClass: "TEMP" }) + .andWhere( + _data.root != undefined && _data.root != null + ? _data.root[0] != null + ? `current_holders.orgRootId IN (:...root)` + : `current_holders.orgRootId is null` + : "1=1", + { root: _data.root }, + ) + .andWhere( + _data.child1 != undefined && _data.child1 != null + ? _data.child1[0] != null + ? `current_holders.orgChild1Id IN (:...child1)` + : `current_holders.orgChild1Id is null` + : "1=1", + { child1: _data.child1 }, + ) + .andWhere( + _data.child2 != undefined && _data.child2 != null + ? _data.child2[0] != null + ? `current_holders.orgChild2Id IN (:...child2)` + : `current_holders.orgChild2Id is null` + : "1=1", + { child2: _data.child2 }, + ) + .andWhere( + _data.child3 != undefined && _data.child3 != null + ? _data.child3[0] != null + ? `current_holders.orgChild3Id IN (:...child3)` + : `current_holders.orgChild3Id is null` + : "1=1", + { child3: _data.child3 }, + ) + .andWhere( + _data.child4 != undefined && _data.child4 != null + ? _data.child4[0] != null + ? `current_holders.orgChild4Id IN (:...child4)` + : `current_holders.orgChild4Id is null` + : "1=1", + { child4: _data.child4 }, + ) + .andWhere( + new Brackets((qb) => { + qb.orWhere(body.keyword ? queryLike : "1=1", { keyword: `%${body.keyword}%` }); + }), + ) + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); const mapDataProfile = await Promise.all( findProfile.map(async (item: ProfileEmployee) => { @@ -2369,7 +2380,10 @@ export class ProfileEmployeeTempController extends Controller { posTypeId: item.posTypeId, posTypeName: item.posType?.posTypeName, posLevelId: item.posLevelId, - posLevelName: item.posLevel?.posLevelName, + posLevelName: + item.posLevel == null && item.posType == null + ? null + : `${item.posType?.posTypeShortName} ${item.posLevel?.posLevelName}`, educationDegree: latestProfileEducation != null && latestProfileEducation.educationLevel != null ? latestProfileEducation.educationLevel @@ -3972,7 +3986,10 @@ export class ProfileEmployeeTempController extends Controller { posTypeId: item.posTypeId, posTypeName: item.posType?.posTypeName, posLevelId: item.posLevelId, - posLevelName: item.posLevel?.posLevelName, + posLevelName: + item.posLevel == null && item.posType == null + ? null + : `${item.posType?.posTypeShortName} ${item.posLevel?.posLevelName}`, educationDegree: latestProfileEducation != null && latestProfileEducation.educationLevel != null ? latestProfileEducation.educationLevel