Skip to content

Commit

Permalink
fix(#9704): throw pouch errors when saving target docs (#9705)
Browse files Browse the repository at this point in the history
  • Loading branch information
dianabarsan authored Dec 17, 2024
1 parent 58a5e7d commit 0738e80
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 38 deletions.
71 changes: 33 additions & 38 deletions shared-libs/rules-engine/src/pouchdb-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,14 @@ const medicPouchProvider = db => {
.then(([contactDocs, reportDocs, taskDocs]) => ({ contactDocs, reportDocs, taskDocs, userSettingsId }));
},

contactsBySubjectId: subjectIds => {
contactsBySubjectId: async (subjectIds) => {
const keys = subjectIds.map(key => ['shortcode', key]);
return dbQuery('medic-client/contacts_by_reference', { keys, include_docs: true })
.then(results => {
const shortcodeIds = results.rows.map(result => result.doc._id);
const idsThatArentShortcodes = subjectIds.filter(id => !results.rows.map(row => row.key[1]).includes(id));
const results = await db.query('medic-client/contacts_by_reference', { keys, include_docs: true });

return [...shortcodeIds, ...idsThatArentShortcodes];
});
const shortcodeIds = results.rows.map(result => result.doc._id);
const idsThatArentShortcodes = subjectIds.filter(id => !results.rows.map(row => row.key[1]).includes(id));

return [...shortcodeIds, ...idsThatArentShortcodes];
},

stateChangeCallback: docUpdateClosure(db),
Expand All @@ -82,6 +81,7 @@ const medicPouchProvider = db => {
reporting_period: docTag,
};
}
throw err;
})
.then(existingDoc => {
if (!updatedTargets && existingDoc._rev) {
Expand Down Expand Up @@ -125,40 +125,35 @@ const medicPouchProvider = db => {
return rowsOf(dbQuery( 'medic-client/tasks_by_contact', options));
},

taskDataFor: (contactIds, userSettingsDoc) => {
taskDataFor: async (contactIds, userSettingsDoc) => {
if (!contactIds || contactIds.length === 0) {
return Promise.resolve({});
return {};
}

return docsOf(db.allDocs({ keys: contactIds, include_docs: true }))
.then(contactDocs => {
const subjectIds = contactDocs.reduce((agg, contactDoc) => {
registrationUtils.getSubjectIds(contactDoc).forEach(subjectId => agg.add(subjectId));
return agg;
}, new Set(contactIds));

const keys = Array.from(subjectIds);
return Promise
.all([
docsOf(dbQuery('medic-client/reports_by_subject', { keys, include_docs: true })),
self.tasksByRelation(contactIds, 'requester'),
])
.then(([reportDocs, taskDocs]) => {
// tighten the connection between reports and contacts
// a report will only be allowed to generate tasks for a single contact!
reportDocs = reportDocs.filter(report => {
const subjectId = registrationUtils.getSubjectId(report);
return subjectIds.has(subjectId);
});

return {
userSettingsId: userSettingsDoc?._id,
contactDocs,
reportDocs,
taskDocs,
};
});
});
const contactDocs = await docsOf(db.allDocs({ keys: contactIds, include_docs: true }));
const subjectIds = contactDocs.reduce((agg, contactDoc) => {
registrationUtils.getSubjectIds(contactDoc).forEach(subjectId => agg.add(subjectId));
return agg;
}, new Set(contactIds));

const keys = Array.from(subjectIds);

const [reportDocs, taskDocs] = await Promise.all([
docsOf(dbQuery('medic-client/reports_by_subject', { keys, include_docs: true })),
self.tasksByRelation(contactIds, 'requester'),
]);

const relevantReportDocs = reportDocs.filter(report => {
const subjectId = registrationUtils.getSubjectId(report);
return subjectIds.has(subjectId);
});

return {
userSettingsId: userSettingsDoc?._id,
contactDocs,
reportDocs: relevantReportDocs,
taskDocs,
};
},
};

Expand Down
23 changes: 23 additions & 0 deletions shared-libs/rules-engine/test/pouchdb-provider.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,29 @@ describe('pouchdb provider', () => {
expect(secondTargetDoc._rev).not.to.equal(firstTargetDoc._rev);
});

it('should throw pouchdb get errors', async () => {
const docTag = '2024-07';
const nextTargets = [{ id: 'target', score: 1 }];

sinon.stub(db, 'get').rejects(new Error('pouch crash'));

await expect(
pouchdbProvider(db).commitTargetDoc(nextTargets, docTag, { userContactDoc, userSettingsDoc })
).to.eventually.be.rejectedWith('pouch crash');
});

it('should throw pouchdb put errors', async () => {
const docTag = '2024-07';
const nextTargets = [{ id: 'target', score: 1 }];

sinon.restore();
sinon.stub(db, 'put').rejects(new Error('pouch crash'));

await expect(
pouchdbProvider(db).commitTargetDoc(nextTargets, docTag, { userContactDoc, userSettingsDoc })
).to.eventually.be.rejectedWith('pouch crash');
});

});

describe('contactsBySubjectId', () => {
Expand Down

0 comments on commit 0738e80

Please sign in to comment.