-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
219 lines (191 loc) · 6.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
window.log = (s) => {
const journal = document.getElementById("journal").innerHTML;
document.getElementById("journal").innerHTML = "- " + s + "\n" + journal;
};
window.token = () => {
const clientId = document.getElementById("client-id").value;
const clientSecret = document.getElementById("client-secret").value;
const token = btoa(`${clientId}:${clientSecret}`);
return token;
};
window.updateCurrentNotification = (notification, patient, template) => {
if (notification === undefined) {
return (document.getElementById("notification").innerHTML = "");
}
window.notification = notification;
window.patient = patient;
window.template = template;
const html = [
`id: ${notification.id}`,
` ${patient.name[0].given[0]} - ${notification.type} - ${notification.status} - ${notification.sendAfter}`,
` template: ${template.template}`,
` templateParameters: ${JSON.stringify(
notification.templateParameters,
)}`,
` message: ${notification.message}`,
].join("\n");
document.getElementById("notification").innerHTML = html;
};
window.requestNotification = async () => {
const baseUrl = document.getElementById("base-url").value;
const resourceUrl = "/TutorNotification";
const url = `${baseUrl}${resourceUrl}`;
const notification = {
resourceType: "TutorNotification",
type: "sms",
status: "requested",
template: {
reference: "TutorNotificationTemplate/welcome",
},
sendAfter: "2024-07-12T12:00:00Z",
subject: {
reference: "Patient/pt-1",
},
};
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Basic ${window.token()}`,
},
body: JSON.stringify(notification),
});
if (!response.ok) {
const json = await response.json();
return alert(
"Error: " + response.status + " " + JSON.stringify(json.text.div),
);
}
const json = await response.json();
window.log("Notification generated: " + json.id);
};
window.getNotification = async () => {
const baseUrl = document.getElementById("base-url").value;
const resourceUrl = "/TutorNotification";
const params = [
"_count=1",
"type=sms",
"status=requested",
"_include=TutorNotification:subject:Patient",
"_include=TutorNotification:template:TutorNotificationTemplate",
].join("&");
const url = `${baseUrl}${resourceUrl}?${params}`;
const clientId = document.getElementById("client-id").value;
const clientSecret = document.getElementById("client-secret").value;
const token = btoa(`${clientId}:${clientSecret}`);
const response = await fetch(url, {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Basic ${window.token()}`,
},
});
if (!response.ok) {
const json = await response.json();
return alert(
"Error: " + response.status + " " + JSON.stringify(json.text.div),
);
}
const json = await response.json();
if (!json.entry || json.entry.length === 0) {
updateCurrentNotification();
return window.log(
"No requested sms notifications found. Request one first.",
);
}
const resources = json.entry.map((entry) => entry.resource);
const {
TutorNotification: notifications,
Patient: patients,
TutorNotificationTemplate: templates,
} = Object.groupBy(resources, (e) => e.resourceType);
if (!notifications || !patients || !templates) {
return window.log(
`Notification looks incomplete. Delete it: ${notifications[0].id}`,
);
}
const notification = notifications[0];
const patient = patients[0];
const template = templates[0];
window.updateCurrentNotification(notification, patient, template);
window.log("Notification retrieved to send: " + notification.id);
window.notification = notification;
window.patient = patient;
window.template = template;
};
window.lockNotification = async () => {
if (window.notification.status !== "requested") {
return window.log("Notification is not available for lock, get it first.");
}
const baseUrl = document.getElementById("base-url").value;
const resourceUrl = "/TutorNotification/" + window.notification.id;
const url = `${baseUrl}${resourceUrl}`;
const notification = { ...window.notification, status: "in-progress" };
const response = await fetch(url, {
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization: `Basic ${window.token()}`,
"If-Match": notification.meta.versionId,
},
body: JSON.stringify(notification),
});
window.updateCurrentNotification(
notification,
window.patient,
window.template,
);
console.log("Lock notification for processing");
if (!response.ok) {
const json = await response.json();
return alert(
"Error: " + response.status + " " + JSON.stringify(json.text.div),
);
}
window.log("Lock notification: " + notification.id);
};
window.sendNotification = async () => {
if (window.notification.status !== "in-progress") {
return window.log(
"Notification is not locked for processing. Lock it first.",
);
}
const baseUrl = document.getElementById("base-url").value;
const resourceUrl = "/TutorNotification/" + window.notification.id;
const url = `${baseUrl}${resourceUrl}`;
////////////////////////////////////////////////////////////
// Place for implementation of sending SMS
const templateParameters = { patientName: window.patient.name[0].given[0] };
const message = window.template.template.replace(
"{{patientName}}",
templateParameters.patientName,
);
const notification = {
...window.notification,
status: "completed",
message: message,
templateParameters: templateParameters,
};
alert("Send SMS:\n\n" + notification.message);
////////////////////////////////////////////////////////////
const response = await fetch(url, {
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization: `Basic ${window.token()}`,
},
body: JSON.stringify(notification),
});
if (!response.ok) {
const json = await response.json();
return alert(
"Error: " + response.status + " " + JSON.stringify(json.text.div),
);
}
window.updateCurrentNotification(
notification,
window.patient,
window.template,
);
window.log("Notification sent: " + notification.id);
};