@@ -2,7 +2,8 @@ let connection = null;
22
33const drLib = {
44 showLoginButton,
5- getSharingToken
5+ getSharingToken,
6+ getPatientsList,
67}
78
89function showLoginButton ( loginSpanId , stateChangeCallBack ) {
@@ -35,7 +36,7 @@ function showLoginButton (loginSpanId, stateChangeCallBack) {
3536 console . log ( '##pryvAuthStateChange' , state ) ;
3637 if ( state . id === Pryv . Browser . AuthStates . AUTHORIZED ) {
3738 connection = new Pryv . Connection ( state . apiEndpoint ) ;
38- await initPatientAccount ( connection ) ;
39+ await initDrAccount ( connection ) ;
3940 stateChangeCallBack ( 'loggedIN' ) ;
4041 }
4142 if ( state . id === Pryv . Browser . AuthStates . INITIALIZED ) {
@@ -45,12 +46,79 @@ function showLoginButton (loginSpanId, stateChangeCallBack) {
4546 }
4647}
4748
49+ // -------- Fetch patient list --------
50+
51+ const patients = { } ;
52+
53+ async function getPatientsList ( ) {
54+ const res = await connection . api ( [ { method : 'events.get' , params : { types : [ 'credentials/pryv-api-endpoint' ] } } ] ) ;
55+ const patientApiEndpointEvents = res [ 0 ] . events ;
56+ for ( const event of patientApiEndpointEvents ) {
57+ if ( event . type === 'credentials/pryv-api-endpoint' ) {
58+ const patient = {
59+ apiEndpoint : event . content ,
60+ formData : { }
61+ } ;
62+ const patientConnection = new Pryv . Connection ( patient . apiEndpoint ) ;
63+ // -- get patient info
64+ const patientInfo = await patientConnection . accessInfo ( ) ;
65+ patient . username = patientInfo . user . username ;
66+
67+ // -- get data
68+ const profileEvents = await patientConnection . api ( [ { method : 'events.get' , params : { streams : [ 'profile' ] } } ] ) ;
69+ for ( const profileEvent of profileEvents [ 0 ] . events ) {
70+ const field = dataFieldFromEvent ( profileEvent ) ;
71+ if ( field ) {
72+ patient . formData [ field . key ] = field ;
73+ }
74+ }
75+ patients [ patient . username ] = patient ;
76+ }
77+ }
78+
79+ console . log ( '## Patients list' , patients ) ;
80+ return patients ;
81+ }
82+
83+ const dataFieldsCache = { } ;
84+ function initFieldsCache ( event ) {
85+ if ( Object . keys ( dataFieldsCache ) . length !== 0 ) return ;
86+ for ( const formField of dataDefs . formContent ) {
87+ const dataFieldId = formField . streamId + ':' + formField . eventType ;
88+ dataFieldsCache [ dataFieldId ] = formField ;
89+ }
90+ }
91+
92+ /**
93+ * Link an event to a data field from form
94+ * @param {* } event
95+ */
96+ function dataFieldFromEvent ( event ) {
97+ initFieldsCache ( event ) ;
98+ const formFieldId = event . streamId + ':' + event . type ;
99+ const dataField = dataFieldsCache [ formFieldId ] ;
100+ if ( ! dataField ) {
101+ console . error ( '## Data field not found for event' , event ) ;
102+ return null ;
103+ }
104+ const field = {
105+ formFieldId,
106+ key : dataField . dataFieldKey ,
107+ label : dataField . label ,
108+ type : dataField . type ,
109+ value : event . content ,
110+ event : event
111+ } ;
112+ return field ;
113+ }
114+
115+ // -------- initualization functions --------
48116
49117/**
50118 * Initialize the doctor account
51119 * @param {* } connection
52120 */
53- async function initPatientAccount ( connection ) {
121+ async function initDrAccount ( connection ) {
54122 await initStreams ( connection ) ;
55123 console . log ( '## Dr account initialized' )
56124}
0 commit comments