Skip to content

Commit c1419e5

Browse files
committed
improve logic around assigning doses. Look at all records before assuming we've found dose 1 and 2
1 parent 0dd8bac commit c1419e5

2 files changed

Lines changed: 55 additions & 62 deletions

File tree

app/utils/dose-schedule.js

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ const MMR_MAX_SEQUENCE = 2
1313
const REASON = {
1414
BeforeAge12Months: 'Given before age 12 months',
1515
BeforeAge15Months: 'Given before age 15 months',
16-
LessThan28DaysAfterPrevious: 'Given less than 28 days after previous dose'
16+
LessThan28DaysAfterPrevious: 'Given less than 28 days after previous dose',
17+
ExtraDose: 'Additional dose'
1718
}
1819

1920
/**
@@ -32,6 +33,7 @@ const REASON = {
3233
* @returns {{
3334
* validDoses: Array<{ vaccination: object, sequence: number }>,
3435
* ignoredDoses: Array<{ vaccination: object, reason: string }>,
36+
* recordedDoses: Array<{ vaccination: object, kind: 'valid'|'ignored', sequence?: number, reason?: string }>,
3537
* nextEligibleFrom: Date|null
3638
* }}
3739
*/
@@ -45,50 +47,61 @@ export function getScheduleSummary({
4547
)
4648

4749
if (programme?.id !== 'mmr') {
50+
const validDoses = givenDoses.map((vaccination, i) => ({
51+
vaccination,
52+
sequence: i + 1
53+
}))
4854
return {
49-
validDoses: givenDoses.map((vaccination, i) => ({
50-
vaccination,
51-
sequence: i + 1
52-
})),
55+
validDoses,
5356
ignoredDoses: [],
57+
recordedDoses: validDoses.map((d) => ({ ...d, kind: 'valid' })),
5458
nextEligibleFrom: null
5559
}
5660
}
5761

5862
const validDoses = []
5963
const ignoredDoses = []
64+
const recordedDoses = []
6065
let lastValidCreatedAt = null
6166

6267
for (const vaccination of givenDoses) {
6368
const nextSequence = validDoses.length + 1
64-
if (nextSequence > MMR_MAX_SEQUENCE) break
65-
69+
const atScheduleMax = nextSequence > MMR_MAX_SEQUENCE
6670
const createdAt = new Date(vaccination.createdAt)
67-
const minAgeDate = addMonths(dob, MMR_MIN_AGE_MONTHS[nextSequence])
6871

69-
if (isBefore(createdAt, minAgeDate)) {
70-
ignoredDoses.push({
71-
vaccination,
72-
reason:
72+
if (!atScheduleMax) {
73+
const minAgeDate = addMonths(dob, MMR_MIN_AGE_MONTHS[nextSequence])
74+
75+
if (isBefore(createdAt, minAgeDate)) {
76+
const reason =
7377
nextSequence === 1
7478
? REASON.BeforeAge12Months
7579
: REASON.BeforeAge15Months
76-
})
77-
continue
80+
ignoredDoses.push({ vaccination, reason })
81+
recordedDoses.push({ vaccination, kind: 'ignored', reason })
82+
continue
83+
}
7884
}
7985

8086
if (lastValidCreatedAt) {
8187
const gap = differenceInCalendarDays(createdAt, lastValidCreatedAt)
8288
if (gap < MMR_MIN_INTERVAL_DAYS) {
83-
ignoredDoses.push({
84-
vaccination,
85-
reason: REASON.LessThan28DaysAfterPrevious
86-
})
89+
const reason = REASON.LessThan28DaysAfterPrevious
90+
ignoredDoses.push({ vaccination, reason })
91+
recordedDoses.push({ vaccination, kind: 'ignored', reason })
8792
continue
8893
}
8994
}
9095

96+
if (atScheduleMax) {
97+
const reason = REASON.ExtraDose
98+
ignoredDoses.push({ vaccination, reason })
99+
recordedDoses.push({ vaccination, kind: 'ignored', reason })
100+
continue
101+
}
102+
91103
validDoses.push({ vaccination, sequence: nextSequence })
104+
recordedDoses.push({ vaccination, kind: 'valid', sequence: nextSequence })
92105
lastValidCreatedAt = createdAt
93106
}
94107

@@ -101,5 +114,5 @@ export function getScheduleSummary({
101114
: minAgeDate
102115
}
103116

104-
return { validDoses, ignoredDoses, nextEligibleFrom }
117+
return { validDoses, ignoredDoses, recordedDoses, nextEligibleFrom }
105118
}

app/views/patient/_vaccination-record.njk

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,63 +15,43 @@
1515

1616
{% set rows = [] %}
1717

18-
{% for ignored in summary.ignoredDoses %}
19-
{% set ignoredLabelHtml %}
20-
<span>
21-
{{- tag({
22-
text: __("patientProgramme.vaccinationRecord.ignored.label"),
23-
classes: "nhsuk-tag--grey"
24-
}) -}}
25-
<br>
26-
<span class="nhsuk-u-secondary-text-colour nhsuk-u-font-size-16">{{ ignored.reason }}</span>
27-
</span>
28-
{% endset %}
29-
{% set rows = rows | push([
30-
{
31-
header: __("patientProgramme.vaccinationRecord.dose.label"),
32-
html: ignoredLabelHtml
33-
},
34-
{
35-
header: __("vaccination.createdAt.label"),
36-
html: ignored.vaccination.link.createdAt
37-
},
38-
{
39-
header: __("vaccination.age.label"),
40-
text: patient.dob | age(at=ignored.vaccination.createdAt)
41-
},
42-
{
43-
header: __("vaccination.source.label"),
44-
text: ignored.vaccination.source
45-
}
46-
]) %}
47-
{% endfor %}
48-
49-
{% for valid in summary.validDoses %}
50-
{% set validDoseHtml %}
51-
{{ __mf("patientProgramme.vaccinationRecord.dose.number", {
52-
sequence: valid.sequence
53-
}) }}
54-
{% if valid.vaccination.vaccine.type %}
55-
<br>
56-
<span class="nhsuk-u-secondary-text-colour nhsuk-u-font-size-16">{{ valid.vaccination.vaccine.type }}</span>
18+
{% for recorded in summary.recordedDoses %}
19+
{% set doseCellHtml %}
20+
{% if recorded.kind == "ignored" %}
21+
<span>
22+
{{- tag({
23+
text: __("patientProgramme.vaccinationRecord.ignored.label"),
24+
classes: "nhsuk-tag--grey"
25+
}) -}}
26+
<br>
27+
<span class="nhsuk-u-secondary-text-colour nhsuk-u-font-size-16">{{ recorded.reason }}</span>
28+
</span>
29+
{% else %}
30+
{{ __mf("patientProgramme.vaccinationRecord.dose.number", {
31+
sequence: recorded.sequence
32+
}) }}
33+
{% if recorded.vaccination.vaccine.type %}
34+
<br>
35+
<span class="nhsuk-u-secondary-text-colour nhsuk-u-font-size-16">{{ recorded.vaccination.vaccine.type }}</span>
36+
{% endif %}
5737
{% endif %}
5838
{% endset %}
5939
{% set rows = rows | push([
6040
{
6141
header: __("patientProgramme.vaccinationRecord.dose.label"),
62-
html: validDoseHtml
42+
html: doseCellHtml
6343
},
6444
{
6545
header: __("vaccination.createdAt.label"),
66-
html: valid.vaccination.link.createdAt
46+
html: recorded.vaccination.link.createdAt
6747
},
6848
{
6949
header: __("vaccination.age.label"),
70-
text: patient.dob | age(at=valid.vaccination.createdAt)
50+
text: patient.dob | age(at=recorded.vaccination.createdAt)
7151
},
7252
{
7353
header: __("vaccination.source.label"),
74-
text: valid.vaccination.source
54+
text: recorded.vaccination.source
7555
}
7656
]) %}
7757
{% endfor %}

0 commit comments

Comments
 (0)