Skip to content

Commit e51df32

Browse files
committed
Refactor AppVaccinateFormComponent
This refactors the component to be based around the vaccine method rather than the delivery method. This allows us to simplify the code while also preparing for a future feature which allows nurses to choose from multiple vaccine methods. Jira-Issue: MAV-1338
1 parent 5037052 commit e51df32

3 files changed

Lines changed: 23 additions & 27 deletions

File tree

app/components/app_vaccinate_form_component.rb

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@ def url
1818
session_patient_programme_vaccinations_path(session, patient, programme)
1919
end
2020

21+
def vaccine_method
22+
patient.approved_vaccine_methods(programme:).first
23+
end
24+
2125
def delivery_method
22-
if patient.approved_vaccine_methods(programme:).include?("nasal")
23-
"nasal_spray"
24-
else
25-
"intramuscular"
26-
end
26+
Vaccine::AVAILABLE_DELIVERY_METHODS.fetch(vaccine_method).first
2727
end
2828

2929
def dose_sequence
3030
programme.default_dose_sequence
3131
end
3232

3333
COMMON_DELIVERY_SITES = {
34-
"intramuscular" => %w[left_arm_upper_position right_arm_upper_position],
35-
"nasal_spray" => %w[nose]
34+
"injection" => %w[left_arm_upper_position right_arm_upper_position],
35+
"nasal" => %w[nose]
3636
}.freeze
3737

3838
CommonDeliverySite = Struct.new(:value, :label)
@@ -42,13 +42,13 @@ def common_delivery_sites_options
4242
begin
4343
options =
4444
COMMON_DELIVERY_SITES
45-
.fetch(delivery_method)
45+
.fetch(vaccine_method)
4646
.map do |value|
4747
label = VaccinationRecord.human_enum_name(:delivery_site, value)
4848
CommonDeliverySite.new(value:, label:)
4949
end
5050

51-
if delivery_method.in?(Vaccine::INJECTION_DELIVERY_METHODS)
51+
if vaccine_method == "injection"
5252
options << CommonDeliverySite.new(value: "other", label: "Other")
5353
end
5454

@@ -59,8 +59,6 @@ def common_delivery_sites_options
5959
def vaccination_name
6060
vaccination =
6161
if programme.has_multiple_vaccine_methods?
62-
vaccine_method =
63-
Vaccine.delivery_method_to_vaccine_method(delivery_method)
6462
Vaccine.human_enum_name(:method, vaccine_method).downcase
6563
else
6664
"vaccination"
@@ -73,6 +71,5 @@ def ask_not_taking_medication? = programme.doubles? || programme.flu?
7371

7472
def ask_not_pregnant? = programme.td_ipv?
7573

76-
def ask_asthma_flare_up? =
77-
delivery_method.in?(Vaccine::NASAL_DELIVERY_METHODS)
74+
def ask_asthma_flare_up? = vaccine_method == "nasal"
7875
end

app/models/draft_vaccination_record.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,12 @@ def reset_unused_fields
296296
end
297297
end
298298

299-
def can_be_half_dose?
300-
delivery_method.in?(Vaccine::NASAL_DELIVERY_METHODS)
299+
def vaccine_method
300+
Vaccine.delivery_method_to_vaccine_method(delivery_method)
301301
end
302302

303+
def can_be_half_dose? = vaccine_method == "nasal"
304+
303305
def can_change_outcome?
304306
outcome != "already_had" || editing? || session.nil? || session.today?
305307
end
@@ -312,14 +314,14 @@ def delivery_site_matches_delivery_method
312314
return
313315
end
314316

315-
case delivery_method
316-
when *Vaccine::NASAL_DELIVERY_METHODS
317-
if delivery_site != "nose"
318-
errors.add(:delivery_site, :nasal_spray_must_be_nose)
319-
end
320-
when *Vaccine::INJECTION_DELIVERY_METHODS
321-
if delivery_site == "nose"
317+
allowed_delivery_sites =
318+
Vaccine::AVAILABLE_DELIVERY_SITES.fetch(vaccine_method)
319+
320+
unless delivery_site.in?(allowed_delivery_sites)
321+
if vaccine_method == "injection"
322322
errors.add(:delivery_site, :injection_cannot_be_nose)
323+
else
324+
errors.add(:delivery_site, :nasal_spray_must_be_nose)
323325
end
324326
end
325327

app/models/vaccine.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,9 @@ def available_delivery_sites
7777
AVAILABLE_DELIVERY_SITES.fetch(method)
7878
end
7979

80-
NASAL_DELIVERY_METHODS = %w[nasal_spray].freeze
81-
INJECTION_DELIVERY_METHODS = %w[intramuscular subcutaneous].freeze
82-
8380
AVAILABLE_DELIVERY_METHODS = {
84-
"nasal" => NASAL_DELIVERY_METHODS,
85-
"injection" => INJECTION_DELIVERY_METHODS
81+
"nasal" => %w[nasal_spray].freeze,
82+
"injection" => %w[intramuscular subcutaneous].freeze
8683
}.freeze
8784

8885
def available_delivery_methods

0 commit comments

Comments
 (0)