Skip to content

Commit de16ce3

Browse files
committed
Now adding credentials to Dr's Account
1 parent 61bb099 commit de16ce3

7 files changed

Lines changed: 116 additions & 55 deletions

File tree

common-data-defs.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const patientBaseStreams = [
2+
// profile
3+
{id: 'profile-name', name: 'Name', parentId: 'profile'},
4+
{id: 'profile-nationality', name: 'Nationality', parentId: 'profile'},
5+
{id: 'profile-date-of-birth', name: 'Date of Birth', parentId: 'profile'},
6+
{id: 'profile-location', name: 'Location', parentId: 'profile'},
7+
{id: 'profile-sex', name: 'Sex', parentId: 'profile'},
8+
// family
9+
{id: 'family-children', name: 'Children', parentId: 'family'},
10+
// fertility
11+
{id: 'fertility-miscarriages', name: 'Miscarriages', parentId: 'fertility'},
12+
{id: 'fertility-traings', name: 'Trainings', parentId: 'fertility'},
13+
{id: 'fertility-cycles', name: 'Cycles', parentId: 'fertility'},
14+
{id: 'fertility-cycles-charted-extimation', name: 'Cycles charted estimation', parentId: 'fertility'},
15+
{id: 'fertility-ttc-tta', name: 'Trying to conceive / Avoiding pregnancy', parentId: 'fertility'},
16+
];
17+
18+
const patientBasePermissions = [
19+
{id: 'profile', name: 'Profile'},
20+
{id: 'family', name: 'Family'},
21+
{id: 'fertility', name: 'Fertility'},
22+
{id: 'body-height', name: 'Body height'},
23+
{id: 'body-weight', name: 'Body weight'},
24+
]
25+
26+
27+
const dataDefs = {
28+
patientBaseStreams,
29+
patientBasePermissions
30+
};

dr-controler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ async function setSharingLink() {
3030
const posDrHTML = currentPage.indexOf('dr.html');
3131
const patientURL = currentPage.substring(0, posDrHTML) + 'patient.html';
3232

33-
const sharedApiEndpoint = await drLib.getSharingToken();
34-
const sharingLink = patientURL + '?sharedApiEndpoint=' + sharedApiEndpoint;
33+
const formApiEndpoint = await drLib.getSharingToken();
34+
const sharingLink = patientURL + '?formApiEndpoint=' + formApiEndpoint;
3535
const sharingMailBody = 'Hello,\n\nI am sending you a link to fill out a form.\nPlease click on the link below to access the form: \n\n' + sharingLink + '\n\nBest regards,\nYour Doctor';
3636
const sharingLinkHTML = `<A HREF="mailto:?subject=Invitation&body=${encodeURI(sharingMailBody)}">Send by email</A> - ${sharingLink}`;
3737
document.getElementById('sharing-link').innerHTML = sharingLinkHTML;

dr-lib.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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 initDrAccount(connection);
38+
await initPatientAccount(connection);
3939
stateChangeCallBack('loggedIN');
4040
}
4141
if (state.id === Pryv.Browser.AuthStates.INITIALIZED) {
@@ -50,7 +50,7 @@ function showLoginButton (loginSpanId, stateChangeCallBack) {
5050
* Initialize the doctor account
5151
* @param {*} connection
5252
*/
53-
async function initDrAccount (connection) {
53+
async function initPatientAccount (connection) {
5454
await initStreams(connection);
5555
console.log('## Dr account initialized')
5656
}
@@ -61,10 +61,10 @@ async function initDrAccount (connection) {
6161
*/
6262
async function getSharingToken () {
6363
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) {
64+
const sharedAccess = accessesCheckRes[0].accesses.find(access => access.name === 'demo-dr-form-shared');
65+
if (sharedAccess) {
6666
console.log('## Dr account already has a shared access');
67-
return sharedAcess.apiEndpoint;
67+
return sharedAccess.apiEndpoint;
6868
}
6969
const accessRes = await connection.api([{
7070
method: 'accesses.create',

dr.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<title>Demo Form - Dr's page</title>
1313
</head>
1414
<script src="https://pryv.github.io/lib-js/pryv.js"></script>
15+
<script src="common-data-defs.js"></script>
1516
<script src="dr-controler.js"></script>
1617
<script src="dr-lib.js"></script>
1718

patient-controler.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
* @param {*} event
66
*/
77

8-
9-
108
window.onload = (event) => {
119
stateChange('loggedOut');
1210
patientLib.showLoginButton('login-button', stateChange);
@@ -17,13 +15,27 @@ function stateChange(state) {
1715
if (state === 'loggedIN') {
1816
document.getElementById('please-login').style.visibility = 'hidden';
1917
document.getElementById('card-form').style.visibility = 'visible';
18+
getDoctorInfo();
2019
updateFormContent();
2120
} else {
2221
document.getElementById('please-login').style.visibility = 'visible';
2322
document.getElementById('card-form').style.visibility = 'hidden';
2423
}
2524
}
2625

26+
// ------- Get Dr's info -------- //
27+
async function getDoctorInfo() {
28+
const params = new URLSearchParams(document.location.search);
29+
const formApiEndpoint = params.get('formApiEndpoint');
30+
if (!formApiEndpoint) {
31+
alert('No formApiEndpoint found in the URL');
32+
return;
33+
}
34+
await initSharingWithDr(formApiEndpoint);
35+
}
36+
37+
38+
// ------- Form -------- //
2739

2840
/**
2941
* Take the from content from the definition and actual values and create the HTML

patient-lib.js

Lines changed: 63 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,23 @@ const patientLib = {
44
showLoginButton,
55
handleFormSubmit,
66
getFormContent,
7+
initSharingWithDr,
78
}
89

910
function 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

109126
const formContent = [

patient.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<title>Demo Form - Patient's page</title>
1313
</head>
1414
<script src="https://api.pryv.com/lib-js/pryv.js"></script>
15+
<script src="common-data-defs.js"></script>
1516
<script src="patient-controler.js"></script>
1617
<script src="patient-lib.js"></script>
1718

0 commit comments

Comments
 (0)