diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 37393e14..430da630 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -1257,15 +1257,7 @@ export class PositionController extends Controller { ) { await new permission().PermissionUpdate(request, "SYS_ORG"); const posMaster = await this.posMasterRepository.findOne({ - relations: [ - "positions", - "orgRevision", - "orgRoot", - "orgChild1", - "orgChild2", - "orgChild3", - "orgChild4", - ], + relations: ["positions", "orgRevision", "orgRoot", "orgChild1", "orgChild2", "orgChild3", "orgChild4"], where: { id: id }, }); if (!posMaster) { @@ -2411,16 +2403,16 @@ export class PositionController extends Controller { ? "posMaster.orgRootId IN (:...root)" : "posMaster.orgRootId is null" : "1=1", - { root: _data.root }, + { root: _data.root } ) .andWhere( _data.child1 != undefined && _data.child1 != null ? _data.child1[0] != null ? "posMaster.orgChild1Id IN (:...child1)" - : // : `posMaster.orgChild1Id is ${_data.privilege == "PARENT" ? "not null" : "null"}` - `posMaster.orgChild1Id is null` + // : `posMaster.orgChild1Id is ${_data.privilege == "PARENT" ? "not null" : "null"}` + : `posMaster.orgChild1Id is null` : "1=1", - { child1: _data.child1 }, + { child1: _data.child1 } ) .andWhere( _data.child2 != undefined && _data.child2 != null @@ -2451,27 +2443,26 @@ export class PositionController extends Controller { { child4: _data.child4, }, - ); + ) // .andWhere(checkChildConditions) // .andWhere(typeCondition) // .andWhere(revisionCondition); if (body.keyword != null && body.keyword != "") { - query - .orWhere( - new Brackets((qb) => { - qb.andWhere( - body.keyword != null && body.keyword != "" - ? body.isAll == false - ? searchShortName - : `CASE WHEN posMaster.orgChild1 is null THEN ${searchShortName0} WHEN posMaster.orgChild2 is null THEN ${searchShortName1} WHEN posMaster.orgChild3 is null THEN ${searchShortName2} WHEN posMaster.orgChild4 is null THEN ${searchShortName3} ELSE ${searchShortName4} END LIKE '%${body.keyword}%'` - : "1=1", - ) - .andWhere(checkChildConditions) - .andWhere(typeCondition) - .andWhere(revisionCondition); - }), - ) + query.orWhere( + new Brackets((qb) => { + qb.andWhere( + body.keyword != null && body.keyword != "" + ? body.isAll == false + ? searchShortName + : `CASE WHEN posMaster.orgChild1 is null THEN ${searchShortName0} WHEN posMaster.orgChild2 is null THEN ${searchShortName1} WHEN posMaster.orgChild3 is null THEN ${searchShortName2} WHEN posMaster.orgChild4 is null THEN ${searchShortName3} ELSE ${searchShortName4} END LIKE '%${body.keyword}%'` + : "1=1", + ) + .andWhere(checkChildConditions) + .andWhere(typeCondition) + .andWhere(revisionCondition); + }), + ) .orWhere( new Brackets((qb) => { qb.andWhere( @@ -2980,50 +2971,50 @@ export class PositionController extends Controller { const type0LastPosMasterNo = requestBody.type == 0 ? await this.posMasterRepository.find({ - where: { - orgRootId: requestBody.id, - orgChild1Id: IsNull(), - }, - }) + where: { + orgRootId: requestBody.id, + orgChild1Id: IsNull(), + }, + }) : []; const type1LastPosMasterNo = requestBody.type == 1 ? await this.posMasterRepository.find({ - where: { - orgChild1Id: requestBody.id, - orgChild2Id: IsNull(), - }, - }) + where: { + orgChild1Id: requestBody.id, + orgChild2Id: IsNull(), + }, + }) : []; const type2LastPosMasterNo = requestBody.type == 2 ? await this.posMasterRepository.find({ - where: { - orgChild2Id: requestBody.id, - orgChild3Id: IsNull(), - }, - }) + where: { + orgChild2Id: requestBody.id, + orgChild3Id: IsNull(), + }, + }) : []; const type3LastPosMasterNo = requestBody.type == 3 ? await this.posMasterRepository.find({ - where: { - orgChild3Id: requestBody.id, - orgChild4Id: IsNull(), - }, - }) + where: { + orgChild3Id: requestBody.id, + orgChild4Id: IsNull(), + }, + }) : []; const type4LastPosMasterNo = requestBody.type == 4 ? await this.posMasterRepository.find({ - where: { - orgChild4Id: requestBody.id, - }, - }) + where: { + orgChild4Id: requestBody.id, + }, + }) : []; const allLastPosMasterNo = [ @@ -3857,7 +3848,7 @@ export class PositionController extends Controller { _profile.posMasterNo = getPosMasterNo(dataMaster); _profile.org = getOrgFullName(dataMaster); // ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ - if (!dataMaster.isSit) { + if(!dataMaster.isSit){ _profile.position = _position.positionName; _profile.posTypeId = _position.posTypeId; _profile.posLevelId = _position.posLevelId; @@ -5202,9 +5193,9 @@ export class PositionController extends Controller { } /** - * API รายการตำแหน่งติดเงื่อนไข + * API รายการอัตรากำลัง * - * @summary รายการตำแหน่งติดเงื่อนไข + * @summary ORG_070 - รายการอัตรากำลัง (ADMIN) #56 * */ @Post("master/position-condition") @@ -5215,7 +5206,7 @@ export class PositionController extends Controller { id: string; revisionId: string; type: number; - isAll: boolean; // true คือเลือกเฉพาะตำแหน่งติดเงื่อนไข / false คือเลือกตำแหน่งทั้งหมด + isAll: boolean; page: number; pageSize: number; keyword?: string; @@ -5235,7 +5226,7 @@ export class PositionController extends Controller { let level: any = resolveNodeLevel(orgDna); const cannotViewRootPosMaster = - _data.privilege === "PARENT" || + (_data.privilege === "PARENT") || (_data.privilege === "BROTHER" && level > 1) || (_data.privilege === "CHILD" && level > 0) || (_data.privilege === "NORMAL" && level != 0); @@ -5267,46 +5258,46 @@ export class PositionController extends Controller { typeCondition = { ...(cannotViewRootPosMaster ? { orgRootId: null } : { orgRootId: body.id }), }; - // if (!body.isAll) { - // checkChildConditions = { - // orgChild1Id: IsNull(), - // }; - // searchShortName = `CONCAT(orgRoot.orgRootShortName," ",COALESCE(posMaster.posMasterNoPrefix, ""),posMaster.posMasterNo,COALESCE(posMaster.posMasterNoSuffix, "")) like '%${body.keyword}%'`; - // } else { - // } + if (!body.isAll) { + checkChildConditions = { + orgChild1Id: IsNull(), + }; + searchShortName = `CONCAT(orgRoot.orgRootShortName," ",COALESCE(posMaster.posMasterNoPrefix, ""),posMaster.posMasterNo,COALESCE(posMaster.posMasterNoSuffix, "")) like '%${body.keyword}%'`; + } else { + } } else if (body.type === 1) { typeCondition = { ...(cannotViewChild1PosMaster ? { orgChild1Id: null } : { orgChild1Id: body.id }), }; - // if (!body.isAll) { - // checkChildConditions = { - // orgChild2Id: IsNull(), - // }; - // searchShortName = `CONCAT(orgChild1.orgChild1ShortName," ",COALESCE(posMaster.posMasterNoPrefix, ""),posMaster.posMasterNo,COALESCE(posMaster.posMasterNoSuffix, "")) like '%${body.keyword}%'`; - // } else { - // } + if (!body.isAll) { + checkChildConditions = { + orgChild2Id: IsNull(), + }; + searchShortName = `CONCAT(orgChild1.orgChild1ShortName," ",COALESCE(posMaster.posMasterNoPrefix, ""),posMaster.posMasterNo,COALESCE(posMaster.posMasterNoSuffix, "")) like '%${body.keyword}%'`; + } else { + } } else if (body.type === 2) { typeCondition = { ...(cannotViewChild2PosMaster ? { orgChild2Id: null } : { orgChild2Id: body.id }), }; - // if (!body.isAll) { - // checkChildConditions = { - // orgChild3Id: IsNull(), - // }; - // searchShortName = `CONCAT(orgChild2.orgChild2ShortName," ",COALESCE(posMaster.posMasterNoPrefix, ""),posMaster.posMasterNo,COALESCE(posMaster.posMasterNoSuffix, "")) like '%${body.keyword}%'`; - // } else { - // } + if (!body.isAll) { + checkChildConditions = { + orgChild3Id: IsNull(), + }; + searchShortName = `CONCAT(orgChild2.orgChild2ShortName," ",COALESCE(posMaster.posMasterNoPrefix, ""),posMaster.posMasterNo,COALESCE(posMaster.posMasterNoSuffix, "")) like '%${body.keyword}%'`; + } else { + } } else if (body.type === 3) { typeCondition = { ...(cannotViewChild3PosMaster ? { orgChild3Id: null } : { orgChild3Id: body.id }), }; - // if (!body.isAll) { - // checkChildConditions = { - // orgChild4Id: IsNull(), - // }; - // searchShortName = `CONCAT(orgChild3.orgChild3ShortName," ",COALESCE(posMaster.posMasterNoPrefix, ""),posMaster.posMasterNo,COALESCE(posMaster.posMasterNoSuffix, "")) like '%${body.keyword}%'`; - // } else { - // } + if (!body.isAll) { + checkChildConditions = { + orgChild4Id: IsNull(), + }; + searchShortName = `CONCAT(orgChild3.orgChild3ShortName," ",COALESCE(posMaster.posMasterNoPrefix, ""),posMaster.posMasterNo,COALESCE(posMaster.posMasterNoSuffix, "")) like '%${body.keyword}%'`; + } else { + } } else if (body.type === 4) { typeCondition = { ...(cannotViewChild4PosMaster ? { orgChild4Id: null } : { orgChild4Id: body.id }), @@ -5379,7 +5370,7 @@ export class PositionController extends Controller { (masterId.length > 0 ? { id: In(masterId) } : { posMasterNo: Like(`%${body.keyword}%`) })), - ...(!body.isAll && { isCondition: true }), + current_holderId: IsNull(), }, ]; let [posMaster, total] = await AppDataSource.getRepository(PosMaster) @@ -5448,15 +5439,15 @@ export class PositionController extends Controller { new Brackets((qb) => { qb.andWhere( body.keyword != null && body.keyword != "" - ? `CASE WHEN posMaster.orgChild1 is null THEN ${searchShortName0} WHEN posMaster.orgChild2 is null THEN ${searchShortName1} WHEN posMaster.orgChild3 is null THEN ${searchShortName2} WHEN posMaster.orgChild4 is null THEN ${searchShortName3} ELSE ${searchShortName4} END LIKE '%${body.keyword}%'` + ? body.isAll == false + ? searchShortName + : `CASE WHEN posMaster.orgChild1 is null THEN ${searchShortName0} WHEN posMaster.orgChild2 is null THEN ${searchShortName1} WHEN posMaster.orgChild3 is null THEN ${searchShortName2} WHEN posMaster.orgChild4 is null THEN ${searchShortName3} ELSE ${searchShortName4} END LIKE '%${body.keyword}%'` : "1=1", ) .andWhere(checkChildConditions) .andWhere(typeCondition) - .andWhere(revisionCondition); - if (!body.isAll) { - qb.andWhere({ isCondition: true }); - } + .andWhere(revisionCondition) + .andWhere({ current_holderId: IsNull() }); }), ) .orWhere( @@ -5466,10 +5457,8 @@ export class PositionController extends Controller { ) .andWhere(checkChildConditions) .andWhere(typeCondition) - .andWhere(revisionCondition); - if (!body.isAll) { - qb.andWhere({ isCondition: true }); - } + .andWhere(revisionCondition) + .andWhere({ current_holderId: IsNull() }); }), ) .orderBy("orgRoot.orgRootOrder", "ASC")