From 750947f34fb6cb62f8e98f7207e1682c8027f1d0 Mon Sep 17 00:00:00 2001 From: waruneeauy Date: Tue, 5 May 2026 16:38:54 +0700 Subject: [PATCH] =?UTF-8?q?1.=20=E0=B9=80=E0=B8=9E=E0=B8=B4=E0=B9=88?= =?UTF-8?q?=E0=B8=A1=20helper=20=E0=B8=AA=E0=B8=B3=E0=B8=AB=E0=B8=A3?= =?UTF-8?q?=E0=B8=B1=E0=B8=9A=20build=20clone=20rows=20=E0=B8=88=E0=B8=B2?= =?UTF-8?q?=E0=B8=81=20metadata=20=E0=B8=82=E0=B8=AD=E0=B8=87=20repository?= =?UTF-8?q?=20=E0=B9=81=E0=B8=A5=E0=B9=89=E0=B8=A7=20pre-generate=20UUID?= =?UTF-8?q?=20=E0=B9=83=E0=B8=AB=E0=B9=89=20parent=20=E0=B9=81=E0=B8=A5?= =?UTF-8?q?=E0=B8=B0=20child=20=E0=B8=A5=E0=B9=88=E0=B8=A7=E0=B8=87?= =?UTF-8?q?=E0=B8=AB=E0=B8=99=E0=B9=89=E0=B8=B2=202.=20=E0=B9=80=E0=B8=9B?= =?UTF-8?q?=E0=B8=A5=E0=B8=B5=E0=B9=88=E0=B8=A2=E0=B8=99=20inner=20clone?= =?UTF-8?q?=20flow=20=E0=B9=80=E0=B8=9B=E0=B9=87=E0=B8=99=20cloneEmployeeN?= =?UTF-8?q?odeBatch(...)=20=E0=B8=97=E0=B8=B5=E0=B9=88=E0=B8=97=E0=B8=B3?= =?UTF-8?q?=E0=B8=87=E0=B8=B2=E0=B8=99=E0=B9=80=E0=B8=9B=E0=B9=87=E0=B8=99?= =?UTF-8?q?=E0=B8=8A=E0=B8=B8=E0=B8=94=20=E0=B9=81=E0=B8=97=E0=B8=99?= =?UTF-8?q?=E0=B8=81=E0=B8=B2=E0=B8=A3=20save()=20parent=20=E0=B9=81?= =?UTF-8?q?=E0=B8=A5=E0=B9=89=E0=B8=A7=20save()=20children=20=E0=B8=97?= =?UTF-8?q?=E0=B8=B5=E0=B8=A5=E0=B8=B0=E0=B8=A3=E0=B8=B2=E0=B8=A2=E0=B8=81?= =?UTF-8?q?=E0=B8=B2=E0=B8=A3=203.=20=E0=B9=83=E0=B8=8A=E0=B9=89=20insertI?= =?UTF-8?q?nChunks(...)=20=E0=B8=AA=E0=B8=B3=E0=B8=AB=E0=B8=A3=E0=B8=B1?= =?UTF-8?q?=E0=B8=9A=20batch=20insert=20=E0=B8=82=E0=B8=AD=E0=B8=87=20pare?= =?UTF-8?q?nt=20rows=20=E0=B9=81=E0=B8=A5=E0=B8=B0=20EmployeePosition=20ro?= =?UTF-8?q?ws=204.=20=E0=B9=83=E0=B8=8A=E0=B9=89=20helper=20=E0=B9=80?= =?UTF-8?q?=E0=B8=94=E0=B8=B5=E0=B8=A2=E0=B8=A7=E0=B8=81=E0=B8=B1=E0=B8=99?= =?UTF-8?q?=E0=B8=8B=E0=B9=89=E0=B8=B3=E0=B8=97=E0=B8=B8=E0=B8=81=E0=B8=A3?= =?UTF-8?q?=E0=B8=B0=E0=B8=94=E0=B8=B1=E0=B8=9A=E0=B8=82=E0=B8=AD=E0=B8=87?= =?UTF-8?q?=20tree=20(root,=20child1,=20child2,=20child3,=20child4)=20?= =?UTF-8?q?=E0=B9=80=E0=B8=9E=E0=B8=B7=E0=B9=88=E0=B8=AD=E0=B8=A5=E0=B8=94?= =?UTF-8?q?=20code=20duplication=20=E0=B9=81=E0=B8=A5=E0=B8=B0=E0=B8=84?= =?UTF-8?q?=E0=B8=87=20mapping=20=E0=B8=82=E0=B8=AD=E0=B8=87=20destination?= =?UTF-8?q?=20org=20ids=20=E0=B8=95=E0=B8=B2=E0=B8=A1=20logic=20=E0=B9=80?= =?UTF-8?q?=E0=B8=94=E0=B8=B4=E0=B8=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/services/rabbitmq.ts | 543 +++++++++++++-------------------------- 1 file changed, 180 insertions(+), 363 deletions(-) diff --git a/src/services/rabbitmq.ts b/src/services/rabbitmq.ts index da93d32f..073042c9 100644 --- a/src/services/rabbitmq.ts +++ b/src/services/rabbitmq.ts @@ -1,3 +1,4 @@ +import { randomUUID } from "crypto"; import amqp from "amqplib"; import { AppDataSource } from "../database/data-source"; import { Command } from "../entities/Command"; @@ -20,7 +21,7 @@ import { OrgChild4 } from "../entities/OrgChild4"; import { OrgRoot } from "../entities/OrgRoot"; import { PosMasterAssign, PosMasterAssignDTO } from "../entities/PosMasterAssign"; import { Position } from "../entities/Position"; -import { In, Not } from "typeorm"; +import { In, Not, Repository } from "typeorm"; import { PosMasterAct } from "../entities/PosMasterAct"; import { PermissionOrg } from "../entities/PermissionOrg"; import { sendWebSocket } from "./webSocket"; @@ -1292,6 +1293,98 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { const orgChild2ByChild1 = groupByParentId(orgChild2, (item) => item.orgChild1Id); const orgChild3ByChild2 = groupByParentId(orgChild3, (item) => item.orgChild2Id); const orgChild4ByChild3 = groupByParentId(orgChild4, (item) => item.orgChild3Id); + type OrgDestinationIds = { + orgRootId?: string | null; + orgChild1Id?: string | null; + orgChild2Id?: string | null; + orgChild3Id?: string | null; + orgChild4Id?: string | null; + }; + type CloneEmployeeSource = { + positions: EmployeePosition[]; + } & (EmployeePosMaster | EmployeeTempPosMaster); + const buildAuditFields = (timestamp: Date) => ({ + createdUserId: "", + createdFullName: "System Administrator", + createdAt: timestamp, + lastUpdateUserId: "", + lastUpdateFullName: "System Administrator", + lastUpdatedAt: timestamp, + }); + const buildColumnData = (repository: Repository, source: T): Partial => { + const row = {} as Partial; + const target = row as Record; + const sourceRecord = source as Record; + for (const column of repository.metadata.columns) { + target[column.propertyName] = sourceRecord[column.propertyName]; + } + return row; + }; + const insertInChunks = async ( + repository: Repository, + rows: Partial[], + chunkSize: number, + ) => { + if (rows.length === 0) { + return; + } + for (const chunk of chunkArray(rows, chunkSize) as Partial[][]) { + await repository.insert(chunk as Parameters["insert"]>[0]); + } + }; + const buildEmployeeCloneBatch = ( + items: T[], + parentRepository: Repository, + positionParentKey: "posMasterId" | "posMasterTempId", + targetIds: OrgDestinationIds, + ) => { + const parentRows: Partial[] = []; + const positionRows: Partial[] = []; + + for (const item of items) { + const parentId = randomUUID(); + const parentTimestamp = new Date(); + parentRows.push({ + ...buildColumnData(parentRepository, item), + id: parentId, + orgRevisionId: orgRevisionDraft.id, + ...targetIds, + ...buildAuditFields(parentTimestamp), + }); + + for (const position of item.positions ?? []) { + const positionTimestamp = new Date(); + positionRows.push({ + ...buildColumnData(employeePositionRepository, position), + id: randomUUID(), + posMasterId: positionParentKey === "posMasterId" ? parentId : undefined, + posMasterTempId: + positionParentKey === "posMasterTempId" ? parentId : undefined, + ...buildAuditFields(positionTimestamp), + }); + } + } + + return { parentRows, positionRows }; + }; + const cloneEmployeeNodeBatch = async ( + items: T[], + parentRepository: Repository, + positionParentKey: "posMasterId" | "posMasterTempId", + targetIds: OrgDestinationIds, + ) => { + if (items.length === 0) { + return; + } + const { parentRows, positionRows } = buildEmployeeCloneBatch( + items, + parentRepository, + positionParentKey, + targetIds, + ); + await insertInChunks(parentRepository, parentRows, 200); + await insertInChunks(employeePositionRepository, positionRows, 500); + }; await repoEmployeeTempPosmaster .createQueryBuilder() @@ -1313,398 +1406,122 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { const filteredEmployeePosMaster = employeePosMasterByNode.get(getNodeKey("root", dataId)) ?? []; - await Promise.all( - filteredEmployeePosMaster.map(async (item: any) => { - delete item.id; - const employeePosMaster = Object.assign(new EmployeePosMaster(), item); - employeePosMaster.positions = []; - 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); - - await Promise.all( - item.positions.map(async (pos: any) => { - delete pos.id; - const employeePosition: EmployeePosition = Object.assign( - new EmployeePosition(), - pos, - ); - employeePosition.posMasterId = employeePosMaster.id; - 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 cloneEmployeeNodeBatch( + filteredEmployeePosMaster, + repoEmployeePosmaster, + "posMasterId", + { orgRootId: matchedOrgRoot?.id ?? null }, ); - await Promise.all( - (employeeTempPosMasterByNode.get(getNodeKey("root", dataId)) ?? []) - .map(async (item: any) => { - delete item.id; - const employeeTempPosMaster = Object.assign(new EmployeeTempPosMaster(), item); - employeeTempPosMaster.positions = []; - employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id; - employeeTempPosMaster.orgRootId = matchedOrgRoot?.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); - - await Promise.all( - item.positions.map(async (pos: any) => { - delete pos.id; - const employeePosition: EmployeePosition = Object.assign( - new EmployeePosition(), - pos, - ); - employeePosition.posMasterTempId = employeeTempPosMaster.id; - 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 cloneEmployeeNodeBatch( + employeeTempPosMasterByNode.get(getNodeKey("root", dataId)) ?? [], + repoEmployeeTempPosmaster, + "posMasterTempId", + { orgRootId: matchedOrgRoot?.id ?? null }, ); for (const x of orgChild1ByRoot.get(dataId) ?? []) { const data1Id = x.id; const matchedOrgChild1 = findMatchedNodeByAncestorDNA(orgChild1Current, x); - await Promise.all( - (employeePosMasterByNode.get(getNodeKey("child1", data1Id)) ?? []) - .map(async (item: any) => { - delete item.id; - const employeePosMaster = Object.assign(new EmployeePosMaster(), item); - employeePosMaster.positions = []; - 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); - - await Promise.all( - item.positions.map(async (pos: any) => { - delete pos.id; - const employeePosition: EmployeePosition = Object.assign( - new EmployeePosition(), - pos, - ); - employeePosition.posMasterId = employeePosMaster.id; - 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 cloneEmployeeNodeBatch( + employeePosMasterByNode.get(getNodeKey("child1", data1Id)) ?? [], + repoEmployeePosmaster, + "posMasterId", + { + orgRootId: matchedOrgRoot?.id ?? null, + orgChild1Id: matchedOrgChild1?.id ?? null, + }, ); - await Promise.all( - (employeeTempPosMasterByNode.get(getNodeKey("child1", data1Id)) ?? []) - .map(async (item: any) => { - delete item.id; - const employeeTempPosMaster = Object.assign(new EmployeeTempPosMaster(), item); - employeeTempPosMaster.positions = []; - 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); - - await Promise.all( - item.positions.map(async (pos: any) => { - delete pos.id; - const employeePosition: EmployeePosition = Object.assign( - new EmployeePosition(), - pos, - ); - employeePosition.posMasterTempId = employeeTempPosMaster.id; - 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 cloneEmployeeNodeBatch( + employeeTempPosMasterByNode.get(getNodeKey("child1", data1Id)) ?? [], + repoEmployeeTempPosmaster, + "posMasterTempId", + { + orgRootId: matchedOrgRoot?.id ?? null, + orgChild1Id: matchedOrgChild1?.id ?? null, + }, ); for (const x of orgChild2ByChild1.get(data1Id) ?? []) { const data2Id = x.id; const matchedOrgChild2 = findMatchedNodeByAncestorDNA(orgChild2Current, x); - await Promise.all( - (employeePosMasterByNode.get(getNodeKey("child2", data2Id)) ?? []) - .map(async (item: any) => { - delete item.id; - const employeePosMaster = Object.assign(new EmployeePosMaster(), item); - employeePosMaster.positions = []; - 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(); - employeePosMaster.lastUpdateUserId = ""; - employeePosMaster.lastUpdateFullName = "System Administrator"; - employeePosMaster.lastUpdatedAt = new Date(); - await repoEmployeePosmaster.save(employeePosMaster); - - await Promise.all( - item.positions.map(async (pos: any) => { - delete pos.id; - const employeePosition: EmployeePosition = Object.assign( - new EmployeePosition(), - pos, - ); - employeePosition.posMasterId = employeePosMaster.id; - 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 cloneEmployeeNodeBatch( + employeePosMasterByNode.get(getNodeKey("child2", data2Id)) ?? [], + repoEmployeePosmaster, + "posMasterId", + { + orgRootId: matchedOrgRoot?.id ?? null, + orgChild1Id: matchedOrgChild1?.id ?? null, + orgChild2Id: matchedOrgChild2?.id ?? null, + }, ); - await Promise.all( - (employeeTempPosMasterByNode.get(getNodeKey("child2", data2Id)) ?? []) - .map(async (item: any) => { - delete item.id; - const employeeTempPosMaster = Object.assign(new EmployeeTempPosMaster(), item); - employeeTempPosMaster.positions = []; - employeeTempPosMaster.orgRevisionId = orgRevisionDraft.id; - employeeTempPosMaster.orgRootId = dataId; - employeeTempPosMaster.orgChild1Id = data1Id; - employeeTempPosMaster.orgChild2Id = data2Id; - employeeTempPosMaster.createdUserId = ""; - employeeTempPosMaster.createdFullName = "System Administrator"; - employeeTempPosMaster.createdAt = new Date(); - employeeTempPosMaster.lastUpdateUserId = ""; - employeeTempPosMaster.lastUpdateFullName = "System Administrator"; - employeeTempPosMaster.lastUpdatedAt = new Date(); - await repoEmployeeTempPosmaster.save(employeeTempPosMaster); - - await Promise.all( - item.positions.map(async (pos: any) => { - delete pos.id; - const employeePosition: EmployeePosition = Object.assign( - new EmployeePosition(), - pos, - ); - employeePosition.posMasterTempId = employeeTempPosMaster.id; - 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 cloneEmployeeNodeBatch( + employeeTempPosMasterByNode.get(getNodeKey("child2", data2Id)) ?? [], + repoEmployeeTempPosmaster, + "posMasterTempId", + { + orgRootId: dataId, + orgChild1Id: data1Id, + orgChild2Id: data2Id, + }, ); for (const x of orgChild3ByChild2.get(data2Id) ?? []) { const data3Id = x.id; const matchedOrgChild3 = findMatchedNodeByAncestorDNA(orgChild3Current, x); - await Promise.all( - (employeePosMasterByNode.get(getNodeKey("child3", data3Id)) ?? []) - .map(async (item: any) => { - delete item.id; - const employeePosMaster = Object.assign(new EmployeePosMaster(), item); - employeePosMaster.positions = []; - 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); - - await Promise.all( - item.positions.map(async (pos: any) => { - delete pos.id; - const employeePosition: EmployeePosition = Object.assign( - new EmployeePosition(), - pos, - ); - employeePosition.posMasterId = employeePosMaster.id; - 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 cloneEmployeeNodeBatch( + employeePosMasterByNode.get(getNodeKey("child3", data3Id)) ?? [], + repoEmployeePosmaster, + "posMasterId", + { + orgRootId: matchedOrgRoot?.id ?? null, + orgChild1Id: matchedOrgChild1?.id ?? null, + orgChild2Id: matchedOrgChild2?.id ?? null, + orgChild3Id: matchedOrgChild3?.id ?? null, + }, ); - await Promise.all( - (employeeTempPosMasterByNode.get(getNodeKey("child3", data3Id)) ?? []) - .map(async (item: any) => { - delete item.id; - const employeeTempPosMaster = Object.assign(new EmployeeTempPosMaster(), item); - employeeTempPosMaster.positions = []; - 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); - - await Promise.all( - item.positions.map(async (pos: any) => { - delete pos.id; - const employeePosition: EmployeePosition = Object.assign( - new EmployeePosition(), - pos, - ); - employeePosition.posMasterTempId = employeeTempPosMaster.id; - 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 cloneEmployeeNodeBatch( + employeeTempPosMasterByNode.get(getNodeKey("child3", data3Id)) ?? [], + repoEmployeeTempPosmaster, + "posMasterTempId", + { + orgRootId: matchedOrgRoot?.id ?? null, + orgChild1Id: matchedOrgChild1?.id ?? null, + orgChild2Id: matchedOrgChild2?.id ?? null, + orgChild3Id: matchedOrgChild3?.id ?? null, + }, ); for (const x of orgChild4ByChild3.get(data3Id) ?? []) { const data4Id = x.id; const matchedOrgChild4 = findMatchedNodeByAncestorDNA(orgChild4Current, x); - await Promise.all( - (employeePosMasterByNode.get(getNodeKey("child4", data4Id)) ?? []) - .map(async (item: any) => { - delete item.id; - const employeePosMaster = Object.assign(new EmployeePosMaster(), item); - employeePosMaster.positions = []; - 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); - - await Promise.all( - item.positions.map(async (pos: any) => { - delete pos.id; - const employeePosition: EmployeePosition = Object.assign( - new EmployeePosition(), - pos, - ); - employeePosition.posMasterId = employeePosMaster.id; - 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 cloneEmployeeNodeBatch( + employeePosMasterByNode.get(getNodeKey("child4", data4Id)) ?? [], + repoEmployeePosmaster, + "posMasterId", + { + orgRootId: matchedOrgRoot?.id ?? null, + orgChild1Id: matchedOrgChild1?.id ?? null, + orgChild2Id: matchedOrgChild2?.id ?? null, + orgChild3Id: matchedOrgChild3?.id ?? null, + orgChild4Id: matchedOrgChild4?.id ?? null, + }, ); - await Promise.all( - (employeeTempPosMasterByNode.get(getNodeKey("child4", data4Id)) ?? []) - .map(async (item: any) => { - delete item.id; - const employeeTempPosMaster = Object.assign( - new EmployeeTempPosMaster(), - item, - ); - employeeTempPosMaster.positions = []; - 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); - - await Promise.all( - item.positions.map(async (pos: any) => { - delete pos.id; - const employeePosition: EmployeePosition = Object.assign( - new EmployeePosition(), - pos, - ); - employeePosition.posMasterTempId = employeeTempPosMaster.id; - 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 cloneEmployeeNodeBatch( + employeeTempPosMasterByNode.get(getNodeKey("child4", data4Id)) ?? [], + repoEmployeeTempPosmaster, + "posMasterTempId", + { + orgRootId: matchedOrgRoot?.id ?? null, + orgChild1Id: matchedOrgChild1?.id ?? null, + orgChild2Id: matchedOrgChild2?.id ?? null, + orgChild3Id: matchedOrgChild3?.id ?? null, + orgChild4Id: matchedOrgChild4?.id ?? null, + }, ); } }