Skip to content

Commit 9db215a

Browse files
committed
Now handlink revokes
1 parent c877b33 commit 9db215a

3 files changed

Lines changed: 101 additions & 26 deletions

File tree

patient-home-controler.js

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,24 @@ window.onload = (event) => {
1010
patientHomeLib.showLoginButton('login-button', stateChange);
1111
};
1212

13-
async function stateChange(state) {
13+
function stateChange(state) {
1414
if (state === 'loggedIN') {
1515
document.getElementById('please-login').style.visibility = 'hidden';
1616
document.getElementById('card-content').style.visibility = 'visible';
17-
const formApiEndpoint = getRequestFrormApiEndPoint();
18-
console.log('## formApiEndpoint:', formApiEndpoint);
19-
const formsInfo = await patientHomeLib.getForms(formApiEndpoint);
20-
showFormList(formsInfo)
17+
refresh();
2118
} else {
2219
document.getElementById('please-login').style.visibility = 'visible';
2320
document.getElementById('card-content').style.visibility = 'hidden';
2421
}
2522
}
2623

24+
async function refresh() {
25+
const formApiEndpoint = getRequestFrormApiEndPoint();
26+
console.log('## formApiEndpoint:', formApiEndpoint);
27+
const formsInfo = await patientHomeLib.getForms(formApiEndpoint);
28+
showFormList(formsInfo)
29+
}
30+
2731
// ------- Get Dr's info -------- //
2832
function getRequestFrormApiEndPoint() {
2933
const params = new URLSearchParams(document.location.search);
@@ -35,13 +39,16 @@ function getRequestFrormApiEndPoint() {
3539
async function showFormList(formsInfo) {
3640
console.log('## showFormList', formsInfo);
3741

38-
3942
// -- table
4043
const table = document.getElementById('questionnary-table');
44+
const tbody = document.getElementById('questionnary-table').getElementsByTagName('tbody')[0];;
4145

46+
// clear previous content
47+
while (tbody.firstChild) {
48+
tbody.removeChild(tbody.firstChild);
49+
}
4250

4351
for (const formInfo of formsInfo) {
44-
4552
// fill the table row
4653
const row = table.insertRow(-1);
4754
const cellQuestionnary = row.insertCell(-1);
@@ -54,7 +61,7 @@ async function showFormList(formsInfo) {
5461
cellDr.innerHTML = formInfo.drUserId;
5562

5663
const cellStatus = row.insertCell(-1);
57-
cellStatus.innerHTML = formInfo.formEvent.streamIds[0];
64+
cellStatus.innerHTML = formInfo.status;
5865
}
5966
}
6067

@@ -87,24 +94,37 @@ async function showFormDetails(formInfo) {
8794
cellLevel.innerHTML = permission.level;
8895
}
8996
// - grant access / open
90-
const button = document.getElementById('grant-access-button');
97+
const buttonOpen = document.getElementById('grant-access-button');
98+
const buttonRevoke = document.getElementById('revoke-access-button');
9199

92100
// -- pass the apiEndpoint to the next page !! Insecure just for demo
93101
const openHREF = `patient-profile.html?patientApiEndpoint=${patientHomeLib.getPatientApiEndpoint()}&questionaryId=${formInfo.questionaryId}`;
94102
if (formDetails.status === 'accepted') {
95-
button.innerHTML = 'Open';
96-
button.onclick = async function () {
103+
buttonOpen.innerHTML = 'Open';
104+
buttonOpen.onclick = async function () {
97105
// -- hack publish access anyway (this should be done just once)
98106
await patientHomeLib.publishAccess(formInfo, formDetails.sharedApiEndpoint);
99107
document.location.href = openHREF;
100108
};
109+
buttonRevoke.innerHTML = 'Revoke';
110+
buttonRevoke.onclick = async function () {
111+
const doRevoke = confirm('Revoke ?');
112+
if (doRevoke) patientHomeLib.revokeAccess(formDetails);
113+
refresh();
114+
};
101115
}
102116
else {
103-
button.innerHTML = 'Grant access and Open';
104-
button.onclick = async function () {
117+
buttonOpen.innerHTML = 'Grant access and Open';
118+
buttonOpen.onclick = async function () {
105119
await patientHomeLib.grantAccess(formInfo, formDetails);
106120
document.location.href = openHREF;
107121
};
122+
buttonRevoke.innerHTML = 'Refuse';
123+
buttonRevoke.onclick = async function () {
124+
const doRevoke = confirm('Refuse ?');
125+
if (doRevoke) patientHomeLib.revokeAccess(formDetails);
126+
refresh();
127+
}
108128
}
109129

110130
// - json

patient-home-lib.js

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
const 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
5866
async 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 ---- //
133179
async 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

180233
let connection = null;

patient.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ <h3>Permission request</h3>
7575
<td>&nbsp; &nbsp;</td>
7676
<td>
7777
<button type="button" id="grant-access-button" class="btn btn-primary mb-2">Open</button>
78+
<br><br>
79+
<button type="button" id="revoke-access-button" class="btn btn-secondary mb-2">Revoke</button>
7880
</td>
7981
</tr>
8082
</table>

0 commit comments

Comments
 (0)