Skip to content

Commit e071ede

Browse files
authored
Validate individual pre-screening questions (#3252)
When recording a vaccination, this ensures that each answer to the pre-screening questions is validated individually so the user understands why a child cannot be vaccinated if the pre-screening requirements are not valid. ## Screenshot <img width="758" alt="Screenshot 2025-03-18 at 13 16 31" src="https://github.com/user-attachments/assets/82547b16-9a49-41eb-b9b3-753a912f5a5a" />
2 parents db01405 + 3b7a12f commit e071ede

11 files changed

Lines changed: 71 additions & 99 deletions

app/components/app_patient_page_component.html.erb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,5 @@
121121
<% end %>
122122

123123
<% if helpers.policy(VaccinationRecord).create? %>
124-
<%= render AppVaccinateFormComponent.new(
125-
patient_session:,
126-
programme:,
127-
vaccinate_form:,
128-
) %>
124+
<%= render AppVaccinateFormComponent.new(vaccinate_form) %>
129125
<% end %>

app/components/app_patient_page_component.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ def default_vaccinate_form
6565
.first
6666

6767
VaccinateForm.new(
68+
patient_session:,
69+
programme:,
6870
feeling_well: pre_screening&.feeling_well,
6971
not_pregnant: pre_screening&.not_pregnant
7072
)

app/components/app_vaccinate_form_component.html.erb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,30 @@
1212
<%= patient.given_name %> has confirmed that they:
1313
</h2>
1414

15-
<%= f.govuk_check_boxes_fieldset :pre_screening, multiple: false, legend: nil do %>
15+
<%= f.govuk_check_boxes_fieldset :knows_vaccination, multiple: false, legend: nil do %>
1616
<%= f.govuk_check_box :knows_vaccination, true, multiple: false, link_errors: true %>
17+
<% end %>
1718

19+
<%= f.govuk_check_boxes_fieldset :not_already_had, multiple: false, legend: nil do %>
1820
<%= f.govuk_check_box :not_already_had, true, multiple: false, link_errors: true %>
21+
<% end %>
1922

23+
<%= f.govuk_check_boxes_fieldset :feeling_well, multiple: false, legend: nil do %>
2024
<%= f.govuk_check_box :feeling_well, true, multiple: false, link_errors: true %>
25+
<% end %>
2126

27+
<%= f.govuk_check_boxes_fieldset :no_allergies, multiple: false, legend: nil do %>
2228
<%= f.govuk_check_box :no_allergies, true, multiple: false, link_errors: true %>
29+
<% end %>
2330

24-
<% if PreScreening.ask_not_taking_medication?(programme:) %>
31+
<% if vaccinate_form.ask_not_taking_medication? %>
32+
<%= f.govuk_check_boxes_fieldset :not_taking_medication, multiple: false, legend: nil do %>
2533
<%= f.govuk_check_box :not_taking_medication, true, multiple: false, link_errors: true %>
2634
<% end %>
35+
<% end %>
2736

28-
<% if PreScreening.ask_not_pregnant?(programme:) %>
37+
<% if vaccinate_form.ask_not_pregnant? %>
38+
<%= f.govuk_check_boxes_fieldset :not_pregnant, multiple: false, legend: nil do %>
2939
<%= f.govuk_check_box :not_pregnant, true, multiple: false, link_errors: true %>
3040
<% end %>
3141
<% end %>
@@ -54,7 +64,6 @@
5464

5565
<%= f.hidden_field :delivery_method, value: delivery_method %>
5666
<%= f.hidden_field :dose_sequence, value: dose_sequence %>
57-
<%= f.hidden_field :programme_id, value: programme.id %>
5867
<%= f.hidden_field :vaccine_id, value: vaccine.id %>
5968

6069
<%= f.govuk_submit "Continue" %>

app/components/app_vaccinate_form_component.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
# frozen_string_literal: true
22

33
class AppVaccinateFormComponent < ViewComponent::Base
4-
def initialize(patient_session:, programme:, vaccinate_form:)
4+
def initialize(vaccinate_form)
55
super
66

7-
@patient_session = patient_session
8-
@programme = programme
97
@vaccinate_form = vaccinate_form
108
end
119

@@ -19,8 +17,9 @@ def render?
1917

2018
private
2119

22-
attr_reader :patient_session, :programme, :vaccinate_form
20+
attr_reader :vaccinate_form
2321

22+
delegate :patient_session, :programme, to: :vaccinate_form
2423
delegate :patient, :session, to: :patient_session
2524

2625
def url

app/controllers/vaccinations_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ def create
1717

1818
@vaccinate_form =
1919
VaccinateForm.new(
20-
patient_session: @patient_session,
2120
current_user:,
21+
patient_session: @patient_session,
22+
programme: @programme,
2223
todays_batch: @todays_batch,
2324
**vaccinate_form_params
2425
)
@@ -61,7 +62,6 @@ def vaccinate_form_params
6162
not_pregnant
6263
not_taking_medication
6364
pre_screening_notes
64-
programme_id
6565
vaccine_id
6666
]
6767
)

app/forms/vaccinate_form.rb

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class VaccinateForm
44
include ActiveModel::Model
55
include ActiveModel::Attributes
66

7-
attr_accessor :patient_session, :current_user, :todays_batch
7+
attr_accessor :patient_session, :programme, :current_user, :todays_batch
88

99
attribute :knows_vaccination, :boolean
1010
attribute :not_already_had, :boolean
@@ -18,18 +18,23 @@ class VaccinateForm
1818
attribute :delivery_method, :string
1919
attribute :delivery_site, :string
2020
attribute :dose_sequence, :integer
21-
attribute :programme_id, :integer
2221
attribute :vaccine_id, :integer
2322

