diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index e6f4c1e7..6118e40d 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -87,6 +87,8 @@ import { ProfileAssistance } from "../entities/ProfileAssistance"; import { CommandRecive } from "../entities/CommandRecive"; import { EmployeePosMaster } from "../entities/EmployeePosMaster"; import { getTopDegrees } from "../services/PositionService"; +import { ProfileLeaveService } from "../services/ProfileLeaveService"; + @Route("api/v1/org/profile") @Tags("Profile") @Security("bearerAuth") @@ -141,6 +143,9 @@ export class ProfileController extends Controller { private profileAssistanceRepository = AppDataSource.getRepository(ProfileAssistance); private commandReciveRepository = AppDataSource.getRepository(CommandRecive); + // Services + private profileLeaveService = new ProfileLeaveService(); + /** * report ประวัติแบบย่อ ข้าราชการ * @@ -2725,19 +2730,19 @@ export class ProfileController extends Controller { }, ); }), - ) + ); if (body.sortBy) { query = query.orderBy( `viewDirectorActing.${body.sortBy}`, - body.descending ? "DESC" : "ASC" + body.descending ? "DESC" : "ASC", ); } const [lists, total] = await query - .skip((body.page - 1) * body.pageSize) - .take(body.pageSize) - .getManyAndCount(); + .skip((body.page - 1) * body.pageSize) + .take(body.pageSize) + .getManyAndCount(); return new HttpSuccess({ data: lists, total }); } else { @@ -2827,13 +2832,10 @@ export class ProfileController extends Controller { }, ); }), - ) + ); if (body.sortBy) { - query = query.orderBy( - `viewDirector.${body.sortBy}`, - body.descending ? "DESC" : "ASC" - ); + query = query.orderBy(`viewDirector.${body.sortBy}`, body.descending ? "DESC" : "ASC"); } const [lists, total] = await query @@ -5706,35 +5708,18 @@ export class ProfileController extends Controller { result: { data: [ { - id: "ecb0b34c-037e-41f2-b95e-7e19f88b42ae", - createdAt: "2024-03-24T12:39:12.105Z", - createdUserId: "00000000-0000-0000-0000-000000000000", - lastUpdatedAt: "2024-03-24T12:41:43.164Z", - lastUpdateUserId: "00000000-0000-0000-0000-000000000000", - createdFullName: "string", - lastUpdateFullName: "string", - rank: null, - prefix: null, - firstName: "Methapon", - lastName: "Metanipat", - citizenId: null, - position: null, - posLevelId: null, - posTypeId: null, - email: null, - phone: null, - keycloak: null, + page: 1, + pageSize: 12, + posLevel: "ปฏิบัติงาน", + posType: "ทั่วไป", isProbation: false, - dateRetire: null, - birthDate: null, - ethnicity: null, - telephoneNumber: null, - gender: null, - relationship: null, - bloodGroup: null, - posLevel: null, - posType: null, - org: null, + isRetire: true, + node: 0, + nodeId: "8349b6b6-d005-4eb7-9960-ceb5b96e1962", + isAll: true, + sortBy: "profile.dateAppoint", + sort: "DESC", + retireType: "RETIRE", }, ], total: 1, @@ -5749,9 +5734,7 @@ export class ProfileController extends Controller { @Query() searchKeyword: string = "", @Query() posType?: string, @Query() posLevel?: string, - @Query() yearLeave?: number, @Query() isProbation?: boolean, - // @Query() isRetire?: boolean, @Query() node?: number, @Query() nodeId?: string, @Query() isAll?: boolean, @@ -5759,325 +5742,236 @@ export class ProfileController extends Controller { @Query() sortBy: string = "current_holders.posMasterNo", @Query() sort: "ASC" | "DESC" = "ASC", ) { - let _data = await new permission().PermissionOrgList(request, "SYS_REGISTRY_OFFICER"); - let queryLike = - "CONCAT(profile.prefix, profile.firstName, ' ', profile.lastName) LIKE :keyword"; - if (searchField == "citizenId") { - queryLike = "profile.citizenId LIKE :keyword"; - } else if (searchField == "position") { - queryLike = "profile.position LIKE :keyword"; - } + const { data, total } = await this.profileLeaveService.getLeaveOfficer(request, { + page, + pageSize, + searchField, + searchKeyword, + posType, + posLevel, + isProbation, + node, + nodeId, + isAll, + retireType, + sortBy, + sort, + }); + + // let _data = await new permission().PermissionOrgList(request, "SYS_REGISTRY_OFFICER"); + // let queryLike = + // "CONCAT(profile.prefix, profile.firstName, ' ', profile.lastName) LIKE :keyword"; + // if (searchField == "citizenId") { + // queryLike = "profile.citizenId LIKE :keyword"; + // } else if (searchField == "position") { + // queryLike = "profile.position LIKE :keyword"; + // } // else if (searchField == "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 - // `; - // } - else if (searchField == "posNo") { - queryLike = ` - CONCAT(profileSalary.posNoAbb, profileSalary.posNo) LIKE :keyword - OR CONCAT(profileSalary.posNoAbb, " ", profileSalary.posNo) LIKE :keyword - OR profileSalary.posNo LIKE :keyword - `; - } - let nodeCondition = "1=1"; - let nodeAll = ""; - let orgRoot = null; - let orgChild1 = null; - let orgChild2 = null; - let orgChild3 = null; - let orgChild4 = null; + // queryLike = ` + // CONCAT(profileSalary.posNoAbb, profileSalary.posNo) LIKE :keyword + // OR CONCAT(profileSalary.posNoAbb, " ", profileSalary.posNo) LIKE :keyword + // OR profileSalary.posNo LIKE :keyword + // `; + // } + // let nodeCondition = "1=1"; + // let nodeAll = ""; + // let orgRoot = null; + // let orgChild1 = null; + // let orgChild2 = null; + // let orgChild3 = null; + // let orgChild4 = null; - let pmsCondition = "1=1"; - let orgRootPms = null; - let orgChild1Pms = null; - let orgChild2Pms = null; - let orgChild3Pms = null; - let orgChild4Pms = null; + // let pmsCondition = "1=1"; + // let orgRootPms = null; + // let orgChild1Pms = null; + // let orgChild2Pms = null; + // let orgChild3Pms = null; + // let orgChild4Pms = null; - if (node === 0 && nodeId) { - orgRoot = await this.orgRootRepo.findOne({ where: { id: nodeId } }); - if (orgRoot) { - nodeCondition = "profileSalary.orgRoot = :orgRoot"; - } - if (isAll == false) nodeAll = " AND profileSalary.orgChild1 IS NULL"; - } else if (node === 1 && nodeId) { - orgChild1 = await this.orgChild1Repo.findOne({ where: { id: nodeId } }); - if (orgChild1) { - nodeCondition = "profileSalary.orgChild1 = :orgChild1"; - } - if (isAll == false) nodeAll = " AND profileSalary.orgChild2 IS NULL"; - } else if (node === 2 && nodeId) { - orgChild2 = await this.orgChild2Repo.findOne({ where: { id: nodeId } }); - if (orgChild2) { - nodeCondition = "profileSalary.orgChild2 = :orgChild2"; - } - if (isAll == false) nodeAll = " AND profileSalary.orgChild3 IS NULL"; - } else if (node === 3 && nodeId) { - orgChild3 = await this.orgChild3Repo.findOne({ where: { id: nodeId } }); - if (orgChild3) { - nodeCondition = "profileSalary.orgChild3 = :orgChild3"; - } - if (isAll == false) nodeAll = " AND profileSalary.orgChild4 IS NULL"; - } else if (node === 4 && nodeId) { - orgChild4 = await this.orgChild4Repo.findOne({ where: { id: nodeId } }); - if (orgChild4) { - nodeCondition = "profileSalary.orgChild4 = :orgChild4"; - } - } - nodeCondition = nodeCondition + nodeAll; + // if (node === 0 && nodeId) { + // orgRoot = await this.orgRootRepo.findOne({ where: { id: nodeId } }); + // if (orgRoot) { + // nodeCondition = "profileSalary.orgRoot = :orgRoot"; + // } + // if (isAll == false) nodeAll = " AND profileSalary.orgChild1 IS NULL"; + // } else if (node === 1 && nodeId) { + // orgChild1 = await this.orgChild1Repo.findOne({ where: { id: nodeId } }); + // if (orgChild1) { + // nodeCondition = "profileSalary.orgChild1 = :orgChild1"; + // } + // if (isAll == false) nodeAll = " AND profileSalary.orgChild2 IS NULL"; + // } else if (node === 2 && nodeId) { + // orgChild2 = await this.orgChild2Repo.findOne({ where: { id: nodeId } }); + // if (orgChild2) { + // nodeCondition = "profileSalary.orgChild2 = :orgChild2"; + // } + // if (isAll == false) nodeAll = " AND profileSalary.orgChild3 IS NULL"; + // } else if (node === 3 && nodeId) { + // orgChild3 = await this.orgChild3Repo.findOne({ where: { id: nodeId } }); + // if (orgChild3) { + // nodeCondition = "profileSalary.orgChild3 = :orgChild3"; + // } + // if (isAll == false) nodeAll = " AND profileSalary.orgChild4 IS NULL"; + // } else if (node === 4 && nodeId) { + // orgChild4 = await this.orgChild4Repo.findOne({ where: { id: nodeId } }); + // if (orgChild4) { + // nodeCondition = "profileSalary.orgChild4 = :orgChild4"; + // } + // } + // nodeCondition = nodeCondition + nodeAll; - if (_data.root) { - orgRootPms = await this.orgRootRepo.findOne({ where: { id: _data.root } }); - if (orgRootPms) { - pmsCondition = "profileSalary.orgRoot = :orgRootPms"; - } - if (isAll == false) nodeAll = " AND profileSalary.orgChild1 IS NULL"; - } else if (_data.child1) { - orgChild1Pms = await this.orgChild1Repo.findOne({ where: { id: _data.child1 } }); - if (orgChild1Pms) { - pmsCondition = "profileSalary.orgChild1 = :orgChild1Pms"; - } - if (isAll == false) nodeAll = " AND profileSalary.orgChild2 IS NULL"; - } else if (_data.child2) { - orgChild2Pms = await this.orgChild2Repo.findOne({ where: { id: _data.child2 } }); - if (orgChild2Pms) { - pmsCondition = "profileSalary.orgChild2 = :orgChild2Pms"; - } - if (isAll == false) nodeAll = " AND profileSalary.orgChild3 IS NULL"; - } else if (_data.child3) { - orgChild3Pms = await this.orgChild3Repo.findOne({ where: { id: _data.child3 } }); - if (orgChild3Pms) { - pmsCondition = "profileSalary.orgChild3 = :orgChild3Pms"; - } - if (isAll == false) nodeAll = " AND profileSalary.orgChild4 IS NULL"; - } else if (_data.child4) { - orgChild4Pms = await this.orgChild4Repo.findOne({ where: { id: _data.child4 } }); - if (orgChild4Pms) { - pmsCondition = "profileSalary.orgChild4 = :orgChild4Pms"; - } - } - pmsCondition = pmsCondition + nodeAll; + // if (_data.root) { + // orgRootPms = await this.orgRootRepo.findOne({ where: { id: _data.root } }); + // if (orgRootPms) { + // pmsCondition = "profileSalary.orgRoot = :orgRootPms"; + // } + // if (isAll == false) nodeAll = " AND profileSalary.orgChild1 IS NULL"; + // } else if (_data.child1) { + // orgChild1Pms = await this.orgChild1Repo.findOne({ where: { id: _data.child1 } }); + // if (orgChild1Pms) { + // pmsCondition = "profileSalary.orgChild1 = :orgChild1Pms"; + // } + // if (isAll == false) nodeAll = " AND profileSalary.orgChild2 IS NULL"; + // } else if (_data.child2) { + // orgChild2Pms = await this.orgChild2Repo.findOne({ where: { id: _data.child2 } }); + // if (orgChild2Pms) { + // pmsCondition = "profileSalary.orgChild2 = :orgChild2Pms"; + // } + // if (isAll == false) nodeAll = " AND profileSalary.orgChild3 IS NULL"; + // } else if (_data.child3) { + // orgChild3Pms = await this.orgChild3Repo.findOne({ where: { id: _data.child3 } }); + // if (orgChild3Pms) { + // pmsCondition = "profileSalary.orgChild3 = :orgChild3Pms"; + // } + // if (isAll == false) nodeAll = " AND profileSalary.orgChild4 IS NULL"; + // } else if (_data.child4) { + // orgChild4Pms = await this.orgChild4Repo.findOne({ where: { id: _data.child4 } }); + // if (orgChild4Pms) { + // pmsCondition = "profileSalary.orgChild4 = :orgChild4Pms"; + // } + // } + // pmsCondition = pmsCondition + nodeAll; - // const findRevision = await this.orgRevisionRepo.findOne({ - // where: { orgRevisionIsCurrent: true }, - // }); - // if (!findRevision) { - // throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); - // } - const [record, total] = await this.profileRepo - .createQueryBuilder("profile") - .leftJoinAndSelect("profile.posLevel", "posLevel") - .leftJoinAndSelect("profile.posType", "posType") - .leftJoinAndSelect("profile.profileSalary", "profileSalary") - // .leftJoinAndSelect("profile.current_holders", "current_holders") - // .leftJoinAndSelect("current_holders.positions", "positions") - // .leftJoinAndSelect("positions.posExecutive", "posExecutive") - .where( - new Brackets((qb) => { - qb.where("profile.isLeave = :isLeave", { isLeave: true }).orWhere( - "profile.isRetirement = :isRetirement", - { isRetirement: true }, - ); - }), - ) - // .andWhere("profile.leaveCommandId Is NOT NULL") - .andWhere( - "profileSalary.order = (SELECT MAX(ps.order) FROM profileSalary ps WHERE ps.profileId = profile.id and ps.positionName != 'เกษียณอายุราชการ')", - ) + // const [record, total] = await this.profileRepo + // .createQueryBuilder("profile") + // .leftJoinAndSelect("profile.posLevel", "posLevel") + // .leftJoinAndSelect("profile.posType", "posType") + // .leftJoinAndSelect("profile.profileSalary", "profileSalary") + // .where( + // new Brackets((qb) => { + // qb.where("profile.isLeave = :isLeave", { isLeave: true }).orWhere( + // "profile.isRetirement = :isRetirement", + // { isRetirement: true }, + // ); + // }), + // ) + // .andWhere( + // "profileSalary.order = (SELECT MAX(ps.order) FROM profileSalary ps WHERE ps.profileId = profile.id and ps.positionName != 'เกษียณอายุราชการ')", + // ) - .andWhere( - posType != undefined && posType != null && posType != "" - ? "posType.posTypeName LIKE :keyword1" - : "1=1", - { - keyword1: `${posType}`, - }, - ) - .andWhere( - posLevel != undefined && posLevel != null && posLevel != "" - ? "posLevel.posLevelName LIKE :keyword2" - : "1=1", - { - keyword2: `${posLevel}`, - }, - ) - .andWhere( - isProbation != undefined && isProbation != null - ? `profile.isProbation = ${isProbation}` - : "1=1", - ) - .andWhere( - retireType != undefined && retireType != null ? `profile.leaveType = :retireType` : "1=1", - { retireType: retireType }, - ) - .andWhere(pmsCondition, { - orgRootPms: orgRootPms ? orgRootPms.orgRootName : "", - orgChild1Pms: orgChild1Pms ? orgChild1Pms.orgChild1Name : "", - orgChild2Pms: orgChild2Pms ? orgChild2Pms.orgChild2Name : "", - orgChild3Pms: orgChild3Pms ? orgChild3Pms.orgChild3Name : "", - orgChild4Pms: orgChild4Pms ? orgChild4Pms.orgChild4Name : "", - }) - .andWhere(nodeCondition, { - orgRoot: orgRoot ? orgRoot.orgRootName : "", - orgChild1: orgChild1 ? orgChild1.orgChild1Name : "", - orgChild2: orgChild2 ? orgChild2.orgChild2Name : "", - orgChild3: orgChild3 ? orgChild3.orgChild3Name : "", - orgChild4: orgChild4 ? orgChild4.orgChild4Name : "", - }) - .andWhere( - new Brackets((qb) => { - qb.orWhere( - searchKeyword != undefined && searchKeyword != null && searchKeyword != "" - ? queryLike - : "1=1", - { - keyword: `%${searchKeyword}%`, - }, - ); - }), - ) - // .orderBy("current_holders.posMasterNo", "ASC") - // .orderBy(`${sortBy}`, sort) - .skip((page - 1) * pageSize) - .take(pageSize) - .getManyAndCount(); + // .andWhere( + // posType != undefined && posType != null && posType != "" + // ? "posType.posTypeName LIKE :keyword1" + // : "1=1", + // { + // keyword1: `${posType}`, + // }, + // ) + // .andWhere( + // posLevel != undefined && posLevel != null && posLevel != "" + // ? "posLevel.posLevelName LIKE :keyword2" + // : "1=1", + // { + // keyword2: `${posLevel}`, + // }, + // ) + // .andWhere( + // isProbation != undefined && isProbation != null + // ? `profile.isProbation = ${isProbation}` + // : "1=1", + // ) + // .andWhere( + // retireType != undefined && retireType != null ? `profile.leaveType = :retireType` : "1=1", + // { retireType: retireType }, + // ) + // .andWhere(pmsCondition, { + // orgRootPms: orgRootPms ? orgRootPms.orgRootName : "", + // orgChild1Pms: orgChild1Pms ? orgChild1Pms.orgChild1Name : "", + // orgChild2Pms: orgChild2Pms ? orgChild2Pms.orgChild2Name : "", + // orgChild3Pms: orgChild3Pms ? orgChild3Pms.orgChild3Name : "", + // orgChild4Pms: orgChild4Pms ? orgChild4Pms.orgChild4Name : "", + // }) + // .andWhere(nodeCondition, { + // orgRoot: orgRoot ? orgRoot.orgRootName : "", + // orgChild1: orgChild1 ? orgChild1.orgChild1Name : "", + // orgChild2: orgChild2 ? orgChild2.orgChild2Name : "", + // orgChild3: orgChild3 ? orgChild3.orgChild3Name : "", + // orgChild4: orgChild4 ? orgChild4.orgChild4Name : "", + // }) + // .andWhere( + // new Brackets((qb) => { + // qb.orWhere( + // searchKeyword != undefined && searchKeyword != null && searchKeyword != "" + // ? queryLike + // : "1=1", + // { + // keyword: `%${searchKeyword}%`, + // }, + // ); + // }), + // ) + // .skip((page - 1) * pageSize) + // .take(pageSize) + // .getManyAndCount(); - const data = await Promise.all( - record.map((_data) => { - // const posExecutive = - // _data.current_holders.length == 0 || - // _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null || - // _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.positions.length == - // 0 || - // _data.current_holders - // .find((x) => x.orgRevisionId == findRevision.id) - // ?.positions.find((x: any) => x.positionIsSelected == true) == null || - // _data.current_holders - // .find((x) => x.orgRevisionId == findRevision.id) - // ?.positions.find((x: any) => x.positionIsSelected == true)?.posExecutive == null - // ? null - // : _data.current_holders - // .find((x) => x.orgRevisionId == findRevision.id) - // ?.positions.find((x: any) => x.positionIsSelected == true)?.posExecutive - // ?.posExecutiveName; + // const data = await Promise.all( + // record.map((_data) => { + // return { + // id: _data.id, + // avatar: _data.avatar, + // avatarName: _data.avatarName, + // dateAppoint: _data.dateAppoint, + // prefix: _data.prefix, + // rank: _data.rank, + // firstName: _data.firstName, + // lastName: _data.lastName, + // citizenId: _data.citizenId, - // const shortName = - // _data.current_holders.length == 0 - // ? null - // : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && - // _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4 != - // null - // ? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4.orgChild4ShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` - // : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && - // _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) - // ?.orgChild3 != null - // ? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3.orgChild3ShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` - // : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && - // _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) - // ?.orgChild2 != null - // ? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2.orgChild2ShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` - // : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && - // _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) - // ?.orgChild1 != null - // ? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild1.orgChild1ShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` - // : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != - // null && - // _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) - // ?.orgRoot != null - // ? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot.orgRootShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` - // : null; - // const root = - // _data.current_holders.length == 0 || - // (_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && - // _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot == null) - // ? null - // : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot; - - // const child1 = - // _data.current_holders == null || - // _data.current_holders.length == 0 || - // _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null - // ? null - // : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild1; - - // const child2 = - // _data.current_holders == null || - // _data.current_holders.length == 0 || - // _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null - // ? null - // : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2; - - // const child3 = - // _data.current_holders == null || - // _data.current_holders.length == 0 || - // _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null - // ? null - // : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3; - - // const child4 = - // _data.current_holders == null || - // _data.current_holders.length == 0 || - // _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null - // ? null - // : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4; - - // let _child1 = child1?.orgChild1Name; - // let _child2 = child2?.orgChild2Name; - // let _child3 = child3?.orgChild3Name; - // let _child4 = child4?.orgChild4Name; - - return { - id: _data.id, - avatar: _data.avatar, - avatarName: _data.avatarName, - dateAppoint: _data.dateAppoint, - prefix: _data.prefix, - rank: _data.rank, - firstName: _data.firstName, - lastName: _data.lastName, - citizenId: _data.citizenId, - - posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName, - posType: _data.posType == null ? null : _data.posType.posTypeName, - posLevelId: _data.posLevel == null ? null : _data.posLevel.id, - posTypeId: _data.posType == null ? null : _data.posType.id, - position: _data.position, - posExecutive: - _data.profileSalary[0].positionExecutive == null - ? null - : _data.profileSalary[0].positionExecutive, - posNo: - _data.profileSalary[0].posNoAbb && _data.profileSalary[0].posNo - ? `${_data.profileSalary[0].posNoAbb} ${_data.profileSalary[0].posNo}` - : _data.profileSalary[0].posNo || "", - rootId: null, - root: _data.profileSalary[0].orgRoot == null ? null : _data.profileSalary[0].orgRoot, - orgRootShortName: - _data.profileSalary[0].posNoAbb == null ? null : _data.profileSalary[0].posNoAbb, - orgRevisionId: null, - org: - (_data.profileSalary[0].orgChild4 == null - ? "" - : _data.profileSalary[0].orgChild4 + "\n") + - (_data.profileSalary[0].orgChild3 == null - ? "" - : _data.profileSalary[0].orgChild3 + "\n") + - (_data.profileSalary[0].orgChild2 == null - ? "" - : _data.profileSalary[0].orgChild2 + "\n") + - (_data.profileSalary[0].orgChild1 == null - ? "" - : _data.profileSalary[0].orgChild1 + "\n") + - (_data.profileSalary[0].orgRoot == null ? "" : _data.profileSalary[0].orgRoot), - }; - }), - ); + // posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName, + // posType: _data.posType == null ? null : _data.posType.posTypeName, + // posLevelId: _data.posLevel == null ? null : _data.posLevel.id, + // posTypeId: _data.posType == null ? null : _data.posType.id, + // position: _data.position, + // posExecutive: + // _data.profileSalary[0].positionExecutive == null + // ? null + // : _data.profileSalary[0].positionExecutive, + // posNo: + // _data.profileSalary[0].posNoAbb && _data.profileSalary[0].posNo + // ? `${_data.profileSalary[0].posNoAbb} ${_data.profileSalary[0].posNo}` + // : _data.profileSalary[0].posNo || "", + // rootId: null, + // root: _data.profileSalary[0].orgRoot == null ? null : _data.profileSalary[0].orgRoot, + // orgRootShortName: + // _data.profileSalary[0].posNoAbb == null ? null : _data.profileSalary[0].posNoAbb, + // orgRevisionId: null, + // org: + // (_data.profileSalary[0].orgChild4 == null + // ? "" + // : _data.profileSalary[0].orgChild4 + "\n") + + // (_data.profileSalary[0].orgChild3 == null + // ? "" + // : _data.profileSalary[0].orgChild3 + "\n") + + // (_data.profileSalary[0].orgChild2 == null + // ? "" + // : _data.profileSalary[0].orgChild2 + "\n") + + // (_data.profileSalary[0].orgChild1 == null + // ? "" + // : _data.profileSalary[0].orgChild1 + "\n") + + // (_data.profileSalary[0].orgRoot == null ? "" : _data.profileSalary[0].orgRoot), + // }; + // }), + // ); return new HttpSuccess({ data: data, total }); } @@ -6220,7 +6114,7 @@ export class ProfileController extends Controller { @Query() nodeId?: string, @Query() isAll?: boolean, @Query() retireType?: string, - @Query() sortBy: string = "current_holders.posMasterNo", + @Query() sortBy?: string, @Query() sort: "ASC" | "DESC" = "ASC", ) { let _data = await new permission().PermissionOrgList(request, "SYS_REGISTRY_OFFICER"); @@ -6384,7 +6278,7 @@ export class ProfileController extends Controller { }), ) .addSelect("CASE WHEN current_holders.posMasterNo IS NULL THEN 1 ELSE 0 END", "sort_order") - .orderBy("sort_order", "ASC") + .orderBy(`${sortBy ? sortBy : "sort_order"}`, `${sort}`) .addOrderBy("orgRoot.orgRootOrder", sort) .addOrderBy("orgChild1.orgChild1Order", sort) .addOrderBy("orgChild2.orgChild2Order", sort) @@ -8568,7 +8462,7 @@ export class ProfileController extends Controller { system?: string; }, @Query("sortBy") sortBy?: string, - @Query("descending") descending?: boolean + @Query("descending") descending?: boolean, ) { // ค้นหารายชื่อถ้าไม่ส่ง system มาให้ default ตามทะเบียนประวัติ let _system: string = "SYS_REGISTRY_OFFICER"; @@ -8695,28 +8589,22 @@ export class ProfileController extends Controller { new Brackets((qb) => { qb.orWhere(body.keyword ? queryLike : "1=1", { keyword: `%${body.keyword}%` }); }), - ) + ); - if (sortBy) { - if(sortBy === "name"){ - query = query - .orderBy(`profile.prefix`,descending ? "DESC" : "ASC") - .addOrderBy(`profile.firstName`,descending ? "DESC" : "ASC") - .addOrderBy(`profile.lastName`,descending ? "DESC" : "ASC") - }else if(sortBy === "organization"){ - query = query.orderBy( - `orgRoot.orgRootName`, - descending ? "DESC" : "ASC" - ); - }else{ - query = query.orderBy( - `profile.${sortBy}`, - descending ? "DESC" : "ASC" - ); - } + if (sortBy) { + if (sortBy === "name") { + query = query + .orderBy(`profile.prefix`, descending ? "DESC" : "ASC") + .addOrderBy(`profile.firstName`, descending ? "DESC" : "ASC") + .addOrderBy(`profile.lastName`, descending ? "DESC" : "ASC"); + } else if (sortBy === "organization") { + query = query.orderBy(`orgRoot.orgRootName`, descending ? "DESC" : "ASC"); + } else { + query = query.orderBy(`profile.${sortBy}`, descending ? "DESC" : "ASC"); } + } - const [findProfile, total] = await query + const [findProfile, total] = await query .skip((page - 1) * pageSize) .take(pageSize) .getManyAndCount(); @@ -9276,40 +9164,25 @@ export class ProfileController extends Controller { }, ); }), - ) + ); - if (body.sortBy) { - if(body.sortBy === "posLevelName"){ - query = query.orderBy( - `posLevel.posLevelName`, - body.descending ? "DESC" : "ASC" - ); - }else if(body.sortBy === "posTypeName"){ - query = query.orderBy( - `posType.posTypeName`, - body.descending ? "DESC" : "ASC" - ); - }else if(body.sortBy === "commandNo"){ - query = query.orderBy( - `profileSalary.commandNo`, - body.descending ? "DESC" : "ASC" - ); - }else if(body.sortBy === "orgRootName"){ - query = query.orderBy( - `orgRoot.orgRootName`, - body.descending ? "DESC" : "ASC" - ); - }else{ - query = query.orderBy( - `profile.${body.sortBy}`, - body.descending ? "DESC" : "ASC" - ); - } - }else{ - query = query.orderBy("profile.citizenId", "ASC") + if (body.sortBy) { + if (body.sortBy === "posLevelName") { + query = query.orderBy(`posLevel.posLevelName`, body.descending ? "DESC" : "ASC"); + } else if (body.sortBy === "posTypeName") { + query = query.orderBy(`posType.posTypeName`, body.descending ? "DESC" : "ASC"); + } else if (body.sortBy === "commandNo") { + query = query.orderBy(`profileSalary.commandNo`, body.descending ? "DESC" : "ASC"); + } else if (body.sortBy === "orgRootName") { + query = query.orderBy(`orgRoot.orgRootName`, body.descending ? "DESC" : "ASC"); + } else { + query = query.orderBy(`profile.${body.sortBy}`, body.descending ? "DESC" : "ASC"); } + } else { + query = query.orderBy("profile.citizenId", "ASC"); + } - const [findProfile, total] = await query + const [findProfile, total] = await query .skip((body.page - 1) * body.pageSize) .take(body.pageSize) .getManyAndCount(); @@ -9924,38 +9797,29 @@ export class ProfileController extends Controller { }, ); }), - ) + ); - if (body.sortBy) { - if(body.sortBy === "posType"){ - query = query.orderBy( - `posType.posTypeName`, - body.descending ? "DESC" : "ASC" - ); - }else if(body.sortBy === "posLevel"){ - query = query.orderBy( - `posLevel.posLevelName`, - body.descending ? "DESC" : "ASC" - ); - }else if(body.sortBy === "orgShortName" || body.sortBy === "posMasterNo"){ - query = query - .orderBy(`orgRoot.orgRootShortName`,body.descending ? "DESC" : "ASC") - .addOrderBy(`orgChild1.orgChild1ShortName`,body.descending ? "DESC" : "ASC") - .addOrderBy(`orgChild2.orgChild2ShortName`,body.descending ? "DESC" : "ASC") - .addOrderBy(`orgChild3.orgChild3ShortName`,body.descending ? "DESC" : "ASC") - .addOrderBy(`orgChild4.orgChild4ShortName`,body.descending ? "DESC" : "ASC") - .addOrderBy(`posMaster.posMasterNo`,body.descending ? "DESC" : "ASC") - }else{ - query = query.orderBy( - `current_holder.${body.sortBy}`, - body.descending ? "DESC" : "ASC" - ); - } - }else{ - query = query.orderBy("current_holder.citizenId", "ASC") + if (body.sortBy) { + if (body.sortBy === "posType") { + query = query.orderBy(`posType.posTypeName`, body.descending ? "DESC" : "ASC"); + } else if (body.sortBy === "posLevel") { + query = query.orderBy(`posLevel.posLevelName`, body.descending ? "DESC" : "ASC"); + } else if (body.sortBy === "orgShortName" || body.sortBy === "posMasterNo") { + query = query + .orderBy(`orgRoot.orgRootShortName`, body.descending ? "DESC" : "ASC") + .addOrderBy(`orgChild1.orgChild1ShortName`, body.descending ? "DESC" : "ASC") + .addOrderBy(`orgChild2.orgChild2ShortName`, body.descending ? "DESC" : "ASC") + .addOrderBy(`orgChild3.orgChild3ShortName`, body.descending ? "DESC" : "ASC") + .addOrderBy(`orgChild4.orgChild4ShortName`, body.descending ? "DESC" : "ASC") + .addOrderBy(`posMaster.posMasterNo`, body.descending ? "DESC" : "ASC"); + } else { + query = query.orderBy(`current_holder.${body.sortBy}`, body.descending ? "DESC" : "ASC"); } + } else { + query = query.orderBy("current_holder.citizenId", "ASC"); + } - const [findPosMaster, total] = await query + const [findPosMaster, total] = await query .skip((body.page - 1) * body.pageSize) .take(body.pageSize) .getManyAndCount(); diff --git a/src/services/ProfileLeaveService.ts b/src/services/ProfileLeaveService.ts index f187675d..c53c475f 100644 --- a/src/services/ProfileLeaveService.ts +++ b/src/services/ProfileLeaveService.ts @@ -1,4 +1,5 @@ import { AppDataSource } from "../database/data-source"; +import { Profile } from "./../entities/Profile"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import { OrgRoot } from "../entities/OrgRoot"; import { OrgChild1 } from "../entities/OrgChild1"; @@ -10,7 +11,7 @@ import Extension from "../interfaces/extension"; import permission from "../interfaces/permission"; import { RequestWithUser } from "../middlewares/user"; -export interface LeaveEmployeeFilter { +export interface LeaveFilter { page: number; pageSize: number; searchField?: "firstName" | "lastName" | "fullName" | "citizenId" | "position" | "posNo"; @@ -32,7 +33,8 @@ export interface OrganizationCondition { } export class ProfileLeaveService { - private profileRepo: Repository; + private profileEmployeeRepo: Repository; + private profileRepo: Repository; private orgRootRepository: Repository; private child1Repository: Repository; private child2Repository: Repository; @@ -40,7 +42,8 @@ export class ProfileLeaveService { private child4Repository: Repository; constructor() { - this.profileRepo = AppDataSource.getRepository(ProfileEmployee); + this.profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee); + this.profileRepo = AppDataSource.getRepository(Profile); this.orgRootRepository = AppDataSource.getRepository(OrgRoot); this.child1Repository = AppDataSource.getRepository(OrgChild1); this.child2Repository = AppDataSource.getRepository(OrgChild2); @@ -48,29 +51,25 @@ export class ProfileLeaveService { this.child4Repository = AppDataSource.getRepository(OrgChild4); } - /** - * สร้าง query สำหรับการค้นหาตามฟิลด์ต่างๆ - */ - buildSearchQuery(searchField?: string): string { + /** สร้าง query สำหรับการค้นหาตามฟิลด์ต่างๆ */ + buildSearchQuery(searchField?: string, type: string = "profile"): string { switch (searchField) { case "citizenId": - return "profileEmployee.citizenId LIKE :keyword"; + return `${type}.citizenId LIKE :keyword`; case "position": - return "profileEmployee.position LIKE :keyword"; + return `${type}.position LIKE :keyword`; case "posNo": return ` - (profileSalary.posNoAbb IS NOT NULL AND CONCAT(profileSalary.posNoAbb, profileSalary.posNo) LIKE :keyword) - OR (profileSalary.posNoAbb IS NOT NULL AND CONCAT(profileSalary.posNoAbb, " ", profileSalary.posNo) LIKE :keyword) - OR (profileSalary.posNo IS NOT NULL AND profileSalary.posNo LIKE :keyword) + (CONCAT(profileSalary.posNoAbb, profileSalary.posNo) LIKE :keyword) + OR (CONCAT(profileSalary.posNoAbb, " ", profileSalary.posNo) LIKE :keyword) + OR (profileSalary.posNo LIKE :keyword) `; default: - return "CONCAT(profileEmployee.prefix, profileEmployee.firstName, ' ', profileEmployee.lastName) LIKE :keyword"; + return `CONCAT(${type}.prefix, ${type}.firstName, ' ', ${type}.lastName) LIKE :keyword`; } } - /** - * สร้างเงื่อนไขการค้นหาตาม node และ nodeId - */ + /** สร้างเงื่อนไขการค้นหาตาม node และ nodeId */ async buildNodeCondition( node?: number, nodeId?: string, @@ -87,7 +86,7 @@ export class ProfileLeaveService { // สร้าง nodeAll condition - เพิ่มการตรวจสอบ IS NULL if (isAll === false && node < 4) { const nextLevels = ["orgChild1", "orgChild2", "orgChild3", "orgChild4"]; - nodeAll = ` AND (profileSalary.${nextLevels[node]} IS NULL OR profileSalary.id IS NULL)`; + nodeAll = ` AND (profileSalary.${nextLevels[node]} IS NULL)`; } try { @@ -95,7 +94,7 @@ export class ProfileLeaveService { case 0: { const orgRoot = await this.orgRootRepository.findOne({ where: { id: nodeId } }); if (orgRoot) { - condition = "(profileSalary.orgRoot = :orgRoot OR profileSalary.id IS NULL)"; + condition = "(profileSalary.orgRoot = :orgRoot)"; params.orgRoot = orgRoot.orgRootName; } break; @@ -103,7 +102,7 @@ export class ProfileLeaveService { case 1: { const orgChild1 = await this.child1Repository.findOne({ where: { id: nodeId } }); if (orgChild1) { - condition = "(profileSalary.orgChild1 = :orgChild1 OR profileSalary.id IS NULL)"; + condition = "(profileSalary.orgChild1 = :orgChild1)"; params.orgChild1 = orgChild1.orgChild1Name; } break; @@ -111,7 +110,7 @@ export class ProfileLeaveService { case 2: { const orgChild2 = await this.child2Repository.findOne({ where: { id: nodeId } }); if (orgChild2) { - condition = "(profileSalary.orgChild2 = :orgChild2 OR profileSalary.id IS NULL)"; + condition = "(profileSalary.orgChild2 = :orgChild2)"; params.orgChild2 = orgChild2.orgChild2Name; } break; @@ -119,7 +118,7 @@ export class ProfileLeaveService { case 3: { const orgChild3 = await this.child3Repository.findOne({ where: { id: nodeId } }); if (orgChild3) { - condition = "(profileSalary.orgChild3 = :orgChild3 OR profileSalary.id IS NULL)"; + condition = "(profileSalary.orgChild3 = :orgChild3)"; params.orgChild3 = orgChild3.orgChild3Name; } break; @@ -127,7 +126,7 @@ export class ProfileLeaveService { case 4: { const orgChild4 = await this.child4Repository.findOne({ where: { id: nodeId } }); if (orgChild4) { - condition = "(profileSalary.orgChild4 = :orgChild4 OR profileSalary.id IS NULL)"; + condition = "(profileSalary.orgChild4 = :orgChild4)"; params.orgChild4 = orgChild4.orgChild4Name; } break; @@ -140,14 +139,13 @@ export class ProfileLeaveService { return { condition: condition + nodeAll, params }; } - /** - * สร้างเงื่อนไขการค้นหาตาม permission - */ + /** สร้างเงื่อนไขการค้นหาตาม permission */ async buildPermissionCondition( request: RequestWithUser, isAll?: boolean, + permissionType: string = "SYS_REGISTRY_OFFICER", ): Promise { - const _data = await new permission().PermissionOrgList(request, "SYS_REGISTRY_EMP"); + const _data = await new permission().PermissionOrgList(request, permissionType); let condition = "1=1"; let nodeAll = ""; const params: Record = {}; @@ -199,9 +197,7 @@ export class ProfileLeaveService { return { condition: condition + nodeAll, params }; } - /** - * แปลงข้อมูลพนักงานเป็น format ที่ต้องการ - */ + /** แปลงข้อมูลลูกจ้างก่อน response */ transformEmployeeData(employee: ProfileEmployee): any { const dateEmployment = employee.profileEmployeeEmployment?.length === 0 || !employee.profileEmployeeEmployment @@ -323,12 +319,10 @@ export class ProfileLeaveService { }; } - /** - * ค้นหาพนักงานที่พ้นจากราชการ - */ + /** ค้นหาลูกจ้างที่พ้นจากราชการ */ async getLeaveEmployees( request: RequestWithUser, - filter: LeaveEmployeeFilter, + filter: LeaveFilter, ): Promise<{ data: any[]; total: number }> { const { page, @@ -349,13 +343,13 @@ export class ProfileLeaveService { // สร้าง query conditions แบบ parallel const [nodeCondition, permissionCondition] = await Promise.all([ this.buildNodeCondition(node, nodeId, isAll), - this.buildPermissionCondition(request, isAll), + this.buildPermissionCondition(request, isAll, "SYS_REGISTRY_EMP"), ]); - const searchQuery = this.buildSearchQuery(searchField); + const searchQuery = this.buildSearchQuery(searchField, "profileEmployee"); // สร้าง main query - เปลี่ยนจาก leftJoinAndSelect เป็น leftJoin สำหรับ profileSalary - const queryBuilder = this.profileRepo + const queryBuilder = this.profileEmployeeRepo .createQueryBuilder("profileEmployee") .leftJoinAndSelect("profileEmployee.posLevel", "posLevel") .leftJoinAndSelect("profileEmployee.posType", "posType") @@ -384,7 +378,19 @@ export class ProfileLeaveService { ); }), ) - .andWhere("profileEmployee.employeeClass LIKE :type", { type: "PERM" }); + .andWhere("profileEmployee.employeeClass LIKE :type", { type: "PERM" }) + .andWhere( + new Brackets((qb) => { + qb.orWhere( + searchKeyword != undefined && searchKeyword != null && searchKeyword != "" + ? searchQuery + : "1=1", + { + keyword: `%${searchKeyword}%`, + }, + ); + }), + ); // เพิ่มเงื่อนไขการค้นหา if (posType) { @@ -393,7 +399,7 @@ export class ProfileLeaveService { if (posLevel) { queryBuilder.andWhere( - "CONCAT(posType.posTypeShortName,' ',posLevel.posLevelName) LIKE :keyword2", + "CONCAT(posType.posTypeShortName, ' ', posLevel.posLevelName) LIKE :keyword2", { keyword2: `${posLevel}` }, ); } @@ -406,8 +412,177 @@ export class ProfileLeaveService { queryBuilder.andWhere("profileEmployee.leaveType = :retireType", { retireType }); } - if (searchKeyword) { - queryBuilder.andWhere(searchQuery, { keyword: `%${searchKeyword}%` }); + // เพิ่ม permission และ node conditions + queryBuilder + .andWhere(permissionCondition.condition, permissionCondition.params) + .andWhere(nodeCondition.condition, nodeCondition.params); + + console.log("Permission Condition:", permissionCondition); + console.log("Node Condition:", nodeCondition); + // เพิ่ม sorting และ pagination + queryBuilder + .orderBy(sortBy, sort) + .skip((page - 1) * pageSize) + .take(pageSize); + + const [records, total] = await queryBuilder.getManyAndCount(); + + // print query for debug + // console.log("SQL Query:", queryBuilder.getSql()); + + // แปลงข้อมูลแบบ parallel + const data = await Promise.all( + records.map((record) => Promise.resolve(this.transformEmployeeData(record))), + ); + + return { data, total }; + } + + /** + * แปลงข้อมูลลูกจ้างก่อน response + */ + transformOfficerData(employee: Profile): any { + // ตรวจสอบว่า profileSalary มีข้อมูลหรือไม่ + const salary = + employee.profileSalary && employee.profileSalary.length > 0 + ? employee.profileSalary[0] + : null; + + const posNo = + salary?.posNoAbb && salary?.posNo + ? `${salary.posNoAbb} ${salary.posNo}` + : salary?.posNo || ""; + + const posExecutive = salary?.positionExecutive ? salary.positionExecutive : null; + + const root = salary?.orgRoot ? salary.orgRoot : null; + + // สร้าง organization hierarchy - ใช้ข้อมูลจาก temp fields ถ้า salary ไม่มี + const org = salary + ? [salary.orgChild4, salary.orgChild3, salary.orgChild2, salary.orgChild1, salary.orgRoot] + .filter(Boolean) + .join("\n") + : ["", "", "", "", ""].filter(Boolean).join("\n"); + + const orgRootShortName = salary?.posNoAbb ? salary.posNoAbb : null; + + return { + id: employee.id, + avatar: employee.avatar, + avatarName: employee.avatarName, + dateAppoint: employee.dateAppoint, + prefix: employee.prefix, + rank: employee.rank, + firstName: employee.firstName, + lastName: employee.lastName, + citizenId: employee.citizenId, + posLevel: employee.posLevel?.posLevelName || null, + posType: employee.posType?.posTypeName || null, + posLevelId: employee.posLevel?.id || null, + posTypeId: employee.posType?.id || null, + position: employee.position, + posExecutive, + posNo, + rootId: null, + root, + orgRootShortName, + orgRevisionId: null, + org, + }; + } + + /** + * ค้นหาข้าราชการที่พ้นจากราชการ + */ + async getLeaveOfficer( + request: RequestWithUser, + filter: LeaveFilter, + ): Promise<{ data: any[]; total: number }> { + const { + page, + pageSize, + searchField, + searchKeyword = "", + posType, + posLevel, + isProbation, + node, + nodeId, + isAll, + retireType, + sortBy = "profile.dateLeave", + sort, + } = filter; + + // สร้าง query conditions แบบ parallel + const [nodeCondition, permissionCondition] = await Promise.all([ + this.buildNodeCondition(node, nodeId, isAll), + this.buildPermissionCondition(request, isAll, "SYS_REGISTRY_OFFICER"), + ]); + + const searchQuery = this.buildSearchQuery(searchField); + + // สร้าง main query - เปลี่ยนจาก leftJoinAndSelect เป็น leftJoin สำหรับ profileSalary + const queryBuilder = this.profileRepo + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.posLevel", "posLevel") + .leftJoinAndSelect("profile.posType", "posType") + .leftJoin( + "profile.profileSalary", + "profileSalary", + "profileSalary.order = (SELECT MAX(ps.order) FROM profileSalary ps WHERE ps.profileId = profile.id and ps.positionName != 'เกษียณอายุราชการ')", + ) + .addSelect([ + "profileSalary.id", + "profileSalary.order", + "profileSalary.posNo", + "profileSalary.posNoAbb", + "profileSalary.orgRoot", + "profileSalary.orgChild1", + "profileSalary.orgChild2", + "profileSalary.orgChild3", + "profileSalary.orgChild4", + "profileSalary.positionExecutive", + ]) + .where( + new Brackets((qb) => { + qb.where("profile.isLeave = :isLeave", { isLeave: true }).orWhere( + "profile.isRetirement = :isRetirement", + { isRetirement: true }, + ); + }), + ) + .andWhere( + new Brackets((qb) => { + qb.orWhere( + searchKeyword != undefined && searchKeyword != null && searchKeyword != "" + ? searchQuery + : "1=1", + { + keyword: `%${searchKeyword}%`, + }, + ); + }), + ); + + // เพิ่มเงื่อนไขการค้นหา + if (posType) { + queryBuilder.andWhere("posType.posTypeName LIKE :keyword1", { keyword1: `${posType}` }); + } + + if (posLevel) { + queryBuilder.andWhere( + "CONCAT(posType.posTypeShortName, ' ', posLevel.posLevelName) LIKE :keyword2", + { keyword2: `${posLevel}` }, + ); + } + + if (isProbation !== undefined && isProbation !== null) { + queryBuilder.andWhere(`profile.isProbation = ${isProbation}`); + } + + if (retireType) { + queryBuilder.andWhere("profile.leaveType = :retireType", { retireType }); } // เพิ่ม permission และ node conditions @@ -423,9 +598,12 @@ export class ProfileLeaveService { const [records, total] = await queryBuilder.getManyAndCount(); + // print query for debug + // console.log("SQL Query:", queryBuilder.getSql()); + // แปลงข้อมูลแบบ parallel const data = await Promise.all( - records.map((record) => Promise.resolve(this.transformEmployeeData(record))), + records.map((record) => Promise.resolve(this.transformOfficerData(record))), ); return { data, total };