From 687a6a231f088d53980754e0c03e48478d02fb2d Mon Sep 17 00:00:00 2001 From: kennsippell Date: Fri, 22 Nov 2024 20:59:24 -0700 Subject: [PATCH] SonarQube --- src/lib/hierarchy-operations/index.js | 30 +++++++-- .../lineage-manipulation.js | 28 ++++---- .../lineage-manipulation.spec.js | 66 +++++++++++++++---- 3 files changed, 93 insertions(+), 31 deletions(-) diff --git a/src/lib/hierarchy-operations/index.js b/src/lib/hierarchy-operations/index.js index f19a8e21..19b6b861 100644 --- a/src/lib/hierarchy-operations/index.js +++ b/src/lib/hierarchy-operations/index.js @@ -117,9 +117,16 @@ const HierarchyOperations = (db, options) => { }); } - function replaceLineageInReports(reportsCreatedByDescendants, replaceWith, startingFromIdInLineage) { + function replaceLineageInReports(reportsCreatedByDescendants, replaceWith, startingFromId) { return reportsCreatedByDescendants.reduce((agg, doc) => { - if (lineageManipulation.replaceLineage(doc, 'contact', replaceWith, startingFromIdInLineage, options)) { + const replaceLineageOptions = { + lineageAttribute: 'contact', + replaceWith, + startingFromId, + merge: options.merge, + }; + + if (lineageManipulation.replaceLineage(doc, replaceLineageOptions)) { agg.push(doc); } return agg; @@ -139,18 +146,27 @@ const HierarchyOperations = (db, options) => { }, []); } - function replaceLineageInContacts(descendantsAndSelf, replacementLineage, destinationId) { + function replaceLineageInContacts(descendantsAndSelf, replaceWith, 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 startingFromId = options.merge || !docIsDestination ? destinationId : undefined; + const replaceLineageOptions = { + lineageAttribute: 'parent', + replaceWith, + startingFromId, + merge: options.merge, + }; + const parentWasUpdated = lineageManipulation.replaceLineage(doc, replaceLineageOptions); + + replaceLineageOptions.lineageAttribute = 'contact'; + replaceLineageOptions.startingFromId = destinationId; + const contactWasUpdated = lineageManipulation.replaceLineage(doc, replaceLineageOptions); const isUpdated = parentWasUpdated || contactWasUpdated; if (isUpdated) { result.push(doc); } } - + const result = []; for (const doc of descendantsAndSelf) { const docIsDestination = doc._id === destinationId; diff --git a/src/lib/hierarchy-operations/lineage-manipulation.js b/src/lib/hierarchy-operations/lineage-manipulation.js index e4967a7e..f966e330 100644 --- a/src/lib/hierarchy-operations/lineage-manipulation.js +++ b/src/lib/hierarchy-operations/lineage-manipulation.js @@ -3,35 +3,37 @@ * Given a doc, replace the lineage information therein with "replaceWith" * * @param {Object} doc A CouchDB document containing a hierarchy that needs replacing - * @param {string} lineageAttributeName Name of the attribute which is a lineage in doc (contact or parent) - * @param {Object} replaceWith The new hierarchy { parent: { _id: 'parent', parent: { _id: 'grandparent' } } - * @param {string} [startingFromIdInLineage] Only the part of the lineage "after" this id will be replaced - * @param {Object} options - * @param {boolean} merge When true, startingFromIdInLineage is replaced and when false, startingFromIdInLineage's parent is replaced + * @param {Object} params SonarQube + * @param {string} params.lineageAttribute Name of the attribute which is a lineage in doc (contact or parent) + * @param {Object} params.replaceWith The new hierarchy { parent: { _id: 'parent', parent: { _id: 'grandparent' } } + * @param {string} params.startingFromId Only the part of the lineage "after" this id will be replaced + * @param {boolean} params.merge When true, startingFromId is replaced and when false, startingFromId's parent is replaced */ -function replaceLineage(doc, lineageAttributeName, replaceWith, startingFromIdInLineage, options={}) { +function replaceLineage(doc, params) { + const { lineageAttribute, replaceWith, startingFromId, merge } = params; + // Replace the full lineage - if (!startingFromIdInLineage) { - return replaceWithinLineage(doc, lineageAttributeName, replaceWith); + if (!startingFromId) { + return replaceWithinLineage(doc, lineageAttribute, replaceWith); } function getInitialState() { - if (options.merge) { + if (merge) { return { element: doc, - attributeName: lineageAttributeName, + attributeName: lineageAttribute, }; } return { - element: doc[lineageAttributeName], + element: doc[lineageAttribute], attributeName: 'parent', }; } function traverseOne() { - const compare = options.merge ? state.element[state.attributeName] : state.element; - if (compare?._id === startingFromIdInLineage) { + const compare = merge ? state.element[state.attributeName] : state.element; + if (compare?._id === startingFromId) { return replaceWithinLineage(state.element, state.attributeName, replaceWith); } diff --git a/test/lib/hierarchy-operations/lineage-manipulation.spec.js b/test/lib/hierarchy-operations/lineage-manipulation.spec.js index be324009..80077aa9 100644 --- a/test/lib/hierarchy-operations/lineage-manipulation.spec.js +++ b/test/lib/hierarchy-operations/lineage-manipulation.spec.js @@ -4,16 +4,19 @@ const log = require('../../../src/lib/log'); log.level = log.LEVEL_TRACE; const { parentsToLineage } = require('../../mock-hierarchies'); -const mergeOption = { merge: true }; describe('lineage manipulation', () => { - describe('kenn replaceLineage', () => { + describe('replaceLineage', () => { const mockReport = data => Object.assign({ _id: 'r', type: 'data_record', contact: parentsToLineage('parent', 'grandparent') }, data); const mockContact = data => Object.assign({ _id: 'c', type: 'person', parent: parentsToLineage('parent', 'grandparent') }, data); it('replace with empty lineage', () => { const mock = mockReport(); - expect(replaceLineage(mock, 'contact', undefined)).to.be.true; + const replaceLineageOptions = { + lineageAttribute: 'contact', + replaceWith: undefined, + }; + expect(replaceLineage(mock, replaceLineageOptions)).to.be.true; expect(mock).to.deep.eq({ _id: 'r', type: 'data_record', @@ -23,7 +26,11 @@ describe('lineage manipulation', () => { it('replace full lineage', () => { const mock = mockContact(); - expect(replaceLineage(mock, 'parent', parentsToLineage('new_parent'))).to.be.true; + const replaceLineageOptions = { + lineageAttribute: 'parent', + replaceWith: parentsToLineage('new_parent'), + }; + expect(replaceLineage(mock, replaceLineageOptions)).to.be.true; expect(mock).to.deep.eq({ _id: 'c', type: 'person', @@ -35,7 +42,11 @@ describe('lineage manipulation', () => { const mock = mockContact(); delete mock.parent; - expect(replaceLineage(mock, 'parent', parentsToLineage('new_parent'))).to.be.true; + const replaceLineageOptions = { + lineageAttribute: 'parent', + replaceWith: parentsToLineage('new_parent'), + }; + expect(replaceLineage(mock, replaceLineageOptions)).to.be.true; expect(mock).to.deep.eq({ _id: 'c', type: 'person', @@ -46,12 +57,23 @@ describe('lineage manipulation', () => { it('replace empty with empty', () => { const mock = mockContact(); delete mock.parent; - expect(replaceLineage(mock, 'parent', undefined)).to.be.false; + + const replaceLineageOptions = { + lineageAttribute: 'parent', + replaceWith: undefined, + }; + expect(replaceLineage(mock, replaceLineageOptions)).to.be.false; }); it('replace lineage starting at contact', () => { const mock = mockContact(); - expect(replaceLineage(mock, 'parent', parentsToLineage('new_grandparent'), 'parent')).to.be.true; + + const replaceLineageOptions = { + lineageAttribute: 'parent', + replaceWith: parentsToLineage('new_grandparent'), + startingFromId: 'parent', + }; + expect(replaceLineage(mock, replaceLineageOptions)).to.be.true; expect(mock).to.deep.eq({ _id: 'c', type: 'person', @@ -61,7 +83,13 @@ describe('lineage manipulation', () => { it('merge new parent', () => { const mock = mockContact(); - expect(replaceLineage(mock, 'parent', parentsToLineage('new_parent', 'new_grandparent'), 'parent', mergeOption)).to.be.true; + const replaceLineageOptions = { + lineageAttribute: 'parent', + replaceWith: parentsToLineage('new_parent', 'new_grandparent'), + startingFromId: 'parent', + merge: true, + }; + expect(replaceLineage(mock, replaceLineageOptions)).to.be.true; expect(mock).to.deep.eq({ _id: 'c', type: 'person', @@ -71,7 +99,13 @@ describe('lineage manipulation', () => { it('merge grandparent of contact', () => { const mock = mockReport(); - expect(replaceLineage(mock, 'contact', parentsToLineage('new_grandparent'), 'grandparent', mergeOption)).to.be.true; + const replaceLineageOptions = { + lineageAttribute: 'contact', + replaceWith: parentsToLineage('new_grandparent'), + startingFromId: 'grandparent', + merge: true, + }; + expect(replaceLineage(mock, replaceLineageOptions)).to.be.true; expect(mock).to.deep.eq({ _id: 'r', type: 'data_record', @@ -81,7 +115,12 @@ describe('lineage manipulation', () => { it('replace empty starting at contact', () => { const mock = mockContact(); - expect(replaceLineage(mock, 'parent', undefined, 'parent')).to.be.true; + const replaceLineageOptions = { + lineageAttribute: 'parent', + replaceWith: undefined, + startingFromId: 'parent', + }; + expect(replaceLineage(mock, replaceLineageOptions)).to.be.true; expect(mock).to.deep.eq({ _id: 'c', type: 'person', @@ -91,7 +130,12 @@ describe('lineage manipulation', () => { it('replace starting at non-existant contact', () => { const mock = mockContact(); - expect(replaceLineage(mock, 'parent', parentsToLineage('irrelevant'), 'dne')).to.be.false; + const replaceLineageOptions = { + lineageAttribute: 'parent', + replaceWith: parentsToLineage('irrelevant'), + startingFromId: 'dne', + }; + expect(replaceLineage(mock, replaceLineageOptions)).to.be.false; }); });