-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreply.js
More file actions
354 lines (302 loc) · 10.1 KB
/
reply.js
File metadata and controls
354 lines (302 loc) · 10.1 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import { faker } from '@faker-js/faker'
import _ from 'lodash'
import { healthConditions } from '../datasets/health-conditions.js'
import {
ConsentOutcome,
ParentalRelationship,
ProgrammeType,
ReplyDecision,
ReplyRefusal
} from '../enums.js'
import { Child } from '../models.js'
import { formatParentalRelationship } from './string.js'
/**
* Add example answers to health questions
*
* @param {string} key - Health question key, i.e. aspirin
* @param {string} healthCondition - Health condition
* @returns {object} Health answer
*/
const enrichWithRealisticAnswer = (key, healthCondition) => {
// Asthma is a more common health condition
const useAnswer = faker.helpers.maybe(() => true, {
probability: key.startsWith('asthma') ? 0.5 : 0.2
})
if (healthConditions[healthCondition][key] && useAnswer) {
return {
answer: 'Yes',
details: healthConditions[healthCondition][key]
}
}
return {
answer: 'No'
}
}
/**
* Get consent responses with answers to health questions
*
* @param {Array} replies - Consent responses
* @returns {Array} Consent responses with answers to health questions
*/
export function getRepliesWithHealthAnswers(replies) {
replies = Array.isArray(replies) ? replies : [replies]
return replies.filter(
(reply) =>
reply.healthAnswers &&
Object.values(reply.healthAnswers).some((value) => value.answer !== 'No')
)
}
/**
* Get combined answers to health questions
*
* @param {import('../models.js').PatientSession} patientSession - Patient session
* @returns {object|boolean} Combined answers to health questions
*/
export function getConsentHealthAnswers(patientSession) {
const consentHealthAnswers = {}
// Get consent responses with health answers
const responsesWithHealthAnswers = Object.values(
patientSession.responses
).filter((reply) => reply.healthAnswers)
if (responsesWithHealthAnswers.length === 0) {
return false
}
for (const response of responsesWithHealthAnswers) {
for (const [key, healthAnswer] of Object.entries(response.healthAnswers)) {
if (!consentHealthAnswers[key]) {
consentHealthAnswers[key] = []
}
// As we are not validating forms, handle cases where no answer given
if (!healthAnswer.answer) {
healthAnswer.answer = 'No'
}
const hasSingleResponse = responsesWithHealthAnswers.length === 1
const hasSameAnswers = responsesWithHealthAnswers.every(
(reply) => reply.healthAnswers[key]?.answer === healthAnswer.answer
)
const hasSameAnswersWithDetails = responsesWithHealthAnswers.some(
(reply) =>
reply.healthAnswers[key]?.details &&
reply.healthAnswers[key]?.answer === healthAnswer.answer
)
// Don’t modify original health answer
const thisHealthAnswer = { ...healthAnswer }
thisHealthAnswer.relationship = formatParentalRelationship(
response.parent
)
if (hasSingleResponse) {
// Mum responded: Yes/No
consentHealthAnswers[key].push(thisHealthAnswer)
} else {
if (hasSameAnswersWithDetails) {
// Mum responded: Yes (Details)
// Dad responded: Yes (Details)
consentHealthAnswers[key].push(thisHealthAnswer)
} else if (hasSameAnswers && consentHealthAnswers[key].length === 0) {
// All responded: Yes/No
thisHealthAnswer.relationship = 'All'
consentHealthAnswers[key].push(thisHealthAnswer)
}
}
}
}
return consentHealthAnswers
}
/**
* Get consent outcome
*
* @param {import('../models.js').Reply} reply - Reply
* @param {import('../models.js').Session} session - Session
* @returns {ConsentOutcome} Consent outcome
*/
export const getConfirmedConsentOutcome = (reply, session) => {
if (reply.decision === ReplyDecision.NoResponse) {
return ConsentOutcome.NoResponse
}
if (reply.decision === ReplyDecision.Refused && reply.confirmed) {
return ConsentOutcome.FinalRefusal
}
if (reply.given) {
if (
session.offersAlternativeVaccine &&
reply.decision === ReplyDecision.OnlyAlternativeInjection
) {
return ConsentOutcome.GivenForAlternativeInjection
}
if (
session.offersIntranasalVaccine &&
reply.decision !== ReplyDecision.OnlyAlternativeInjection &&
!reply.alternative
) {
return ConsentOutcome.GivenForIntranasal
}
return ConsentOutcome.Given
}
return reply.decision
}
/**
* Get consent outcome
*
* @param {import('../models.js').PatientSession} patientSession - Patient session
* @returns {ConsentOutcome} Consent outcome
*/
export const getConsentOutcome = (patientSession) => {
const parentalRelationships = Object.values(ParentalRelationship)
// Get valid replies
// Include undelivered replies so can return ConsentOutcome.NotDelivered
let replies = Object.values(patientSession.replies).filter(
(reply) => !reply.invalid
)
if (replies.length === 1) {
// Check if request was delivered
if (!replies[0].delivered) {
return ConsentOutcome.NotDelivered
}
// Reply decision value matches consent outcome key
return getConfirmedConsentOutcome(replies[0], patientSession.session)
} else if (replies.length > 1) {
// Exclude undelivered replies so can return ConsentOutcome.NotDelivered
replies = replies.filter((reply) => reply.delivered)
// If no replies, no requests were delivered
if (replies.length === 0) {
return ConsentOutcome.NotDelivered
}
const decisions = _.uniqBy(replies, 'decision')
if (decisions.length > 1) {
// If one of the replies is not from parent (so from child), use that
const childReply = replies.find(
(reply) => !parentalRelationships.includes(reply.relationship)
)
if (childReply) {
return getConfirmedConsentOutcome(childReply, patientSession.session)
}
// If one of the replies has declined (requested follow up), show this
// status over showing responses as inconsistent
if (decisions.find((reply) => reply.declined)) {
return ConsentOutcome.Declined
}
return ConsentOutcome.Inconsistent
}
return getConfirmedConsentOutcome(decisions[0], patientSession.session)
}
return ConsentOutcome.NoResponse
}
/**
* Get combined refusal reasons
*
* @param {import('../models.js').PatientSession} patientSession - Patient session
* @returns {Array} Refusal reasons
*/
export const getConsentRefusalReasons = (patientSession) => {
const reasons = []
// Get consent responses with a refusal reason
const repliesWithRefusalReasons = Object.values(
patientSession.replies
).filter((reply) => reply.refusalReason)
for (const reply of repliesWithRefusalReasons) {
if (reply.refusalReason && !reply.invalid) {
// Indicate confirmed refusal reason
const refusalReason = reply.confirmed
? `${reply.refusalReason}<br><b>Confirmed</b>`
: reply.refusalReason
reasons.push(refusalReason)
}
}
return reasons ? [...new Set(reasons)] : []
}
/**
* Get faked answers for health questions needed for a vaccine
*
* @param {import('../models.js').Vaccine} vaccine - Vaccine
* @param {string} healthCondition - Health condition
* @returns {object} Health answers
*/
export const getHealthAnswers = (vaccine, healthCondition) => {
// If no vaccine, we don’t have consent
if (!vaccine) {
return
}
const answers = {}
for (const key of Object.keys(vaccine.flatHealthQuestions)) {
answers[key] = enrichWithRealisticAnswer(key, healthCondition)
}
// If asthma sub-question(s) has 'Yes’ answer, change parent answer to ‘Yes’
if (
[answers.asthmaSteroids?.answer, answers.asthmaAdmitted?.answer].includes(
'Yes'
)
) {
answers.asthma.answer = 'Yes'
}
return answers
}
/**
* Get faked triage note for health answer given for a child’s health condition
*
* @param {object} healthAnswers - Health answers
* @param {string} healthCondition - Health condition
* @returns {string} Triage note
*/
export const getTriageNote = (healthAnswers, healthCondition) => {
if (countAnswersNeedingTriage(healthAnswers)) {
return healthConditions[healthCondition].triageNote
}
}
/**
* Get child’s preferred names, based on information in consent replies
*
* @param {Array<import('../models.js').Reply>} replies - Consent replies
* @returns {string|boolean} Names(s)
*/
export const getPreferredNames = (replies) => {
const names = new Set()
Object.values(replies).forEach((reply) => {
const child = new Child(reply.child)
if (child.preferredName) {
names.add(child.preferredName)
}
})
return names.size && [...names].join(', ')
}
/**
* Get valid refusal reasons for a programme
*
* @param {ProgrammeType} type - Programme type
* @param {ReplyDecision} decision - Reply decision
* @returns {string} Refusal reason
*/
export const getRefusalReason = (type, decision) => {
// Gelatine content only a valid refusal reason for flu vaccine
let refusalReasons = Object.values(ReplyRefusal).filter((value) =>
type !== ProgrammeType.Flu ? value !== ReplyRefusal.Gelatine : value
)
// Gelatine content only a valid refusal reason for MMR vaccine
refusalReasons = Object.values(ReplyRefusal).filter((value) =>
type !== ProgrammeType.MMR ? value !== ReplyRefusal.GelatineMMR : value
)
// You cannot decline on the basis of already having had the vaccine
if (decision === ReplyDecision.Declined) {
refusalReasons = refusalReasons.filter((value) =>
[ReplyRefusal.AlreadyVaccinated, ReplyRefusal.GettingElsewhere].includes(
value
)
)
}
return faker.helpers.arrayElement(refusalReasons)
}
/**
* Has health answers needing triage
*
* @param {object} healthAnswers - Health answers
* @returns {number} Number of health answers needing triage
*/
export const countAnswersNeedingTriage = (healthAnswers) => {
if (!healthAnswers) {
return 0
}
const ignoredKeys = new Set(['asthma'])
return Object.entries(healthAnswers)
.filter(([key]) => !ignoredKeys.has(key))
.flatMap(([, answer]) => (Array.isArray(answer) ? answer : [answer]))
.filter((answer) => answer.answer === 'Yes').length
}