Skip to content

Commit 27759a8

Browse files
authored
Merge pull request #3548 from nhsuk/merge-health-answers-with-follow-ups
2 parents 76d2977 + 552dbee commit 27759a8

File tree

5 files changed

+220
-32
lines changed

5 files changed

+220
-32
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# frozen_string_literal: true
2+
3+
class HealthAnswersDeduplicator
4+
def initialize(vaccines:)
5+
@vaccines = vaccines
6+
end
7+
8+
def call
9+
@health_answers = []
10+
11+
vaccines.each { |vaccine| add_unique_health_answers(vaccine) }
12+
13+
re_map_question_indexes
14+
fill_in_next_question_gaps
15+
16+
@health_answers
17+
end
18+
19+
def self.call(...) = new(...).call
20+
21+
private_class_method :new
22+
23+
private
24+
25+
attr_reader :vaccines
26+
27+
def add_unique_health_answers(vaccine)
28+
vaccine_health_answers = vaccine.health_questions.to_health_answers
29+
30+
vaccine_health_answers.each do |health_answer|
31+
existing_index = existing_question_index(health_answer.question)
32+
33+
# This doesn't work very well if the question already exists but is
34+
# otherwise different, for example if the followup question is
35+
# different. We don't have instances of this currently.
36+
37+
next unless existing_index.nil?
38+
39+
health_answer.id = @health_answers.length
40+
41+
# We store the questions here and re-map them to indexes later when we
42+
# know what all the questions will be.
43+
44+
if (index = health_answer.next_question).present?
45+
health_answer.next_question =
46+
vaccine_health_answers.find { it.id == index }.question
47+
end
48+
49+
if (index = health_answer.follow_up_question).present?
50+
health_answer.follow_up_question =
51+
vaccine_health_answers.find { it.id == index }.question
52+
end
53+
54+
@health_answers << health_answer
55+
end
56+
end
57+
58+
def existing_question_index(question)
59+
@health_answers.index { it.question == question }
60+
end
61+
62+
def re_map_question_indexes
63+
@health_answers.each do |health_answer|
64+
if (question = health_answer.next_question)
65+
health_answer.next_question = existing_question_index(question)
66+
end
67+
68+
if (question = health_answer.follow_up_question)
69+
health_answer.follow_up_question = existing_question_index(question)
70+
end
71+
end
72+
end
73+
74+
def fill_in_next_question_gaps
75+
@health_answers.each_with_index do |health_answer, index|
76+
if health_answer.next_question.nil? && index < @health_answers.length - 1
77+
health_answer.next_question = index + 1
78+
end
79+
end
80+
end
81+
end

app/models/consent_form.rb

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -471,30 +471,20 @@ def seed_health_questions
471471
}
472472
end
473473

474-
health_answers_for_chosen_vaccines =
475-
chosen_vaccines.flat_map { it.health_questions.to_health_answers }
476-
477-
# TODO: This doesn't work if we have follow up questions. Currently no vaccines have these.
478-
deduplicated_health_answers =
479-
health_answers_for_chosen_vaccines.uniq(&:question)
480-
481474
self.health_answers =
482-
deduplicated_health_answers.each_with_index.map do |health_answer, index|
483-
health_answer.id = index
484-
485-
health_answer.next_question =
486-
(index + 1 if index < deduplicated_health_answers.count - 1)
487-
488-
if (
489-
existing_health_answer =
490-
existing_health_answers[health_answer.question]
491-
)
492-
health_answer.response = existing_health_answer[:response]
493-
health_answer.notes = existing_health_answer[:notes]
494-
end
475+
HealthAnswersDeduplicator
476+
.call(vaccines: chosen_vaccines)
477+
.map do |health_answer|
478+
if (
479+
existing_health_answer =
480+
existing_health_answers[health_answer.question]
481+
)
482+
health_answer.response = existing_health_answer[:response]
483+
health_answer.notes = existing_health_answer[:notes]
484+
end
495485

496-
health_answer
497-
end
486+
health_answer
487+
end
498488
end
499489

500490
private

spec/features/parental_consent_change_answers_spec.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@
2828
when_i_change_my_answer_to_yes_for_the_asthma_question
2929
then_i_see_the_first_follow_up_question
3030

31-
# TODO: Follow up questions currently don't work.
32-
# when_i_answer_yes_to_the_follow_up_question_and_continue
33-
# then_i_see_the_second_follow_up_question
31+
when_i_answer_yes_to_the_follow_up_question_and_continue
32+
then_i_see_the_second_follow_up_question
3433

35-
# when_i_answer_yes_to_the_second_follow_up_question_and_continue
34+
when_i_answer_yes_to_the_second_follow_up_question_and_continue
3635
then_i_see_the_consent_form_confirmation_page
3736
and_i_see_the_answer_i_changed_is_yes
3837

@@ -148,7 +147,7 @@ def when_i_accept_parental_responsibility
148147
# BUG: The page should be the consent confirm page, but because we
149148
# encountered a validation error, the skip_to_confirm flag gets lost and we
150149
# end up on the next page in the wizard.
151-
12.times { click_button "Continue" }
150+
10.times { click_button "Continue" }
152151
end
153152

154153
def when_i_change_my_parental_relationship_to_dad
@@ -209,9 +208,8 @@ def then_i_see_the_second_follow_up_question
209208
def and_i_see_the_answer_i_changed_is_yes
210209
expect(page).to have_content("Yes – He has had asthma since he was 2")
211210

