Skip to content

Commit 512fe13

Browse files
committed
Now marking revoked access
1 parent 514f109 commit 512fe13

5 files changed

Lines changed: 92 additions & 41 deletions

File tree

dr-controler.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ async function setPatientList() {
3131
const fields = drLib.getFields();
3232
// --- headers
3333
const headerRow = table.insertRow(-1);
34+
const headerStatusCell = document.createElement("TH");
35+
headerStatusCell.innerHTML = 'Status';
36+
headerRow.appendChild(headerStatusCell);
3437
const headerUserCell = document.createElement("TH");
3538
headerUserCell.innerHTML = 'Username';
3639
headerRow.appendChild(headerUserCell);
@@ -44,6 +47,8 @@ async function setPatientList() {
4447
const patients = await drLib.getPatientsList();
4548
for (const patient of Object.values(patients)) {
4649
const row = table.insertRow(-1);
50+
const cellStatus = row.insertCell(-1);
51+
cellStatus.innerHTML = patient.status;
4752
const cellUsername = row.insertCell(-1);
4853
cellUsername.innerHTML = patient.username;
4954
for (const field of fields) {

dr-lib.js

Lines changed: 79 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
let connection = null;
1+
let drConnection = null;
22

33
const 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) {
5252
const patients = {};
5353

5454
async 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
*/
154190
async 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

190226
async 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
}

index.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<img src="https://healthdatasafe.github.io/style/images/Horizontal/hds-logo-hz-1024x400-hz-color.png" alt="Logo" width="300px">
1818
<h1>Demo Form</h1>
1919
<table width="100%">
20-
<td>
20+
<td width="50%">
2121
<div class="card-login">
2222
<h2><A class="card-title" HREF="dr.html">Dr's Page</A></h2>
2323
From this page you will be able to visualize the data from patients and share an invitation link to fill your questionnaire.
@@ -42,12 +42,13 @@ <h2><A class="card-title" HREF="dr.html">Dr's Page</A></h2>
4242
</ul>
4343
</div>
4444
</td>
45-
<td>
45+
<td valign="top" width="50%">
4646
<div class="card-login">
47-
<!--
4847
<h2><A class="card-title" HREF="patient.html">Patient's Page</A></h2>
4948
From this page you will be able to visualize your current data and forms you particpated in.<BR>
50-
-->
49+
<BR>
50+
<BR>
51+
Nothing will appear until you received an invitation link from your doctor.
5152
</div>
5253
</td>
5354
</table>

patient-home-controler.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ async function showFormDetails(formInfo) {
7878
if (formDetails.status === 'accepted') {
7979
button.innerHTML = 'Open';
8080
button.onclick = async function () {
81-
// await patientHomeLib.publishAccess(formInfo, formDetails.sharedApiEndpoint);
81+
// -- hack publish access anyway (this should be done just once)
82+
await patientHomeLib.publishAccess(formInfo, formDetails.sharedApiEndpoint);
8283
document.location.href = openHREF;
8384
};
8485
}

patient-home-lib.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ async function grantAccess (formInfo, formDetails) {
108108
}]);
109109
const createdAccess = accessRes[0].access;
110110
console.log('## Patient shared access created', accessRes);
111-
publishAccess (formInfo, createdAccess.apiEndpoint);
111+
await publishAccess (formInfo, createdAccess.apiEndpoint);
112112
}
113113

114114
async function publishAccess (formInfo, apiEndpoint) {

0 commit comments

Comments
 (0)