1- let connection = null ;
1+ let drConnection = null ;
22
33const drLib = {
44 showLoginButton,
@@ -36,12 +36,12 @@ function showLoginButton (loginSpanId, stateChangeCallBack) {
3636 async function pryvAuthStateChange ( state ) { // called each time the authentication state changes
3737 console . log ( '##pryvAuthStateChange' , state ) ;
3838 if ( state . id === Pryv . Browser . AuthStates . AUTHORIZED ) {
39- connection = new Pryv . Connection ( state . apiEndpoint ) ;
40- await initDrAccount ( connection ) ;
39+ drConnection = new Pryv . Connection ( state . apiEndpoint ) ;
40+ await initDrAccount ( drConnection ) ;
4141 stateChangeCallBack ( 'loggedIN' ) ;
4242 }
4343 if ( state . id === Pryv . Browser . AuthStates . INITIALIZED ) {
44- connection = null ;
44+ drConnection = null ;
4545 stateChangeCallBack ( 'loggedOUT' ) ;
4646 }
4747 }
@@ -52,41 +52,77 @@ function showLoginButton (loginSpanId, stateChangeCallBack) {
5252const patients = { } ;
5353
5454async function getPatientsList ( ) {
55- const res = await connection . api ( [ { method : 'events.get' , params : { types : [ 'credentials/pryv-api-endpoint' ] } } ] ) ;
55+ const res = await drConnection . api ( [ { method : 'events.get' , params : { types : [ 'credentials/pryv-api-endpoint' ] } } ] ) ;
5656 const patientApiEndpointEvents = res [ 0 ] . events ;
57- for ( const event of patientApiEndpointEvents ) {
58- if ( event . type === 'credentials/pryv-api-endpoint' ) {
59- const patient = {
60- apiEndpoint : event . content ,
61- formData : { }
62- } ;
63- const patientConnection = new Pryv . Connection ( patient . apiEndpoint ) ;
64- // -- get patient info
65- try {
66- const patientInfo = await patientConnection . accessInfo ( ) ;
67- patient . username = patientInfo . user . username ;
68- } catch ( e ) {
69- console . error ( '## Error getting patient info: ' + patient . apiEndpoint , e ) ;
70- continue ;
71- }
72-
73-
74- // -- get data
75- const profileEvents = await patientConnection . api ( [ { method : 'events.get' , params : { limit : 100 } } ] ) ;
76- for ( const profileEvent of profileEvents [ 0 ] . events ) {
77- const field = dataFieldFromEvent ( profileEvent ) ;
78- if ( field ) {
79- patient . formData [ field . key ] = field ;
80- }
81- }
82- patients [ patient . username ] = patient ;
57+ for ( const patientEvent of patientApiEndpointEvents ) {
58+ const patient = await getPatientDetails ( patientEvent ) ;
59+ if ( patient ) {
60+ patients [ patient . apiEndpoint ] = patient ;
61+ console . log ( '## Patient details' , patient ) ;
8362 }
8463 }
8564
8665 console . log ( '## Patients list' , patients ) ;
8766 return patients ;
8867}
8968
69+ /**
70+ * get patients details
71+ */
72+ async function getPatientDetails ( patientEvent ) {
73+ if ( patientEvent . type !== 'credentials/pryv-api-endpoint' ) return null ;
74+ const patient = {
75+ status : 'active' ,
76+ apiEndpoint : patientEvent . content ,
77+ formData : { }
78+ } ;
79+ const patientConnection = new Pryv . Connection ( patient . apiEndpoint ) ;
80+
81+ // -- get patient data
82+ if ( ! patientEvent . streamIds . includes ( 'patients-revoked' ) ) {
83+ // -- get patient info
84+ try {
85+ const patientInfo = await patientConnection . accessInfo ( ) ;
86+ patient . username = patientInfo . user . username ;
87+ } catch ( e ) {
88+ console . error ( '## Error getting patient info: ' + patient . apiEndpoint , e ) ;
89+ // -- mark as revoked
90+ const revokeRequest = await drConnection . api ( [ {
91+ method : 'events.update' ,
92+ params : {
93+ id : patientEvent . id ,
94+ update : {
95+ streamIds : [ 'patients-revoked' ]
96+ }
97+ } } ] ) ;
98+ console . log ( '## Patient maked as revoked' , revokeRequest ) ;
99+ patientEvent . streamIds = [ 'patients-revoked' ] ;
100+ }
101+ }
102+
103+ // -- mark as revoked
104+ if ( patientEvent . streamIds . includes ( 'patients-revoked' ) ) {
105+ patient . status = 'revoked' ;
106+ const usernameFromApiEndpoint = patientEvent . content . split ( '/' ) [ 3 ] ;
107+ patient . username = patientEvent . clientData ?. username || usernameFromApiEndpoint ;
108+ return patient ;
109+ } ;
110+
111+
112+ // -- get data
113+ const profileEvents = await patientConnection . api ( [ { method : 'events.get' , params : { limit : 100 } } ] ) ;
114+ for ( const profileEvent of profileEvents [ 0 ] . events ) {
115+ const field = dataFieldFromEvent ( profileEvent ) ;
116+ if ( field ) {
117+ patient . formData [ field . key ] = field ;
118+ }
119+ }
120+ return patient ;
121+ }
122+
123+
124+
125+
90126/**
91127 * get the list of rows for the table
92128 */
@@ -152,13 +188,13 @@ async function initDrAccount (connection) {
152188 * @returns
153189 */
154190async function getSharingToken ( ) {
155- const accessesCheckRes = await connection . api ( [ { method : 'accesses.get' , params : { } } ] ) ;
191+ const accessesCheckRes = await drConnection . api ( [ { method : 'accesses.get' , params : { } } ] ) ;
156192 const sharedAccess = accessesCheckRes [ 0 ] . accesses . find ( access => access . name === 'demo-dr-form-shared' ) ;
157193 if ( sharedAccess ) {
158194 console . log ( '## Dr account already has a shared access' ) ;
159195 return sharedAccess . apiEndpoint ;
160196 }
161- const accessRes = await connection . api ( [ {
197+ const accessRes = await drConnection . api ( [ {
162198 method : 'accesses.create' ,
163199 params : {
164200 name : 'demo-dr-form-shared' ,
@@ -189,7 +225,7 @@ async function getSharingToken () {
189225
190226async function initStreams ( ) {
191227 // check if the account is already initialized
192- const resStreams = await connection . api ( [ { method : 'streams.get' , params : { parentId : 'patients' } } ] ) ;
228+ const resStreams = await drConnection . api ( [ { method : 'streams.get' , params : { parentId : 'patients' } } ] ) ;
193229 if ( resStreams [ 0 ] . streams . length > 0 ) {
194230 console . log ( '## Dr account streams already initialized' ) ;
195231 return ;
@@ -220,6 +256,14 @@ async function initStreams () {
220256 parentId : 'patients'
221257 }
222258 } ,
259+ {
260+ method : 'streams.create' ,
261+ params : {
262+ id : 'patients-revoked' ,
263+ name : 'Patients Revoked' ,
264+ parentId : 'patients'
265+ }
266+ } ,
223267 {
224268 method : 'streams.create' ,
225269 params : {
@@ -235,6 +279,6 @@ async function initStreams () {
235279 }
236280 }
237281 ] ;
238- const result = await connection . api ( apiCalls ) ;
282+ const result = await drConnection . api ( apiCalls ) ;
239283 console . log ( '## Dr account streams created' , result ) ;
240284}
0 commit comments