diff --git a/src/services/rabbitmq.ts b/src/services/rabbitmq.ts index 02880d8c..e06474b9 100644 --- a/src/services/rabbitmq.ts +++ b/src/services/rabbitmq.ts @@ -782,8 +782,9 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { // 2. Batch load profiles ทั้งหมดในครั้งเดียว (แก้ปัญหา N+1 Query) const profilesMap = new Map(); if (profileIds.length > 0) { - const profiles = await repoProfile.findBy({ - id: In(profileIds), + const profiles = await repoProfile.find({ + where: { id: In(profileIds) }, + relations: ["posType", "posLevel"], }); profiles.forEach((p) => profilesMap.set(p.id, p)); } @@ -878,6 +879,18 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { item.orgRoot?.orgRootShortName, ].find((name) => typeof name === "string" && name.trim().length > 0) ?? _null; + // ถ้าเป็นตำแหน่งนั่งทับ (isSit = true) และมีคนครอง ใช้ตำแหน่งจาก profile แทน + let positionName = selectedPosition?.positionName ?? _null; + let posTypeName = selectedPosition?.posType?.posTypeName ?? _null; + let posLevelName = selectedPosition?.posLevel?.posLevelName ?? _null; + let posExecutiveName = selectedPosition?.posExecutive?.posExecutiveName ?? _null; + if (item.isSit && nextHolderProfile) { + positionName = nextHolderProfile.position ?? _null; + posTypeName = nextHolderProfile.posType?.posTypeName ?? _null; + posLevelName = nextHolderProfile.posLevel?.posLevelName ?? _null; + posExecutiveName = nextHolderProfile.posExecutive ?? _null; + } + historyRowsToSave.push({ ancestorDNA: item.ancestorDNA ? item.ancestorDNA : _null, prefix: nextHolderProfile?.prefix || _null, @@ -887,10 +900,10 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { posMasterNoPrefix: item.posMasterNoPrefix ?? _null, posMasterNo: item.posMasterNo ?? _null, posMasterNoSuffix: item.posMasterNoSuffix ?? _null, - position: selectedPosition?.positionName ?? _null, - posType: selectedPosition?.posType?.posTypeName ?? _null, - posLevel: selectedPosition?.posLevel?.posLevelName ?? _null, - posExecutive: selectedPosition?.posExecutive?.posExecutiveName ?? _null, + position: positionName, + posType: posTypeName, + posLevel: posLevelName, + posExecutive: posExecutiveName, profileId: _null, rootDnaId: item.orgRoot?.ancestorDNA || _null, child1DnaId: item.orgChild1?.ancestorDNA || _null,