diff --git a/shared-libs/lineage/src/hydration.js b/shared-libs/lineage/src/hydration.js index 33ee9c0547f..9ff44603198 100644 --- a/shared-libs/lineage/src/hydration.js +++ b/shared-libs/lineage/src/hydration.js @@ -46,7 +46,7 @@ const getContactIds = (contacts) => { return _.uniq(ids); }; -module.exports = function(Promise, DB) { +module.exports = function(Promise, DB, dataContext) { const fillParentsInDocs = function(doc, lineage) { if (!doc || !lineage.length) { return doc; @@ -114,10 +114,11 @@ module.exports = function(Promise, DB) { } }); - return fetchDocs(contactsToFetch) - .then(function(fetchedContacts) { - return lineageContacts.concat(fetchedContacts); - }); + const getContact = dataContext.Contact.v1.get; + + return Promise.all(contactsToFetch.map(id => getContact(id))) + .then(fetchedContacts => lineageContacts.concat(fetchedContacts.filter(Boolean))); + }; const mergeLineagesIntoDoc = function(lineage, contacts, patientLineage, placeLineage) { @@ -229,17 +230,10 @@ module.exports = function(Promise, DB) { }; const fetchLineageById = function(id) { - const options = { - startkey: [id], - endkey: [id, {}], - include_docs: true - }; - return DB.query('medic-client/docs_by_id_lineage', options) - .then(function(result) { - return result.rows.map(function(row) { - return row.doc; - }); - }); + if (dataContext.Contact?.v1?.getWithLineage){ + return dataContext.Contact.v1.getWithLineage(id) + .then(lineageArr => Array.isArray(lineageArr) ? lineageArr : []); + } }; const fetchLineageByIds = function(ids) { @@ -257,9 +251,9 @@ module.exports = function(Promise, DB) { }; const fetchDoc = function(id) { - return DB.get(id) - .catch(function(err) { - if (err.status === 404) { + return dataContext.Contact.v1.get(id) + .catch(function(err){ + if (err.status === 404){ err.statusCode = 404; } throw err; @@ -362,16 +356,9 @@ module.exports = function(Promise, DB) { return Promise.resolve([]); } - return DB.allDocs({ keys, include_docs: true }) - .then(function(results) { - return results.rows - .map(function(row) { - return row.doc; - }) - .filter(function(doc) { - return !!doc; - }); - }); + const getContact = dataContext.Contact.v1.get; + return Promise.all(keys.amp(id => getContact(id))) + .then(docs => docs.filter(Boolean)); }; const hydrateDocs = function(docs) { diff --git a/shared-libs/lineage/src/index.js b/shared-libs/lineage/src/index.js index 3eb31dcfc6a..5d77236ecbd 100644 --- a/shared-libs/lineage/src/index.js +++ b/shared-libs/lineage/src/index.js @@ -1,8 +1,8 @@ /** * @module lineage */ -module.exports = (Promise, DB) => Object.assign( +module.exports = (Promise, DB, dataContext) => Object.assign( {}, - require('./hydration')(Promise, DB), + require('./hydration')(Promise, DB, dataContext), require('./minify') ); diff --git a/shared-libs/lineage/test/hydration.spec.js b/shared-libs/lineage/test/hydration.spec.js index acfceb65cbc..75f13ce248a 100644 --- a/shared-libs/lineage/test/hydration.spec.js +++ b/shared-libs/lineage/test/hydration.spec.js @@ -4,17 +4,15 @@ const lineageFactory = require('../src'); describe('Lineage', function() { let lineage; - let allDocs; + let dataContext; let get; - let query; - let DB; + let getWithLineage; beforeEach(function() { - allDocs = sinon.stub(); get = sinon.stub(); - query = sinon.stub(); - DB = { allDocs, get, query }; - lineage = lineageFactory(Promise, DB); + getWithLineage = sinon.stub(); + dataContext = { Contact: { v1: { get, getWithLineage } } }; + lineage = lineageFactory(Promise, {}, dataContext); }); afterEach(function() { @@ -22,43 +20,40 @@ describe('Lineage', function() { }); describe('fetchLineageById', function() { - it('queries db with correct parameters', function() { - query.resolves({ rows: [] }); + it('calls getWithLineage with correct parameters', function() { + getWithLineage.resolves([]); const id = 'banana'; return lineage.fetchLineageById(id).then(() => { - chai.expect(query.callCount).to.equal(1); - chai.expect(query.getCall(0).args[0]).to.equal('medic-client/docs_by_id_lineage'); - chai.expect(query.getCall(0).args[1].startkey).to.deep.equal([ id ]); - chai.expect(query.getCall(0).args[1].endkey).to.deep.equal([ id, {} ]); - chai.expect(query.getCall(0).args[1].include_docs).to.deep.equal(true); + chai.expect(getWithLineage.callCount).to.equal(1); + chai.expect(getWithLineage.getCall(0).args[0]).to.equal(id); }); }); }); describe('fetchContacts', function() { it('fetches contacts with correct parameters', function() { - allDocs.resolves({ rows: [] }); + get.resolves({ _id: 'def' }); const fakeLineage = [ { _id: 'abc', contact: { _id: 'def' }, parent: { _id: 'ghi' } }, { _id: 'ghi' } ]; return lineage.fetchContacts(fakeLineage).then(() => { - chai.expect(allDocs.callCount).to.equal(1); - chai.expect(allDocs.getCall(0).args[0]).to.deep.equal({ keys: ['def'], include_docs: true }); + chai.expect(get.callCount).to.equal(1); + chai.expect(get.getCall(0).args[0]).to.deep.equal('def'); }); }); it('does not fetch contacts that it has already got via lineage', function() { - allDocs.resolves({ rows: [] }); + get.resolves(); const fakeLineage = [ { _id: 'abc', contact: { _id: 'def' }, parent: { _id: 'def' } }, { _id: 'def' } ]; return lineage.fetchContacts(fakeLineage).then(() => { - chai.expect(allDocs.callCount).to.equal(0); + chai.expect(get.callCount).to.equal(0); }); }); }); diff --git a/tests/e2e/default/tasks/tasks.wdio-spec.js b/tests/e2e/default/tasks/tasks.wdio-spec.js index 0e5f0a2ee0e..1c929da69a4 100644 --- a/tests/e2e/default/tasks/tasks.wdio-spec.js +++ b/tests/e2e/default/tasks/tasks.wdio-spec.js @@ -86,7 +86,8 @@ describe('Tasks', () => { }); it('should add a task when CHW completes a task successfully, and that task creates another task', async () => { - await tasksPage.compileTasks('tasks-breadcrumbs-config.js', false); + await tasksPage.compileTasks('tasks-breadcrumbs-config.js', true); + await browser.pause(500); await commonPage.goToTasks(); let list = await tasksPage.getTasks(); @@ -134,10 +135,9 @@ describe('Tasks', () => { it('Should show error message for bad config', async () => { await tasksPage.compileTasks('tasks-error-config.js', true); - await commonPage.goToTasks(); + await commonPage.goToTasks(); const { errorMessage, url, username, errorStack } = await commonPage.getErrorLog(); - expect(username).to.equal(chw.username); expect(url).to.equal('localhost'); expect(errorMessage).to.equal('Error fetching tasks');