Skip to content

Commit

Permalink
Fix failing test in mock-hierarchies
Browse files Browse the repository at this point in the history
  • Loading branch information
kennsippell committed Nov 23, 2024
1 parent a0a0c84 commit f73f9c6
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 63 deletions.
2 changes: 1 addition & 1 deletion src/lib/hierarchy-operations/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async function descendantsOf(db, contactId) {
}

async function reportsCreatedByOrAt(db, createdByIds, createdAtId, skip) {
const createdByKeys = createdByIds.map(descendantId => [`contact:${descendantId}`]);
const createdByKeys = createdByIds.map(id => [`contact:${id}`]);
const createdAtKeys = createdAtId ? [
[`patient_id:${createdAtId}`],
[`patient_uuid:${createdAtId}`],
Expand Down
134 changes: 72 additions & 62 deletions src/lib/hierarchy-operations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const JsDocs = require('./jsdocFolder');
const Backend = require('./backend');

const HierarchyOperations = (options) => {
const move = async (sourceIds, destinationId, db) => {
async function move(sourceIds, destinationId, db) {
JsDocs.prepareFolder(options);
trace(`Fetching contact details: ${destinationId}`);
const constraints = await LineageConstraints(db, options);
Expand Down Expand Up @@ -55,10 +55,9 @@ const HierarchyOperations = (options) => {
}

info(`Staged changes to lineage information for ${affectedContactCount} contact(s) and ${affectedReportCount} report(s).`);
};
}


const moveReports = async (db, descendantsAndSelf, replacementLineage, sourceId, destinationId) => {
async function moveReports(db, descendantsAndSelf, replacementLineage, sourceId, destinationId) {
const descendantIds = descendantsAndSelf.map(contact => contact._id);

let skip = 0;
Expand All @@ -71,28 +70,7 @@ const HierarchyOperations = (options) => {
const updatedReports = replaceLineageInReports(reportDocsBatch, replacementLineage, sourceId);

if (options.merge) {
reportDocsBatch.forEach(report => {
let updated = false;
const subjectIds = ['patient_id', 'patient_uuid', 'place_id', 'place_uuid'];
for (const subjectId of subjectIds) {
if (report[subjectId] === sourceId) {
report[subjectId] = destinationId;
updated = true;
}

if (report.fields[subjectId] === sourceId) {
report.fields[subjectId] = destinationId;
updated = true;
}

if (updated) {
const isAlreadyUpdated = !!updatedReports.find(updated => updated._id === report._id);
if (!isAlreadyUpdated) {
updatedReports.push(report);
}
}
}
});
reassignReports(reportDocsBatch, sourceId, destinationId, updatedReports);
}

minifyLineageAndWriteToDisk(updatedReports);
Expand All @@ -101,51 +79,82 @@ const HierarchyOperations = (options) => {
} while (reportDocsBatch.length >= Backend.BATCH_SIZE);

return skip;
};
}

function reassignReports(reports, sourceId, destinationId, updatedReports) {
reports.forEach(report => {
let updated = false;
const subjectIds = ['patient_id', 'patient_uuid', 'place_id', 'place_uuid'];
for (const subjectId of subjectIds) {
if (report[subjectId] === sourceId) {
report[subjectId] = destinationId;
updated = true;
}

if (report.fields[subjectId] === sourceId) {
report.fields[subjectId] = destinationId;
updated = true;
}

if (updated) {
const isAlreadyUpdated = !!updatedReports.find(updated => updated._id === report._id);
if (!isAlreadyUpdated) {
updatedReports.push(report);
}
}
}
});
}

const minifyLineageAndWriteToDisk = (docs) => {
function minifyLineageAndWriteToDisk(docs) {
docs.forEach(doc => {
lineageManipulation.minifyLineagesInDoc(doc);
JsDocs.writeDoc(options, doc);
});
};

const replaceLineageInReports = (reportsCreatedByDescendants, replaceWith, startingFromIdInLineage) => reportsCreatedByDescendants.reduce((agg, doc) => {
if (lineageManipulation.replaceLineage(doc, 'contact', replaceWith, startingFromIdInLineage, options)) {
agg.push(doc);
}
return agg;
}, []);

const replaceLineageInAncestors = (descendantsAndSelf, ancestors) => ancestors.reduce((agg, ancestor) => {
let result = agg;
const primaryContact = descendantsAndSelf.find(descendant => ancestor.contact && descendant._id === ancestor.contact._id);
if (primaryContact) {
ancestor.contact = lineageManipulation.createLineageFromDoc(primaryContact);
result = [ancestor, ...result];
}

return result;
}, []);
}

const replaceLineageInContacts = (descendantsAndSelf, replacementLineage, destinationId) => descendantsAndSelf.reduce((agg, doc) => {
const startingFromIdInLineage = options.merge ? destinationId :
doc._id === destinationId ? undefined : destinationId;
function replaceLineageInReports(reportsCreatedByDescendants, replaceWith, startingFromIdInLineage) {
return reportsCreatedByDescendants.reduce((agg, doc) => {
if (lineageManipulation.replaceLineage(doc, 'contact', replaceWith, startingFromIdInLineage, options)) {
agg.push(doc);
}
return agg;
}, []);
}

function replaceLineageInAncestors(descendantsAndSelf, ancestors) {
return ancestors.reduce((agg, ancestor) => {
let result = agg;
const primaryContact = descendantsAndSelf.find(descendant => ancestor.contact && descendant._id === ancestor.contact._id);
if (primaryContact) {
ancestor.contact = lineageManipulation.createLineageFromDoc(primaryContact);
result = [ancestor, ...result];
}

// skip top-level because it will be deleted
if (options.merge) {
if (doc._id === destinationId) {
return agg;
return result;
}, []);
}

function replaceLineageInContacts(descendantsAndSelf, replacementLineage, destinationId) {
return descendantsAndSelf.reduce((agg, doc) => {
const startingFromIdInLineage = options.merge ? destinationId :
doc._id === destinationId ? undefined : destinationId;

// skip top-level because it will be deleted
if (options.merge) {
if (doc._id === destinationId) {
return agg;
}
}
}

const parentWasUpdated = lineageManipulation.replaceLineage(doc, 'parent', replacementLineage, startingFromIdInLineage, options);
const contactWasUpdated = lineageManipulation.replaceLineage(doc, 'contact', replacementLineage, destinationId, options);
if (parentWasUpdated || contactWasUpdated) {
agg.push(doc);
}
return agg;
}, []);
const parentWasUpdated = lineageManipulation.replaceLineage(doc, 'parent', replacementLineage, startingFromIdInLineage, options);
const contactWasUpdated = lineageManipulation.replaceLineage(doc, 'contact', replacementLineage, destinationId, options);
if (parentWasUpdated || contactWasUpdated) {
agg.push(doc);
}
return agg;
}, []);
}

return { move };
};
Expand All @@ -155,3 +164,4 @@ module.exports = options => ({
move: HierarchyOperations({ ...options, merge: false }).move,
merge: HierarchyOperations({ ...options, merge: true }).move,
});

1 change: 1 addition & 0 deletions test/mock-hierarchies.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ describe('mocks', () => {
_id: 'report_1',
type: 'data_record',
form: 'foo',
fields: {},
contact: {
_id: 'health_center_1_contact',
parent: {
Expand Down

0 comments on commit f73f9c6

Please sign in to comment.