fixed#1568 แก้ไขรายการตำแหน่งติดเงื่อนไข

This commit is contained in:
Warunee Tamkoo 2026-04-24 11:38:45 +07:00
parent 8912e83227
commit 5980c140f0

View file

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