From f1c8ecf6993269d008fd537b11589f916e2f562d Mon Sep 17 00:00:00 2001 From: Adisak Date: Mon, 20 Apr 2026 16:01:38 +0700 Subject: [PATCH] insert position to profile --- src/controllers/CommandController.ts | 14 ++++++---- src/controllers/PositionController.ts | 9 +++--- src/services/rabbitmq.ts | 40 +++++++++++++++------------ 3 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index e505aa3a..e97abce5 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -3722,6 +3722,9 @@ export class CommandController extends Controller { // ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ if (positionNew != null) { positionNew.positionIsSelected = true; + // อัพเดท org และ posMasterNo ตลอดไม่ต้องดัก isSit + profile.posMasterNo = getPosMasterNo(posMaster); + profile.org = getOrgFullName(posMaster); if(!posMaster.isSit){ profile.posLevelId = positionNew.posLevelId; profile.posTypeId = positionNew.posTypeId; @@ -3730,8 +3733,6 @@ export class CommandController extends Controller { profile.posExecutive = positionNew.posExecutive?.posExecutiveName ?? null; profile.positionArea = positionNew.positionArea ?? null; profile.positionExecutiveField = positionNew.positionExecutiveField ?? null; - profile.posMasterNo = getPosMasterNo(posMaster); - profile.org = getOrgFullName(posMaster); } profile.amount = item.amount ?? null; profile.amountSpecial = item.amountSpecial ?? null; @@ -6998,6 +6999,9 @@ export class CommandController extends Controller { // ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ if (positionNew != null) { positionNew.positionIsSelected = true; + // อัพเดท org และ posMasterNo ตลอดไม่ต้องดัก isSit + profile.posMasterNo = getPosMasterNo(posMaster); + profile.org = getOrgFullName(posMaster); if(!posMaster.isSit){ profile.posLevelId = positionNew.posLevelId; profile.posTypeId = positionNew.posTypeId; @@ -7006,12 +7010,10 @@ export class CommandController extends Controller { profile.posExecutive = positionNew.posExecutive?.posExecutiveName ?? null; profile.positionArea = positionNew.positionArea ?? null; profile.positionExecutiveField = positionNew.positionExecutiveField ?? null; - profile.posMasterNo = getPosMasterNo(posMaster); - profile.org = getOrgFullName(posMaster); // profile.dateStart = new Date(); - await this.profileRepository.save(profile, { data: req }); - setLogDataDiff(req, { before, after: profile }); } + await this.profileRepository.save(profile, { data: req }); + setLogDataDiff(req, { before, after: profile }); await this.positionRepository.save(positionNew, { data: req }); } // await CreatePosMasterHistoryOfficer(posMaster.id, req); diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 23806e6c..a1378892 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -3829,6 +3829,9 @@ export class PositionController extends Controller { relations: ["posExecutive"], }); if (_position) { + // อัพเดท org และ posMasterNo ตลอดไม่ต้องดัก isSit + _profile.posMasterNo = getPosMasterNo(dataMaster); + _profile.org = getOrgFullName(dataMaster); // ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ if(!dataMaster.isSit){ _profile.position = _position.positionName; @@ -3838,11 +3841,9 @@ export class PositionController extends Controller { _profile.posExecutive = _position.posExecutive?.posExecutiveName ?? undefined; _profile.positionArea = _position.positionArea ?? undefined; _profile.positionExecutiveField = _position.positionExecutiveField ?? undefined; - _profile.posMasterNo = getPosMasterNo(dataMaster); - _profile.org = getOrgFullName(dataMaster); - await this.profileRepository.save(_profile); - setLogDataDiff(request, { before, after: _profile }); } + await this.profileRepository.save(_profile); + setLogDataDiff(request, { before, after: _profile }); } } dataMaster.current_holderId = requestBody.profileId; diff --git a/src/services/rabbitmq.ts b/src/services/rabbitmq.ts index 5341d03c..6ba40258 100644 --- a/src/services/rabbitmq.ts +++ b/src/services/rabbitmq.ts @@ -652,29 +652,33 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { await posMasterAssignRepository.save(newAssigns); } - // ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ - if (item.next_holderId != null && !item.isSit) { + // อัพเดท org และ posMasterNo ตลอดไม่ต้องดัก isSit + if (item.next_holderId != null) { const profile = await repoProfile.findOne({ where: { id: item.next_holderId == null ? "" : item.next_holderId }, }); - if (profile != null && item.positions.length > 0) { - let position = await item.positions.find((x) => x.positionIsSelected == true); - if (position == null) { - position = await item.positions.find((x) => x.posLevelId == profile?.posLevelId); - if (position == null) { - position = await item.positions.sort((a, b) => a.orderNo - b.orderNo)[0]; - } - } - - profile.posLevelId = position?.posLevelId ?? _null; - profile.posTypeId = position?.posTypeId ?? _null; - profile.position = position?.positionName ?? _null; - profile.positionField = position?.positionField ?? _null; - profile.posExecutive = position?.posExecutive?.posExecutiveName ?? _null; - profile.positionArea = position?.positionArea ?? _null; - profile.positionExecutiveField = position?.positionExecutiveField ?? _null; + if (profile != null) { profile.posMasterNo = getPosMasterNo(item) ?? _null; profile.org = getOrgFullName(item) ?? _null; + + // ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ + if (!item.isSit && item.positions.length > 0) { + let position = await item.positions.find((x) => x.positionIsSelected == true); + if (position == null) { + position = await item.positions.find((x) => x.posLevelId == profile?.posLevelId); + if (position == null) { + position = await item.positions.sort((a, b) => a.orderNo - b.orderNo)[0]; + } + } + + profile.posLevelId = position?.posLevelId ?? _null; + profile.posTypeId = position?.posTypeId ?? _null; + profile.position = position?.positionName ?? _null; + profile.positionField = position?.positionField ?? _null; + profile.posExecutive = position?.posExecutive?.posExecutiveName ?? _null; + profile.positionArea = position?.positionArea ?? _null; + profile.positionExecutiveField = position?.positionExecutiveField ?? _null; + } await repoProfile.save(profile); } }