24-
validates :knows_vaccination, inclusion: { in: [true, nil] }
25-
validates :not_already_had, inclusion: { in: [true, nil] }
26-
validates :feeling_well, inclusion: { in: [true, nil] }
27-
validates :no_allergies, inclusion: { in: [true, nil] }
28-
validates :not_taking_medication, inclusion: { in: [true, nil] }
29-
validates :not_pregnant, inclusion: { in: [true, nil] }
23+
validates :administered, inclusion: [true, false]
3024

31-
validate :valid_administered_values
32-
validates :programme_id, presence: true
25+
with_options if: :administered do
26+
validates :knows_vaccination, presence: true
27+
validates :not_already_had, presence: true
28+
validates :no_allergies, presence: true
29+
end
30+
31+
with_options if: -> { administered && ask_not_taking_medication? } do
32+
validates :not_taking_medication, presence: true
33+
end
34+
35+
with_options if: -> { administered && ask_not_pregnant? } do
36+
validates :not_pregnant, presence: true
37+
end
3338

3439
with_options if: :administered do
3540
validates :delivery_method, presence: true
@@ -59,13 +64,21 @@ def save(draft_vaccination_record:)
5964
draft_vaccination_record.performed_at = Time.current
6065
draft_vaccination_record.performed_by_user = current_user
6166
draft_vaccination_record.performed_ods_code = organisation.ods_code
62-
draft_vaccination_record.programme_id = programme_id
67+
draft_vaccination_record.programme = programme
6368
draft_vaccination_record.session_id = patient_session.session_id
6469
draft_vaccination_record.vaccine_id = vaccine_id
6570

6671
draft_vaccination_record.save # rubocop:disable Rails/SaveBang
6772
end
6873

74+
def ask_not_taking_medication?
75+
programme.doubles?
76+
end
77+
78+
def ask_not_pregnant?
79+
programme.hpv? || programme.td_ipv?
80+
end
81+
6982
private
7083

7184
delegate :organisation, to: :patient_session
@@ -81,25 +94,12 @@ def pre_screening
8194
not_taking_medication: not_taking_medication || false,
8295
notes: pre_screening_notes,
8396
performed_by: current_user,
84-
programme_id:,
97+
programme:,
8598
session_date_id:
8699
)
87100
end
88101

89102
def session_date_id
90103
patient_session.session.session_dates.today.first&.id
91104
end
92-
93-
def valid_administered_values
94-
if administered.nil?
95-
errors.add(:administered, "Choose if they are ready to vaccinate")
96-
end
97-
98-
vaccination_allowed =
99-
pre_screening.invalid? || pre_screening.allows_vaccination?
100-
101-
if administered && !vaccination_allowed
102-
errors.add(:administered, "Patient should not be vaccinated")
103-
end
104-
end
105105
end

app/models/pre_screening.rb

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,4 @@ class PreScreening < ApplicationRecord
4646
has_one :patient, through: :patient_session
4747

4848
encrypts :notes
49-
50-
validates :knows_vaccination,
51-
:not_already_had,
52-
:feeling_well,
53-
:no_allergies,
54-
:not_taking_medication,
55-
:not_pregnant,
56-
inclusion: {
57-
in: [true, false]
58-
}
59-
60-
def allows_vaccination?
61-
knows_vaccination && not_already_had && no_allergies &&
62-
(
63-
!PreScreening.ask_not_taking_medication?(programme:) ||
64-
not_taking_medication
65-
) && (!PreScreening.ask_not_pregnant?(programme:) || not_pregnant)
66-
end
67-
68-
def self.ask_not_taking_medication?(programme:)
69-
programme.doubles?
70-
end
71-
72-
def self.ask_not_pregnant?(programme:)
73-
programme.hpv? || programme.td_ipv?
74-
end
7549
end

config/locales/en.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,22 @@ en:
108108
inclusion: You cannot remove a programme from the session once it has been added
109109
vaccinate_form:
110110
attributes:
111+
administered:
112+
inclusion: Choose if they are ready to vaccinate
111113
delivery_site:
112114
blank: Choose where the injection will be given
115+
feeling_well:
116+
blank: Confirm that they are feeling well
117+
knows_vaccination:
118+
blank: Confirm that they know what the vaccination is for, and are happy to have it
119+
no_allergies:
120+
blank: Confirm that they have no allergies which would prevent vaccination
121+
not_already_had:
122+
blank: Confirm that they have not already had the vaccination
123+
not_pregnant:
124+
blank: Confirm that they are not pregnant
125+
not_taking_medication:
126+
blank: Confirm that they are not taking any medication which prevents vaccination
113127
vaccination_report:
114128
attributes:
115129
file_format:

spec/components/app_vaccinate_form_component_spec.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@
1919
create(:patient_session, :in_attendance, programmes:, patient:, session:)
2020
end
2121

22-
let(:component) do
23-
described_class.new(
24-
patient_session:,
25-
programme: programmes.first,
26-
vaccinate_form: VaccinateForm.new
27-
)
22+
let(:vaccinate_form) do
23+
VaccinateForm.new(patient_session:, programme: programmes.first)
2824
end
2925

26+
let(:component) { described_class.new(vaccinate_form) }
27+
3028
before { patient_session.strict_loading!(false) }
3129

3230
it { should have_css(".nhsuk-card") }

spec/features/hpv_vaccination_pre_screening_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ def and_i_choose_that_the_patient_is_ready_to_vaccinate
5353
end
5454

5555
def then_i_see_an_error_message
56-
expect(page).to have_content("Patient should not be vaccinated")
56+
expect(page).to have_content(
57+
"Confirm that they have not already had the vaccination"
58+
)
5759
end
5860
end

0 commit comments

Comments
 (0)