-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreply.js
More file actions
526 lines (478 loc) · 14.2 KB
/
reply.js
File metadata and controls
526 lines (478 loc) · 14.2 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
import { fakerEN_GB as faker } from '@faker-js/faker'
import { addMonths } from 'date-fns'
import _ from 'lodash'
import vaccines from '../datasets/vaccines.js'
import {
ConsentOutcome,
ConsentVaccineCriteria,
NotifyEmailStatus,
NotifySmsStatus,
ProgrammeType,
ReplyDecision,
ReplyMethod,
ReplyRefusal,
VaccineCriteria
} from '../enums.js'
import {
Child,
Parent,
Patient,
Programme,
Session,
User,
Vaccination
} from '../models.js'
import { formatDate, today } from '../utils/date.js'
import {
getConsentOutcomeStatus,
getReplyDecisionStatus
} from '../utils/status.js'
import {
formatMarkdown,
formatOther,
formatParent,
formatTag,
formatWithSecondaryText,
stringToBoolean
} from '../utils/string.js'
/**
* @class Reply
* @param {object} options - Options
* @param {object} [context] - Global context
* @property {object} [context] - Global context
* @property {string} uuid - UUID
* @property {Date} [createdAt] - Created date
* @property {string} [createdBy_uid] - User who created reply
* @property {Date} [updatedAt] - Updated date
* @property {import('./child.js').Child} [child] - Child
* @property {import('./parent.js').Parent} [parent] - Parent or guardian
* @property {ReplyDecision} [decision] - Consent decision
* @property {boolean} [alternative] - Consent for alternative vaccine
* @property {boolean} [confirmed] - Decision confirmed
* @property {boolean} [consultation] - Consultation requested
* @property {boolean} declined - Reply declines consent
* @property {boolean} ethnicity - Answered ethnicity questions
* @property {boolean} given - Reply gives consent
* @property {boolean} invalid - Reply is invalid
* @property {ReplyMethod} [method] - Reply method
* @property {object} [healthAnswers] - Answers to health questions
* @property {Array} [triageNote] - Triage note for answered health questions
* @property {ReplyRefusal} [refusalReason] - Refusal reason
* @property {string} [refusalReasonOther] - Other refusal reason
* @property {string} [refusalReasonDetails] - Refusal reason details
* @property {boolean} [selfConsent] - Reply given by child
* @property {string} [note] - Note about this response
* @property {string} patient_uuid - Patient UUID
* @property {string} [programme_id] - Programme ID
* @property {string} session_id - Session ID
*/
export class Reply {
constructor(options, context) {
this.context = context
this.uuid = options?.uuid || faker.string.uuid()
this.createdAt = options?.createdAt ? new Date(options.createdAt) : today()
this.createdBy_uid = options?.createdBy_uid
this.updatedAt = options?.updatedAt && new Date(options.updatedAt)
this.child = options?.child && new Child(options.child)
this.confirmed = stringToBoolean(options?.confirmed)
this.decision =
this.confirmed === true ? ReplyDecision.Refused : options?.decision
this.ethnicity = stringToBoolean(options?.ethnicity)
this.parent = options?.parent && new Parent(options.parent)
this.method = options?.method
this.selfConsent = options?.selfConsent
this.note = options?.note || ''
this.patient_uuid = options?.patient_uuid
this.programme_id = options?.programme_id
this.session_id = options?.session_id
// Some values only valid if the consent request was received
if (this.delivered) {
this.decision =
options?.refusalReason === ReplyRefusal.AlreadyVaccinatedMMR
? ReplyDecision.AlreadyVaccinated
: this.decision
this.alternative =
options?.alternative && stringToBoolean(options?.alternative)
this.consultation = stringToBoolean(options?.consultation)
this.declined = this.decision === ReplyDecision.Declined
this.given = [
ReplyDecision.Given,
ReplyDecision.OnlyAlternativeInjection,
ReplyDecision.OnlyMenACWY,
ReplyDecision.OnlyTdIPV
].includes(this.decision)
this.healthAnswers = this.given && options?.healthAnswers
this.triageNote = this.given && options?.triageNote
this.invalid =
this?.decision === ReplyDecision.NoResponse
? false // Don’t show non response as invalid
: stringToBoolean(options?.invalid) || false
}
if (
[ReplyDecision.AlreadyVaccinated, ReplyDecision.Refused].includes(
this.decision
)
) {
this.firstDose = options?.firstDose && new Vaccination(options.firstDose)
if (options?.firstDose?.scheduled) {
this.firstDose.createdAt = addMonths(this.child?.dob, 12)
}
this.secondDose =
options?.secondDose && new Vaccination(options.secondDose)
if (options?.secondDose?.scheduled) {
this.secondDose.createdAt = addMonths(this.child?.dob, 40)
}
}
if (
[
ReplyDecision.Declined,
ReplyDecision.Refused,
ReplyDecision.OnlyMenACWY,
ReplyDecision.OnlyTdIPV
].includes(this.decision)
) {
this.refusalReason = options?.refusalReason || ''
if (this.refusalReason === ReplyRefusal.Other) {
this.refusalReasonOther = options?.refusalReasonOther
}
if (
![ReplyRefusal.Personal, ReplyRefusal.Other].includes(
this.refusalReason
)
) {
this.refusalReasonDetails = options?.refusalReasonDetails || ''
}
}
}
/**
* Get respondent’s full name
*
* @returns {string|undefined} Full name
*/
get fullName() {
if (this.parent) {
return this.parent.fullName
} else if (this.child) {
return this.child.fullName
}
}
/**
* Get respondent’s relationship to child
*
* @returns {string|undefined} Relationship to child
*/
get relationship() {
if (this.parent) {
return this.parent.relationship
} else if (this.child) {
return 'Child (Gillick competent)'
}
}
/**
* Get full name and relationship to child
*
* @returns {string} Full name and relationship
*/
get fullNameAndRelationship() {
return this.selfConsent
? this.relationship
: formatParent(this.parent, false)
}
/**
* Was the consent response delivered?
*
* @returns {boolean} Response was delivered
*/
get delivered() {
// Only invites to give consent online can have delivery failures
if (this.method !== ReplyMethod.Website) {
return true
}
const hasEmailGotEmail =
this.parent?.email &&
this.parent?.emailStatus === NotifyEmailStatus.Delivered
const hasTelSmsGotSms =
this.parent?.tel && this.parent?.smsStatus === NotifySmsStatus.Delivered
return hasEmailGotEmail || hasTelSmsGotSms
}
/**
* Get user who created reply
*
* @returns {User|undefined} User
*/
get createdBy() {
try {
if (this.createdBy_uid) {
return User.findOne(this.createdBy_uid, this.context)
}
} catch (error) {
console.error('Reply.createdBy', error.message)
}
}
/**
* Get chosen vaccine method
*
* @returns {ConsentVaccineCriteria|undefined} Chosen vaccination method
*/
get vaccineCriteria() {
if (this.given && this.programme.type === ProgrammeType.Flu) {
switch (true) {
case this.decision === ReplyDecision.Given && !this.alternative:
return ConsentVaccineCriteria.IntranasalOnly
case this.decision === ReplyDecision.OnlyAlternativeInjection:
return ConsentVaccineCriteria.AlternativeFluInjectionOnly
default:
return ConsentVaccineCriteria.IntranasalPreferred
}
}
if (this.given && this.programme.type === ProgrammeType.MMR) {
if (this.decision === ReplyDecision.OnlyAlternativeInjection) {
return ConsentVaccineCriteria.AlternativeMMRInjectionOnly
}
}
}
/**
* Has parent given consent for an injected vaccine?
*
* @returns {boolean} Consent given for an injected vaccine
*/
get hasConsentForInjection() {
return (
this.decision === ReplyDecision.OnlyAlternativeInjection ||
this.alternative
)
}
/**
* Get health questions to show based on programme and decision given
*
* @returns {Array} Health questions
*/
get healthQuestionsForDecision() {
const { Flu, HPV, MenACWY, TdIPV } = ProgrammeType
const programme = this.session.programmes[0]
const healthQuestionsForDecision = new Map()
let consentedMethod
let consentedVaccine
// Consent given for flu programme with method of vaccination
if (programme?.type === Flu) {
consentedVaccine = Object.values(vaccines).filter(
(programme) => programme.type === Flu
)
// If no consent for alternative injection or only consent for injection
if (!this.alternative) {
consentedMethod =
this.decision === ReplyDecision.OnlyAlternativeInjection
? VaccineCriteria.AlternativeInjection
: VaccineCriteria.Intranasal
consentedVaccine = Object.values(vaccines).find(
(programme) => programme.method === consentedMethod
)
}
}
// Consent given for HPV programme
if (programme?.type === HPV) {
consentedVaccine = Object.values(vaccines).find(
(programme) => programme.type === HPV
)
}
// Consent given for MenACWY programme only
if (this.decision === ReplyDecision.OnlyMenACWY) {
consentedVaccine = Object.values(vaccines).find(
(programme) => programme.type === MenACWY
)
}
// Consent given for Td/IPV programme only
if (this.decision === ReplyDecision.OnlyTdIPV) {
consentedVaccine = Object.values(vaccines).find(
(programme) => programme.type === TdIPV
)
}
// Consent given for all programmes
if (ReplyDecision.Given && !consentedVaccine) {
consentedVaccine = this.session.vaccines
}
/** @type {Array} */
const consentedVaccines = Array.isArray(consentedVaccine)
? consentedVaccine
: [consentedVaccine]
for (const vaccine of consentedVaccines) {
for (const [key, value] of Object.entries(vaccine.healthQuestions)) {
healthQuestionsForDecision.set(key, value)
}
}
return Object.fromEntries(healthQuestionsForDecision)
}
/**
* Get patient
*
* @returns {Patient|undefined} Patient
*/
get patient() {
try {
if (this.patient_uuid) {
return Patient.findOne(this.patient_uuid, this.context)
}
} catch (error) {
console.error('Reply.patient', error.message)
}
}
/**
* Get programme
*
* @returns {Programme|undefined} User
*/
get programme() {
try {
if (this.programme_id) {
return Programme.findOne(this.programme_id, this.context)
}
} catch (error) {
console.error('Upload.programme', error.message)
}
}
/**
* Get session
*
* @returns {Session|undefined} Session
*/
get session() {
try {
if (this.session_id) {
return Session.findOne(this.session_id, this.context)
}
} catch (error) {
console.error('Reply.session', error.message)
}
}
/**
* Get formatted values
*
* @returns {object} Formatted values
*/
get formatted() {
let decisionStatus = formatTag(getReplyDecisionStatus(this.decision))
if (!this.delivered) {
decisionStatus = formatTag(
getConsentOutcomeStatus(ConsentOutcome.NotDelivered)
)
} else if (this.invalid) {
decisionStatus = formatWithSecondaryText(
formatTag({
colour: 'grey',
html: `<s>${this.decision}</s>`
}),
'Invalid',
false
)
} else if (this.confirmed) {
decisionStatus = formatWithSecondaryText(
decisionStatus,
'Confirmed',
false
)
}
return {
createdAt: formatDate(this.createdAt, {
day: 'numeric',
month: 'long',
year: 'numeric',
hour: 'numeric',
minute: '2-digit',
hour12: true
}),
createdBy: this.createdBy?.fullName || '',
decisionStatus,
parent: formatParent(this.parent, true),
tel: this.parent && this.parent.tel,
email: this.parent && this.parent.email,
programme: this.programme?.nameTag,
refusalReason: formatOther(this.refusalReasonOther, this.refusalReason),
refusalReasonDetails: formatMarkdown(this.refusalReasonDetails),
note: formatMarkdown(this.note)
}
}
/**
* Get namespace
*
* @returns {string} Namespace
*/
get ns() {
return 'reply'
}
/**
* Get URI
*
* @returns {string} URI
*/
get uri() {
return `/sessions/${this.session_id}/patients/${this.patient.nhsn}/${this.programme_id}/replies/${this.uuid}`
}
/**
* Get parent form URI
*
* @returns {string} Parent form URI
*/
get parentUri() {
return `${this.session.consentUrl}/${this.uuid}`
}
/**
* Find all
*
* @param {object} context - Context
* @returns {Array<Reply>|undefined} Replies
* @static
*/
static findAll(context) {
return Object.values(context.replies).map(
(reply) => new Reply(reply, context)
)
}
/**
* Find one
*
* @param {string} uuid - Reply UUID
* @param {object} context - Context
* @returns {Reply|undefined} Reply
* @static
*/
static findOne(uuid, context) {
if (context?.replies?.[uuid]) {
return new Reply(context.replies[uuid], context)
}
}
/**
* Create
*
* @param {object} reply - Consent
* @param {object} context - Context
* @returns {Reply} Created reply
* @static
*/
static create(reply, context) {
const createdReply = new Reply(reply)
// Update context
context.replies = context.replies || {}
context.replies[createdReply.uuid] = createdReply
return createdReply
}
/**
* Update
*
* @param {string} uuid - Reply UUID
* @param {object} updates - Updates
* @param {object} context - Context
* @returns {Reply} Updated reply
* @static
*/
static update(uuid, updates, context) {
const updatedReply = _.merge(Reply.findOne(uuid, context), updates)
updatedReply.updatedAt = today()
// Remove reply context
delete updatedReply.context
// Delete original reply (with previous UUID)
delete context.replies[uuid]
// Update context
context.replies[updatedReply.uuid] = updatedReply
return updatedReply
}
}