diff --git a/src/services/rabbitmq.ts b/src/services/rabbitmq.ts index 326cb9a1..8e9f11f4 100644 --- a/src/services/rabbitmq.ts +++ b/src/services/rabbitmq.ts @@ -85,7 +85,7 @@ export async function init() { console.log("[AMQ] Listening for message..."); createConsumer(queue, channel, handler), //----> (3) Process Consumer - createConsumer(queue_org, channel, handler_org); + createConsumer(queue_org, channel, handler_org); createConsumer(queue_org_draft, channel, handler_org_draft); createConsumer(queue_command_noti, channel, handler_command_noti); // createConsumer(queue2, channel, handler2); @@ -561,6 +561,14 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { "positions.posExecutive", ], }); + + const oldPosMasters = await repoPosmaster.find({ + where: { + orgRevisionId: orgRevisionPublish!.id, + }, + select: ['id', 'current_holderId', 'ancestorDNA'] + }); + // Task #2160 ดึง posMasterAssign ของ revision เดิม const oldposMasterAssigns = await posMasterAssignRepository.find({ relations: ["posMaster"], @@ -613,9 +621,20 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { posMasterIdMap.set(pm.ancestorDNA?.trim() ?? '', pm.id); } + const oldPosMasterMap = new Map(); + for (const oldPm of oldPosMasters) { + const dna = oldPm.ancestorDNA?.trim(); + if (dna) { + oldPosMasterMap.set(dna, oldPm); + } + } + const _null: any = null; for (const item of posMaster) { + const dna = item.ancestorDNA?.trim(); + const oldPm = dna ? oldPosMasterMap.get(dna) : null; + // Task #2160 Clone posMasterAssign const assigns = assignMap.get(item.ancestorDNA); if (assigns && assigns.length > 0) { @@ -651,18 +670,26 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { await repoProfile.save(profile); } } - item.current_holderId = item.next_holderId; - // item.conditionReason = _null; - // if (item.current_holderId) { - // item.conditionReason = _null; - // item.isCondition = false; - // } - item.next_holderId = null; - item.lastUpdateUserId = lastUpdateUserId; - item.lastUpdateFullName = lastUpdateFullName; - item.lastUpdatedAt = lastUpdatedAt; - await repoPosmaster.save(item).catch((e) => console.log(e)); - await CreatePosMasterHistoryOfficer(item.id, null); + // item.current_holderId = item.next_holderId; + // item.next_holderId = null; + // item.lastUpdateUserId = lastUpdateUserId; + // item.lastUpdateFullName = lastUpdateFullName; + // item.lastUpdatedAt = lastUpdatedAt; + await repoPosmaster.update(item.id, { + current_holderId: item.next_holderId, + next_holderId: null, + lastUpdateUserId, + lastUpdateFullName, + lastUpdatedAt, + }); + + const oldHolderId = oldPm ? oldPm.current_holderId : null; + const newHolderId = item ? item.next_holderId : null; + const isHolderChanged = oldHolderId !== newHolderId; + + if (isHolderChanged) { + await CreatePosMasterHistoryOfficer(item.id, null); + } } for (const act of oldposMasterAct) {