11const patientHomeLib = {
22 getForms,
33 getQuestionnaryDetails,
4- getQuestionnaryInfo,
54 grantAccess,
65 showLoginButton,
76 getPatientApiEndpoint,
8- publishAccess
7+ publishAccess,
8+ revokeAccess
99}
1010
11+ const AppBaseStreams = {
12+ base : { id : 'demo-dr-forms' , name : 'Demo Dr Forms' } ,
13+ inbox : { id : 'demo-dr-forms-inbox' , name : 'Demo Dr Forms - Inbox' , parentId : 'demo-dr-forms' } ,
14+ accepted : { id : 'demo-dr-forms-accepted' , name : 'Demo Dr Forms - Accepted' , parentId : 'demo-dr-forms' } ,
15+ rejected : { id : 'demo-dr-forms-rejected' , name : 'Demo Dr Forms - Rejected' , parentId : 'demo-dr-forms' } ,
16+ } ;
17+
18+
1119/**
1220 * Load app & get the forms
1321 * - initBaseFormsStreams
@@ -19,7 +27,7 @@ async function getForms (formApiEndpoint) {
1927 // init base for streams
2028 await initBaseFormsStreams ( connection ) ;
2129 // get list of form
22- const eventRes = await connection . api ( [ { method : 'events.get' , params : { streams : [ 'demo-dr-forms' ] , limit : 100 } } ] ) ;
30+ const eventRes = await connection . api ( [ { method : 'events.get' , params : { streams : [ AppBaseStreams . base . id ] , limit : 100 } } ] ) ;
2331
2432 const forms = eventRes [ 0 ] . events ;
2533
@@ -30,7 +38,7 @@ async function getForms (formApiEndpoint) {
3038 const apiCalls = [ {
3139 method : 'events.create' ,
3240 params : {
33- streamIds : [ 'demo-dr-forms- inbox' ] ,
41+ streamIds : [ AppBaseStreams . inbox . id ] ,
3442 type : 'credentials/pryv-api-endpoint' ,
3543 content : formApiEndpoint
3644 }
@@ -45,7 +53,7 @@ async function getForms (formApiEndpoint) {
4553 const formsInfo = [ ] ;
4654 // add forms details
4755 for ( const formEvent of forms ) {
48- const formInfo = await patientHomeLib . getQuestionnaryInfo ( formEvent ) ;
56+ const formInfo = await getQuestionnaryInfo ( formEvent ) ;
4957 formsInfo . push ( formInfo ) ;
5058 }
5159
@@ -56,12 +64,7 @@ async function getForms (formApiEndpoint) {
5664
5765// Creates the the streams structure for accepted and rejected froms
5866async function initBaseFormsStreams ( connection ) {
59- const streams = [
60- { id : 'demo-dr-forms' , name : 'Demo Dr Forms' } ,
61- { id : 'demo-dr-forms-inbox' , name : 'Demo Dr Forms - Inbox' , parentId : 'demo-dr-forms' } ,
62- { id : 'demo-dr-forms-accepted' , name : 'Demo Dr Forms - Accepted' , parentId : 'demo-dr-forms' } ,
63- { id : 'demo-dr-forms-rejected' , name : 'Demo Dr Forms - Rejected' , parentId : 'demo-dr-forms' } ,
64- ] ;
67+ const streams = Object . values ( AppBaseStreams ) ;
6568 await createsPatientAccountStreams ( connection , streams ) ;
6669}
6770
@@ -127,23 +130,67 @@ async function publishAccess (formInfo, apiEndpoint) {
127130 } ] ;
128131 const publishRes = await formInfo . drConnection . api ( apiCalls ) ;
129132 console . log ( '## Shared access published to Dr Account' , publishRes ) ;
133+ if ( ! publishRes [ 0 ] . event ) {
134+ const error = new Error ( 'Failed publishing Acces' ) ;
135+ error . innerObject = publishRes
136+ throw error ;
137+ }
138+ // -- update access and place in 'demo-dr-forms-accepted'
139+ await updateEventFormStatus ( formInfo , 'accepted' ) ;
140+ }
141+
142+ /**
143+ *
144+ * @param {* } formInfo
145+ * @param {string } newStatus 'accepted', 'rejected'
146+ */
147+ async function updateEventFormStatus ( formInfo , newStatus ) {
148+ const newStreamId = AppBaseStreams [ newStatus ] . id ;
149+ const previousStreamsId = formInfo . formEvent . streamIds [ 0 ] ;
150+ if ( newStreamId === previousStreamsId ) return ;
151+ const apiCalls = [ {
152+ method : 'events.update' ,
153+ params : {
154+ id : formInfo . formEvent . id ,
155+ update : {
156+ streamIds : [ newStreamId ]
157+ }
158+ }
159+ } ] ;
160+ const updateEvent = await connection . api ( apiCalls ) ;
161+ console . log ( '## event Form status updated' , newStatus , updateEvent ) ;
162+ formInfo . formEvent . streamIds = [ newStreamId ]
163+ }
164+
165+ async function revokeAccess ( formDetails ) {
166+ // revoke access
167+ const revokeRes = await connection . api ( [ {
168+ "method" : "accesses.delete" ,
169+ "params" : {
170+ "id" : formDetails . sharedAccessId
171+ }
172+ } ] ) ;
173+ console . log ( "## revokeAccess res" , revokeRes ) ;
174+ // -- update access and place in 'demo-dr-forms-rejected'
175+ await updateEventFormStatus ( formDetails . formInfo , 'rejected' ) ;
130176}
131177
132178// ---- Get questionnary details ---- //
133179async function getQuestionnaryDetails ( formInfo ) {
134180 const details = {
181+ formInfo,
135182 status : 'pending' ,
136183 }
137184
138185 //-- check if the access already exists --//
139- const accessesCheckRes = await connection . api ( [ { method : 'accesses.get' , params : { } } ] ) ;
186+ const accessesCheckRes = await connection . api ( [ { method : 'accesses.get' , params : { includeDeletions : true } } ] ) ;
140187 const sharedAccess = accessesCheckRes [ 0 ] . accesses . find ( access => access . name === formInfo . sharingAccessId ) ;
141188 if ( sharedAccess ) {
142189 details . status = 'accepted' ;
143190 details . sharedApiEndpoint = sharedAccess . apiEndpoint ;
191+ details . sharedAccessId = sharedAccess . id ;
144192 }
145193
146-
147194 //-- get access permissions request --//
148195 const drAccessInfo = await formInfo . drConnection . accessInfo ( ) ;
149196 const questionaryId = drAccessInfo . clientData ?. [ 'demo-dr-form' ] ?. questionaryId ;
@@ -165,16 +212,22 @@ async function getQuestionnaryInfo (formEvent) {
165212 console . log ( '## Dr Form info' , drAccessInfo ) ;
166213 const questionaryId = drAccessInfo . clientData ?. [ 'demo-dr-form' ] ?. questionaryId ;
167214 const drUserId = drAccessInfo . user . username ;
215+
216+ const status = formEvent . streamIds [ 0 ] ;
217+
168218 return {
219+ status,
169220 formApiEndpoint,
170221 questionaryId,
171222 drUserId,
172223 drConnection,
224+ created : new Date ( drAccessInfo . created ) ,
173225 formEvent,
174226 sharingAccessId : `${ drUserId } -${ questionaryId } `
175227 }
176228}
177229
230+
178231// ---------- connection to the pryv account ------------- //
179232
180233let connection = null ;
0 commit comments