1+
2+ import { dataDefs } from './common-data-defs.js' ;
3+
4+ export const drPatientLib = {
5+ getPatientData
6+ }
7+
8+ // prepare data for easy lookup
9+ // map streamId/EventType with form data
10+ const eventMapByQuestionnary = { } ;
11+ for ( const [ questionaryId , questionary ] of Object . entries ( dataDefs . questionnaires ) ) {
12+ eventMapByQuestionnary [ questionaryId ] = { } ;
13+ for ( const form of Object . values ( questionary . forms ) ) {
14+ for ( const field of form . content ) {
15+ eventMapByQuestionnary [ questionaryId ] [ field . streamId + ':' + field . eventType ] = Object . assign ( {
16+ formLabel : form . name ,
17+ formType : form . type
18+ } , field ) ;
19+ }
20+ }
21+ }
22+ console . log ( '>> eventMapByQuestionnary' , eventMapByQuestionnary ) ;
23+
24+ async function getPatientData ( patientApiEndoint , questionaryId ) {
25+ const patientData = [ ] ;
26+ const queryParams = { limit : 10000 } ;
27+ function forEachEvent ( event ) {
28+ patientData . push ( getLineForEvent ( event , questionaryId ) ) ;
29+ }
30+
31+ const connection = new Pryv . Connection ( patientApiEndoint ) ;
32+ await connection . getEventsStreamed ( queryParams , forEachEvent ) ;
33+ return patientData ;
34+ }
35+
36+
37+ function getLineForEvent ( event , questionaryId ) {
38+ const line = {
39+ time : ( new Date ( event . time * 1000 ) ) . toISOString ( ) ,
40+ formLabel : 'Unkown' ,
41+ formType : 'Unkown' ,
42+ label : event . streamId + ' - ' + event . type ,
43+ value : JSON . stringify ( event . content ) ,
44+ description : ''
45+ }
46+
47+ const field = eventMapByQuestionnary [ questionaryId ] [ event . streamId + ':' + event . type ] ;
48+ console . log ( '>> field' , field , event ) ;
49+ if ( field ) {
50+ Object . assign ( line , field ) ;
51+ if ( field . type === 'date' ) {
52+ line . value = ( new Date ( event . time * 1000 ) ) . toISOString ( ) . split ( 'T' ) [ 0 ] ;
53+ }
54+ if ( field . type === 'select' ) {
55+ line . value = event . content ;
56+
57+ const selected = field . options . find ( ( o ) => ( o . value === line . value ) ) ;
58+ line . description = selected ?. label ;
59+ }
60+ }
61+ return line ;
62+ }
0 commit comments