@@ -4,42 +4,23 @@ const patientLib = {
44 showLoginButton,
55 handleFormSubmit,
66 getFormContent,
7+ initSharingWithDr,
78}
89
910function showLoginButton ( loginSpanId , stateChangeCallBack ) {
1011
12+ const requestedPermissions = dataDefs . patientBasePermissions . map ( perm => ( {
13+ streamId : perm . id ,
14+ defaultName : perm . name ,
15+ level : 'manage'
16+ } ) ) ;
17+
1118 const authSettings = {
1219 spanButtonID : loginSpanId , // div id the DOM that will be replaced by the Service specific button
1320 onStateChange : pryvAuthStateChange , // event Listener for Authentication steps
1421 authRequest : { // See: https://api.pryv.com/reference/#auth-request
1522 requestingAppId : 'demo-dr-form-patient' , // to customize for your own app
16- requestedPermissions : [
17- {
18- streamId : 'profile' ,
19- defaultName : 'Profile' ,
20- level : 'manage'
21- } ,
22- {
23- streamId : 'family' ,
24- defaultName : 'Family' ,
25- level : 'manage'
26- } ,
27- {
28- streamId : 'fertility' ,
29- defaultName : 'Fertility' ,
30- level : 'manage'
31- } ,
32- {
33- streamId : 'body-height' ,
34- defaultName : 'Body height' ,
35- level : 'manage'
36- } ,
37- {
38- streamId : 'body-weight' ,
39- defaultName : 'Body weight' ,
40- level : 'manage'
41- }
42- ] ,
23+ requestedPermissions,
4324 clientData : {
4425 'app-web-auth:description' : {
4526 'type' : 'note/txt' ,
@@ -62,7 +43,7 @@ function showLoginButton (loginSpanId, stateChangeCallBack) {
6243 console . log ( '##pryvAuthStateChange' , state ) ;
6344 if ( state . id === Pryv . Browser . AuthStates . AUTHORIZED ) {
6445 connection = new Pryv . Connection ( state . apiEndpoint ) ;
65- await initDrAccount ( connection ) ;
46+ await initPatientAccount ( connection ) ;
6647 stateChangeCallBack ( 'loggedIN' ) ;
6748 }
6849 if ( state . id === Pryv . Browser . AuthStates . INITIALIZED ) {
@@ -73,24 +54,9 @@ function showLoginButton (loginSpanId, stateChangeCallBack) {
7354}
7455
7556// Creates the streams structure for the patient account after the user has logged in
76- async function initDrAccount ( connection ) {
77- const patientBaseStreams = [
78- // profile
79- { id : 'profile-name' , name : 'Name' , parentId : 'profile' } ,
80- { id : 'profile-nationality' , name : 'Nationality' , parentId : 'profile' } ,
81- { id : 'profile-date-of-birth' , name : 'Date of Birth' , parentId : 'profile' } ,
82- { id : 'profile-location' , name : 'Location' , parentId : 'profile' } ,
83- { id : 'profile-sex' , name : 'Sex' , parentId : 'profile' } ,
84- // family
85- { id : 'family-children' , name : 'Children' , parentId : 'family' } ,
86- // fertility
87- { id : 'fertility-miscarriages' , name : 'Miscarriages' , parentId : 'fertility' } ,
88- { id : 'fertility-traings' , name : 'Trainings' , parentId : 'fertility' } ,
89- { id : 'fertility-cycles' , name : 'Cycles' , parentId : 'fertility' } ,
90- { id : 'fertility-cycles-charted-extimation' , name : 'Cycles charted estimation' , parentId : 'fertility' } ,
91- { id : 'fertility-ttc-tta' , name : 'Trying to conceive / Avoiding pregnancy' , parentId : 'fertility' } ,
92- ] ;
93- const apiCalls = patientBaseStreams . map ( stream => ( {
57+ async function initPatientAccount ( connection ) {
58+
59+ const apiCalls = dataDefs . patientBaseStreams . map ( stream => ( {
9460 method : 'streams.create' ,
9561 params : {
9662 id : stream . id ,
@@ -104,6 +70,57 @@ async function initDrAccount (connection) {
10470 console . log ( '## Patient account initialized' )
10571}
10672
73+ // ---- if first connection to the app create a sharing for the Dr and submit it ---- //
74+ async function initSharingWithDr ( formApiEndpoint ) {
75+ const drConnection = new Pryv . Connection ( formApiEndpoint ) ;
76+ const drAccessInfo = await drConnection . accessInfo ( ) ;
77+ console . log ( '## Dr connection info' , drAccessInfo ) ;
78+ const questionaryId = drAccessInfo . clientData ?. [ 'demo-dr-form' ] ?. questionaryId ;
79+ const drUserId = drAccessInfo . user . username ;
80+ // creates a unique id for the shaing access in case of two drs using the same app
81+ const sharingAccessId = `${ drUserId } -${ questionaryId } ` ;
82+
83+ //-- check if the access already exists --//
84+ const accessesCheckRes = await connection . api ( [ { method : 'accesses.get' , params : { } } ] ) ;
85+ const sharedAccess = accessesCheckRes [ 0 ] . accesses . find ( access => access . name === sharingAccessId ) ;
86+ if ( sharedAccess ) {
87+ console . log ( '## Already created access for this Dr' ) ;
88+ }
89+ //-- create a set of permission based on the datadefs streams --//
90+ const permissions = dataDefs . patientBasePermissions . map ( perm => ( {
91+ streamId : perm . id ,
92+ level : 'read'
93+ } ) ) ;
94+
95+ const accessRes = await connection . api ( [ {
96+ method : 'accesses.create' ,
97+ params : {
98+ name : sharingAccessId ,
99+ type : 'shared' ,
100+ permissions : permissions ,
101+ clientData : {
102+ 'demo-dr-form' : {
103+ questionaryId : 'demo-dr-forms-questionary-x'
104+ }
105+ }
106+ }
107+ } ] ) ;
108+ const createdAccess = accessRes [ 0 ] . access ;
109+ console . log ( '## Patient shared access created' , accessRes ) ;
110+
111+ // publishing access on Dr Account
112+ const apiCalls = [ {
113+ method : 'events.create' ,
114+ params : {
115+ streamIds : [ 'patients-inbox' ] ,
116+ type : 'credentials/pryv-api-endpoint' ,
117+ content : createdAccess . apiEndpoint
118+ }
119+ } ] ;
120+ const publishRes = await drConnection . api ( apiCalls ) ;
121+ console . log ( '## Shared access published to Dr Account' , publishRes ) ;
122+ }
123+
107124// ---------------- form content ---------------- //
108125
109126const formContent = [
0 commit comments