@@ -93,6 +93,7 @@ async function getPatientsList (questionnaryId, limit = 100) {
9393 * get patients details
9494 */
9595async 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 ) {
0 commit comments