-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpatient-session.js
More file actions
1044 lines (948 loc) · 29.2 KB
/
Copy pathpatient-session.js
File metadata and controls
1044 lines (948 loc) · 29.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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { fakerEN_GB as faker } from '@faker-js/faker'
import filters from '@x-govuk/govuk-prototype-filters'
import {
AcademicYear,
AuditEventType,
ConsentOutcome,
ConsentWindow,
PatientStatus,
PatientConsentStatus,
PatientDeferredStatus,
PatientRefusedStatus,
PatientVaccinatedStatus,
RecordVaccineCriteria,
ReplyDecision,
RegistrationOutcome,
ScreenOutcome,
VaccinationOutcome,
ProgrammeType
} from '../enums.js'
import { Gillick, Instruction, Patient, Programme, Session } from '../models.js'
import { getDateValueDifference, getYearGroup, today } from '../utils/date.js'
import {
getInstructionOutcome,
getRegistrationOutcome,
getRecordOutcome,
getSessionOutcome
} from '../utils/patient-session.js'
import {
getConsentOutcome,
getConsentHealthAnswers,
getConsentRefusalReasons
} from '../utils/reply.js'
import {
getConsentOutcomeStatus,
getInstructionOutcomeStatus,
getPatientConsentStatus,
getRegistrationStatus,
getScreenOutcomeStatus,
getVaccinationOutcomeStatus
} from '../utils/status.js'
import {
formatLink,
formatTag,
formatVaccineCriteria,
formatYearGroup
} from '../utils/string.js'
import {
getScreenOutcome,
getScreenOutcomesForConsentMethod,
getScreenVaccineCriteria
} from '../utils/triage.js'
/**
* @class Patient Session
* @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 patient session
* @property {Date} [updatedAt] - Updated date
* @property {Gillick} [gillick] - Gillick assessment
* @property {Array<AuditEvent>} [notes] - Notes
* @property {boolean} alternative - Administer alternative vaccine
* @property {string} patient_uuid - Patient UUID
* @property {string} instruction_uuid - Instruction UUID
* @property {string} programme_id - Programme ID
* @property {string} session_id - Session ID
*/
export class PatientSession {
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.gillick = options?.gillick && new Gillick(options.gillick)
this.notes = options?.notes || []
this.alternative = options?.alternative || false
this.patient_uuid = options?.patient_uuid
this.instruction_uuid = options?.instruction_uuid
this.programme_id = options?.programme_id
this.session_id = options?.session_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('PatientSession.patient', error.message)
}
}
/**
* Get patient programme
*
* @returns {import('./patient-programme.js').PatientProgramme|undefined} Patient programme
*/
get patientProgramme() {
return this.patient.programmes[this.programme_id]
}
/**
* Get year group, within context of patient session’s academic year
*
* @returns {number} Year group in patient session’s academic year
*/
get yearGroup() {
return getYearGroup(this.patient.dob, this.session.academicYear)
}
/**
* Get instruction
*
* @returns {Instruction|undefined} Instruction
*/
get instruction() {
try {
return Instruction.findOne(this.instruction_uuid, this.context)
} catch (error) {
console.error('PatientSession.instruction', error.message)
}
}
/**
* Get audit events for patient session
*
* @returns {Array<import('./audit-event.js').AuditEvent>} Audit events
*/
get auditEvents() {
return this.patient.auditEvents.filter(({ programme_ids }) =>
programme_ids?.some((id) => this.session.programme_ids.includes(id))
)
}
/**
* Get audit events grouped by date
*
* @returns {object} Events grouped by date
*/
get auditEventLog() {
return this.auditEvents
.sort((a, b) => getDateValueDifference(b.createdAt, a.createdAt))
.reverse()
}
/**
* Get triage notes
*
* @returns {Array<import('./audit-event.js').AuditEvent>} Audit events
*/
get triageNotes() {
return this.auditEvents
.filter(({ programme_ids }) => programme_ids.includes(this.programme_id))
.filter(({ outcome }) => outcome)
}
/**
* Get pinned session notes
*
* @returns {Array<import('./audit-event.js').AuditEvent>} Audit event
*/
get pinnedNotes() {
return this.auditEvents
.filter(({ programme_ids }) => programme_ids.includes(this.programme_id))
.filter(({ name }) => name === AuditEventType.Pinned)
.sort((a, b) => getDateValueDifference(b.createdAt, a.createdAt))
}
/**
* Get replies for patient session
*
* @returns {Array<import('./reply.js').Reply>} Replies
*/
get replies() {
return this.patient.replies
.filter(({ programme_id }) => programme_id === this.programme_id)
.sort((a, b) => getDateValueDifference(b.createdAt, a.createdAt))
}
/** Get parental relationships from valid replies
*
* @returns {Array<string>} Parental relationships
*/
get parentalRelationships() {
return this.responses
.filter((reply) => !reply.invalid)
.flatMap((reply) => reply.relationship || 'Parent or guardian')
}
/** Get names of parents who have requested a follow up
*
* @returns {Array<string>} Parent names and relationships
*/
get parentsRequestingFollowUp() {
return this.responses
.filter((reply) => !reply.invalid)
.filter((reply) => reply.declined)
.flatMap((reply) => reply.parent.formatted.fullNameAndRelationship)
}
/**
* Get responses (consent requests that were delivered)
*
* @returns {Array<import('./reply.js').Reply>} Responses
*/
get responses() {
return this.replies.filter((reply) => reply.delivered)
}
/**
* Has every parent given consent for an injected vaccine?
*
* Some parents may give consent for the nasal spray, but also given consent
* for the injection as an alternative
*
* @returns {boolean} Consent given for an injected vaccine
*/
get hasConsentForInjection() {
return this.responses.every(
({ hasConsentForInjection }) => hasConsentForInjection
)
}
/**
* Has every parent given consent only for an injected vaccine?
*
* We need this so that we don’t offer multiple triage outcomes if consent has
* only been given for the injected vaccine
*
* @returns {boolean} Consent given for an injected vaccine
*/
get hasConsentForAlternativeInjectionOnly() {
return this.responses.every(
({ decision }) => decision === ReplyDecision.OnlyAlternativeInjection
)
}
/**
* Get screen outcomes for vaccination method(s) consented to
*
* @returns {Array<ScreenOutcome>} Screen outcomes
*/
get screenOutcomesForConsentMethod() {
return getScreenOutcomesForConsentMethod(this.programme, this.responses)
}
/**
* Get vaccination criteria consented to use if safe to vaccinate
*
* @returns {import('../enums.js').ScreenVaccineCriteria|boolean} Criteria
*/
get screenVaccineCriteria() {
return getScreenVaccineCriteria(this.programme, this.responses)
}
/**
* Get programme
*
* @returns {Programme|undefined} Programme
*/
get programme() {
try {
return Programme.findOne(this.programme_id, this.context)
} catch (error) {
console.error('PatientSession.programme', error.message)
}
}
/**
* Get session
*
* @returns {Session|undefined} Session
*/
get session() {
try {
return Session.findOne(this.session_id, this.context)
} catch (error) {
console.error('PatientSession.session', error.message)
}
}
/**
* Get related patient sessions
*
* @returns {Array<PatientSession>} Patient sessions
*/
get siblingPatientSessions() {
try {
return PatientSession.findAll(this.context)
.filter(({ patient_uuid }) => patient_uuid === this.patient_uuid)
.filter(({ session_id }) => session_id === this.session_id)
.sort((a, b) => a.programme.name.localeCompare(b.programme.name))
} catch (error) {
console.error('PatientSession.siblingPatientSessions', error.message)
}
}
/**
* Get vaccine to administer (or was administered) in this patient session
*
* For all programmes besides flu, this will be an injection.
* For the flu programme, this depends on consent responses
*
* @returns {import('./vaccine.js').Vaccine|undefined} Vaccine method
*/
get vaccine() {
const standardVaccine = this.programme.vaccines.find((vaccine) => vaccine)
const alternativeVaccine = this.programme.alternativeVaccine
// Need consent response(s) before we can determine the chosen method
// We only want to instruct on patients being vaccinated using nasal spray
if (!this.consentGiven) {
return
}
// If no alternative, can only have been the standard vaccine
if (!this.programme.alternativeVaccine) {
return standardVaccine
}
// Administered vaccine was the alternative
if (this.alternative) {
return alternativeVaccine
}
// Return vaccine based on consent (and triage) outcomes
const hasScreenedForInjection =
this.screen &&
[
ScreenOutcome.VaccinateAlternativeFluInjectionOnly,
ScreenOutcome.VaccinateAlternativeMMRInjectionOnly
].includes(String(this.screen))
return this.hasConsentForAlternativeInjectionOnly || hasScreenedForInjection
? alternativeVaccine // Injection
: standardVaccine // Nasal
}
/**
* Get vaccine to administer (or was administered) in this patient session
*
* For all programmes besides flu, this will be an injection.
* For the flu programme, this depends on consent responses
*
* @returns {import('../enums.js').RecordVaccineCriteria|undefined} Vaccination method
*/
get vaccineCriteria() {
// If no programme does not offer alternatives, don’t return a method
if (!this.programme.alternativeVaccine) {
return
}
// Need consent response(s) before we can determine the chosen method
if (!this.consentGiven) {
return
}
if (this.programme.type === ProgrammeType.Flu) {
if (
this.consent === ConsentOutcome.GivenForIntranasal ||
this.screen === ScreenOutcome.VaccinateIntranasalOnly
) {
return RecordVaccineCriteria.IntranasalOnly
}
if (
this.consent === ConsentOutcome.GivenForAlternativeInjection ||
this.screen === ScreenOutcome.VaccinateAlternativeFluInjectionOnly
) {
return RecordVaccineCriteria.AlternativeFluInjectionOnly
}
return RecordVaccineCriteria.IntranasalPreferred
}
if (this.programme.type === ProgrammeType.MMR) {
if (
this.consent === ConsentOutcome.GivenForAlternativeInjection ||
this.screen === ScreenOutcome.VaccinateAlternativeMMRInjectionOnly
) {
return RecordVaccineCriteria.AlternativeMMRInjectionOnly
}
return RecordVaccineCriteria.NoMMRPreference
}
}
/**
* Can either vaccine be administered
*
* @returns {boolean} Either vaccine be administered
*/
get canRecordAlternativeVaccine() {
const hasScreenedForNasal =
this.screen === ScreenOutcome.VaccinateIntranasalOnly
return (
this.hasConsentForInjection &&
!this.hasConsentForAlternativeInjectionOnly &&
!hasScreenedForNasal
)
}
/**
* Get vaccinations for patient session
*
* @returns {Array<import('./vaccination.js').Vaccination>|undefined} Vaccinations
*/
get vaccinationOutcomes() {
try {
if (this.patient.vaccinations && this.programme_id) {
return this.patient.vaccinations.filter(
({ programme }) => programme.id === this.programme_id
)
}
} catch (error) {
console.error('PatientSession.vaccinations', error.message)
}
}
/**
* Get last recorded vaccination
*
* @returns {import('./vaccination.js').Vaccination} Vaccination
*/
get lastVaccinationOutcome() {
if (this.vaccinationOutcomes?.length > 0) {
return this.vaccinationOutcomes.at(-1)
}
}
/**
* Get next activity, per programme
*
* @returns {Array<PatientSession>} Patient sessions per programme
*/
get outstandingVaccinations() {
return this.siblingPatientSessions.filter(
({ report }) => report === PatientStatus.Due
)
}
/**
* Get consent outcome
*
* @returns {ConsentOutcome} Consent outcome
*/
get consent() {
return getConsentOutcome(this)
}
/**
* Get explanatory notes about consent outcome
*
* @returns {string} Explanatory notes
*/
get consentNotes() {
const relationships = filters.formatList(this.parentalRelationships)
const parentNames = filters.formatList(this.parentsRequestingFollowUp)
if (this.patient.hasNoContactDetails) {
return 'There are no contact details for this child.'
}
if (this.session.consentWindow === ConsentWindow.Opening) {
return this.session.formatted.consentWindowSentence
}
switch (this.consent) {
case ConsentOutcome.NoResponse:
return 'No-one responded to our requests for consent.'
case ConsentOutcome.NotDelivered:
return 'Consent response could not be delivered.'
case ConsentOutcome.Inconsistent:
return 'You can only vaccinate if all respondents give consent.'
case ConsentOutcome.Declined:
return `${parentNames} would like to speak to a member of the team about other options for their child’s vaccination.`
case ConsentOutcome.Given:
case ConsentOutcome.GivenForAlternativeInjection:
case ConsentOutcome.GivenForIntranasal:
return `${relationships} gave consent.`
case ConsentOutcome.Refused:
return `${relationships} refused consent.`
case ConsentOutcome.FinalRefusal:
return `Refusal to give consent confirmed by ${relationships}.`
default:
}
}
/**
* Get patient consent status
*
* @returns {PatientConsentStatus} Patient consent status
*/
get patientConsent() {
if (this.patient.hasNoContactDetails) {
return PatientConsentStatus.NoDetails
}
if (this.session.consentWindow === ConsentWindow.None) {
return PatientConsentStatus.NotScheduled
} else if (this.session.consentWindow === ConsentWindow.Opening) {
return PatientConsentStatus.Scheduled
}
switch (this.consent) {
case ConsentOutcome.NotDelivered:
return PatientConsentStatus.NotDelivered
case ConsentOutcome.NoResponse:
return PatientConsentStatus.NoResponse
case ConsentOutcome.Declined:
return PatientConsentStatus.FollowUp
}
}
/**
* Get patient deferred status
*
* @returns {PatientDeferredStatus} Patient deferred status
*/
get patientDeferred() {
if (this.screen === ScreenOutcome.DoNotVaccinate) {
return PatientDeferredStatus.DoNotVaccinate
} else if (this.screen === ScreenOutcome.DelayVaccination) {
return PatientDeferredStatus.DelayVaccination
} else if (this.screen === ScreenOutcome.InviteToClinic) {
return PatientDeferredStatus.InviteToClinic
}
switch (this.outcome) {
case VaccinationOutcome.Absent:
return PatientDeferredStatus.ChildAbsent
case VaccinationOutcome.Refused:
return PatientDeferredStatus.ChildRefused
case VaccinationOutcome.Unwell:
return PatientDeferredStatus.ChildUnwell
case VaccinationOutcome.InviteToClinic:
return PatientDeferredStatus.InviteToClinic
case VaccinationOutcome.DelayVaccination:
return PatientDeferredStatus.DelayVaccination
case VaccinationOutcome.DoNotVaccinate:
return PatientDeferredStatus.DoNotVaccinate
}
}
/**
* Get patient refused status
*
* @returns {PatientRefusedStatus} Patient refused status
*/
get patientRefused() {
switch (this.consent) {
case ConsentOutcome.Inconsistent:
return PatientRefusedStatus.Conflict
case ConsentOutcome.Refused:
case ConsentOutcome.FinalRefusal:
return PatientRefusedStatus.Refusal
}
}
/**
* Get patient vaccinated status
*
* @returns {PatientVaccinatedStatus} Patient vaccinated status
*/
get patientVaccinated() {
switch (this.outcome) {
case VaccinationOutcome.Vaccinated:
case VaccinationOutcome.PartVaccinated:
return PatientVaccinatedStatus.Vaccinated
case VaccinationOutcome.AlreadyVaccinated:
return PatientVaccinatedStatus.AlreadyVaccinated
}
}
/**
* Consent has been given
*
* @returns {boolean} Consent has been given
*/
get consentGiven() {
return [
ConsentOutcome.Given,
ConsentOutcome.GivenForAlternativeInjection,
ConsentOutcome.GivenForIntranasal
].includes(this.consent)
}
/**
* Get consent health answers
*
* @returns {object|boolean} Consent health answers
*/
get consentHealthAnswers() {
return getConsentHealthAnswers(this)
}
/**
* Get responses with triage notes for consent health answers
*
* @returns {Array} Triage notes
*/
get responsesWithTriageNotes() {
return this.responses.filter((response) => response.triageNote)
}
/**
* Get consent refusal reasons (from replies)
*
* @returns {object|boolean} Consent refusal reasons
*/
get consentRefusalReasons() {
return getConsentRefusalReasons(this)
}
/**
* Get screening outcome
*
* @returns {ScreenOutcome|boolean} Screening outcome
*/
get screen() {
return getScreenOutcome(this)
}
/**
* Get explanatory notes about consent outcome
*
* @returns {string} Explanatory notes
*/
get screenNotes() {
const { patient, triageNotes } = this
const triageNote = triageNotes.at(-1)
const user = triageNote?.createdBy || { fullName: 'Jane Joy' }
switch (this.screen) {
case ScreenOutcome.NeedsTriage:
return `You need to decide if it’s safe to vaccinate ${patient.firstName}.`
case ScreenOutcome.InviteToClinic:
return `${user.fullName} decided that ${patient.firstName}’s vaccination should take place at a clinic.`
case ScreenOutcome.DelayVaccination:
return `${user.fullName} decided that ${patient.firstName}’s vaccination should be delayed until ${triageNote.formatted.outcomeAt}.`
case ScreenOutcome.DoNotVaccinate:
return `${user.fullName} decided that ${patient.firstName} should not be vaccinated.`
case ScreenOutcome.Vaccinate:
return `${user.fullName} decided that ${patient.firstName} is safe to vaccinate.`
case ScreenOutcome.VaccinateAlternativeFluInjectionOnly:
return `${user.fullName} decided that ${patient.firstName} is safe to vaccinate using the injected vaccine only.`
case ScreenOutcome.VaccinateAlternativeMMRInjectionOnly:
return `${user.fullName} decided that ${patient.firstName} is safe to vaccinate using the gelatine-free injection only.`
case ScreenOutcome.VaccinateIntranasalOnly:
return `${user.fullName} decided that ${patient.firstName} is safe to vaccinate using the nasal spray only.`
default:
return `No triage is needed for ${patient.firstName}.`
}
}
/**
* Get instruction outcome
*
* @returns {import('../enums.js').InstructionOutcome|boolean} Instruction outcome
*/
get instruct() {
return getInstructionOutcome(this)
}
/**
* Get registration outcome
*
* @returns {import('../enums.js').RegistrationOutcome} Registration outcome
*/
get register() {
return getRegistrationOutcome(this)
}
/**
* Get explanatory notes about registration outcome
*
* @returns {string} Explanatory notes
*/
get registerNotes() {
const { patient } = this
switch (this.register) {
case RegistrationOutcome.Present:
return `${patient.firstName} is attending this session.`
case RegistrationOutcome.Absent:
return `${patient.firstName} is absent from this session.`
case RegistrationOutcome.Pending:
return `${patient.firstName} has not been registered as attending yet.`
case RegistrationOutcome.Complete:
return `${patient.firstName} has completed this session.`
}
}
/**
* Get ready to record outcome
*
* @returns {boolean} Ready to record outcome
*/
get record() {
return getRecordOutcome(this)
}
/**
* Get vaccination (session) outcome
*
* @returns {import('../enums.js').VaccinationOutcome} Vaccination (session) outcome
*/
get outcome() {
return getSessionOutcome(this)
}
/**
* Get patient status
*
* @returns {PatientStatus} Patient status
*/
get report() {
return this.patientProgramme.status
}
/**
* Get explanatory notes about patient status
*
* @returns {string} Explanatory notes
*/
get reportNotes() {
switch (this.report) {
case PatientStatus.Vaccinated:
return `${this.patient.firstName} was vaccinated by ${this.lastVaccinationOutcome.createdBy.fullName} on ${this.lastVaccinationOutcome.formatted.createdAt}.`
case PatientStatus.Due:
return this.vaccineCriteria
? `${this.patient.firstName} is ready to vaccinate (${this.vaccineCriteria.toLowerCase()}).`
: `${this.patient.firstName} is ready to vaccinate.`
case PatientStatus.Deferred:
return this.lastVaccinationOutcome
? `${this.patientDeferred} on ${this.lastVaccinationOutcome.formatted.createdAt}.`
: `${this.patientDeferred}.`
case PatientStatus.Triage:
return this.screenNotes
case PatientStatus.Refused:
return `${this.patientRefused}.`
case PatientStatus.Consent:
return `${this.patientConsent}.`
}
}
/**
* Get formatted links
*
* @returns {object} Formatted links
*/
get link() {
return {
fullName: formatLink(this.uri, this.patient.fullName)
}
}
/**
* Get status properties per activity
*
* @returns {object} Status properties
*/
get status() {
return {
consent: getConsentOutcomeStatus(this.consent),
patientConsent: getPatientConsentStatus(this.patientConsent),
screen: getScreenOutcomeStatus(this.screen),
instruct: getInstructionOutcomeStatus(this.instruct),
register: getRegistrationStatus(this.register),
outcome: getVaccinationOutcomeStatus(this.outcome),
report: this.patientProgramme?.status
}
}
/**
* Get formatted values
*
* @returns {object} Formatted values
*/
get formatted() {
const outstandingVaccinations = this.outstandingVaccinations.map(
({ programme }) => programme.name
)
let formattedYearGroup = formatYearGroup(this.yearGroup)
formattedYearGroup += this.patient.registrationGroup
? `, ${this.patient.registrationGroup}`
: ''
formattedYearGroup += ` (${AcademicYear[this.session.academicYear]} academic year)`
return {
programme: this.programme.nameTag,
consent: formatTag(this.status.consent),
patientConsent: formatTag(this.status.patientConsent),
screen: this.screen && formatTag(this.status.screen),
instruct: this.session.psdProtocol && formatTag(this.status.instruct),
register: formatTag(this.status.register),
outcome: this.outcome && formatTag(this.status.outcome),
report: this.patientProgramme.formatted.programmeStatus,
outstandingVaccinations: filters.formatList(outstandingVaccinations),
vaccineCriteria: formatVaccineCriteria(this.vaccineCriteria),
yearGroup: formattedYearGroup
}
}
/**
* Get namespace
*
* @returns {string} Namespace
*/
get ns() {
return 'patientSession'
}
/**
* Get URI
*
* @returns {string} URI
*/
get uri() {
return `/sessions/${this.session_id}/patients/${this.patient.nhsn}/${this.programme_id}`
}
/**
* Find all
*
* @param {object} context - Context
* @returns {Array<PatientSession>|undefined} Patient sessions
* @static
*/
static findAll(context) {
return Object.values(context.patientSessions).map(
(patientSession) => new PatientSession(patientSession, context)
)
}
/**
* Find one
*
* @param {string} uuid - Patient UUID
* @param {object} context - Context
* @returns {PatientSession|undefined} Patient
* @static
*/
static findOne(uuid, context) {
if (context?.patientSessions?.[uuid]) {
return new PatientSession(context.patientSessions[uuid], context)
}
}
/**
* Create
*
* @param {object} patientSession - Patient session
* @param {object} context - Context
* @returns {PatientSession} Created patient session
* @static
*/
static create(patientSession, context) {
const createdPatientSession = new PatientSession(patientSession)
// Update context
context.patientSessions = context.patientSessions || {}
context.patientSessions[createdPatientSession.uuid] = createdPatientSession
return createdPatientSession
}
/**
* Update
*
* @param {string} uuid - Patient UUID
* @param {object} updates - Updates
* @param {object} context - Context
* @returns {PatientSession} Updated patient session
* @static
*/
static update(uuid, updates, context) {
const updatedPatientSession = Object.assign(
PatientSession.findOne(uuid, context),
updates
)
updatedPatientSession.updatedAt = today()
// Remove patient context
delete updatedPatientSession.context
// Delete original patient session (with previous UUID)
delete context.patientSessions[uuid]
// Update context
context.patientSessions[updatedPatientSession.uuid] = updatedPatientSession
return updatedPatientSession
}
/**
* Remove patient from session
*
* @param {import('./audit-event.js').AuditEvent} event - Event
*/
removeFromSession(event) {
this.patient.patientSession_uuids =
this.patient.patientSession_uuids.filter((uuid) => uuid !== this.uuid)
this.patient.addEvent({
name: `Removed from the ${this.session.name.replace('Flu', 'flu')}`,
createdBy_uid: event.createdBy_uid,
programme_ids: this.session.programme_ids
})
}
/**
* Assess Gillick competence
*
* @param {object} event - Event
* @param {Gillick} gillick - gillick
*/
assessGillick(event, gillick) {
this.patient.addEvent({
name: event.name,
note: gillick.note,
createdAt: gillick.createdAt,
createdBy_uid: event.createdBy_uid,
programme_ids: this.session.programme_ids
})
PatientSession.update(this.uuid, { gillick }, this.context)
}
/**
* Record triage
*
* @param {import('./audit-event.js').AuditEvent} event - Event
*/
recordTriage(event) {
this.patient.addEvent({
name: event.name,
note: event.note,
outcome: event.outcome,
outcomeAt_: event.outcomeAt_,
createdAt: event.createdAt,
createdBy_uid: event.createdBy_uid,
programme_ids: [this.programme_id]
})
}
/**
* Give PSD instruction
*
* @param {Instruction} instruction - Instruction
*/
giveInstruction(instruction) {
this.instruction_uuid = instruction.uuid
this.patient.addEvent({
name: 'PSD added',
createdAt: instruction.createdAt,
createdBy_uid: instruction.createdBy_uid,
programme_ids: [this.programme_id]
})
}
/**
* Register attendance
*
* @param {import('./audit-event.js').AuditEvent} event - Event
* @param {RegistrationOutcome} register - Registration
*/
registerAttendance(event, register) {
this.session.updateRegister(this.patient.uuid, register)
let name
switch (register) {
case RegistrationOutcome.Present:
name = `Registered as attending today’s session at ${this.session.location.name}`
break
case RegistrationOutcome.Absent:
name = `Registered as absent from today’s session at ${this.session.location.name}`
break
default:
}
this.patient.addEvent({
name,
createdAt: event.createdAt,
createdBy_uid: event.createdBy_uid,
programme_ids: this.session.programme_ids
})
}