11let connection = null ;
22
33const drLib = {
4- showLoginButton
5-
4+ showLoginButton,
5+ getSharingToken
66}
77
88function showLoginButton ( loginSpanId , stateChangeCallBack ) {
@@ -35,7 +35,7 @@ function showLoginButton (loginSpanId, stateChangeCallBack) {
3535 console . log ( '##pryvAuthStateChange' , state ) ;
3636 if ( state . id === Pryv . Browser . AuthStates . AUTHORIZED ) {
3737 connection = new Pryv . Connection ( state . apiEndpoint ) ;
38- await initPatientAccount ( connection ) ;
38+ await initDrAccount ( connection ) ;
3939 stateChangeCallBack ( 'loggedIN' ) ;
4040 }
4141 if ( state . id === Pryv . Browser . AuthStates . INITIALIZED ) {
@@ -45,7 +45,64 @@ function showLoginButton (loginSpanId, stateChangeCallBack) {
4545 }
4646}
4747
48- async function initPatientAccount ( connection ) {
48+
49+ /**
50+ * Initialize the doctor account
51+ * @param {* } connection
52+ */
53+ async function initDrAccount ( connection ) {
54+ await initStreams ( connection ) ;
55+ console . log ( '## Dr account initialized' )
56+ }
57+
58+ /**
59+ * Initialize or get the sharing token for patients
60+ * @returns
61+ */
62+ async function getSharingToken ( ) {
63+ const accessesCheckRes = await connection . api ( [ { method : 'accesses.get' , params : { } } ] ) ;
64+ const sharedAcess = accessesCheckRes [ 0 ] . accesses . find ( access => access . name === 'demo-dr-form-shared' ) ;
65+ if ( sharedAcess ) {
66+ console . log ( '## Dr account already has a shared access' ) ;
67+ return sharedAcess . apiEndpoint ;
68+ }
69+ const accessRes = await connection . api ( [ {
70+ method : 'accesses.create' ,
71+ params : {
72+ name : 'demo-dr-form-shared' ,
73+ type : 'shared' ,
74+ permissions : [ {
75+ streamId : 'patients-inbox' ,
76+ level : 'create-only'
77+ } ,
78+ {
79+ streamId : 'demo-dr-forms-questionary-x' ,
80+ level : 'read'
81+ } ,
82+ { // for "publicly shared access" always forbid the selfRevoke feature
83+ feature : "selfRevoke" ,
84+ setting : "forbidden"
85+ } ] ,
86+ clientData : {
87+ 'demo-dr-form' : {
88+ questionaryId : 'demo-dr-forms-questionary-x'
89+ }
90+ }
91+ }
92+ } ] ) ;
93+ console . log ( '## Dr account shared access created' , accessRes ) ;
94+ return accessRes [ 0 ] . access . apiEndpoint ;
95+ }
96+
97+
98+ async function initStreams ( ) {
99+ // check if the account is already initialized
100+ const resStreams = await connection . api ( [ { method : 'streams.get' , params : { parentId : 'patients' } } ] ) ;
101+ if ( resStreams [ 0 ] . streams . length > 0 ) {
102+ console . log ( '## Dr account streams already initialized' ) ;
103+ return ;
104+ }
105+
49106 // create stream structure (even if already exists)
50107 const apiCalls = [
51108 {
@@ -87,6 +144,5 @@ async function initPatientAccount (connection) {
87144 }
88145 ] ;
89146 const result = await connection . api ( apiCalls ) ;
90- console . log ( result ) ;
91- console . log ( '## Dr account initialized' )
147+ console . log ( '## Dr account streams created' , result ) ;
92148}
0 commit comments