diff --git a/src/entities/PosMasterAssign.ts b/src/entities/PosMasterAssign.ts index 056b08fb..a0e2a89e 100644 --- a/src/entities/PosMasterAssign.ts +++ b/src/entities/PosMasterAssign.ts @@ -35,4 +35,10 @@ export class CreatePosMaster { posMasterNoPrefix: string; } +export class PosMasterAssignDTO { + id: string; + posMasterId: string; + assignId: string; +} + export type UpdatePosMaster = Partial; diff --git a/src/services/rabbitmq.ts b/src/services/rabbitmq.ts index 24ea581d..e1af0adb 100644 --- a/src/services/rabbitmq.ts +++ b/src/services/rabbitmq.ts @@ -17,7 +17,7 @@ import { OrgChild2 } from "../entities/OrgChild2"; import { OrgChild3 } from "../entities/OrgChild3"; import { OrgChild4 } from "../entities/OrgChild4"; import { OrgRoot } from "../entities/OrgRoot"; -import { PosMasterAssign } from "../entities/PosMasterAssign"; +import { PosMasterAssign, PosMasterAssignDTO } from "../entities/PosMasterAssign"; import { Position } from "../entities/Position"; import { In, Not } from "typeorm"; import { PosMasterAct } from "../entities/PosMasterAct"; @@ -561,7 +561,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { "positions.posExecutive", ], }); - // ดึง posMasterAssign ของ revision เดิม xxx + // Task #2160 ดึง posMasterAssign ของ revision เดิม const oldposMasterAssigns = await posMasterAssignRepository.find({ relations: ["posMaster"], where: { @@ -570,14 +570,18 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { }, }, }); - // สร้าง Map: ancestorDNA → posMasterAssign[] - const assignMap = new Map(); + // สร้าง assignMap เอาไว้เก็บ posMasterAssign.ancestorDNA ของ revision เดิม + const assignMap = new Map(); for (const posmasterAssign of oldposMasterAssigns) { const dna = posmasterAssign.posMaster.ancestorDNA; if (!assignMap.has(dna)) { assignMap.set(dna, []); } - assignMap.get(dna)!.push(posmasterAssign); + assignMap.get(dna)!.push({ + id: posmasterAssign.id, + posMasterId: posmasterAssign.posMasterId, + assignId: posmasterAssign.assignId + }); } // ดึง posMasterAct ของ revision เดิม xxx @@ -612,18 +616,18 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { const _null: any = null; for (const item of posMaster) { - // Clone posMasterAssign xxx + // Task #2160 Clone posMasterAssign const assigns = assignMap.get(item.ancestorDNA); if (assigns && assigns.length > 0) { const newAssigns = assigns.map(({ id, ...fields }) => ({ ...fields, // copy ทุก field ยกเว้น id posMasterId: item.id, // ผูกกับ posMasterId ใหม่ - createdAt: new Date(), - createdFullName: user.name, - createdUserId: user.sub, - lastUpdatedAt: new Date(), - lastUpdateFullName: user.name, - lastUpdateUserId: user.sub, + createdAt: lastUpdatedAt, + createdFullName: lastUpdateFullName, + createdUserId: lastUpdateUserId, + lastUpdatedAt: lastUpdatedAt, + lastUpdateFullName: lastUpdateFullName, + lastUpdateUserId: lastUpdateUserId, })); await posMasterAssignRepository.save(newAssigns); } @@ -720,11 +724,11 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { const orgChild4 = await child4Repository.find({ where: { orgRevisionId: orgRevisionPublish.id }, }); - + // Task #2172 ดึง orgRoot ของ revision ใหม่ const newRoots = await orgRootRepository.find({ where: { orgRevisionId: orgRevisionDraft.id }, }); - // map ancestorDNA -> newRootId + // สร้าง newRootMap เอาไว้เก็บ orgRoot.ancestorDNA ของ revision ใหม่ const newRootMap = new Map( newRoots.map(r => [r.ancestorDNA, r.id]) ); @@ -739,23 +743,24 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { }); const inserts: any[] = []; for (const permiss of oldPermissionProfiles) { + // หา orgRootId ใหม่จาก newRootMap const newRootId = newRootMap.get(permiss.orgRootTree.ancestorDNA); if (!newRootId) continue; - - const { id, ...fields } = permiss; - + // ตัด id กับ orgRootTree ออกแล้วสร้าง object ใหม่ + const { id, orgRootTree, ...fields } = permiss; + // เตรียมข้อมูลสำหรับ insert inserts.push({ ...fields, orgRootId: newRootId, - createdAt: new Date(), - createdFullName: user.name, - createdUserId: user.sub, - lastUpdatedAt: new Date(), - lastUpdateFullName: user.name, - lastUpdateUserId: user.sub, + createdAt: lastUpdatedAt, + createdFullName: lastUpdateFullName, + createdUserId: lastUpdateUserId, + lastUpdatedAt: lastUpdatedAt, + lastUpdateFullName: lastUpdateFullName, + lastUpdateUserId: lastUpdateUserId, }); } - + // ทำการ insert ข้อมูลใหม่ครั้งเดียว if (inserts.length > 0) { await permissionProfilesRepository.insert(inserts); } @@ -2422,7 +2427,7 @@ async function handler_org_draft(msg: amqp.ConsumeMessage): Promise { await child3Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }); await child2Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }); await child1Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }); - // ถ้าเลือกทำสำเนาให้อัพเดทจากแบบร่างเดิมไปแบบร่างใหม่แทนการลบ xxx + // Task #2160 อัพเดทหน้าที่จัดการโครงสร้างแบบร่าง if (["ORG", "ORG_POSITION", "ORG_POSITION_PERSON", "ORG_POSITION_ROLE", "ORG_POSITION_PERSON_ROLE"].includes(requestBody.typeDraft?.toUpperCase())) { const _newRoots = await orgRootRepository.find({ where: { orgRevisionId: revision.id }