From b69f8a6c085ab7cd0b8aa723c58f15759a4c9a64 Mon Sep 17 00:00:00 2001 From: harid Date: Thu, 2 Apr 2026 11:27:39 +0700 Subject: [PATCH] =?UTF-8?q?fix=20=E0=B8=AA=E0=B9=88=E0=B8=87=E0=B8=AD?= =?UTF-8?q?=E0=B8=AD=E0=B8=81=E0=B8=84=E0=B8=B3=E0=B8=AA=E0=B8=B1=E0=B9=88?= =?UTF-8?q?=E0=B8=87=E0=B8=A3=E0=B8=B2=E0=B8=A2=E0=B8=8A=E0=B8=B7=E0=B9=88?= =?UTF-8?q?=E0=B8=AD=E0=B8=9C=E0=B8=B9=E0=B9=89=E0=B8=AA=E0=B8=AD=E0=B8=9A?= =?UTF-8?q?=E0=B8=9C=E0=B9=88=E0=B8=B2=E0=B8=99=20=E0=B8=A3=E0=B8=B2?= =?UTF-8?q?=E0=B8=A2=E0=B8=8A=E0=B8=B7=E0=B9=88=E0=B8=AD=E0=B9=84=E0=B8=A1?= =?UTF-8?q?=E0=B9=88=E0=B9=81=E0=B8=AA=E0=B8=94=E0=B8=87=E0=B9=83=E0=B8=99?= =?UTF-8?q?=E0=B9=82=E0=B8=84=E0=B8=A3=E0=B8=87=E0=B8=AA=E0=B8=A3=E0=B9=89?= =?UTF-8?q?=E0=B8=B2=E0=B8=87=20#2402?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/CommandController.ts | 37 +++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 2993f480..6e7cb637 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -6844,12 +6844,36 @@ export class CommandController extends Controller { } //Position if (item.bodyPosition && item.bodyPosition != null) { - const posMaster = await this.posMasterRepository.findOne({ - where: { id: item.bodyPosition.posmasterId }, + // STEP 1: หา posMaster ที่จะใช้งานตาม id ที่ส่งมา (อาจเป็นตำแหน่งเก่าหรือใหม่ก็ได้) + let posMaster = await this.posMasterRepository.findOne({ + where: { + id: item.bodyPosition.posmasterId, + }, + relations: { orgRevision: true } }); + + // เช็คว่า posMaster ที่หามาอยู่ในโครงสร้างปัจจุบันหรือไม่ + const isCurrent = posMaster?.orgRevision?.orgRevisionIsCurrent === true && + posMaster?.orgRevision?.orgRevisionIsDraft === false; + + // ถ้าไม่อยู่ในโครงสร้างปัจจุบัน ให้หาตัวใหม่จาก ancestorDNA + if (!isCurrent && posMaster?.ancestorDNA) { + posMaster = await this.posMasterRepository.findOne({ + where: { + ancestorDNA: posMaster.ancestorDNA, + orgRevision: { + orgRevisionIsCurrent: true, + orgRevisionIsDraft: false + } + }, + relations: { orgRevision: true } + }); + } + if (posMaster == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้"); + // STEP 2: เคลียร์ข้อมูลตำแหน่งเก่าที่ครองอยู่ ในโครงสร้างปัจจุบัน const posMasterOld = await this.posMasterRepository.findOne({ where: { current_holderId: profile.id, @@ -6857,10 +6881,12 @@ export class CommandController extends Controller { }, }); if (posMasterOld != null) { + // เคลียร์คนครองเก่าออกจากตำแหน่งเดิม posMasterOld.current_holderId = null; posMasterOld.lastUpdatedAt = new Date(); } + // หา position เก่าที่เลือกไว้ แล้วเคลียร์การเลือก const positionOld = await this.positionRepository.findOne({ where: { posMasterId: posMasterOld?.id, @@ -6872,9 +6898,10 @@ export class CommandController extends Controller { await this.positionRepository.save(positionOld); } + // STEP 3: เคลียร์ position ที่เลือกไว้อื่นๆ ใน posMaster ตัวใหม่ const checkPosition = await this.positionRepository.find({ where: { - posMasterId: item.bodyPosition.posmasterId, + posMasterId: posMaster.id, positionIsSelected: true, }, }); @@ -6886,6 +6913,7 @@ export class CommandController extends Controller { await this.positionRepository.save(clearPosition); } + // STEP 4: กำหนดคนครองใหม่ให้กับ posMaster posMaster.current_holderId = profile.id; posMaster.lastUpdatedAt = new Date(); // posMaster.conditionReason = _null; @@ -6896,10 +6924,11 @@ export class CommandController extends Controller { } await this.posMasterRepository.save(posMaster); + // STEP 5: กำหนด position ใหม่ const positionNew = await this.positionRepository.findOne({ where: { id: item.bodyPosition.positionId, - posMasterId: item.bodyPosition.posmasterId, + posMasterId: posMaster.id, // ใช้ id ของ posMaster ตัวใหม่ }, }); if (positionNew != null) {