Skip to content

Commit

Permalink
Last code review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
kennsippell committed Dec 9, 2024
1 parent 28be7fb commit 99745c6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
3 changes: 2 additions & 1 deletion src/lib/hierarchy-operations/hierarchy-data-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ async function getContactWithDescendants(db, contactId) {

return descendantDocs.rows
.map(row => row.doc)
/* We should not move or update tombstone documents */
// We should not move or update tombstone documents
// Not relevant for 4.x cht-core versions, but needed in older versions.
.filter(doc => doc && doc.type !== 'tombstone');
}

Expand Down
36 changes: 13 additions & 23 deletions src/lib/hierarchy-operations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async function updateReports(db, options, moveContext) {
const createdAtId = options.merge && moveContext.sourceId;
reportDocsBatch = await DataSource.getReportsForContacts(db, descendantIds, createdAtId, skip);

const lineageUpdates = replaceCreatorLineageInReports(reportDocsBatch, moveContext);
const lineageUpdates = replaceLineageOfReportCreator(reportDocsBatch, moveContext);
const reassignUpdates = reassignReports(reportDocsBatch, moveContext);
const updatedReports = reportDocsBatch.filter(doc => lineageUpdates.has(doc._id) || reassignUpdates.has(doc._id));

Expand Down Expand Up @@ -115,14 +115,15 @@ function reassignReports(reports, moveContext) {
return updated;
}

// This ensures all documents written are fully minified. Some docs in CouchDB are not minified to start with.
function minifyLineageAndWriteToDisk(options, docs) {
docs.forEach(doc => {
lineageManipulation.minifyLineagesInDoc(doc);
JsDocs.writeDoc(options, doc);
});
}

function replaceCreatorLineageInReports(reports, moveContext) {
function replaceLineageOfReportCreator(reports, moveContext) {
const replaceContactLineage = doc => lineageManipulation.replaceContactLineage(doc, {
replaceWith: moveContext.replacementLineage,
startingFromId: moveContext.sourceId,
Expand Down Expand Up @@ -152,10 +153,14 @@ function replaceLineageInAncestors(descendantsAndSelf, ancestors) {
return updatedAncestors;
}

function replaceForSingleContact(doc, moveContext) {
function replaceLineageInSingleContact(doc, moveContext) {
const { sourceId } = moveContext;
const docIsDestination = doc._id === sourceId;
const startingFromId = moveContext.merge || !docIsDestination ? sourceId : undefined;
const docIsSource = doc._id === moveContext.sourceId;
if (docIsSource && moveContext.merge) {
return;
}

const startingFromId = moveContext.merge || !docIsSource ? sourceId : undefined;
const replaceLineageOptions = {
replaceWith: moveContext.replacementLineage,
startingFromId,
Expand All @@ -171,24 +176,9 @@ function replaceForSingleContact(doc, moveContext) {
}

function replaceLineageInContacts(options, moveContext) {
function sonarQubeComplexityFiveIsTooLow(doc) {
const docIsSource = doc._id === moveContext.sourceId;

// skip source because it will be deleted
if (!options.merge || !docIsSource) {
return replaceForSingleContact(doc, moveContext);
}
}

const result = [];
for (const doc of moveContext.descendantsAndSelf) {
const updatedDoc = sonarQubeComplexityFiveIsTooLow(doc);
if (updatedDoc) {
result.push(updatedDoc);
}
}

return result;
return moveContext.descendantsAndSelf
.map(descendant => replaceLineageInSingleContact(descendant, moveContext))
.filter(Boolean);
}

module.exports = (db, options) => {
Expand Down

0 comments on commit 99745c6

Please sign in to comment.