Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#9704): throw pouch errors when saving target docs #9705

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
jkuester marked this conversation as resolved.
Show resolved Hide resolved
})
.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
Loading