Skip to content

Commit 9436104

Browse files
authored
Merge pull request #3941 from nhsuk/refactor-delivery-vaccine-methods
Refactor methods related to delivery and vaccine methods
2 parents 3981776 + 7ddedc6 commit 9436104

7 files changed

Lines changed: 36 additions & 40 deletions

File tree

app/components/app_programme_status_tags_component.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def call
1313
programme_statuses.map do |programme, hash|
1414
status = hash[:status]
1515
vaccine_methods =
16-
(hash[:vaccine_methods] if programme.has_multiple_delivery_methods?)
16+
(hash[:vaccine_methods] if programme.has_multiple_vaccine_methods?)
1717
programme_status_tag(programme, status, vaccine_methods)
1818
end
1919
)

app/components/app_triage_form_component.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def builder = GOVUKDesignSystemFormBuilder::FormBuilder
2929
def fieldset_options
3030
text = "Is it safe to vaccinate #{patient.given_name}?"
3131
hint =
32-
if programme.has_multiple_delivery_methods?
32+
if programme.has_multiple_vaccine_methods?
3333
if triage_form.consented_to_injection_only?
3434
"The parent has consented to the injected vaccine only"
3535
elsif triage_form.consented_to_injection?

app/forms/triage_form.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def save!
4141

4242
def status_and_vaccine_method_options
4343
safe_to_vaccinate_choices =
44-
if programme.has_multiple_delivery_methods?
44+
if programme.has_multiple_vaccine_methods?
4545
consented_vaccine_methods.map { |method| "safe_to_vaccinate_#{method}" }
4646
else
4747
["safe_to_vaccinate"]

app/helpers/consents_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def consent_status_tag(consent)
2222

2323
vaccine_method =
2424
if consent.vaccine_methods.present? &&
25-
consent.programme.has_multiple_delivery_methods?
25+
consent.programme.has_multiple_vaccine_methods?
2626
tag.span(
2727
Vaccine.human_enum_name(:method, consent.vaccine_methods.join("_")),
2828
class: "nhsuk-u-secondary-text-color"

app/models/draft_vaccination_record.rb

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ def self.request_session_key
3838
if: :performed_by_user
3939
}
4040

41-
INJECTION_DELIVERY_METHODS =
42-
Vaccine::AVAILABLE_DELIVERY_METHODS["injection"].freeze
43-
4441
def wizard_steps
4542
[
4643
:notes,
@@ -207,8 +204,9 @@ def delivery_method=(value)
207204
super
208205
return if delivery_method_was.nil? # Don't clear batch on first set
209206

210-
previous_value = compute_vaccine_method(delivery_method_was)
211-
new_value = compute_vaccine_method(value)
207+
previous_value =
208+
Vaccine.delivery_method_to_vaccine_method(delivery_method_was)
209+
new_value = Vaccine.delivery_method_to_vaccine_method(value)
212210

213211
self.batch_id = nil unless previous_value == new_value
214212
end
@@ -248,29 +246,13 @@ def vaccine_method_matches_consent_and_triage?
248246
return true if delivery_method.blank? || !administered?
249247

250248
approved_methods = patient.approved_vaccine_methods(programme:)
251-
vaccine_method = delivery_method_to_vaccine_method(delivery_method)
249+
vaccine_method = Vaccine.delivery_method_to_vaccine_method(delivery_method)
252250

253251
approved_methods.include?(vaccine_method)
254252
end
255253

256254
private
257255

258-
def delivery_method_to_vaccine_method(delivery_method)
259-
return nil if delivery_method.nil?
260-
261-
delivery_method.in?(INJECTION_DELIVERY_METHODS) ? "injection" : "nasal"
262-
end
263-
264-
def compute_vaccine_method(delivery_method)
265-
return nil if delivery_method.nil?
266-
267-
if delivery_method.in?(INJECTION_DELIVERY_METHODS)
268-
"injection"
269-
else
270-
"nasal_spray"
271-
end
272-
end
273-
274256
def readable_attribute_names
275257
writable_attribute_names - %w[vaccine_id]
276258
end
@@ -328,11 +310,11 @@ def delivery_site_matches_delivery_method
328310
end
329311

330312
case delivery_method
331-
when "nasal_spray"
313+
when *Vaccine::NASAL_DELIVERY_METHODS
332314
if delivery_site != "nose"
333315
errors.add(:delivery_site, :nasal_spray_must_be_nose)
334316
end
335-
when *INJECTION_DELIVERY_METHODS
317+
when *Vaccine::INJECTION_DELIVERY_METHODS
336318
if delivery_site == "nose"
337319
errors.add(:delivery_site, :injection_cannot_be_nose)
338320
end

app/models/programme.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,24 @@ def birth_academic_years
7575

7676
def vaccine_methods = vaccines.map(&:method).uniq
7777

78-
def available_delivery_methods
79-
vaccines.flat_map(&:available_delivery_methods).uniq
80-
end
81-
82-
def available_delivery_sites
83-
vaccines.flat_map(&:available_delivery_sites).uniq
84-
end
85-
86-
def has_multiple_delivery_methods?
78+
def has_multiple_vaccine_methods?
8779
# TODO: Ideally this would work as below, however that doesn't work well
8880
# in a list as it results in N+1 issues, without deeply pre-fetching
8981
# the vaccines which is a lot of data.
9082

91-
# available_delivery_methods.length > 1
83+
# vaccine_methods.length > 1
9284

9385
flu?
9486
end
9587

88+
def available_delivery_methods
89+
vaccines.flat_map(&:available_delivery_methods).uniq
90+
end
91+
92+
def available_delivery_sites
93+
vaccines.flat_map(&:available_delivery_sites).uniq
94+
end
95+
9696
DOSE_SEQUENCES = {
9797
"flu" => 1,
9898
"hpv" => 1,

app/models/vaccine.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,29 @@ def available_delivery_sites
7979
AVAILABLE_DELIVERY_SITES.fetch(method)
8080
end
8181

82+
NASAL_DELIVERY_METHODS = %w[nasal_spray].freeze
83+
INJECTION_DELIVERY_METHODS = %w[intramuscular subcutaneous].freeze
84+
8285
AVAILABLE_DELIVERY_METHODS = {
83-
"nasal" => %w[nasal_spray],
84-
"injection" => %w[intramuscular subcutaneous]
86+
"nasal" => NASAL_DELIVERY_METHODS,
87+
"injection" => INJECTION_DELIVERY_METHODS
8588
}.freeze
8689

8790
def available_delivery_methods
8891
AVAILABLE_DELIVERY_METHODS.fetch(method)
8992
end
9093

94+
def self.delivery_method_to_vaccine_method(delivery_method)
95+
return nil if delivery_method.nil?
96+
97+
suitable_delivery_methods =
98+
AVAILABLE_DELIVERY_METHODS.select do |_key, value|
99+
delivery_method.in?(value)
100+
end
101+
102+
suitable_delivery_methods.keys.first
103+
end
104+
91105
private
92106

93107
def fhir_mapper = @fhir_mapper ||= FHIRMapper::Vaccine.new(self)

0 commit comments

Comments
 (0)