Skip to content

Commit 2216712

Browse files
authored
Merge pull request #6400 from NHSDigital/slim-down-govuk-notify-personalisation
Slim down GovukNotifyPersonalisation
2 parents a7b9d7f + c730444 commit 2216712

11 files changed

Lines changed: 237 additions & 298 deletions

app/components/app_triage_form_component.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def initialize(form, url:, method: :post, heading: true, continue: false)
1919
def builder = MavisFormBuilder
2020

2121
def hint_text
22-
if programme.mmr? && patient_eligible_for_additional_dose?
22+
if programme.mmr? && patient_on_last_dose?
2323
"2nd dose is not due until #{form.next_mmr_dose_date.to_fs(:long)}"
2424
else
2525
"For example, #{hint_date.to_fs(:long)} "
@@ -51,13 +51,10 @@ def fieldset_options
5151
end
5252
end
5353

54-
def patient_eligible_for_additional_dose?
55-
next_dose =
56-
patient.programme_status(
57-
programme,
58-
academic_year: session.academic_year
59-
).dose_sequence
60-
61-
next_dose == programme.maximum_dose_sequence
54+
def patient_on_last_dose?
55+
patient.programme_status(
56+
programme,
57+
academic_year: session.academic_year
58+
).on_last_dose?
6259
end
6360
end

app/forms/triage_form.rb

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,7 @@ def show_add_patient_specific_direction?(option)
9494
end
9595

9696
def next_mmr_dose_date
97-
programme_status = patient.programme_status(programme, academic_year:)
98-
99-
if programme_status.cannot_vaccinate_delay_vaccination?
100-
programme_status.date
101-
elsif (first_dose_date = programme_status.date)
102-
(first_dose_date + 28.days).to_date
103-
end
97+
patient.programme_status(programme, academic_year:).next_dose_eligible_date
10498
end
10599

106100
private
@@ -256,7 +250,7 @@ def validate_delay_vaccination_until_date
256250
)
257251
end
258252

