Skip to content

Commit 09aa517

Browse files
committed
Full module for questionnaries
1 parent de2c239 commit 09aa517

4 files changed

Lines changed: 39 additions & 24 deletions

File tree

common-data-defs.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,13 @@ questionnaires = {
155155
forms: {
156156
profile: {
157157
type: 'permanent',
158+
key: 'profile-x',
158159
name: 'Profile',
159160
content: formProfileContent
160161
},
161162
historical: {
162163
type: 'recurring',
164+
key: 'recurring-x',
163165
name: 'Historical',
164166
content: formHistoricalContent
165167
}
@@ -168,9 +170,5 @@ questionnaires = {
168170
}
169171

170172
const dataDefs = {
171-
patientBaseStreams,
172-
patientBasePermissions,
173-
formProfileContent,
174-
formHistoricalContent,
175173
questionnaires
176174
};

dr-controler.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ async function stateChange(state) {
1515
document.getElementById('please-login').style.visibility = 'hidden';
1616
document.getElementById('data-view').style.visibility = 'visible';
1717
setSharingLink();
18-
setPatientList();
18+
setPatientList('demo-dr-forms-questionary-x');
1919
} else {
2020
document.getElementById('please-login').style.visibility = 'visible';
2121
document.getElementById('data-view').style.visibility = 'hidden';
@@ -26,9 +26,9 @@ const rowItems = ['name', 'surname', 'nationality'];
2626
/**
2727
* Update the patient list
2828
*/
29-
async function setPatientList() {
29+
async function setPatientList(questionaryId) {
3030
const table = document.getElementById('patients-table');
31-
const fields = drLib.getFields();
31+
const fields = drLib.getFields(questionaryId);
3232
// --- headers
3333
const headerRow = table.insertRow(-1);
3434
const headerStatusCell = document.createElement("TH");
@@ -44,7 +44,7 @@ async function setPatientList() {
4444
}
4545

4646
// --- patients
47-
const patients = await drLib.getPatientsList('demo-dr-forms-questionary-x', 100);
47+
const patients = await drLib.getPatientsList(questionaryId, 100);
4848
for (const patient of Object.values(patients)) {
4949
const row = table.insertRow(-1);
5050
const cellStatus = row.insertCell(-1);

dr-lib.js

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ async function getPatientsList (questionnaryId, limit = 100) {
9393
* get patients details
9494
*/
9595
async function getPatientDetails (questionnaryId, patientEvent) {
96+
// -- check if the event is a patient event
9697
if (patientEvent.type !== 'credentials/pryv-api-endpoint') return null;
9798
const patient = {
9899
status: 'active',
@@ -133,13 +134,25 @@ async function getPatientDetails (questionnaryId, patientEvent) {
133134

134135

135136
// -- get data
136-
137-
const profileEvents = await patientConnection.api([{ method: 'events.get', params: { limit: 100 } }]);
138-
for (const profileEvent of profileEvents[0].events) {
139-
const field = dataFieldFromEvent(profileEvent);
140-
if (field && patient.formData[field.key] == null) { // drop historical values
141-
patient.formData[field.key] = field;
137+
// get profile form data
138+
const formProfile = dataDefs.questionnaires[questionnaryId].forms.profile;
139+
140+
// get the last value of each field
141+
const apiCalls = formProfile.content.map(field => ({
142+
method: 'events.get',
143+
params: {
144+
streams: [field.streamId],
145+
types: [field.eventType],
146+
limit: 1,
142147
}
148+
}));
149+
150+
const profileEventsResults = await patientConnection.api(apiCalls);
151+
for (const profileEventRes of profileEventsResults) {
152+
const profileEvent = profileEventRes?.events[0];
153+
if (!profileEvent) continue;
154+
const field = dataFieldFromEvent(formProfile, profileEvent);
155+
patient.formData[field.key] = field;
143156
}
144157
return patient;
145158
}
@@ -150,25 +163,31 @@ async function getPatientDetails (questionnaryId, patientEvent) {
150163
/**
151164
* get the list of rows for the table
152165
*/
153-
function getFields () {
154-
return dataDefs.formProfileContent;
166+
function getFields (questionaryId) {
167+
return dataDefs.questionnaires[questionaryId].forms.profile.content;
155168
};
156169

157-
const dataFieldsCache = {};
158-
function initFieldsCache () {
159-
if (Object.keys(dataFieldsCache).length !== 0) return;
160-
for (const formField of dataDefs.formProfileContent) {
170+
const dataFieldsCaches = {};
171+
function initFieldsCache (formProfile) {
172+
if (dataFieldsCaches[formProfile.key] == null) {
173+
dataFieldsCaches[formProfile.key] = {};
174+
}
175+
const dataFieldsCache = dataFieldsCaches[formProfile.key];
176+
177+
if (Object.keys(dataFieldsCache).length !== 0) return dataFieldsCache;
178+
for (const formField of formProfile.content) {
161179
const dataFieldId = formField.streamId + ':' + formField.eventType;
162180
dataFieldsCache[dataFieldId] = formField;
163181
}
182+
return dataFieldsCache;
164183
}
165184

166185
/**
167186
* Link an event to a data field from form
168187
* @param {*} event
169188
*/
170-
function dataFieldFromEvent (event) {
171-
initFieldsCache(event);
189+
function dataFieldFromEvent (formProfile, event) {
190+
const dataFieldsCache = initFieldsCache(formProfile);
172191
const formFieldId = event.streamId + ':' + event.type;
173192
const dataField = dataFieldsCache[formFieldId];
174193
if (!dataField) {

patient-lib.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ async function connect (apiEndpoint, questionaryId) {
1515

1616
// ---------------- form content ---------------- //
1717

18-
19-
2018
async function getFormContent (questionaryId, formKey) {
2119
const form = dataDefs.questionnaires[questionaryId].forms[formKey];
2220
console.log('## getFormContent', form);

0 commit comments

Comments
 (0)