From 7af035cd5df3fa10a993501ccc40b35200582a26 Mon Sep 17 00:00:00 2001 From: kennsippell Date: Fri, 22 Nov 2024 20:45:24 -0700 Subject: [PATCH] SonarQube - Fix? --- src/lib/hierarchy-operations/index.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/lib/hierarchy-operations/index.js b/src/lib/hierarchy-operations/index.js index fd6b5f6d..f19a8e21 100644 --- a/src/lib/hierarchy-operations/index.js +++ b/src/lib/hierarchy-operations/index.js @@ -140,22 +140,27 @@ const HierarchyOperations = (db, options) => { } function replaceLineageInContacts(descendantsAndSelf, replacementLineage, destinationId) { + function replaceForSingleContact(doc) { + const docIsDestination = doc._id === destinationId; + const startingFromIdInLineage = options.merge || !docIsDestination ? destinationId : undefined; + const parentWasUpdated = lineageManipulation.replaceLineage(doc, 'parent', replacementLineage, startingFromIdInLineage, options); + const contactWasUpdated = lineageManipulation.replaceLineage(doc, 'contact', replacementLineage, destinationId, options); + const isUpdated = parentWasUpdated || contactWasUpdated; + if (isUpdated) { + result.push(doc); + } + } + const result = []; for (const doc of descendantsAndSelf) { const docIsDestination = doc._id === destinationId; - const startingFromIdInLineage = options.merge || !docIsDestination ? destinationId : undefined; // skip top-level because it will be deleted if (options.merge && docIsDestination) { continue; } - const parentWasUpdated = lineageManipulation.replaceLineage(doc, 'parent', replacementLineage, startingFromIdInLineage, options); - const contactWasUpdated = lineageManipulation.replaceLineage(doc, 'contact', replacementLineage, destinationId, options); - const isUpdated = parentWasUpdated || contactWasUpdated; - if (isUpdated) { - result.push(doc); - } + replaceForSingleContact(doc); } return result;