Skip to content

feat(#10019): refactor shared-libs/lineage library to use cht-datasource #10025

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

Closed
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
45 changes: 16 additions & 29 deletions shared-libs/lineage/src/hydration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions shared-libs/lineage/src/index.js
Original file line number Diff line number Diff line change
@@ -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')
);
33 changes: 14 additions & 19 deletions shared-libs/lineage/test/hydration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,56 @@ 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() {
sinon.restore();
});

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);
});
});
});
Expand Down
6 changes: 3 additions & 3 deletions tests/e2e/default/tasks/tasks.wdio-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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');
Expand Down
Loading