From 869bb093a38909947af501e170c41d346ff0a3ae Mon Sep 17 00:00:00 2001 From: waruneeauy Date: Tue, 5 May 2026 12:08:37 +0700 Subject: [PATCH] refactor code function handler_org --- src/services/rabbitmq.ts | 1311 +++++++++++++++++++------------------- 1 file changed, 649 insertions(+), 662 deletions(-) diff --git a/src/services/rabbitmq.ts b/src/services/rabbitmq.ts index 31a4269d..00b1f27a 100644 --- a/src/services/rabbitmq.ts +++ b/src/services/rabbitmq.ts @@ -92,13 +92,12 @@ export async function init() { // createConsumer(queue2, channel, handler2); } -let retries = 0; - function createConsumer( //----> consumer queue: string, channel: amqp.Channel, handler: (msg: amqp.ConsumeMessage) => Promise | boolean, ) { + let retries = 0; channel.consume( queue, async (msg) => { @@ -405,7 +404,7 @@ async function handler_command_noti(msg: amqp.ConsumeMessage): Promise try { let profilesNotiRequest: Promise | undefined; - if (!(["C-PM-10"].includes(command.commandType.code))) { + if (!["C-PM-10"].includes(command.commandType.code)) { profilesNotiRequest = new CallAPI() .PostData( { headers: { authorization: token } }, @@ -441,14 +440,14 @@ async function handler_command_noti(msg: amqp.ConsumeMessage): Promise 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 profilesSendRequest = new CallAPI() @@ -482,8 +481,7 @@ async function handler_command_noti(msg: amqp.ConsumeMessage): Promise /*เฉพาะคำสั่ง C-PM-10 ให้ตัด profilesNotiRequest ที่ส่ง noti ครั้งแรกออก*/ if (["C-PM-10"].includes(command.commandType.code)) { await Promise.all([profilesSendRequest]); - } - else { + } else { await Promise.all([profilesNotiRequest!, profilesSendRequest]); } @@ -497,7 +495,7 @@ async function handler_command_noti(msg: amqp.ConsumeMessage): Promise async function handler_org(msg: amqp.ConsumeMessage): Promise { //----> condition before process consume - console.time('[AMQ] handler_org_total'); + console.time("[AMQ] handler_org_total"); const startTime = Date.now(); console.log(`[AMQ] handler_org START at ${new Date(startTime).toISOString()}`); @@ -520,6 +518,30 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { const { id, status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt } = data; console.log(`[AMQ] Received message - revisionId: ${id}, status: ${status}`); + const targetOrgRevision = await repoOrgRevision.findOne({ + where: { id }, + }); + + if (!targetOrgRevision) { + console.error(`[AMQ] Skip publish: revision ${id} not found`); + console.timeEnd("[AMQ] handler_org_total"); + return true; + } + + if (targetOrgRevision.orgRevisionIsCurrent && !targetOrgRevision.orgRevisionIsDraft) { + console.log(`[AMQ] Skip publish: revision ${id} is already current`); + console.timeEnd("[AMQ] handler_org_total"); + return true; + } + + if (!targetOrgRevision.orgRevisionIsDraft || targetOrgRevision.orgRevisionIsCurrent) { + console.log( + `[AMQ] Skip publish: revision ${id} is no longer publishable (isDraft=${targetOrgRevision.orgRevisionIsDraft}, isCurrent=${targetOrgRevision.orgRevisionIsCurrent})`, + ); + console.timeEnd("[AMQ] handler_org_total"); + return true; + } + if (user) { sendWebSocket( "send-publish-org", @@ -531,25 +553,30 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { ).catch(console.error); } - console.time('[AMQ] query_revisions'); - const orgRevisionPublish = await repoOrgRevision - .createQueryBuilder("orgRevision") - .where("orgRevision.orgRevisionIsDraft = false") - .andWhere("orgRevision.orgRevisionIsCurrent = true") - .getOne(); - - const orgRevisionDraft = await repoOrgRevision - .createQueryBuilder("orgRevision") - .where("orgRevision.orgRevisionIsDraft = true") - .andWhere("orgRevision.orgRevisionIsCurrent = false") - .getOne(); - console.timeEnd('[AMQ] query_revisions'); - console.log(`[AMQ] orgRevisionPublish found: ${orgRevisionPublish ? orgRevisionPublish.id : 'null'}`); - console.log(`[AMQ] orgRevisionDraft found: ${orgRevisionDraft ? orgRevisionDraft.id : 'null'}`); + console.time("[AMQ] query_revisions"); + const [orgRevisionPublish, orgRevisionDraft] = await Promise.all([ + repoOrgRevision + .createQueryBuilder("orgRevision") + .where("orgRevision.orgRevisionIsDraft = false") + .andWhere("orgRevision.orgRevisionIsCurrent = true") + .getOne(), + repoOrgRevision + .createQueryBuilder("orgRevision") + .where("orgRevision.orgRevisionIsDraft = true") + .andWhere("orgRevision.orgRevisionIsCurrent = false") + .getOne(), + ]); + console.timeEnd("[AMQ] query_revisions"); + console.log( + `[AMQ] orgRevisionPublish found: ${orgRevisionPublish ? orgRevisionPublish.id : "null"}`, + ); + console.log(`[AMQ] orgRevisionDraft found: ${orgRevisionDraft ? orgRevisionDraft.id : "null"}`); // Validate: ต้องมี orgRevisionPublish เสมอสำหรับการเผยแพร่ if (!orgRevisionPublish) { - console.error('[AMQ] Cannot publish: No current org revision found (isDraft=false, isCurrent=true)'); + console.error( + "[AMQ] Cannot publish: No current org revision found (isDraft=false, isCurrent=true)", + ); if (user) { sendWebSocket( "send-publish-org", @@ -565,7 +592,9 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { // Validate: ต้องมี orgRevisionDraft ที่จะเผยแพร่ if (!orgRevisionDraft) { - console.error('[AMQ] Cannot publish: No draft org revision found (isDraft=true, isCurrent=false)'); + console.error( + "[AMQ] Cannot publish: No draft org revision found (isDraft=true, isCurrent=false)", + ); if (user) { sendWebSocket( "send-publish-org", @@ -579,11 +608,19 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { return false; } + if (orgRevisionDraft.id !== targetOrgRevision.id) { + console.log( + `[AMQ] Skip publish: revision ${id} is stale because draft ${orgRevisionDraft.id} is now the active publish candidate`, + ); + console.timeEnd("[AMQ] handler_org_total"); + return true; + } + // NOTE: ย้ายการอัปเดตสถานะไปไว้หลังจากทำงานเสร็จทั้งหมด // เพื่อป้องกันกรณี timeout/retry ทำให้สถานะเพี้ยน (ทุก row เป็น false,false) try { - console.time('[AMQ] query_posMaster'); + console.time("[AMQ] query_posMaster"); const posMaster = await repoPosmaster.find({ where: { orgRevisionId: id }, relations: [ @@ -598,15 +635,15 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { "positions.posExecutive", ], }); - console.timeEnd('[AMQ] query_posMaster'); + console.timeEnd("[AMQ] query_posMaster"); console.log(`[AMQ] posMaster count: ${posMaster.length}`); - console.time('[AMQ] query_old_data'); + console.time("[AMQ] query_old_data"); const oldPosMasters = await repoPosmaster.find({ where: { orgRevisionId: orgRevisionPublish.id, }, - select: ['id', 'current_holderId', 'ancestorDNA'] + select: ["id", "current_holderId", "ancestorDNA"], }); // Task #2160 ดึง posMasterAssign ของ revision เดิม @@ -618,11 +655,11 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { }, }, }); - console.timeEnd('[AMQ] query_old_data'); + console.timeEnd("[AMQ] query_old_data"); console.log(`[AMQ] oldPosMasters count: ${oldPosMasters.length}`); console.log(`[AMQ] oldposMasterAssigns count: ${oldposMasterAssigns.length}`); - console.time('[AMQ] build_assignMap'); + console.time("[AMQ] build_assignMap"); // สร้าง assignMap เอาไว้เก็บ posMasterAssign.ancestorDNA ของ revision เดิม const assignMap = new Map(); for (const posmasterAssign of oldposMasterAssigns) { @@ -633,12 +670,12 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { assignMap.get(dna)!.push({ id: posmasterAssign.id, posMasterId: posmasterAssign.posMasterId, - assignId: posmasterAssign.assignId + assignId: posmasterAssign.assignId, }); } - console.timeEnd('[AMQ] build_assignMap'); + console.timeEnd("[AMQ] build_assignMap"); - console.time('[AMQ] query_oldposMasterAct'); + console.time("[AMQ] query_oldposMasterAct"); // ดึง posMasterAct ของ revision เดิม xxx const oldposMasterAct = await posMasterActRepository.find({ relations: ["posMaster", "posMasterChild"], @@ -648,16 +685,16 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { }, }, }); - console.timeEnd('[AMQ] query_oldposMasterAct'); + console.timeEnd("[AMQ] query_oldposMasterAct"); console.log(`[AMQ] oldposMasterAct count: ${oldposMasterAct.length}`); type ActKey = string; // `${parentDNA}|${childDNA}` - console.time('[AMQ] build_maps'); + console.time("[AMQ] build_maps"); const posMasterActMap = new Map(); for (const act of oldposMasterAct) { - const parentDNA = act.posMaster?.ancestorDNA?.trim() ?? ''; - const childDNA = act.posMasterChild?.ancestorDNA?.trim() ?? ''; + const parentDNA = act.posMaster?.ancestorDNA?.trim() ?? ""; + const childDNA = act.posMasterChild?.ancestorDNA?.trim() ?? ""; const key = `${parentDNA}|${childDNA}`; if (!posMasterActMap.has(key)) { @@ -668,7 +705,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { const posMasterIdMap = new Map(); for (const pm of posMaster) { - posMasterIdMap.set(pm.ancestorDNA?.trim() ?? '', pm.id); + posMasterIdMap.set(pm.ancestorDNA?.trim() ?? "", pm.id); } const oldPosMasterMap = new Map(); @@ -678,25 +715,25 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { oldPosMasterMap.set(dna, oldPm); } } - console.timeEnd('[AMQ] build_maps'); + console.timeEnd("[AMQ] build_maps"); const _null: any = null; // ===== BATCH PROCESSING: เตรียมข้อมูลก่อน loop ===== - console.time('[AMQ] prepare_batch_data'); + console.time("[AMQ] prepare_batch_data"); // 1. รวบรวม profileIds ทั้งหมดที่ต้องอัพเดท const profileIds = posMaster - .filter(item => item.next_holderId != null) - .map(item => item.next_holderId!) - .filter(id => id != null && id !== ""); + .filter((item) => item.next_holderId != null) + .map((item) => item.next_holderId!) + .filter((id) => id != null && id !== ""); // 2. Batch load profiles ทั้งหมดในครั้งเดียว (แก้ปัญหา N+1 Query) const profilesMap = new Map(); if (profileIds.length > 0) { const profiles = await repoProfile.findBy({ - id: In(profileIds) + id: In(profileIds), }); - profiles.forEach(p => profilesMap.set(p.id, p)); + profiles.forEach((p) => profilesMap.set(p.id, p)); } console.log(`[AMQ] profiles to update: ${profilesMap.size}`); @@ -724,7 +761,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { lastUpdatedAt: lastUpdatedAt, lastUpdateFullName: lastUpdateFullName, lastUpdateUserId: lastUpdateUserId, - }) + }), ); posMasterAssignsToSave.push(...newAssigns); } @@ -775,33 +812,35 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { historyCreateIds.push(item.id); } } - console.timeEnd('[AMQ] prepare_batch_data'); - console.log(`[AMQ] Prepared - posMasterAssignsToSave: ${posMasterAssignsToSave.length}, profilesToSave: ${profilesToSave.length}, posMasterUpdates: ${posMasterUpdates.length}, historyCreateIds: ${historyCreateIds.length}`); + console.timeEnd("[AMQ] prepare_batch_data"); + console.log( + `[AMQ] Prepared - posMasterAssignsToSave: ${posMasterAssignsToSave.length}, profilesToSave: ${profilesToSave.length}, posMasterUpdates: ${posMasterUpdates.length}, historyCreateIds: ${historyCreateIds.length}`, + ); // ===== BATCH EXECUTION: save ทีละ batch ===== // 4. Batch save posMasterAssign (chunk 500) - console.time('[AMQ] batch_save_posMasterAssign'); + console.time("[AMQ] batch_save_posMasterAssign"); if (posMasterAssignsToSave.length > 0) { const chunks = chunkArray(posMasterAssignsToSave, 500); for (const chunk of chunks) { await posMasterAssignRepository.save(chunk); } } - console.timeEnd('[AMQ] batch_save_posMasterAssign'); + console.timeEnd("[AMQ] batch_save_posMasterAssign"); // 5. Batch save profiles (chunk 200) - console.time('[AMQ] batch_save_profiles'); + console.time("[AMQ] batch_save_profiles"); if (profilesToSave.length > 0) { const chunks = chunkArray(profilesToSave, 200); for (const chunk of chunks) { await repoProfile.save(chunk); } } - console.timeEnd('[AMQ] batch_save_profiles'); + console.timeEnd("[AMQ] batch_save_profiles"); // 6. Batch update posMasters - console.time('[AMQ] batch_update_posMasters'); + console.time("[AMQ] batch_update_posMasters"); for (const update of posMasterUpdates) { await repoPosmaster.update(update.id, { current_holderId: update.current_holderId, @@ -811,20 +850,20 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { lastUpdatedAt, }); } - console.timeEnd('[AMQ] batch_update_posMasters'); + console.timeEnd("[AMQ] batch_update_posMasters"); // 7. Batch create history - console.time('[AMQ] batch_create_history'); + console.time("[AMQ] batch_create_history"); for (const id of historyCreateIds) { await CreatePosMasterHistoryOfficer(id, null); } - console.timeEnd('[AMQ] batch_create_history'); + console.timeEnd("[AMQ] batch_create_history"); // Clone oldposMasterAct - console.time('[AMQ] clone_oldposMasterAct'); + console.time("[AMQ] clone_oldposMasterAct"); for (const act of oldposMasterAct) { - const parentDNA = act.posMaster?.ancestorDNA?.trim()?.toLowerCase() ?? ''; - const childDNA = act.posMasterChild?.ancestorDNA?.trim()?.toLowerCase() ?? ''; + const parentDNA = act.posMaster?.ancestorDNA?.trim()?.toLowerCase() ?? ""; + const childDNA = act.posMasterChild?.ancestorDNA?.trim()?.toLowerCase() ?? ""; const newParentId = posMasterIdMap.get(parentDNA); const newChildId = posMasterIdMap.get(childDNA); @@ -847,16 +886,16 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { await posMasterActRepository.save(newAct); } - console.timeEnd('[AMQ] clone_oldposMasterAct'); + console.timeEnd("[AMQ] clone_oldposMasterAct"); if (orgRevisionPublish != null && orgRevisionDraft != null) { - console.time('[AMQ] clone_org_structure'); + console.time("[AMQ] clone_org_structure"); //new main revision const before = null; //ทุก orgRoot และ orgChild ข้างล่างนี้จะเป็นตัวเก่าที่ไม่ได้เป็น current revision //cone tree - console.time('[AMQ] query_old_org_structure'); + console.time("[AMQ] query_old_org_structure"); //หา dna tree const orgRoot = await orgRootRepository.find({ where: { orgRevisionId: orgRevisionPublish.id }, @@ -877,27 +916,55 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { const orgChild4 = await child4Repository.find({ where: { orgRevisionId: orgRevisionPublish.id }, }); - console.timeEnd('[AMQ] query_old_org_structure'); - console.log(`[AMQ] Old structure - orgRoot: ${orgRoot.length}, orgChild1: ${orgChild1.length}, orgChild2: ${orgChild2.length}, orgChild3: ${orgChild3.length}, orgChild4: ${orgChild4.length}`); + console.timeEnd("[AMQ] query_old_org_structure"); + console.log( + `[AMQ] Old structure - orgRoot: ${orgRoot.length}, orgChild1: ${orgChild1.length}, orgChild2: ${orgChild2.length}, orgChild3: ${orgChild3.length}, orgChild4: ${orgChild4.length}`, + ); // Task #2172 ดึง orgRoot ของ revision ใหม่ const newRoots = await orgRootRepository.find({ where: { orgRevisionId: orgRevisionDraft.id }, }); // สร้าง newRootMap เอาไว้เก็บ orgRoot.ancestorDNA ของ revision ใหม่ - const newRootMap = new Map( - newRoots.map(r => [r.ancestorDNA, r.id]) - ); + const newRootMap = new Map(newRoots.map((r) => [r.ancestorDNA, r.id])); + const emptyAncestorDNA = "00000000-0000-0000-0000-000000000000"; + const hasEmptyAncestorDNA = (ancestorDNA?: string | null) => + ancestorDNA == null || ancestorDNA === emptyAncestorDNA; + const hasSelfOrEmptyAncestorDNA = (node: { id: string; ancestorDNA: string | null }) => + node.ancestorDNA === node.id || hasEmptyAncestorDNA(node.ancestorDNA); + const findMatchedNodeByAncestorDNA = ( + nodes: T[], + node: T, + ) => + nodes.find((item) => { + if (hasSelfOrEmptyAncestorDNA(node)) { + return hasEmptyAncestorDNA(item.ancestorDNA); + } + return item.ancestorDNA === node.ancestorDNA; + }); + const [ + orgRootCurrent, + orgChild1Current, + orgChild2Current, + orgChild3Current, + orgChild4Current, + ] = await Promise.all([ + orgRootRepository.find({ where: { orgRevisionId: orgRevisionDraft.id } }), + child1Repository.find({ where: { orgRevisionId: orgRevisionDraft.id } }), + child2Repository.find({ where: { orgRevisionId: orgRevisionDraft.id } }), + child3Repository.find({ where: { orgRevisionId: orgRevisionDraft.id } }), + child4Repository.find({ where: { orgRevisionId: orgRevisionDraft.id } }), + ]); - console.time('[AMQ] clone_permissionProfiles'); + console.time("[AMQ] clone_permissionProfiles"); // ดึง permissionProfiles ของ revision เดิม const oldPermissionProfiles = await permissionProfilesRepository.find({ relations: ["orgRootTree"], where: { orgRootTree: { orgRevisionId: orgRevisionPublish.id, - } - } + }, + }, }); const inserts: any[] = []; for (const permiss of oldPermissionProfiles) { @@ -922,15 +989,15 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { if (inserts.length > 0) { await permissionProfilesRepository.insert(inserts); } - console.timeEnd('[AMQ] clone_permissionProfiles'); + console.timeEnd("[AMQ] clone_permissionProfiles"); //หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna - console.time('[AMQ] query_employeePosMaster'); + console.time("[AMQ] query_employeePosMaster"); const orgemployeePosMaster = await repoEmployeePosmaster.find({ where: { orgRevisionId: orgRevisionPublish.id }, relations: ["positions"], }); - console.timeEnd('[AMQ] query_employeePosMaster'); + console.timeEnd("[AMQ] query_employeePosMaster"); console.log(`[AMQ] orgemployeePosMaster count: ${orgemployeePosMaster.length}`); let _orgemployeePosMaster: EmployeePosMaster[]; @@ -962,7 +1029,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { : x.ancestorDNA, })); - console.time('[AMQ] insert_employeePosMaster'); + console.time("[AMQ] insert_employeePosMaster"); await repoEmployeePosmaster .createQueryBuilder() .insert() @@ -973,16 +1040,16 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { overwrite: ["ancestorDNA"], }) .execute(); - console.timeEnd('[AMQ] insert_employeePosMaster'); + console.timeEnd("[AMQ] insert_employeePosMaster"); // } //หา dna posmaster ถ้าไม่มีให้เอาตัวเองเป็น dna - console.time('[AMQ] query_employeeTempPosMaster'); + console.time("[AMQ] query_employeeTempPosMaster"); const orgemployeeTempPosMaster = await repoEmployeeTempPosmaster.find({ where: { orgRevisionId: orgRevisionPublish.id }, relations: ["positions"], }); - console.timeEnd('[AMQ] query_employeeTempPosMaster'); + console.timeEnd("[AMQ] query_employeeTempPosMaster"); console.log(`[AMQ] orgemployeeTempPosMaster count: ${orgemployeeTempPosMaster.length}`); let _orgemployeeTempPosMaster: EmployeeTempPosMaster[]; @@ -1012,30 +1079,12 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { .execute(); // } - //create org - forEach orgRoot (WARNING: async forEach without await) - console.time('[AMQ] forEach_orgRoot'); + //create org + console.time("[AMQ] forEach_orgRoot"); console.log(`[AMQ] Starting forEach orgRoot loop (${orgRoot.length} items)`); - let processedOrgRoot = 0; - orgRoot.forEach(async (x: any) => { - const itemStartTime = Date.now(); - var dataId = x.id; - - const orgRootCurrent = await orgRootRepository.find({ - where: { orgRevisionId: orgRevisionDraft.id }, - }); - - const matchedOrgRoot = orgRootCurrent.find((i: OrgRoot) => { - if ( - x.ancestorDNA === x.id || // ถ้า ancestorDNA ถูกตั้งเป็น id ตัวเอง - x.ancestorDNA === null || - x.ancestorDNA === "00000000-0000-0000-0000-000000000000" - ) { - return ( - i.ancestorDNA === null || i.ancestorDNA === "00000000-0000-0000-0000-000000000000" - ); - } - return i.ancestorDNA === x.ancestorDNA; - }); + for (const x of orgRoot) { + const dataId = x.id; + const matchedOrgRoot = findMatchedNodeByAncestorDNA(orgRootCurrent, x); // if ( // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || @@ -1044,44 +1093,45 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" // ) { //create employeePosmaster - const filteredEmployeePosMaster = _orgemployeePosMaster - .filter((x: EmployeePosMaster) => x.orgRootId == dataId && x.orgChild1Id == null); + const filteredEmployeePosMaster = _orgemployeePosMaster.filter( + (x: EmployeePosMaster) => x.orgRootId == dataId && x.orgChild1Id == null, + ); await Promise.all( - filteredEmployeePosMaster - .map(async (item: any) => { - delete item.id; - const employeePosMaster = Object.assign(new EmployeePosMaster(), item); - employeePosMaster.positions = []; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeePosMaster.current_holderId = item.current_holderId; - // } else { - // // employeePosMaster.next_holderId = null; - // employeePosMaster.isSit = false; - // } - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeePosMaster.authRoleId = item.authRoleId; - // } else { - // employeePosMaster.authRoleId = null; - // } - // employeePosMaster.current_holderId = null; - employeePosMaster.orgRevisionId = orgRevisionDraft.id; - employeePosMaster.orgRootId = matchedOrgRoot?.id ?? null; - employeePosMaster.createdUserId = ""; - employeePosMaster.createdFullName = "System Administrator"; - employeePosMaster.createdAt = new Date(); - employeePosMaster.lastUpdateUserId = ""; - employeePosMaster.lastUpdateFullName = "System Administrator"; - employeePosMaster.lastUpdatedAt = new Date(); - await repoEmployeePosmaster.save(employeePosMaster); + filteredEmployeePosMaster.map(async (item: any) => { + delete item.id; + const employeePosMaster = Object.assign(new EmployeePosMaster(), item); + employeePosMaster.positions = []; + // if ( + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" + // ) { + // employeePosMaster.current_holderId = item.current_holderId; + // } else { + // // employeePosMaster.next_holderId = null; + // employeePosMaster.isSit = false; + // } + // if ( + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" + // ) { + // employeePosMaster.authRoleId = item.authRoleId; + // } else { + // employeePosMaster.authRoleId = null; + // } + // employeePosMaster.current_holderId = null; + employeePosMaster.orgRevisionId = orgRevisionDraft.id; + employeePosMaster.orgRootId = matchedOrgRoot?.id ?? null; + employeePosMaster.createdUserId = ""; + employeePosMaster.createdFullName = "System Administrator"; + employeePosMaster.createdAt = new Date(); + employeePosMaster.lastUpdateUserId = ""; + employeePosMaster.lastUpdateFullName = "System Administrator"; + employeePosMaster.lastUpdatedAt = new Date(); + await repoEmployeePosmaster.save(employeePosMaster); - //create employeePosition + //create employeePosition + await Promise.all( item.positions.map(async (pos: any) => { delete pos.id; const employeePosition: EmployeePosition = Object.assign( @@ -1102,8 +1152,9 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { employeePosition.lastUpdateFullName = "System Administrator"; employeePosition.lastUpdatedAt = new Date(); await employeePositionRepository.save(employeePosition); - }); - }), + }), + ); + }), ); //create employeeTempPosmaster await Promise.all( @@ -1142,53 +1193,180 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { await repoEmployeeTempPosmaster.save(employeeTempPosMaster); //create employeePosition - item.positions.map(async (pos: any) => { - delete pos.id; - const employeePosition: EmployeePosition = Object.assign( - new EmployeePosition(), - pos, - ); - employeePosition.posMasterTempId = employeeTempPosMaster.id; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" - // ) { - // employeePosition.positionIsSelected = false; - // } - employeePosition.createdUserId = ""; - employeePosition.createdFullName = "System Administrator"; - employeePosition.createdAt = new Date(); - employeePosition.lastUpdateUserId = ""; - employeePosition.lastUpdateFullName = "System Administrator"; - employeePosition.lastUpdatedAt = new Date(); - await employeePositionRepository.save(employeePosition); - }); + await Promise.all( + item.positions.map(async (pos: any) => { + delete pos.id; + const employeePosition: EmployeePosition = Object.assign( + new EmployeePosition(), + pos, + ); + employeePosition.posMasterTempId = employeeTempPosMaster.id; + // if ( + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" + // ) { + // employeePosition.positionIsSelected = false; + // } + employeePosition.createdUserId = ""; + employeePosition.createdFullName = "System Administrator"; + employeePosition.createdAt = new Date(); + employeePosition.lastUpdateUserId = ""; + employeePosition.lastUpdateFullName = "System Administrator"; + employeePosition.lastUpdatedAt = new Date(); + await employeePositionRepository.save(employeePosition); + }), + ); }), ); // } //create org - orgChild1 - .filter((x: OrgChild1) => x.orgRootId == dataId) - .forEach(async (x: any) => { - var data1Id = x.id; - const orgChild1Current = await child1Repository.find({ - where: { orgRevisionId: orgRevisionDraft.id }, - }); + for (const x of orgChild1.filter((item: OrgChild1) => item.orgRootId == dataId)) { + const data1Id = x.id; + const matchedOrgChild1 = findMatchedNodeByAncestorDNA(orgChild1Current, x); + // ("[in case Child1] ancestorDNA", `${x.orgChild1Id == matchedOrgChild1?.id}`); + // if ( + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" + // ) { + //create employeePosmaster + await Promise.all( + _orgemployeePosMaster + .filter((x: EmployeePosMaster) => x.orgChild1Id == data1Id && x.orgChild2Id == null) + .map(async (item: any) => { + delete item.id; + // console.log("[in case Child1] orgChild1Id == data1Id"); + const employeePosMaster = Object.assign(new EmployeePosMaster(), item); + employeePosMaster.positions = []; + // if ( + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" + // ) { + // employeePosMaster.current_holderId = item.current_holderId; + // } else { + // // employeePosMaster.next_holderId = null; + // employeePosMaster.isSit = false; + // } + // if ( + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" + // ) { + // employeePosMaster.authRoleId = item.authRoleId; + // } else { + // employeePosMaster.authRoleId = null; + // } + // employeePosMaster.current_holderId = null; + employeePosMaster.orgRevisionId = orgRevisionDraft.id; + employeePosMaster.orgRootId = matchedOrgRoot?.id ?? null; + employeePosMaster.orgChild1Id = matchedOrgChild1?.id ?? null; + employeePosMaster.createdUserId = ""; + employeePosMaster.createdFullName = "System Administrator"; + employeePosMaster.createdAt = new Date(); + employeePosMaster.lastUpdateUserId = ""; + employeePosMaster.lastUpdateFullName = "System Administrator"; + employeePosMaster.lastUpdatedAt = new Date(); + await repoEmployeePosmaster.save(employeePosMaster); - const matchedOrgChild1 = orgChild1Current.find((i: OrgChild1) => { - if ( - x.ancestorDNA === x.id || // ถ้า ancestorDNA ถูกตั้งเป็น id ตัวเอง - x.ancestorDNA === null || - x.ancestorDNA === "00000000-0000-0000-0000-000000000000" - ) { - return ( - i.ancestorDNA === null || i.ancestorDNA === "00000000-0000-0000-0000-000000000000" + //create employeePosition + await Promise.all( + item.positions.map(async (pos: any) => { + delete pos.id; + const employeePosition: EmployeePosition = Object.assign( + new EmployeePosition(), + pos, + ); + employeePosition.posMasterId = employeePosMaster.id; + // if ( + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" + // ) { + // employeePosition.positionIsSelected = false; + // } + employeePosition.createdUserId = ""; + employeePosition.createdFullName = "System Administrator"; + employeePosition.createdAt = new Date(); + employeePosition.lastUpdateUserId = ""; + employeePosition.lastUpdateFullName = "System Administrator"; + employeePosition.lastUpdatedAt = new Date(); + await employeePositionRepository.save(employeePosition); + }), ); - } - return i.ancestorDNA === x.ancestorDNA; - }); - // ("[in case Child1] ancestorDNA", `${x.orgChild1Id == matchedOrgChild1?.id}`); + }), + ); + // create employeeTempPosmaster + await Promise.all( + _orgemployeeTempPosMaster + .filter( + (x: EmployeeTempPosMaster) => x.orgChild1Id == data1Id && x.orgChild2Id == null, + ) + .map(async (item: any) => { + delete item.id; + const employeeTempPosMaster = Object.assign(new EmployeeTempPosMaster(), item); + employeeTempPosMaster.positions = []; + // if ( + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" + // ) { + // employeeTempPosMaster.current_holderId = item.current_holderId; + // } else { + // // employeeTempPosMaster.next_holderId = null; + // employeeTempPosMaster.isSit = false; + // } + // if ( + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" + // ) { + // employeeTempPosMaster.authRoleId = item.authRoleId; + // } else { + // employeeTempPosMaster.authRoleId = null; + // } + // employeeTempPosMaster.current_holderId = null; + employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id; + employeeTempPosMaster.orgRootId = matchedOrgRoot?.id ?? null; + employeeTempPosMaster.orgChild1Id = matchedOrgChild1?.id ?? null; + employeeTempPosMaster.createdUserId = ""; + employeeTempPosMaster.createdFullName = "System Administrator"; + employeeTempPosMaster.createdAt = new Date(); + employeeTempPosMaster.lastUpdateUserId = ""; + employeeTempPosMaster.lastUpdateFullName = "System Administrator"; + employeeTempPosMaster.lastUpdatedAt = new Date(); + await repoEmployeeTempPosmaster.save(employeeTempPosMaster); + + //create employeePosition + await Promise.all( + item.positions.map(async (pos: any) => { + delete pos.id; + const employeePosition: EmployeePosition = Object.assign( + new EmployeePosition(), + pos, + ); + employeePosition.posMasterTempId = employeeTempPosMaster.id; + // if ( + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" + // ) { + // employeePosition.positionIsSelected = false; + // } + employeePosition.createdUserId = ""; + employeePosition.createdFullName = "System Administrator"; + employeePosition.createdAt = new Date(); + employeePosition.lastUpdateUserId = ""; + employeePosition.lastUpdateFullName = "System Administrator"; + employeePosition.lastUpdatedAt = new Date(); + await employeePositionRepository.save(employeePosition); + }), + ); + }), + ); + // } + + //create org + for (const x of orgChild2.filter((item: OrgChild2) => item.orgChild1Id == data1Id)) { + const data2Id = x.id; + const matchedOrgChild2 = findMatchedNodeByAncestorDNA(orgChild2Current, x); + // console.log("[in case Child2] ancestorDNA", `${x.orgChild2Id == matchedOrgChild2?.id}`); // if ( // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || @@ -1198,10 +1376,10 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { //create employeePosmaster await Promise.all( _orgemployeePosMaster - .filter((x: EmployeePosMaster) => x.orgChild1Id == data1Id && x.orgChild2Id == null) + .filter((x: EmployeePosMaster) => x.orgChild2Id == data2Id && x.orgChild3Id == null) .map(async (item: any) => { delete item.id; - // console.log("[in case Child1] orgChild1Id == data1Id"); + // console.log("[in case Child2] orgChild2Id == data2Id"); const employeePosMaster = Object.assign(new EmployeePosMaster(), item); employeePosMaster.positions = []; // if ( @@ -1225,6 +1403,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { employeePosMaster.orgRevisionId = orgRevisionDraft.id; employeePosMaster.orgRootId = matchedOrgRoot?.id ?? null; employeePosMaster.orgChild1Id = matchedOrgChild1?.id ?? null; + employeePosMaster.orgChild2Id = matchedOrgChild2?.id ?? null; employeePosMaster.createdUserId = ""; employeePosMaster.createdFullName = "System Administrator"; employeePosMaster.createdAt = new Date(); @@ -1234,34 +1413,36 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { await repoEmployeePosmaster.save(employeePosMaster); //create employeePosition - item.positions.map(async (pos: any) => { - delete pos.id; - const employeePosition: EmployeePosition = Object.assign( - new EmployeePosition(), - pos, - ); - employeePosition.posMasterId = employeePosMaster.id; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" - // ) { - // employeePosition.positionIsSelected = false; - // } - employeePosition.createdUserId = ""; - employeePosition.createdFullName = "System Administrator"; - employeePosition.createdAt = new Date(); - employeePosition.lastUpdateUserId = ""; - employeePosition.lastUpdateFullName = "System Administrator"; - employeePosition.lastUpdatedAt = new Date(); - await employeePositionRepository.save(employeePosition); - }); + await Promise.all( + item.positions.map(async (pos: any) => { + delete pos.id; + const employeePosition: EmployeePosition = Object.assign( + new EmployeePosition(), + pos, + ); + employeePosition.posMasterId = employeePosMaster.id; + // if ( + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" + // ) { + // employeePosition.positionIsSelected = false; + // } + employeePosition.createdUserId = ""; + employeePosition.createdFullName = "System Administrator"; + employeePosition.createdAt = new Date(); + employeePosition.lastUpdateUserId = ""; + employeePosition.lastUpdateFullName = "System Administrator"; + employeePosition.lastUpdatedAt = new Date(); + await employeePositionRepository.save(employeePosition); + }), + ); }), ); // create employeeTempPosmaster await Promise.all( _orgemployeeTempPosMaster .filter( - (x: EmployeeTempPosMaster) => x.orgChild1Id == data1Id && x.orgChild2Id == null, + (x: EmployeeTempPosMaster) => x.orgChild2Id == data2Id && x.orgChild3Id == null, ) .map(async (item: any) => { delete item.id; @@ -1286,8 +1467,9 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { // } // employeeTempPosMaster.current_holderId = null; employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id; - employeeTempPosMaster.orgRootId = matchedOrgRoot?.id ?? null; - employeeTempPosMaster.orgChild1Id = matchedOrgChild1?.id ?? null; + employeeTempPosMaster.orgRootId = dataId; + employeeTempPosMaster.orgChild1Id = data1Id; + employeeTempPosMaster.orgChild2Id = data2Id; employeeTempPosMaster.createdUserId = ""; employeeTempPosMaster.createdFullName = "System Administrator"; employeeTempPosMaster.createdAt = new Date(); @@ -1297,54 +1479,186 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { await repoEmployeeTempPosmaster.save(employeeTempPosMaster); //create employeePosition - item.positions.map(async (pos: any) => { - delete pos.id; - const employeePosition: EmployeePosition = Object.assign( - new EmployeePosition(), - pos, - ); - employeePosition.posMasterTempId = employeeTempPosMaster.id; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" - // ) { - // employeePosition.positionIsSelected = false; - // } - employeePosition.createdUserId = ""; - employeePosition.createdFullName = "System Administrator"; - employeePosition.createdAt = new Date(); - employeePosition.lastUpdateUserId = ""; - employeePosition.lastUpdateFullName = "System Administrator"; - employeePosition.lastUpdatedAt = new Date(); - await employeePositionRepository.save(employeePosition); - }); + await Promise.all( + item.positions.map(async (pos: any) => { + delete pos.id; + const employeePosition: EmployeePosition = Object.assign( + new EmployeePosition(), + pos, + ); + employeePosition.posMasterTempId = employeeTempPosMaster.id; + // if ( + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" + // ) { + // employeePosition.positionIsSelected = false; + // } + employeePosition.createdUserId = ""; + employeePosition.createdFullName = "System Administrator"; + employeePosition.createdAt = new Date(); + employeePosition.lastUpdateUserId = ""; + employeePosition.lastUpdateFullName = "System Administrator"; + employeePosition.lastUpdatedAt = new Date(); + await employeePositionRepository.save(employeePosition); + }), + ); }), ); // } //create org - orgChild2 - .filter((x: OrgChild2) => x.orgChild1Id == data1Id) - .forEach(async (x: any) => { - var data2Id = x.id; - const orgChild2Current = await child2Repository.find({ - where: { orgRevisionId: orgRevisionDraft.id }, - }); + for (const x of orgChild3.filter((item: OrgChild3) => item.orgChild2Id == data2Id)) { + const data3Id = x.id; + const matchedOrgChild3 = findMatchedNodeByAncestorDNA(orgChild3Current, x); + // console.log("[in case Child3] ancestorDNA", `${x.orgChild3Id == matchedOrgChild3?.id}`); + // if ( + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" + // ) { + //create employeePosmaster + await Promise.all( + _orgemployeePosMaster + .filter( + (x: EmployeePosMaster) => x.orgChild3Id == data3Id && x.orgChild4Id == null, + ) + .map(async (item: any) => { + delete item.id; + // console.log("[in case Child3] orgChild3Id == data3Id"); + const employeePosMaster = Object.assign(new EmployeePosMaster(), item); + employeePosMaster.positions = []; + // if ( + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" + // ) { + // employeePosMaster.current_holderId = item.current_holderId; + // } else { + // // employeePosMaster.next_holderId = null; + // employeePosMaster.isSit = false; + // } + // if ( + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" + // ) { + // employeePosMaster.authRoleId = item.authRoleId; + // } else { + // employeePosMaster.authRoleId = null; + // } + // employeePosMaster.current_holderId = null; + employeePosMaster.orgRevisionId = orgRevisionDraft.id; + employeePosMaster.orgRootId = matchedOrgRoot?.id ?? null; + employeePosMaster.orgChild1Id = matchedOrgChild1?.id ?? null; + employeePosMaster.orgChild2Id = matchedOrgChild2?.id ?? null; + employeePosMaster.orgChild3Id = matchedOrgChild3?.id ?? null; + employeePosMaster.createdUserId = ""; + employeePosMaster.createdFullName = "System Administrator"; + employeePosMaster.createdAt = new Date(); + employeePosMaster.lastUpdateUserId = ""; + employeePosMaster.lastUpdateFullName = "System Administrator"; + employeePosMaster.lastUpdatedAt = new Date(); + await repoEmployeePosmaster.save(employeePosMaster); - const matchedOrgChild2 = orgChild2Current.find((i: OrgChild2) => { - if ( - x.ancestorDNA === x.id || // ถ้า ancestorDNA ถูกตั้งเป็น id ตัวเอง - x.ancestorDNA === null || - x.ancestorDNA === "00000000-0000-0000-0000-000000000000" - ) { - return ( - i.ancestorDNA === null || - i.ancestorDNA === "00000000-0000-0000-0000-000000000000" + //create employeePosition + await Promise.all( + item.positions.map(async (pos: any) => { + delete pos.id; + const employeePosition: EmployeePosition = Object.assign( + new EmployeePosition(), + pos, + ); + employeePosition.posMasterId = employeePosMaster.id; + // if ( + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" + // ) { + // employeePosition.positionIsSelected = false; + // } + employeePosition.createdUserId = ""; + employeePosition.createdFullName = "System Administrator"; + employeePosition.createdAt = new Date(); + employeePosition.lastUpdateUserId = ""; + employeePosition.lastUpdateFullName = "System Administrator"; + employeePosition.lastUpdatedAt = new Date(); + await employeePositionRepository.save(employeePosition); + }), ); - } - return i.ancestorDNA === x.ancestorDNA; - }); - // console.log("[in case Child2] ancestorDNA", `${x.orgChild2Id == matchedOrgChild2?.id}`); + }), + ); + // create employeeTempPosmaster + await Promise.all( + _orgemployeeTempPosMaster + .filter( + (x: EmployeeTempPosMaster) => x.orgChild3Id == data3Id && x.orgChild4Id == null, + ) + .map(async (item: any) => { + delete item.id; + const employeeTempPosMaster = Object.assign(new EmployeeTempPosMaster(), item); + employeeTempPosMaster.positions = []; + // if ( + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" + // ) { + // employeeTempPosMaster.current_holderId = item.current_holderId; + // } else { + // // employeeTempPosMaster.next_holderId = null; + // employeeTempPosMaster.isSit = false; + // } + // if ( + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" + // ) { + // employeeTempPosMaster.authRoleId = item.authRoleId; + // } else { + // employeeTempPosMaster.authRoleId = null; + // } + // employeeTempPosMaster.current_holderId = null; + employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id; + employeeTempPosMaster.orgRootId = matchedOrgRoot?.id ?? null; + employeeTempPosMaster.orgChild1Id = matchedOrgChild1?.id ?? null; + employeeTempPosMaster.orgChild2Id = matchedOrgChild2?.id ?? null; + employeeTempPosMaster.orgChild3Id = matchedOrgChild3?.id ?? null; + employeeTempPosMaster.createdUserId = ""; + employeeTempPosMaster.createdFullName = "System Administrator"; + employeeTempPosMaster.createdAt = new Date(); + employeeTempPosMaster.lastUpdateUserId = ""; + employeeTempPosMaster.lastUpdateFullName = "System Administrator"; + employeeTempPosMaster.lastUpdatedAt = new Date(); + await repoEmployeeTempPosmaster.save(employeeTempPosMaster); + + //create employeePosition + await Promise.all( + item.positions.map(async (pos: any) => { + delete pos.id; + const employeePosition: EmployeePosition = Object.assign( + new EmployeePosition(), + pos, + ); + employeePosition.posMasterTempId = employeeTempPosMaster.id; + // if ( + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" + // ) { + // employeePosition.positionIsSelected = false; + // } + employeePosition.createdUserId = ""; + employeePosition.createdFullName = "System Administrator"; + employeePosition.createdAt = new Date(); + employeePosition.lastUpdateUserId = ""; + employeePosition.lastUpdateFullName = "System Administrator"; + employeePosition.lastUpdatedAt = new Date(); + await employeePositionRepository.save(employeePosition); + }), + ); + }), + ); + // } + + //create org + for (const x of orgChild4.filter((item: OrgChild4) => item.orgChild3Id == data3Id)) { + const data4Id = x.id; + const matchedOrgChild4 = findMatchedNodeByAncestorDNA(orgChild4Current, x); + // console.log("[in case Child4] ancestorDNA", `${x.orgChild4Id == matchedOrgChild4?.id}`); // if ( // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || @@ -1354,12 +1668,10 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { //create employeePosmaster await Promise.all( _orgemployeePosMaster - .filter( - (x: EmployeePosMaster) => x.orgChild2Id == data2Id && x.orgChild3Id == null, - ) + .filter((x: EmployeePosMaster) => x.orgChild4Id == data4Id) .map(async (item: any) => { delete item.id; - // console.log("[in case Child2] orgChild2Id == data2Id"); + // console.log("[in case Child4] orgChild4Id == data4Id"); const employeePosMaster = Object.assign(new EmployeePosMaster(), item); employeePosMaster.positions = []; // if ( @@ -1384,6 +1696,8 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { employeePosMaster.orgRootId = matchedOrgRoot?.id ?? null; employeePosMaster.orgChild1Id = matchedOrgChild1?.id ?? null; employeePosMaster.orgChild2Id = matchedOrgChild2?.id ?? null; + employeePosMaster.orgChild3Id = matchedOrgChild3?.id ?? null; + employeePosMaster.orgChild4Id = matchedOrgChild4?.id ?? null; employeePosMaster.createdUserId = ""; employeePosMaster.createdFullName = "System Administrator"; employeePosMaster.createdAt = new Date(); @@ -1393,36 +1707,35 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { await repoEmployeePosmaster.save(employeePosMaster); //create employeePosition - item.positions.map(async (pos: any) => { - delete pos.id; - const employeePosition: EmployeePosition = Object.assign( - new EmployeePosition(), - pos, - ); - employeePosition.posMasterId = employeePosMaster.id; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" - // ) { - // employeePosition.positionIsSelected = false; - // } - employeePosition.createdUserId = ""; - employeePosition.createdFullName = "System Administrator"; - employeePosition.createdAt = new Date(); - employeePosition.lastUpdateUserId = ""; - employeePosition.lastUpdateFullName = "System Administrator"; - employeePosition.lastUpdatedAt = new Date(); - await employeePositionRepository.save(employeePosition); - }); + await Promise.all( + item.positions.map(async (pos: any) => { + delete pos.id; + const employeePosition: EmployeePosition = Object.assign( + new EmployeePosition(), + pos, + ); + employeePosition.posMasterId = employeePosMaster.id; + // if ( + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" + // ) { + // employeePosition.positionIsSelected = false; + // } + employeePosition.createdUserId = ""; + employeePosition.createdFullName = "System Administrator"; + employeePosition.createdAt = new Date(); + employeePosition.lastUpdateUserId = ""; + employeePosition.lastUpdateFullName = "System Administrator"; + employeePosition.lastUpdatedAt = new Date(); + await employeePositionRepository.save(employeePosition); + }), + ); }), ); - // create employeeTempPosmaster + //create employeeTempPosmaster await Promise.all( _orgemployeeTempPosMaster - .filter( - (x: EmployeeTempPosMaster) => - x.orgChild2Id == data2Id && x.orgChild3Id == null, - ) + .filter((x: EmployeeTempPosMaster) => x.orgChild4Id == data4Id) .map(async (item: any) => { delete item.id; const employeeTempPosMaster = Object.assign( @@ -1449,9 +1762,11 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { // } // employeeTempPosMaster.current_holderId = null; employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id; - employeeTempPosMaster.orgRootId = dataId; - employeeTempPosMaster.orgChild1Id = data1Id; - employeeTempPosMaster.orgChild2Id = data2Id; + employeeTempPosMaster.orgRootId = matchedOrgRoot?.id ?? null; + employeeTempPosMaster.orgChild1Id = matchedOrgChild1?.id ?? null; + employeeTempPosMaster.orgChild2Id = matchedOrgChild2?.id ?? null; + employeeTempPosMaster.orgChild3Id = matchedOrgChild3?.id ?? null; + employeeTempPosMaster.orgChild4Id = matchedOrgChild4?.id ?? null; employeeTempPosMaster.createdUserId = ""; employeeTempPosMaster.createdFullName = "System Administrator"; employeeTempPosMaster.createdAt = new Date(); @@ -1461,368 +1776,37 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { await repoEmployeeTempPosmaster.save(employeeTempPosMaster); //create employeePosition - item.positions.map(async (pos: any) => { - delete pos.id; - const employeePosition: EmployeePosition = Object.assign( - new EmployeePosition(), - pos, - ); - employeePosition.posMasterTempId = employeeTempPosMaster.id; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" - // ) { - // employeePosition.positionIsSelected = false; - // } - employeePosition.createdUserId = ""; - employeePosition.createdFullName = "System Administrator"; - employeePosition.createdAt = new Date(); - employeePosition.lastUpdateUserId = ""; - employeePosition.lastUpdateFullName = "System Administrator"; - employeePosition.lastUpdatedAt = new Date(); - await employeePositionRepository.save(employeePosition); - }); + await Promise.all( + item.positions.map(async (pos: any) => { + delete pos.id; + const employeePosition: EmployeePosition = Object.assign( + new EmployeePosition(), + pos, + ); + employeePosition.posMasterTempId = employeeTempPosMaster.id; + // if ( + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || + // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" + // ) { + // employeePosition.positionIsSelected = false; + // } + employeePosition.createdUserId = ""; + employeePosition.createdFullName = "System Administrator"; + employeePosition.createdAt = new Date(); + employeePosition.lastUpdateUserId = ""; + employeePosition.lastUpdateFullName = "System Administrator"; + employeePosition.lastUpdatedAt = new Date(); + await employeePositionRepository.save(employeePosition); + }), + ); }), ); // } - - //create org - orgChild3 - .filter((x: OrgChild3) => x.orgChild2Id == data2Id) - .forEach(async (x: any) => { - var data3Id = x.id; - const orgChild3Current = await child3Repository.find({ - where: { orgRevisionId: orgRevisionDraft.id }, - }); - - const matchedOrgChild3 = orgChild3Current.find((i: OrgChild3) => { - if ( - x.ancestorDNA === x.id || // ถ้า ancestorDNA ถูกตั้งเป็น id ตัวเอง - x.ancestorDNA === null || - x.ancestorDNA === "00000000-0000-0000-0000-000000000000" - ) { - return ( - i.ancestorDNA === null || - i.ancestorDNA === "00000000-0000-0000-0000-000000000000" - ); - } - return i.ancestorDNA === x.ancestorDNA; - }); - // console.log("[in case Child3] ancestorDNA", `${x.orgChild3Id == matchedOrgChild3?.id}`); - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - //create employeePosmaster - await Promise.all( - _orgemployeePosMaster - .filter( - (x: EmployeePosMaster) => - x.orgChild3Id == data3Id && x.orgChild4Id == null, - ) - .map(async (item: any) => { - delete item.id; - // console.log("[in case Child3] orgChild3Id == data3Id"); - const employeePosMaster = Object.assign(new EmployeePosMaster(), item); - employeePosMaster.positions = []; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeePosMaster.current_holderId = item.current_holderId; - // } else { - // // employeePosMaster.next_holderId = null; - // employeePosMaster.isSit = false; - // } - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeePosMaster.authRoleId = item.authRoleId; - // } else { - // employeePosMaster.authRoleId = null; - // } - // employeePosMaster.current_holderId = null; - employeePosMaster.orgRevisionId = orgRevisionDraft.id; - employeePosMaster.orgRootId = matchedOrgRoot?.id ?? null; - employeePosMaster.orgChild1Id = matchedOrgChild1?.id ?? null; - employeePosMaster.orgChild2Id = matchedOrgChild2?.id ?? null; - employeePosMaster.orgChild3Id = matchedOrgChild3?.id ?? null; - employeePosMaster.createdUserId = ""; - employeePosMaster.createdFullName = "System Administrator"; - employeePosMaster.createdAt = new Date(); - employeePosMaster.lastUpdateUserId = ""; - employeePosMaster.lastUpdateFullName = "System Administrator"; - employeePosMaster.lastUpdatedAt = new Date(); - await repoEmployeePosmaster.save(employeePosMaster); - - //create employeePosition - item.positions.map(async (pos: any) => { - delete pos.id; - const employeePosition: EmployeePosition = Object.assign( - new EmployeePosition(), - pos, - ); - employeePosition.posMasterId = employeePosMaster.id; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" - // ) { - // employeePosition.positionIsSelected = false; - // } - employeePosition.createdUserId = ""; - employeePosition.createdFullName = "System Administrator"; - employeePosition.createdAt = new Date(); - employeePosition.lastUpdateUserId = ""; - employeePosition.lastUpdateFullName = "System Administrator"; - employeePosition.lastUpdatedAt = new Date(); - await employeePositionRepository.save(employeePosition); - }); - }), - ); - // create employeeTempPosmaster - await Promise.all( - _orgemployeeTempPosMaster - .filter( - (x: EmployeeTempPosMaster) => - x.orgChild3Id == data3Id && x.orgChild4Id == null, - ) - .map(async (item: any) => { - delete item.id; - const employeeTempPosMaster = Object.assign( - new EmployeeTempPosMaster(), - item, - ); - employeeTempPosMaster.positions = []; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeeTempPosMaster.current_holderId = item.current_holderId; - // } else { - // // employeeTempPosMaster.next_holderId = null; - // employeeTempPosMaster.isSit = false; - // } - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeeTempPosMaster.authRoleId = item.authRoleId; - // } else { - // employeeTempPosMaster.authRoleId = null; - // } - // employeeTempPosMaster.current_holderId = null; - employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id; - employeeTempPosMaster.orgRootId = matchedOrgRoot?.id ?? null; - employeeTempPosMaster.orgChild1Id = matchedOrgChild1?.id ?? null; - employeeTempPosMaster.orgChild2Id = matchedOrgChild2?.id ?? null; - employeeTempPosMaster.orgChild3Id = matchedOrgChild3?.id ?? null; - employeeTempPosMaster.createdUserId = ""; - employeeTempPosMaster.createdFullName = "System Administrator"; - employeeTempPosMaster.createdAt = new Date(); - employeeTempPosMaster.lastUpdateUserId = ""; - employeeTempPosMaster.lastUpdateFullName = "System Administrator"; - employeeTempPosMaster.lastUpdatedAt = new Date(); - await repoEmployeeTempPosmaster.save(employeeTempPosMaster); - - //create employeePosition - item.positions.map(async (pos: any) => { - delete pos.id; - const employeePosition: EmployeePosition = Object.assign( - new EmployeePosition(), - pos, - ); - employeePosition.posMasterTempId = employeeTempPosMaster.id; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" - // ) { - // employeePosition.positionIsSelected = false; - // } - employeePosition.createdUserId = ""; - employeePosition.createdFullName = "System Administrator"; - employeePosition.createdAt = new Date(); - employeePosition.lastUpdateUserId = ""; - employeePosition.lastUpdateFullName = "System Administrator"; - employeePosition.lastUpdatedAt = new Date(); - await employeePositionRepository.save(employeePosition); - }); - }), - ); - // } - - //create org - orgChild4 - .filter((x: OrgChild4) => x.orgChild3Id == data3Id) - .forEach(async (x: any) => { - var data4Id = x.id; - const orgChild4Current = await child4Repository.find({ - where: { orgRevisionId: orgRevisionDraft.id }, - }); - - const matchedOrgChild4 = orgChild4Current.find((i: OrgChild4) => { - if ( - x.ancestorDNA === x.id || // ถ้า ancestorDNA ถูกตั้งเป็น id ตัวเอง - x.ancestorDNA === null || - x.ancestorDNA === "00000000-0000-0000-0000-000000000000" - ) { - return ( - i.ancestorDNA === null || - i.ancestorDNA === "00000000-0000-0000-0000-000000000000" - ); - } - return i.ancestorDNA === x.ancestorDNA; - }); - // console.log("[in case Child4] ancestorDNA", `${x.orgChild4Id == matchedOrgChild4?.id}`); - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - //create employeePosmaster - await Promise.all( - _orgemployeePosMaster - .filter((x: EmployeePosMaster) => x.orgChild4Id == data4Id) - .map(async (item: any) => { - delete item.id; - // console.log("[in case Child4] orgChild4Id == data4Id"); - const employeePosMaster = Object.assign( - new EmployeePosMaster(), - item, - ); - employeePosMaster.positions = []; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeePosMaster.current_holderId = item.current_holderId; - // } else { - // // employeePosMaster.next_holderId = null; - // employeePosMaster.isSit = false; - // } - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeePosMaster.authRoleId = item.authRoleId; - // } else { - // employeePosMaster.authRoleId = null; - // } - // employeePosMaster.current_holderId = null; - employeePosMaster.orgRevisionId = orgRevisionDraft.id; - employeePosMaster.orgRootId = matchedOrgRoot?.id ?? null; - employeePosMaster.orgChild1Id = matchedOrgChild1?.id ?? null; - employeePosMaster.orgChild2Id = matchedOrgChild2?.id ?? null; - employeePosMaster.orgChild3Id = matchedOrgChild3?.id ?? null; - employeePosMaster.orgChild4Id = matchedOrgChild4?.id ?? null; - employeePosMaster.createdUserId = ""; - employeePosMaster.createdFullName = "System Administrator"; - employeePosMaster.createdAt = new Date(); - employeePosMaster.lastUpdateUserId = ""; - employeePosMaster.lastUpdateFullName = "System Administrator"; - employeePosMaster.lastUpdatedAt = new Date(); - await repoEmployeePosmaster.save(employeePosMaster); - - //create employeePosition - item.positions.map(async (pos: any) => { - delete pos.id; - const employeePosition: EmployeePosition = Object.assign( - new EmployeePosition(), - pos, - ); - employeePosition.posMasterId = employeePosMaster.id; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" - // ) { - // employeePosition.positionIsSelected = false; - // } - employeePosition.createdUserId = ""; - employeePosition.createdFullName = "System Administrator"; - employeePosition.createdAt = new Date(); - employeePosition.lastUpdateUserId = ""; - employeePosition.lastUpdateFullName = "System Administrator"; - employeePosition.lastUpdatedAt = new Date(); - await employeePositionRepository.save(employeePosition); - }); - }), - ); - //create employeeTempPosmaster - await Promise.all( - _orgemployeeTempPosMaster - .filter((x: EmployeeTempPosMaster) => x.orgChild4Id == data4Id) - .map(async (item: any) => { - delete item.id; - const employeeTempPosMaster = Object.assign( - new EmployeeTempPosMaster(), - item, - ); - employeeTempPosMaster.positions = []; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeeTempPosMaster.current_holderId = item.current_holderId; - // } else { - // // employeeTempPosMaster.next_holderId = null; - // employeeTempPosMaster.isSit = false; - // } - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_PERSON_ROLE" - // ) { - // employeeTempPosMaster.authRoleId = item.authRoleId; - // } else { - // employeeTempPosMaster.authRoleId = null; - // } - // employeeTempPosMaster.current_holderId = null; - employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id; - employeeTempPosMaster.orgRootId = matchedOrgRoot?.id ?? null; - employeeTempPosMaster.orgChild1Id = matchedOrgChild1?.id ?? null; - employeeTempPosMaster.orgChild2Id = matchedOrgChild2?.id ?? null; - employeeTempPosMaster.orgChild3Id = matchedOrgChild3?.id ?? null; - employeeTempPosMaster.orgChild4Id = matchedOrgChild4?.id ?? null; - employeeTempPosMaster.createdUserId = ""; - employeeTempPosMaster.createdFullName = "System Administrator"; - employeeTempPosMaster.createdAt = new Date(); - employeeTempPosMaster.lastUpdateUserId = ""; - employeeTempPosMaster.lastUpdateFullName = "System Administrator"; - employeeTempPosMaster.lastUpdatedAt = new Date(); - await repoEmployeeTempPosmaster.save(employeeTempPosMaster); - - //create employeePosition - item.positions.map(async (pos: any) => { - delete pos.id; - const employeePosition: EmployeePosition = Object.assign( - new EmployeePosition(), - pos, - ); - employeePosition.posMasterTempId = employeeTempPosMaster.id; - // if ( - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION" || - // requestBody.typeDraft.toUpperCase() == "ORG_POSITION_ROLE" - // ) { - // employeePosition.positionIsSelected = false; - // } - employeePosition.createdUserId = ""; - employeePosition.createdFullName = "System Administrator"; - employeePosition.createdAt = new Date(); - employeePosition.lastUpdateUserId = ""; - employeePosition.lastUpdateFullName = "System Administrator"; - employeePosition.lastUpdatedAt = new Date(); - await employeePositionRepository.save(employeePosition); - }); - }), - ); - // } - }); - }); - }); - }); - }); + } + } + } + } + } // } const employeePosMaster = await repoEmployeePosmaster.find({ @@ -1848,7 +1832,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { item.lastUpdateUserId = lastUpdateUserId; item.lastUpdateFullName = lastUpdateFullName; item.lastUpdatedAt = lastUpdatedAt; - await repoEmployeePosmaster.save(item).catch((e) => console.log(e)); + await repoEmployeePosmaster.save(item); } const employeeTempPosMaster = await repoEmployeeTempPosmaster.find({ where: { orgRevisionId: orgRevisionDraft.id }, @@ -1873,7 +1857,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { item.lastUpdateUserId = lastUpdateUserId; item.lastUpdateFullName = lastUpdateFullName; item.lastUpdatedAt = lastUpdatedAt; - await repoEmployeeTempPosmaster.save(item).catch((e) => console.log(e)); + await repoEmployeeTempPosmaster.save(item); } } console.log("[AMQ] Excecute Organization Success"); @@ -1887,10 +1871,10 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { { userId: user?.sub }, ).catch(console.error); } - console.timeEnd('[AMQ] clone_org_structure'); + console.timeEnd("[AMQ] clone_org_structure"); // อัปเดตสถานะ orgRevision หลังจากทำงานเสร็จทั้งหมด - console.time('[AMQ] save_revision_status'); + console.time("[AMQ] save_revision_status"); orgRevisionPublish.orgRevisionIsDraft = false; orgRevisionPublish.orgRevisionIsCurrent = false; await repoOrgRevision.save(orgRevisionPublish); @@ -1898,10 +1882,10 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { orgRevisionDraft.orgRevisionIsCurrent = true; orgRevisionDraft.orgRevisionIsDraft = false; await repoOrgRevision.save(orgRevisionDraft); - console.timeEnd('[AMQ] save_revision_status'); + console.timeEnd("[AMQ] save_revision_status"); console.log(`[AMQ] handler_org SUCCESS - Total time: ${Date.now() - startTime}ms`); - console.timeEnd('[AMQ] handler_org_total'); + console.timeEnd("[AMQ] handler_org_total"); return true; } catch (error) { const totalTime = Date.now() - startTime; @@ -1916,7 +1900,7 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { { userId: user?.sub }, ).catch(console.error); } - console.timeEnd('[AMQ] handler_org_total'); + console.timeEnd("[AMQ] handler_org_total"); return false; } } @@ -2591,7 +2575,8 @@ async function handler_org_draft(msg: amqp.ConsumeMessage): Promise { }); await posMasterAssignRepository.delete({ posMasterId: In(_posMasters.map((x) => x.id)) }); await posMasterActRepository.delete({ posMasterId: In(_posMasters.map((x) => x.id)) }); //ใช้ posMasterId ของ revision: draft *แต่ยังไม่เจอช็อดไหนที่ใช้โครงสร้างแบบร่างในรักษาการแทน - await posMasterActRepository.delete({ //ใช้ posMasterId ของ revision: draft *แต่ยังไม่เจอช็อดไหนที่ใช้โครงสร้างแบบร่างในรักษาการแทน + await posMasterActRepository.delete({ + //ใช้ posMasterId ของ revision: draft *แต่ยังไม่เจอช็อดไหนที่ใช้โครงสร้างแบบร่างในรักษาการแทน posMasterChildId: In(_posMasters.map((x) => x.id)), }); // await posMasterRepository.remove(_posMasters); @@ -2619,24 +2604,26 @@ 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)) }); // Task #2160 อัพเดทหน้าที่จัดการโครงสร้างแบบร่าง - 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]) - ); + const newRootMap = new Map(_newRoots.map((r) => [r.ancestorDNA, r.id])); for (const oldRoot of _roots) { const newRootId = newRootMap.get(oldRoot.ancestorDNA); if (!newRootId) continue; // อัพเดท orgRootId ที่อยู่ภายใต้ orgRevision แบบร่างเดิมเป็นของ orgRevision แบบร่างใหม่ - await permissionOrgRepository.update( - { orgRootId: oldRoot.id }, - { orgRootId: newRootId } - ); + await permissionOrgRepository.update({ orgRootId: oldRoot.id }, { orgRootId: newRootId }); } - } - else { + } else { await permissionOrgRepository.delete({ orgRootId: In(_roots.map((x) => x.id)), });