Skip to content
Draft
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
7 changes: 4 additions & 3 deletions admin/src/js/controllers/display-languages.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const _ = require('lodash/core');
_.uniq = require('lodash/uniq');
const constants = require('@medic/constants');
const DOC_IDS = constants.DOC_IDS;
const DOC_TYPES = constants.DOC_TYPES;
const PREFIXES = constants.PREFIXES;

angular.module('controllers').controller('DisplayLanguagesCtrl',
function (
Expand Down Expand Up @@ -91,8 +91,9 @@ angular.module('controllers').controller('DisplayLanguagesCtrl',
$scope.loading = true;
$q
.all([
DB().query('medic-client/doc_by_type', {
key: [ DOC_TYPES.TRANSLATIONS ],
DB().allDocs({
start_key: PREFIXES.TRANSLATIONS,
end_key: PREFIXES.TRANSLATIONS + '\ufff0',
include_docs: true
}),
Settings()
Expand Down
7 changes: 4 additions & 3 deletions admin/src/js/controllers/display-translations.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const _ = require('lodash/core');
_.union = require('lodash/union');
const constants = require('@medic/constants');
const DOC_TYPES = constants.DOC_TYPES;
const PREFIXES = constants.PREFIXES;

const TRANSLATION_KEYS_OPTION = { doc: {code: 'keys', name: 'Translation Keys'} };
const DEFAULT_LANGUAGE = 'en';
Expand Down Expand Up @@ -56,8 +56,9 @@ angular.module('controllers').controller('DisplayTranslationsCtrl',

const updateTranslations = function() {
return DB()
.query('medic-client/doc_by_type', {
key: [ DOC_TYPES.TRANSLATIONS ],
.allDocs({
start_key: PREFIXES.TRANSLATIONS,
end_key: PREFIXES.TRANSLATIONS + '\ufff0',
include_docs: true
})
.then(function(results) {
Expand Down
9 changes: 6 additions & 3 deletions admin/src/js/controllers/forms-xml.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const _ = require('lodash/core');
const constants = require('@medic/constants');
const PREFIXES = constants.PREFIXES;

angular.module('controllers').controller('FormsXmlCtrl',
function (
Expand All @@ -20,10 +22,11 @@ angular.module('controllers').controller('FormsXmlCtrl',
const getForms = () => {
const options = {
include_docs: true,
key: ['form']
start_key: PREFIXES.FORM,
end_key: PREFIXES.FORM + '\ufff0',
};
return DB()
.query('medic-client/doc_by_type', options)
.allDocs(options)
.then(res => res.rows.map(row => row.doc));
};

Expand Down Expand Up @@ -111,7 +114,7 @@ angular.module('controllers').controller('FormsXmlCtrl',
const $xml = $($.parseXML(xml));
const title = getXmlTitle($xml, xml);
const formId = getXmlFormId($xml, meta);
const couchId = 'form:' + formId;
const couchId = PREFIXES.FORM + formId;
return DB()
.get(couchId, { include_attachments: true })
.catch(err => {
Expand Down
10 changes: 8 additions & 2 deletions admin/src/js/controllers/users.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const _ = require('lodash/core');
const constants = require('@medic/constants');
const PREFIXES = constants.PREFIXES;

angular.module('controllers').controller('UsersCtrl',
function (
Expand All @@ -22,8 +24,12 @@ angular.module('controllers').controller('UsersCtrl',

$scope.updateList = function() {
$scope.loading = true;
const params = { include_docs: true, key: ['user-settings'] };
DB().query('medic-client/doc_by_type', params)
const params = {
include_docs: true,
start_key: PREFIXES.COUCH_USER,
end_key: PREFIXES.COUCH_USER + '\ufff0',
};
DB().allDocs(params)
.then(function(settings) {
$scope.users = _.map(settings.rows, 'doc');
$scope.loading = false;
Expand Down
14 changes: 10 additions & 4 deletions admin/src/js/services/get-data-records.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,17 @@ angular.module('inboxServices').factory('GetDataRecords',
});
};

const getSummaries = function(ids, options) {
return GetSummaries(ids)
const getSummaries = function(ids, type, options) {
const summariesPromise = type === 'report' ? GetSummaries.getReports(ids) : GetSummaries.getContacts(ids);
return summariesPromise
.then(summaries => {
const promiseToSummary = options.hydrateContactNames ?
HydrateContactNames(summaries) : Promise.resolve(summaries);
return promiseToSummary.then(GetSubjectSummaries);
});
};

return function(ids, options) {
const getRecords = function(ids, type, options) {
const opts = Object.assign({ hydrateContactNames: false, include_docs: false }, options);

if (!ids) {
Expand All @@ -58,7 +59,7 @@ angular.module('inboxServices').factory('GetDataRecords',
if (!ids.length) {
return $q.resolve([]);
}
const getFn = opts.include_docs ? getDocs : ids => getSummaries(ids, opts);
const getFn = opts.include_docs ? getDocs : idList => getSummaries(idList, type, opts);
return getFn(ids)
.then(function(response) {
if (!arrayGiven) {
Expand All @@ -67,4 +68,9 @@ angular.module('inboxServices').factory('GetDataRecords',
return response;
});
};

return {
getContacts: (ids, options) => getRecords(ids, 'contact', options),
getReports: (ids, options) => getRecords(ids, 'report', options),
};
});
2 changes: 1 addition & 1 deletion admin/src/js/services/get-subject-summaries.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ angular.module('inboxServices').factory('GetSubjectSummaries',
return $q.resolve(summaries);
}

return GetSummaries([...new Set(ids)])
return GetSummaries.getContacts([...new Set(ids)])
.then(function(response) {
return replaceIdsWithNames(summaries, response);
});
Expand Down
134 changes: 17 additions & 117 deletions admin/src/js/services/get-summaries.js
Original file line number Diff line number Diff line change
@@ -1,131 +1,31 @@
const constants = require('@medic/constants');
const DOC_TYPES = constants.DOC_TYPES;
const cht = require('@medic/cht-datasource');

angular.module('inboxServices').factory('GetSummaries',
function(
$q,
ContactTypes,
DB,
Session
DataContext
) {

'use strict';
'ngInject';

const SUBJECT_FIELDS = [ 'patient_id', 'patient_name', 'place_id' ];

const getLineage = contact => {
const parts = [];
while (contact) {
if (contact._id) {
parts.push(contact._id);
}
contact = contact.parent;
}
return parts;
};

const isMissingSubjectError = error => {
return error.code === 'sys.missing_fields' &&
error.fields &&
error.fields.some(field => SUBJECT_FIELDS.includes(field));
};

const getSubject = doc => {
const subject = {};
const reference = doc.patient_id ||
(doc.fields && doc.fields.patient_id) ||
doc.place_id;
const patientName = doc.fields && doc.fields.patient_name;
if (patientName) {
subject.name = patientName;
}

if (reference) {
subject.value = reference;
subject.type = 'reference';
} else if (doc.fields && doc.fields.place_id) {
subject.value = doc.fields.place_id;
subject.type = 'id';
} else if (patientName) {
subject.value = patientName;
subject.type = 'name';
} else if (doc.errors) {
if (doc.errors.some(error => isMissingSubjectError(error))) {
subject.type = 'unknown';
const collect = (generator) => {
const summaries = [];
const iterate = () => generator.next().then(result => {
if (result.done) {
return summaries;
}
}

return subject;
};

// WARNING: This is a copy of the medic/doc_summaries_by_id view
// with some minor modifications and needs to be kept in sync until
// this workaround is no longer needed.
// https://github.com/medic/medic/issues/4666
const summarise = doc => {
if (!doc) {
// happens when the doc with the requested id wasn't found in the DB
return;
}

if (doc.type === DOC_TYPES.DATA_RECORD && doc.form) { // report
return {
_id: doc._id,
_rev: doc._rev,
from: doc.from || doc.sent_by,
phone: doc.contact && doc.contact.phone,
form: doc.form,
read: doc.read,
valid: !doc.errors || !doc.errors.length,
verified: doc.verified,
reported_date: doc.reported_date,
contact: doc.contact && doc.contact._id,
lineage: getLineage(doc.contact && doc.contact.parent),
subject: getSubject(doc),
case_id: doc.case_id || (doc.fields && doc.fields.case_id)
};
}
if (ContactTypes.includes(doc)) { // contact
return {
_id: doc._id,
_rev: doc._rev,
name: doc.name || doc.phone,
phone: doc.phone,
type: doc.type,
contact_type: doc.contact_type,
contact: doc.contact && doc.contact._id,
lineage: getLineage(doc.parent),
date_of_death: doc.date_of_death,
muted: doc.muted
};
}
};

const getRemote = ids => {
return DB().query('medic/doc_summaries_by_id', { keys: ids }).then(response => {
return response.rows.map(row => {
row.value._id = row.id;
return row.value;
});
});
};

const getLocal = ids => {
return DB().allDocs({ keys: ids, include_docs: true }).then(response => {
return response.rows
.map(row => summarise(row.doc))
.filter(summary => summary);
summaries.push(result.value);
return iterate();
});
return iterate();
};

return ids => {
if (!ids || !ids.length) {
return $q.resolve([]);
}
if (Session.isOnlineOnly()) {
return getRemote(ids);
}
return getLocal(ids);
return {
getContacts: ids => DataContext.then(
dataContext => collect(dataContext.bind(cht.Contact.v1.getSummaries)(cht.Qualifier.byIds(ids)))
),
getReports: ids => DataContext.then(
dataContext => collect(dataContext.bind(cht.Report.v1.getSummaries)(cht.Qualifier.byIds(ids)))
),
};
});
2 changes: 1 addition & 1 deletion admin/src/js/services/hydrate-contact-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ angular.module('inboxServices').factory('HydrateContactNames',
return $q.resolve(summaries);
}

return GetSummaries(ids)
return GetSummaries.getContacts(ids)
.then(function(response) {
summaries = getMutedState(summaries, response);
return replaceContactIdsWithNames(summaries, response);
Expand Down
9 changes: 4 additions & 5 deletions admin/src/js/services/message-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ angular.module('services').factory('MessageQueue',
$q,
$translate,
DB,
GetSummaries,
Languages,
MessageQueueUtils,
Settings
Expand All @@ -39,11 +40,9 @@ angular.module('services').factory('MessageQueue',
return;
}

const summary = summaries.rows.find(function(summary) {
return summary.value && summary.value.phone === message.sms.to;
return summaries.find(function(summary) {
return summary && summary.phone === message.sms.to;
});

return summary && summary.value;
};

const findIdByKey = (contactsByReference, key) => {
Expand Down Expand Up @@ -101,7 +100,7 @@ angular.module('services').factory('MessageQueue',
return row.id;
});

return DB({ remote: true }).query('medic/doc_summaries_by_id', { keys: ids });
return GetSummaries.getContacts(ids);
})
.then(function(summaries) {
messages.forEach(function(message) {
Expand Down
4 changes: 3 additions & 1 deletion admin/src/js/services/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ const Search = require('@medic/search');
}
return _search(type, filters, options)
.then(function(searchResults) {
return GetDataRecords(searchResults.docIds, options);
return type === 'reports' ?
GetDataRecords.getReports(searchResults.docIds, options) :
GetDataRecords.getContacts(searchResults.docIds, options);
})
.then(function(results) {
_currentQuery = {};
Expand Down
Loading
Loading