Skip to content

Commit

Permalink
Very basic version to merge contacts
Browse files Browse the repository at this point in the history
  • Loading branch information
kennsippell committed Dec 30, 2023
1 parent 958f0b4 commit 59b095f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ coverage
.nyc_output
.DS_Store
test/.DS_Store
json_docs
27 changes: 19 additions & 8 deletions src/fn/move-contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,31 @@ const updateLineagesAndStage = async (options, db) => {
const replacementLineage = lineageManipulation.createLineageFromDoc(parentDoc);
for (let contactId of options.contactIds) {
const contactDoc = contactDocs[contactId];
const descendantsAndSelf = await fetch.descendantsOf(db, contactId);
const descendantsNoSelf = await fetch.descendantsOf(db, contactId);

const self = descendantsNoSelf.find(d => d._id === contactId);
writeDocumentToDisk(options, {
_id: contactId,
_rev: self._rev,
_deleted: true,
});

// Check that primary contact is not removed from areas where they are required
const invalidPrimaryContactDoc = await constraints.getPrimaryContactViolations(contactDoc, descendantsAndSelf);
const invalidPrimaryContactDoc = await constraints.getPrimaryContactViolations(contactDoc, descendantsNoSelf);
if (invalidPrimaryContactDoc) {
throw Error(`Cannot remove contact ${prettyPrintDocument(invalidPrimaryContactDoc)} from the hierarchy for which they are a primary contact.`);
}

trace(`Considering lineage updates to ${descendantsAndSelf.length} descendant(s) of contact ${prettyPrintDocument(contactDoc)}.`);
const updatedDescendants = replaceLineageInContacts(descendantsAndSelf, replacementLineage, contactId);
trace(`Considering lineage updates to ${descendantsNoSelf.length} descendant(s) of contact ${prettyPrintDocument(contactDoc)}.`);
const updatedDescendants = replaceLineageInContacts(descendantsNoSelf, replacementLineage, contactId);

const ancestors = await fetch.ancestorsOf(db, contactDoc);
trace(`Considering primary contact updates to ${ancestors.length} ancestor(s) of contact ${prettyPrintDocument(contactDoc)}.`);
const updatedAncestors = replaceLineageInAncestors(descendantsAndSelf, ancestors);
const updatedAncestors = replaceLineageInAncestors(descendantsNoSelf, ancestors);

minifyLineageAndWriteToDisk([...updatedDescendants, ...updatedAncestors], options);

const movedReportsCount = await moveReports(db, descendantsAndSelf, options, replacementLineage, contactId);
const movedReportsCount = await moveReports(db, descendantsNoSelf, options, replacementLineage, contactId);
trace(`${movedReportsCount} report(s) created by these affected contact(s) will be updated`);

affectedContactCount += updatedDescendants.length + updatedAncestors.length;
Expand Down Expand Up @@ -270,8 +277,12 @@ const replaceLineageInReports = (reportsCreatedByDescendants, replaceWith, start
}, []);

const replaceLineageInContacts = (descendantsAndSelf, replacementLineage, contactId) => descendantsAndSelf.reduce((agg, doc) => {
const startingFromIdInLineage = doc._id === contactId ? undefined : contactId;
const parentWasUpdated = lineageManipulation.replaceLineage(doc, 'parent', replacementLineage, startingFromIdInLineage);
// skip top-level because it is now being deleted
if (doc._id === contactId) {
return agg;
}

const parentWasUpdated = lineageManipulation.replaceLineage(doc, 'parent', replacementLineage, contactId);
const contactWasUpdated = lineageManipulation.replaceLineage(doc, 'contact', replacementLineage, contactId);
if (parentWasUpdated || contactWasUpdated) {
agg.push(doc);
Expand Down
10 changes: 5 additions & 5 deletions src/lib/lineage-constraints.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const lineageConstraints = async (repository, parentDoc) => {
trace('Found app_settings.contact_types. Configurable hierarchy constraints will be enforced.');
mapTypeToAllowedParents = contact_types
.filter(rule => rule)
.reduce((agg, curr) => Object.assign(agg, { [curr.id]: curr.parents }), {});
.reduce((agg, curr) => Object.assign(agg, { [curr.id]: [curr.id] }), {});
}
} catch (err) {
if (err.name !== 'not_found') {
Expand All @@ -24,10 +24,10 @@ const lineageConstraints = async (repository, parentDoc) => {
if (!mapTypeToAllowedParents) {
trace('Default hierarchy constraints will be enforced.');
mapTypeToAllowedParents = {
district_hospital: [],
health_center: ['district_hospital'],
clinic: ['health_center'],
person: ['district_hospital', 'health_center', 'clinic'],
district_hospital: ['district_hospital'],
health_center: ['health_center'],
clinic: ['clinic'],
person: [],
};
}

Expand Down
8 changes: 5 additions & 3 deletions src/lib/lineage-manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ const replaceLineage = (doc, lineageAttributeName, replaceWith, startingFromIdIn
if (!replaceWith) {
const lineageWasDeleted = !!replaceInDoc[docAttr];
replaceInDoc[docAttr] = undefined;
throw 'lazy';
return lineageWasDeleted;

Check failure on line 14 in src/lib/lineage-manipulation.js

View workflow job for this annotation

GitHub Actions / Build for Node version 8.x

Unreachable code

Check failure on line 14 in src/lib/lineage-manipulation.js

View workflow job for this annotation

GitHub Actions / Build for Node version 10.x

Unreachable code

Check failure on line 14 in src/lib/lineage-manipulation.js

View workflow job for this annotation

GitHub Actions / Build for Node version 12.x

Unreachable code

Check failure on line 14 in src/lib/lineage-manipulation.js

View workflow job for this annotation

GitHub Actions / Build for Node version 14.x

Unreachable code

Check failure on line 14 in src/lib/lineage-manipulation.js

View workflow job for this annotation

GitHub Actions / Build for Node version 16.x

Unreachable code

Check failure on line 14 in src/lib/lineage-manipulation.js

View workflow job for this annotation

GitHub Actions / Build for Node version 18.x

Unreachable code
} else if (replaceInDoc[docAttr]) {
replaceInDoc[docAttr]._id = replaceWith._id;
replaceInDoc[docAttr].parent = replaceWith.parent;
replaceInDoc._id = replaceWith._id;
replaceInDoc.parent = replaceWith.parent;
} else {
replaceInDoc[docAttr] = replaceWith;
replaceInDoc = replaceWith;
throw 'lazy';
}

return true;
Expand Down

0 comments on commit 59b095f

Please sign in to comment.