condition ที่ชื่อตำแหน่ง, ประเภท และ ระดับซ้ำกัน ให้ใช้แถวลำดับแรก
All checks were successful
Build & Deploy on Dev / build (push) Successful in 55s

This commit is contained in:
harid 2026-05-22 13:55:55 +07:00
parent ce114cf769
commit 2ce104b852

View file

@ -3834,6 +3834,7 @@ export class CommandController extends Controller {
const positionBy7Fields = await this.positionRepository.findOne({ const positionBy7Fields = await this.positionRepository.findOne({
where: whereCondition, where: whereCondition,
relations: ["posExecutive"], relations: ["posExecutive"],
order: { orderNo: "ASC" }
}); });
if (positionBy7Fields) { if (positionBy7Fields) {
@ -3849,10 +3850,11 @@ export class CommandController extends Controller {
where: { where: {
posMasterId: posMaster.id, posMasterId: posMaster.id,
positionName: item.positionName, positionName: item.positionName,
posTypeId: item.positionType, // positionType = posTypeId posTypeId: item.positionType,
posLevelId: item.positionLevel, // positionLevel = posLevelId posLevelId: item.positionLevel,
}, },
relations: ["posExecutive"], relations: ["posExecutive"],
order: { orderNo: "ASC" }
}); });
if (positionBy3Fields) { if (positionBy3Fields) {
@ -4138,6 +4140,7 @@ export class CommandController extends Controller {
commandCode?: string | null; commandCode?: string | null;
commandName?: string | null; commandName?: string | null;
remark: string | null; remark: string | null;
positionId?: string | null;
positionTypeNew?: string | null; positionTypeNew?: string | null;
positionLevelNew?: string | null; positionLevelNew?: string | null;
positionNameNew?: string | null; positionNameNew?: string | null;
@ -4403,11 +4406,36 @@ export class CommandController extends Controller {
// posMaster.isCondition = false; // posMaster.isCondition = false;
await this.posMasterRepository.save(posMaster); await this.posMasterRepository.save(posMaster);
// Match position ตามลำดับ priority // Match position ตามลำดับ priority:
// Condition 1: match จาก positionId
// Condition 2: match 7 ฟิลด์ (positionName, posTypeId, posLevelId, positionField, positionArea, positionExecutiveField, posExecutiveId)
// Condition 3: match 3 ฟิลด์ (positionName, posTypeId, posLevelId)
// Fallback: เลือก position แรกใน posMaster
let positionNew: Position | null = null; let positionNew: Position | null = null;
// CONDITION 1: Match 7 ฟิลด์ (ไม่มี positionId ใน body สำหรับกรณีพักราชการ) // ═══════════════════════════════════════════════════════════
if (item.positionNameNew && item.positionTypeNew && item.positionLevelNew) { // CONDITION 1: เช็คจาก positionId ตรง
// ═══════════════════════════════════════════════════════════
if (item.positionId) {
const positionById = await this.positionRepository.findOne({
where: {
id: item.positionId,
posMasterId: posMaster.id, // ต้องอยู่ใน posMaster ที่ถูกต้อง
},
relations: ["posExecutive"],
});
if (positionById) {
positionNew = positionById;
}
}
// ═══════════════════════════════════════════════════════════
// CONDITION 2: Match 7 ฟิลด์ (ถ้า Condition 1 ไม่ match)
// ═══════════════════════════════════════════════════════════
if (!positionNew && item.positionNameNew && item.positionTypeNew && item.positionLevelNew) {
// สร้าง where clause แบบ dynamic - ใส่เฉพาะฟิลด์ที่มีค่า
const whereCondition: any = { const whereCondition: any = {
posMasterId: posMaster.id, posMasterId: posMaster.id,
positionName: item.positionNameNew, positionName: item.positionNameNew,
@ -4431,6 +4459,7 @@ export class CommandController extends Controller {
const positionBy7Fields = await this.positionRepository.findOne({ const positionBy7Fields = await this.positionRepository.findOne({
where: whereCondition, where: whereCondition,
relations: ["posExecutive"], relations: ["posExecutive"],
order: { orderNo: "ASC" }
}); });
if (positionBy7Fields) { if (positionBy7Fields) {
@ -4438,7 +4467,9 @@ export class CommandController extends Controller {
} }
} }
// CONDITION 2: Match 3 ฟิลด์ (ถ้า Condition 1 ไม่ match) // ═══════════════════════════════════════════════════════════
// CONDITION 3: Match 3 ฟิลด์ (ถ้า Condition 2 ไม่ match)
// ═══════════════════════════════════════════════════════════
if (!positionNew && item.positionNameNew && item.positionTypeNew && item.positionLevelNew) { if (!positionNew && item.positionNameNew && item.positionTypeNew && item.positionLevelNew) {
const positionBy3Fields = await this.positionRepository.findOne({ const positionBy3Fields = await this.positionRepository.findOne({
where: { where: {
@ -4448,6 +4479,7 @@ export class CommandController extends Controller {
posLevelId: item.positionLevelNew, posLevelId: item.positionLevelNew,
}, },
relations: ["posExecutive"], relations: ["posExecutive"],
order: { orderNo: "ASC" }
}); });
if (positionBy3Fields) { if (positionBy3Fields) {
@ -7305,6 +7337,7 @@ export class CommandController extends Controller {
const positionBy7Fields = await this.positionRepository.findOne({ const positionBy7Fields = await this.positionRepository.findOne({
where: whereCondition, where: whereCondition,
relations: ["posExecutive"], relations: ["posExecutive"],
order: { orderNo: "ASC" }
}); });
if (positionBy7Fields) { if (positionBy7Fields) {
@ -7324,6 +7357,7 @@ export class CommandController extends Controller {
posLevelId: item.bodyPosition.posLevelId, posLevelId: item.bodyPosition.posLevelId,
}, },
relations: ["posExecutive"], relations: ["posExecutive"],
order: { orderNo: "ASC" }
}); });
if (positionBy3Fields) { if (positionBy3Fields) {