259-
if programme.mmr? && patient_eligible_for_additional_dose? &&
253+
if programme.mmr? && patient_on_last_dose? &&
260254
delay_vaccination_until < next_mmr_dose_date
261255
errors.add(
262256
:delay_vaccination_until,
@@ -276,13 +270,10 @@ def associate_triage_with_vaccination_record(next_dose_delay_triage)
276270
vaccination_record.presence&.update!(next_dose_delay_triage:)
277271
end
278272

279-
def patient_eligible_for_additional_dose?
280-
next_dose =
281-
patient.programme_status(
282-
programme,
283-
academic_year: session.academic_year
284-
).dose_sequence
285-
286-
next_dose == programme.maximum_dose_sequence
273+
def patient_on_last_dose?
274+
patient.programme_status(
275+
programme,
276+
academic_year: session.academic_year
277+
).on_last_dose?
287278
end
288279
end

app/lib/govuk_notify_personalisation.rb

Lines changed: 17 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -49,53 +49,6 @@ def initialize(
4949
end
5050
end
5151

52-
def to_h
53-
{
54-
batch_name:,
55-
consent_deadline:,
56-
consent_link:,
57-
consented_vaccine_methods_message:,
58-
day_month_year_of_vaccination:,
59-
delay_vaccination_review_context:,
60-
full_and_preferred_patient_name:,
61-
invitation_to_clinic_generic_message:,
62-
location_name:,
63-
invitation_to_clinic_custom_mmr_message:,
64-
mmr_or_mmrv_vaccine:,
65-
mmr_second_dose_message:,
66-
mmr_second_dose_required:,
67-
next_or_today_session_date:,
68-
next_or_today_session_dates:,
69-
next_or_today_session_dates_or:,
70-
next_session_date:,
71-
next_session_dates:,
72-
next_session_dates_or:,
73-
patient_date_of_birth:,
74-
reason_did_not_vaccinate:,
75-
reason_for_refusal:,
76-
short_patient_name:,
77-
short_patient_name_apos:,
78-
subsequent_session_dates_offered_message:,
79-
subteam_email:,
80-
subteam_name:,
81-
subteam_phone:,
82-
survey_deadline_date:,
83-
talk_to_your_child_message:,
84-
team_privacy_notice_url:,
85-
team_privacy_policy_url:,
86-
today_or_date_of_vaccination:,
87-
vaccination:,
88-
vaccination_and_dates:,
89-
vaccination_and_dates_sms:,
90-
vaccination_and_method:,
91-
vaccine:,
92-
vaccine_and_dose:,
93-
vaccine_and_method:,
94-
vaccine_brand:,
95-
vaccine_side_effects:
96-
}.compact
97-
end
98-
9952
attr_reader :academic_year,
10053
:consent,
10154
:consent_form,
@@ -113,14 +66,7 @@ def batch_name
11366
end
11467

11568
def consent_deadline
116-
return nil if session.nil?
117-
118-
next_date = session.future_dates.first
119-
120-
close_consent_at =
121-
next_date ? (next_date - 1.day) : session.close_consent_at
122-
123-
close_consent_at&.to_fs(:short_day_of_week)
69+
session&.consent_deadline_date&.to_fs(:short_day_of_week)
12470
end
12571

12672
def consent_link
@@ -206,7 +152,7 @@ def mmr_second_dose_message
206152
end
207153

208154
def mmr_second_dose_required?
209-
mmr_programme.present? && patient_eligible_for_additional_dose?
155+
mmr_programme.present? && patient_on_last_dose?
210156
end
211157

212158
def mmr_second_dose_required
@@ -259,29 +205,17 @@ def next_mmr_dose_date
259205
return if patient.nil?
260206
return if mmr_programme.nil?
261207

262-
programme_status = patient.programme_status(mmr_programme, academic_year:)
263-
264-
date =
265-
if programme_status.cannot_vaccinate_delay_vaccination?
266-
programme_status.date
267-
elsif (first_dose_date = programme_status.date)
268-
(first_dose_date + 28.days).to_date
269-
end
270-
271-
date.to_fs(:long)
208+
patient
209+
.programme_status(mmr_programme, academic_year:)
210+
.next_dose_eligible_date
211+
&.to_fs(:long)
272212
end
273213

274-
def patient_eligible_for_additional_dose?
214+
def patient_on_last_dose?
275215
return unless patient
276216
return if mmr_programme.nil?
277217

278-
next_dose =
279-
patient
280-
.reload
281-
.programme_status(mmr_programme, academic_year:)
282-
.dose_sequence
283-
284-
next_dose == mmr_programme.maximum_dose_sequence
218+
patient.reload.programme_status(mmr_programme, academic_year:).on_last_dose?
285219
end
286220

287221
def mmr_or_mmrv_vaccine
@@ -302,13 +236,7 @@ def delay_vaccination_review_context
302236
return if patient.nil? || session.nil?
303237

304238
latest_delayed_triage =
305-
patient
306-
.triages
307-
.not_invalidated
308-
.where(programme_type: session.programme_types)
309-
.delay_vaccination
310-
.order(created_at: :desc)
311-
.first
239+
patient.latest_delayed_triage(programme_types: session.programme_types)
312240

313241
return if latest_delayed_triage.nil?
314242

@@ -393,12 +321,7 @@ def reason_for_refusal
393321
end
394322

395323
def short_patient_name
396-
[
397-
consent_form&.preferred_given_name,
398-
consent_form&.given_name,
399-
patient&.preferred_given_name,
400-
patient&.given_name
401-
].compact_blank.first
324+
(consent_form || patient)&.short_name
402325
end
403326

404327
def short_patient_name_apos
@@ -532,13 +455,8 @@ def vaccine_is?(method)
532455
elsif programmes.present?
533456
if patient
534457
programmes.any? do |programme|
535-
# We pick the first method as it's the one most likely to be used
536-
# to vaccinate the patient. For example, in the case of Flu, the
537-
# parents will approve nasal (and then optionally injection).
538-
patient
539-
.vaccine_criteria(programme:, academic_year:)
540-
.vaccine_methods
541-
.first == method
458+
patient.vaccine_criteria(programme:, academic_year:).primary_method ==
459+
method
542460
end
543461
else
544462
Vaccine.for_programmes(programmes).exists?(method:)
@@ -553,18 +471,7 @@ def vaccine_side_effects
553471
elsif programmes.present?
554472
if patient
555473
programmes.flat_map do |programme|
556-
# We pick the first method as it's the one most likely to be used
557-
# to vaccinate the patient. For example, in the case of Flu, the
558-
# parents will approve nasal (and then optionally injection).
559-
method =
560-
patient
561-
.vaccine_criteria(programme:, academic_year:)
562-
.vaccine_methods
563-
.first
564-
Vaccine
565-
.for_programme(programme)
566-
.where(method:)
567-
.flat_map(&:side_effects)
474+
patient.vaccine_criteria(programme:, academic_year:).side_effects
568475
end
569476
else
570477
Vaccine.for_programmes(programmes).flat_map(&:side_effects)
@@ -620,10 +527,10 @@ def programme_names_and_methods
620527
vaccination_record.delivery_method
621528
)
622529
elsif patient
623-
patient
624-
.vaccine_criteria(programme:, academic_year:)
625-
.vaccine_methods
626-
.first
530+
patient.vaccine_criteria(
531+
programme:,
532+
academic_year:
533+
).primary_method
627534
end
628535

629536
method_prefix =

app/lib/notifier/consent.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def send_triage_email(triage, params)
4141

4242
def triage_email_template(triage, session)
4343
if triage.safe_to_vaccinate?
44-
if programme.mmr? && patient_eligible_for_additional_dose?(session)
44+
if programme.mmr? && patient_on_last_dose?(session)
4545
:triage_vaccination_will_happen_mmr_second_dose
4646
else
4747
:triage_vaccination_will_happen
@@ -80,13 +80,10 @@ def resolve_email_template(template_name, team)
8080
template_names.find { NotifyTemplate.exists?(it, channel: :email) }
8181
end
8282

83-
def patient_eligible_for_additional_dose?(session)
84-
next_dose =
85-
patient
86-
.reload
87-
.programme_status(programme, academic_year: session.academic_year)
88-
.dose_sequence
89-
90-
next_dose == programme.maximum_dose_sequence
83+
def patient_on_last_dose?(session)
84+
patient
85+
.reload
86+
.programme_status(programme, academic_year: session.academic_year)
87+
.on_last_dose?
9188
end
9289
end

app/lib/vaccine_criteria.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ def self.from_programme_status(programme_status)
3636

3737
attr_reader :programme, :vaccine_methods, :without_gelatine
3838

39+
# The first method is the one most likely to be used to vaccinate the
40+
# patient. For example, in the case of Flu, the parents will approve
41+
# nasal (and then optionally injection).
42+
def primary_method
43+
vaccine_methods&.first
44+
end
45+
46+
def side_effects
47+
Vaccine
48+
.for_programme(programme)
49+
.where(method: primary_method)
50+
.flat_map(&:side_effects)
51+
end
52+
3953
def apply(scope)
4054
scope = scope.with_disease_types(programme.disease_types)
4155

app/models/concerns/consentable.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ def close_consent_at
1010
dates.max - 1.day
1111
end
1212

13+
def consent_deadline_date
14+
next_date = future_dates.first
15+
next_date ? (next_date - 1.day) : close_consent_at
16+
end
17+
1318
def can_receive_consent?
1419
!close_consent_at.nil? && Date.current <= close_consent_at
1520
end

app/models/concerns/full_name_concern.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ def full_name(context: :internal)
1212
FullNameFormatter.call(self, context:)
1313
end
1414

15+
def short_name
16+
preferred_given_name.presence || given_name
17+
end
18+
1519
def has_preferred_name?
1620
preferred_given_name.present? || preferred_family_name.present?
1721
end

app/models/patient.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,15 @@ def vaccine_criteria(programme:, academic_year:)
578578
programme_status(programme, academic_year:).vaccine_criteria
579579
end
580580

581+
def latest_delayed_triage(programme_types:)
582+
triages
583+
.not_invalidated
584+
.where(programme_type: programme_types)
585+
.delay_vaccination
586+
.order(created_at: :desc)
587+
.first
588+
end
589+
581590
def eligible_for_mmrv?
582591
date_of_birth >= Programme::MIN_MMRV_ELIGIBILITY_DATE
583592
end

app/models/patient/programme_status.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,21 @@ def assign
161161

162162
def vaccine_criteria = VaccineCriteria.from_programme_status(self)
163163

164+
def on_last_dose?
165+
dose_sequence == Programme.find(programme_type).maximum_dose_sequence
166+
end
167+
168+
def next_dose_eligible_date
169+
interval = Programme.find(programme_type).minimum_dose_interval
170+
return unless interval
171+
172+
if cannot_vaccinate_delay_vaccination?
173+
date
174+
elsif date
175+
(date + interval).to_date
176+
end
177+
end
178+
164179
private
165180

166181
def generator

0 commit comments

Comments
 (0)