-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatient-programme.js
More file actions
450 lines (409 loc) · 11.1 KB
/
patient-programme.js
File metadata and controls
450 lines (409 loc) · 11.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
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
import { addMonths, addWeeks } from 'date-fns'
import {
PatientConsentStatus,
PatientDueStatus,
PatientStatus,
ProgrammeType
} from '../enums.js'
import { AuditEvent, Patient, Programme, Vaccination } from '../models.js'
import {
getCurrentAcademicYear,
getDateValueDifference
} from '../utils/date.js'
import { ordinal } from '../utils/number.js'
import { getReportOutcome } from '../utils/patient-session.js'
import { getPatientStatus } from '../utils/status.js'
import {
formatProgrammeStatus,
formatTag,
formatWithSecondaryText
} from '../utils/string.js'
/**
* @class Patient Programme
* @param {object} options - Options
* @param {object} [context] - Global context
* @property {object} [context] - Global context
* @property {boolean} [invitedToClinic] - Invited to clinic
* @property {string} patient_uuid - Patient UUID
* @property {string} programme_id - Programme ID
*/
export class PatientProgramme {
constructor(options, context) {
this.context = context
this.invitedToClinic = options?.invitedToClinic
this.patient_uuid = options?.patient_uuid
this.programme_id = options?.programme_id
}
/**
* 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('PatientProgramme.patient', error.message)
}
}
/**
* Get programme
*
* @returns {Programme|undefined} Programme
*/
get programme() {
try {
const programme = Programme.findOne(this.programme_id, this.context)
if (this.programme_id === 'mmr' && this.patient?.age <= 6) {
programme.name = 'MMRV'
}
return programme
} catch (error) {
console.error('PatientProgramme.programme', error.message)
}
}
/**
* Year patient is eligible for programme
*
* @returns {number} Year patient becomes eligible for programme
*/
get year() {
if (!this.programme) {
return
}
if (this.programme.type === ProgrammeType.Flu) {
return getCurrentAcademicYear()
}
const yearsUntilEligible =
this.programme.targetYearGroup - this.patient.yearGroup
return getCurrentAcademicYear() + yearsUntilEligible
}
/**
* Get audit events for this patient programme
*
* @returns {Array<import('./audit-event.js').AuditEvent>} Audit events
*/
get auditEvents() {
return this.patient.events
.map((auditEvent) => new AuditEvent(auditEvent, this.context))
.filter(({ programme_ids }) =>
programme_ids?.some((id) => this.programme_id === id)
)
}
/**
* Get patient sessions for this patient programme
*
* @returns {Array<import('./patient-session.js').PatientSession>} Patient sessions
*/
get patientSessions() {
return this.patient?.patientSessions.filter(
({ programme_id }) => programme_id === this.programme_id
)
}
/**
* Get most recent patient session
*
* @returns {import('./patient-session.js').PatientSession} Patient session
*/
get lastPatientSession() {
if (this.patientSessions?.length > 0) {
return this.patientSessions.at(-1)
}
}
/**
* Eligible for vaccination
*
* @returns {boolean} Eligible for vaccination
*/
get inviteToSession() {
return (
this.status !== PatientStatus.Ineligible &&
this.status !== PatientStatus.Vaccinated
)
}
/**
* Eligible for programme in the current academic year
*
* @returns {boolean} Eligible for programme
*/
get eligible() {
return getCurrentAcademicYear() >= this.year
}
/**
* Get vaccination outcomes
*
* @returns {Array<import('./vaccination.js').Vaccination>|undefined} Vaccinations
*/
get vaccinationOutcomes() {
return this.patient?.vaccinations.filter(
({ programme }) => programme.id === this.programme_id
)
}
/**
* Get last vaccination outcome
*
* @returns {import('./vaccination.js').Vaccination} Vaccination
*/
get lastVaccinationOutcome() {
if (this.vaccinationOutcomes?.length > 0) {
return this.vaccinationOutcomes.at(-1)
}
}
/**
* Get vaccinations given
*
* @returns {Array<import('./vaccination.js').Vaccination>|undefined} Vaccinations
*/
get vaccinationsGiven() {
return this.vaccinationOutcomes.filter((vaccination) => vaccination.given)
}
/**
* Get TTCV vaccinations given
*
* @returns {Array<import('./vaccination.js').Vaccination>|undefined} Vaccinations
*/
get ttcvVaccinationsGiven() {
return this.patient?.vaccinations
.filter((vaccination) => vaccination.programme?.ttcv)
.filter((vaccination) => vaccination.given)
}
/**
* Get other vaccinations given
*
* @returns {Array<import('./vaccination.js').Vaccination>|undefined} Vaccinations
*/
get otherVaccinationsGiven() {
return this.patient?.vaccinations
.filter((vaccination) => vaccination.programmeOther)
.filter((vaccination) => vaccination.given)
}
/**
* Get last vaccination outcome
*
* @returns {import('./vaccination.js').Vaccination} Vaccination
*/
get lastVaccinationGiven() {
if (this.vaccinationsGiven?.length > 0) {
return this.vaccinationsGiven.at(-1)
}
}
/**
* Get doses needed
*
* @returns {number} Doses needed
*/
get dosesNeeded() {
if (
this.patient.immunocompromised &&
this.programme.immunocompromisedSequence
) {
return this.programme.immunocompromisedSequence.length
}
return this.programme.sequence.length
}
/**
* Get doses remaining
*
* @returns {number} Doses remaining
*/
get dosesRemaining() {
if (this.vaccinationsGiven?.length > 0) {
return this.dosesNeeded - this.vaccinationsGiven?.length
}
return this.dosesNeeded
}
/**
* Get dose due (ordinal)
*
* @returns {number} Dose due (ordinal)
*/
get doseDue() {
switch (true) {
case this.dosesNeeded === 3 && this.dosesRemaining === 1:
return 3
case this.dosesNeeded === 3 && this.dosesRemaining === 2:
case this.dosesNeeded === 2 && this.dosesRemaining === 1:
return 2
case this.dosesNeeded === 3 && this.dosesRemaining === 3:
case this.dosesNeeded === 2 && this.dosesRemaining === 2:
return 1
case this.dosesNeeded === 1 && this.dosesRemaining === 1:
default:
return 0
}
}
/**
* Get dose sequence code
*
* @returns {number} Dose sequence code
*/
get sequence() {
if (
this.patient.immunocompromised &&
this.programme.immunocompromisedSequence
) {
return this.programme.immunocompromisedSequence[this.doseDue - 1]
}
return this.programme.sequence[this.doseDue - 1]
}
get ttcvVaccinations() {
if (this.programme.type === ProgrammeType.TdIPV) {
return [
new Vaccination(
{
createdAt: addWeeks(this.patient.dob, 8),
programme_id: '5in1',
sequence: '1P'
},
this.context
),
new Vaccination(
{
createdAt: addWeeks(this.patient.dob, 12),
programme_id: '5in1',
sequence: '2P'
},
this.context
),
new Vaccination(
{
createdAt: addWeeks(this.patient.dob, 16),
programme_id: '5in1',
sequence: '3P'
},
this.context
),
new Vaccination(
{
createdAt: addMonths(this.patient.dob, 40),
programme_id: '4in1',
sequence: '1B'
},
this.context
),
...this.ttcvVaccinationsGiven,
...this.otherVaccinationsGiven
].sort((a, b) => getDateValueDifference(a.createdAt, b.createdAt))
}
}
/**
* Get vaccination due
*
* @returns {PatientDueStatus} Vaccination due
*/
get vaccinationDue() {
switch (true) {
case this.dosesNeeded === 3 && this.dosesRemaining === 1:
return PatientDueStatus.Third
case this.dosesNeeded === 3 && this.dosesRemaining === 2:
case this.dosesNeeded === 2 && this.dosesRemaining === 1:
return PatientDueStatus.Second
case this.dosesNeeded === 3 && this.dosesRemaining === 3:
case this.dosesNeeded === 2 && this.dosesRemaining === 2:
return PatientDueStatus.First
case this.dosesNeeded === 1 && this.dosesRemaining === 1:
default:
return PatientDueStatus.Only
}
}
/**
* Get status
*
* @returns {PatientStatus} Status properties
*/
get status() {
// Not eligible for programme yet
if (!this.eligible) {
return PatientStatus.Ineligible
}
// Is fully vaccinated
if (this.dosesRemaining === 0) {
return PatientStatus.Vaccinated
}
// Has been invited to a session
if (this.lastPatientSession) {
return getReportOutcome(this.lastPatientSession)
}
// Needs to be invited to a session
return PatientStatus.Consent
}
/**
* Get status colour name
*
* @returns {string} Colour name
*/
get statusColour() {
return getPatientStatus(this.status, this.vaccinationDue).colour
}
/**
* Get explanatory notes
*
* @returns {string} Explanatory notes
*/
get statusNotes() {
switch (this.status) {
case PatientStatus.Ineligible:
return this.patient.post16
? 'Not eligible for school age immunisation'
: `Eligible from 1 September ${this.year}`
case PatientStatus.Vaccinated:
return `Vaccinated on ${this.lastVaccinationGiven.formatted.createdAt_dateShort}`
case PatientStatus.Due:
return this.lastPatientSession.vaccineCriteria
case PatientStatus.Deferred:
return this.lastVaccinationOutcome
? `${this.lastPatientSession.patientDeferred} on ${this.lastVaccinationOutcome.formatted.createdAt_dateShort}`
: this.lastPatientSession.patientDeferred
case PatientStatus.Refused:
return this.lastPatientSession.patientRefused
case PatientStatus.Consent:
return this.lastPatientSession
? this.lastPatientSession.patientConsent
: PatientConsentStatus.NotScheduled
}
}
/**
* Get vaccine to administer (or was administered) in this patient session
*
* @returns {import('../enums.js').RecordVaccineCriteria} Vaccine criteria
*/
get vaccineCriteria() {
return this.lastPatientSession.vaccineCriteria
}
/**
* Get formatted values
*
* @returns {object} Formatted values
*/
get formatted() {
const status = formatTag(getPatientStatus(this.status, this.vaccinationDue))
return {
doseDue: ordinal(this.doseDue),
status,
statusWithNotes: formatWithSecondaryText(status, this.statusNotes, false),
programmeStatus: formatProgrammeStatus(
this.programme,
getPatientStatus(this.status, this.vaccinationDue),
this.statusNotes
)
}
}
/**
* Get namespace
*
* @returns {string} Namespace
*/
get ns() {
return 'patientProgramme'
}
/**
* Get URI
*
* @returns {string} URI
*/
get uri() {
return `/patients/${this.patient_uuid}/programmes/${this.programme_id}`
}
}