212-
# TODO: Follow up questions currently don't work.
213-
# expect(page).to have_content("Yes – Follow up details")
214-
# expect(page).to have_content("Yes – Even more follow up details")
211+
expect(page).to have_content("Yes – Follow up details")
212+
expect(page).to have_content("Yes – Even more follow up details")
215213
end
216214

217215
def when_i_click_back

spec/features/parental_consent_flu_spec.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
describe "Parental consent" do
44
scenario "Flu programme" do
5-
skip "We don't handle flu yet and health answers with follow up questions doesn't work."
6-
75
given_a_flu_programme_is_underway
86
when_i_go_to_the_consent_form
97
then_i_see_the_consent_form
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# frozen_string_literal: true
2+
3+
describe HealthAnswersDeduplicator do
4+
subject(:health_answers) { described_class.call(vaccines:) }
5+
6+
let(:vaccines) { Vaccine.where(programme: programmes) }
7+
8+
context "with doubles programmes" do
9+
let(:programmes) do
10+
[create(:programme, :menacwy), create(:programme, :td_ipv)]
11+
end
12+
13+
it "generates the correct health answers" do
14+
expect(health_answers.count).to eq(6)
15+
16+
expect(health_answers[0].question).to eq(
17+
"Does your child have a bleeding disorder or another medical condition they receive treatment for?"
18+
)
19+
expect(health_answers[0].next_question).to eq(1)
20+
expect(health_answers[0].follow_up_question).to be_nil
21+
22+
expect(health_answers[1].question).to eq(
23+
"Does your child have any severe allergies?"
24+
)
25+
expect(health_answers[1].next_question).to eq(2)
26+
expect(health_answers[1].follow_up_question).to be_nil
27+
28+
expect(health_answers[2].question).to eq(
29+
"Has your child ever had a severe reaction to any medicines, including vaccines?"
30+
)
31+
expect(health_answers[2].next_question).to eq(3)
32+
expect(health_answers[2].follow_up_question).to be_nil
33+
34+
expect(health_answers[3].question).to eq(
35+
"Does your child need extra support during vaccination sessions?"
36+
)
37+
expect(health_answers[3].next_question).to eq(4)
38+
expect(health_answers[3].follow_up_question).to be_nil
39+
40+
expect(health_answers[4].question).to eq(
41+
"Has your child had a meningitis (MenACWY) vaccination in the last 5 years?"
42+
)
43+
expect(health_answers[4].next_question).to eq(5)
44+
expect(health_answers[4].follow_up_question).to be_nil
45+
46+
expect(health_answers[5].question).to eq(
47+
"Has your child had a tetanus, diphtheria and polio vaccination in the last 5 years?"
48+
)
49+
expect(health_answers[5].next_question).to be_nil
50+
expect(health_answers[5].follow_up_question).to be_nil
51+
end
52+
end
53+
54+
context "with a Flu programme" do
55+
let(:programmes) { [create(:programme, :flu_all_vaccines)] }
56+
57+
it "generates the correct health answers" do
58+
expect(health_answers.count).to eq(10)
59+
60+
expect(health_answers[0].question).to eq(
61+
"Has your child been diagnosed with asthma?"
62+
)
63+
expect(health_answers[0].next_question).to eq(3)
64+
expect(health_answers[0].follow_up_question).to eq(1)
65+
66+
expect(health_answers[1].question).to eq(
67+
"Have they taken oral steroids in the last 2 weeks?"
68+
)
69+
expect(health_answers[1].next_question).to eq(2)
70+
expect(health_answers[1].follow_up_question).to be_nil
71+
72+
expect(health_answers[2].question).to eq(
73+
"Have they been admitted to intensive care for their asthma?"
74+
)
75+
expect(health_answers[2].next_question).to eq(3)
76+
expect(health_answers[2].follow_up_question).to be_nil
77+
78+
expect(health_answers[3].question).to eq(
79+
"Has your child had a flu vaccination in the last 5 months?"
80+
)
81+
expect(health_answers[3].next_question).to eq(4)
82+
expect(health_answers[3].follow_up_question).to be_nil
83+
84+
expect(health_answers[4].question).to eq(
85+
"Does your child have a disease or treatment that severely affects their immune system?"
86+
)
87+
expect(health_answers[4].next_question).to eq(5)
88+
expect(health_answers[4].follow_up_question).to be_nil
89+
90+
expect(health_answers[5].question).to eq(
91+
"Is anyone in your household currently having treatment that severely affects their immune system?"
92+
)
93+
expect(health_answers[5].next_question).to eq(6)
94+
expect(health_answers[5].follow_up_question).to be_nil
95+
96+
expect(health_answers[6].question).to eq(
97+
"Has your child ever been admitted to intensive care due to an allergic reaction to egg?"
98+
)
99+
expect(health_answers[6].next_question).to eq(7)
100+
expect(health_answers[6].follow_up_question).to be_nil
101+
102+
expect(health_answers[7].question).to eq(
103+
"Does your child have any allergies to medication?"
104+
)
105+
expect(health_answers[7].next_question).to eq(8)
106+
expect(health_answers[7].follow_up_question).to be_nil
107+
108+
expect(health_answers[8].question).to eq(
109+
"Has your child ever had a reaction to previous vaccinations?"
110+
)
111+
expect(health_answers[8].next_question).to eq(9)
112+
expect(health_answers[8].follow_up_question).to be_nil
113+
114+
expect(health_answers[9].question).to eq(
115+
"Does you child take regular aspirin?"
116+
)
117+
expect(health_answers[9].next_question).to be_nil
118+
expect(health_answers[9].follow_up_question).to be_nil
119+
end
120+
end
121+
end

0 commit comments

Comments
 (0)