diff --git a/src/services/rabbitmq.ts b/src/services/rabbitmq.ts index 5093f52a..24ea581d 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); @@ -406,50 +406,50 @@ async function handler_command_noti(msg: amqp.ConsumeMessage): Promise let profilesNotiRequest: Promise | undefined; if (!(["C-PM-10"].includes(command.commandType.code))) { profilesNotiRequest = new CallAPI() - .PostData( - { headers: { authorization: token } }, - "/placement/noti/profiles", - { - subject: `${command.issue}`, - body: `${command.issue}`, - receiverUserIds: profiles, - payload: "", // แนบไฟล์ (ถ้าจำเป็น) - isSendMail: true, - isSendInbox: true, - isSendNotification: true, - }, - false, - ) - .catch((error) => { - if (error.response) { - // Server ตอบกลับ (มี status code 4xx หรือ 5xx) - console.error("Error status:", error.response.status); - console.error("Error data:", error.response.data); - console.error("Error headers:", error.response.headers); - } else if (error.request) { - // ไม่มีการตอบกลับจาก server - console.error("No response received:", error.request); - } else { - // เกิดข้อผิดพลาดอื่น เช่น โค้ด Axios ผิด - console.error("Axios error:", error.message); - } - console.error("Full error object:", error); - }); + .PostData( + { headers: { authorization: token } }, + "/placement/noti/profiles", + { + subject: `${command.issue}`, + body: `${command.issue}`, + receiverUserIds: profiles, + payload: "", // แนบไฟล์ (ถ้าจำเป็น) + isSendMail: true, + isSendInbox: true, + isSendNotification: true, + }, + false, + ) + .catch((error) => { + if (error.response) { + // Server ตอบกลับ (มี status code 4xx หรือ 5xx) + console.error("Error status:", error.response.status); + console.error("Error data:", error.response.data); + console.error("Error headers:", error.response.headers); + } else if (error.request) { + // ไม่มีการตอบกลับจาก server + console.error("No response received:", error.request); + } else { + // เกิดข้อผิดพลาดอื่น เช่น โค้ด Axios ผิด + console.error("Axios error:", error.message); + } + console.error("Full error object:", error); + }); } let profilesSend = command && command.commandSends.length > 0 ? command.commandSends - .filter((x: any) => x.profileId != null) - .map((x: any) => ({ - receiverUserId: x.profileId, - notiLink: "", - isSendMail: x.commandSendCCs.map((x: any) => x.name == "EMAIL").length > 0, - isSendInbox: x.commandSendCCs.map((x: any) => x.name == "INBOX").length > 0, - isSendNotification: true, - })) + .filter((x: any) => x.profileId != null) + .map((x: any) => ({ + receiverUserId: x.profileId, + notiLink: "", + isSendMail: x.commandSendCCs.map((x: any) => x.name == "EMAIL").length > 0, + isSendInbox: x.commandSendCCs.map((x: any) => x.name == "INBOX").length > 0, + isSendNotification: true, + })) : []; - const payloadStr = await PayloadSendNoti(command.id); + const payloadStr = await PayloadSendNoti(command.id); const profilesSendRequest = new CallAPI() .PostData( { headers: { authorization: token } }, @@ -498,6 +498,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { //----> condition before process consume const repoPosmaster = AppDataSource.getRepository(PosMaster); const posMasterAssignRepository = AppDataSource.getRepository(PosMasterAssign); + const posMasterActRepository = AppDataSource.getRepository(PosMasterAct); const permissionProfilesRepository = AppDataSource.getRepository(PermissionProfile); const repoEmployeePosmaster = AppDataSource.getRepository(EmployeePosMaster); const repoEmployeeTempPosmaster = AppDataSource.getRepository(EmployeeTempPosMaster); @@ -578,7 +579,36 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { } assignMap.get(dna)!.push(posmasterAssign); } - + + // ดึง posMasterAct ของ revision เดิม xxx + const oldposMasterAct = await posMasterActRepository.find({ + relations: ["posMaster", "posMasterChild"], + where: { + posMaster: { + orgRevisionId: orgRevisionPublish!.id, + }, + }, + }); + + type ActKey = string; // `${parentDNA}|${childDNA}` + + const posMasterActMap = new Map(); + for (const act of oldposMasterAct) { + const parentDNA = act.posMaster.ancestorDNA.trim(); + const childDNA = act.posMasterChild.ancestorDNA.trim(); + const key = `${parentDNA}|${childDNA}`; + + if (!posMasterActMap.has(key)) { + posMasterActMap.set(key, []); + } + posMasterActMap.get(key)!.push(act); + } + + const posMasterIdMap = new Map(); + for (const pm of posMaster) { + posMasterIdMap.set(pm.ancestorDNA.trim(), pm.id); + } + const _null: any = null; for (const item of posMaster) { @@ -630,6 +660,33 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { await repoPosmaster.save(item).catch((e) => console.log(e)); await CreatePosMasterHistoryOfficer(item.id, null); } + + for (const act of oldposMasterAct) { + const parentDNA = act.posMaster.ancestorDNA.trim().toLowerCase(); + const childDNA = act.posMasterChild.ancestorDNA.trim().toLowerCase(); + + const newParentId = posMasterIdMap.get(parentDNA); + const newChildId = posMasterIdMap.get(childDNA); + + if (!newParentId || !newChildId) continue; + + const { id, posMaster, posMasterChild, ...fields } = act; + + const newAct = { + ...fields, + posMasterId: newParentId, + posMasterChildId: newChildId, + createdAt: new Date(), + createdFullName: user.name, + createdUserId: user.sub, + lastUpdatedAt: new Date(), + lastUpdateFullName: user.name, + lastUpdateUserId: user.sub, + }; + + await posMasterActRepository.save(newAct); + } + if (orgRevisionPublish != null && orgRevisionDraft != null) { //new main revision const before = null; @@ -663,7 +720,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { const orgChild4 = await child4Repository.find({ where: { orgRevisionId: orgRevisionPublish.id }, }); - + const newRoots = await orgRootRepository.find({ where: { orgRevisionId: orgRevisionDraft.id }, }); @@ -698,7 +755,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { lastUpdateUserId: user.sub, }); } - + if (inserts.length > 0) { await permissionProfilesRepository.insert(inserts); } @@ -2337,8 +2394,8 @@ async function handler_org_draft(msg: amqp.ConsumeMessage): Promise { posMasterTempId: In(_employeeTempPosMasters.map((x) => x.id)), }); await posMasterAssignRepository.delete({ posMasterId: In(_posMasters.map((x) => x.id)) }); - await posMasterActRepository.delete({ posMasterId: In(_posMasters.map((x) => x.id)) }); - await posMasterActRepository.delete({ + await posMasterActRepository.delete({ posMasterId: In(_posMasters.map((x) => x.id)) }); //ใช้ posMasterId ของ revision: draft *แต่ยังไม่เจอช็อดไหนที่ใช้โครงสร้างแบบร่างในรักษาการแทน + await posMasterActRepository.delete({ //ใช้ posMasterId ของ revision: draft *แต่ยังไม่เจอช็อดไหนที่ใช้โครงสร้างแบบร่างในรักษาการแทน posMasterChildId: In(_posMasters.map((x) => x.id)), }); // await posMasterRepository.remove(_posMasters); @@ -2366,10 +2423,9 @@ async function handler_org_draft(msg: amqp.ConsumeMessage): Promise { await child2Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }); await child1Repository.delete({ orgRevisionId: In(_orgRevisions.map((x) => x.id)) }); // ถ้าเลือกทำสำเนาให้อัพเดทจากแบบร่างเดิมไปแบบร่างใหม่แทนการลบ xxx - if (["ORG", "ORG_POSITION", "ORG_POSITION_PERSON", "ORG_POSITION_ROLE", "ORG_POSITION_PERSON_ROLE"].includes(requestBody.typeDraft?.toUpperCase())) - { + 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} + where: { orgRevisionId: revision.id } }); const newRootMap = new Map( _newRoots.map(r => [r.ancestorDNA, r.id])