-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ecf723
commit af9a9ac
Showing
3 changed files
with
81 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
function replaceLineage(doc, lineageAttributeName, params) { | ||
// Replace the full lineage | ||
if (!params.startingFromId) { | ||
return replaceEntireLineage(doc, lineageAttributeName, params.replaceWith); | ||
} | ||
|
||
const selectedFunction = params.merge ? replaceLineageForMerge : replaceLineageForMove; | ||
return selectedFunction(doc, lineageAttributeName, params); | ||
} | ||
|
||
function replaceLineageForMove(doc, lineageAttributeName, params) { | ||
let currentElement = doc[lineageAttributeName]; | ||
while (currentElement) { | ||
if (currentElement?._id === params.startingFromId) { | ||
return replaceEntireLineage(currentElement, 'parent', params.replaceWith); | ||
} | ||
|
||
currentElement = currentElement.parent; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function replaceLineageForMerge(doc, lineageAttributeName, params) { | ||
let currentElement = doc; | ||
let currentAttributeName = lineageAttributeName; | ||
while (currentElement) { | ||
if (currentElement[currentAttributeName]?._id === params.startingFromId) { | ||
return replaceEntireLineage(currentElement, currentAttributeName, params.replaceWith); | ||
} | ||
|
||
currentElement = currentElement[currentAttributeName]; | ||
currentAttributeName = 'parent'; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function replaceEntireLineage(replaceInDoc, lineageAttributeName, replaceWith) { | ||
if (!replaceWith) { | ||
const lineageWasDeleted = !!replaceInDoc[lineageAttributeName]; | ||
replaceInDoc[lineageAttributeName] = undefined; | ||
return lineageWasDeleted; | ||
} else { | ||
replaceInDoc[lineageAttributeName] = replaceWith; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
module.exports = { | ||
/** | ||
* Given a doc, replace the lineage information therein with "replaceWith" | ||
* | ||
* @param {Object} doc A CouchDB document containing a hierarchy that needs replacing | ||
* @param {Object} params | ||
* @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 | ||
*/ | ||
replaceParentLineage: (doc, params) => { | ||
return replaceLineage(doc, 'parent', params); | ||
}, | ||
|
||
/** | ||
* Given a doc, replace the lineage information therein with "replaceWith" | ||
* | ||
* @param {Object} doc A CouchDB document containing a hierarchy that needs replacing | ||
* @param {Object} params | ||
* @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 | ||
*/ | ||
replaceContactLineage: (doc, params) => { | ||
return replaceLineage(doc, 'contact', params); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters