Skip to content

Commit c5a5879

Browse files
committed
Improve how session is chosen for consent form
This ensures that when filling in a consent form where there exists multiple sessions with the same programmes at the same location, we pick the one that is most approriate based on which one has dates in the future. Jira-Issue: MAV-2036
1 parent 459b422 commit c5a5879

File tree

9 files changed

+82
-136
lines changed

9 files changed

+82
-136
lines changed

app/components/app_consent_confirmation_component.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ def full_name
3737
end
3838

3939
def panel_text
40+
location = (@consent_form.session.school? ? " at school" : "")
41+
4042
if response_given?
4143
if @consent_form.health_answers_require_triage?
4244
<<-END_OF_TEXT
@@ -45,12 +47,12 @@ def panel_text
4547
your answers and get in touch again soon.
4648
END_OF_TEXT
4749
else
48-
"#{full_name} is due to get the #{given_vaccinations} at school" +
50+
"#{full_name} is due to get the #{given_vaccinations}#{location}" +
4951
(session_dates.present? ? " on #{session_dates}" : "")
5052
end
5153
else
5254
"You’ve told us that you do not want #{full_name} to get the" \
53-
" #{refused_vaccinations} at school"
55+
" #{refused_vaccinations}#{location}"
5456
end
5557
end
5658

@@ -86,7 +88,7 @@ def given_vaccinations_are
8688

8789
def session_dates
8890
@consent_form
89-
.actual_session
91+
.session
9092
.today_or_future_dates
9193
.map { it.to_fs(:short_day_of_week) }
9294
.to_sentence(two_words_connector: " or ", last_word_connector: " or ")

app/controllers/concerns/consent_form_mailer_concern.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ def send_consent_form_confirmation(consent_form)
1414
consent_form:,
1515
programmes:
1616
)
17-
elsif consent_form.actual_session.clinic? ||
18-
consent_form.actual_session.completed?
17+
elsif consent_form.session.clinic? || consent_form.session.completed?
1918
EmailDeliveryJob.perform_later(
2019
:consent_confirmation_clinic,
2120
consent_form:,

app/controllers/consent_forms_controller.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ def update_match
4040
.sessions
4141
.includes(:location_programme_year_groups, :programmes)
4242
.has_programmes(@consent_form.programmes)
43-
.find_by(academic_year: AcademicYear.pending) ||
44-
@consent_form.original_session
43+
.find_by(academic_year: AcademicYear.pending) || @consent_form.session
4544

4645
programme = session.programmes_for(patient: @patient).first
4746

app/controllers/parent_interface/consent_forms/base_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def set_session
3131
if params[:session_slug]
3232
@session = Session.find_by!(slug: params[:session_slug])
3333
elsif @consent_form.present?
34-
@session = @consent_form.original_session
34+
@session = @consent_form.session
3535
end
3636
end
3737

app/jobs/consent_form_matching_job.rb

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ def perform(consent_form)
1414
# Match if we find a patient with the PDS NHS number
1515
return if match_with_exact_nhs_number
1616

17-
# Look for patients in the original session with no NHS number
18-
if session_patients.count == 1
17+
# Look for patients in the original location with no NHS number
18+
if location_patients.count == 1
1919
# If we found exactly one, match the consent form to this patient
20-
match_patient(session_patients.first)
20+
match_patient(location_patients.first)
2121
end
2222

2323
# Look for patients in any session with matching details
@@ -65,11 +65,16 @@ def match_with_exact_nhs_number
6565
@consent_form.match_with_patient!(patient, current_user: nil)
6666
end
6767

68-
def session_patients
69-
@session_patients ||=
70-
@consent_form
71-
.original_session
72-
.patients
68+
def location_patients
69+
@location_patients ||=
70+
Patient
71+
.joins(:patient_locations)
72+
.where(
73+
patient_locations: {
74+
location: @consent_form.location,
75+
academic_year: @consent_form.academic_year
76+
}
77+
)
7378
.includes(:school, :parents)
7479
.match_existing(nhs_number: nil, **query)
7580
end

app/lib/govuk_notify_personalisation.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ def initialize(
2626
@programmes =
2727
programmes.presence || consent_form&.programmes.presence ||
2828
[consent&.programme || vaccination_record&.programme].compact
29-
@session =
30-
session || consent_form&.actual_session ||
31-
consent_form&.original_session || vaccination_record&.session
29+
@session = session || consent_form&.session || vaccination_record&.session
3230
@team =
3331
session&.team || consent_form&.team || consent&.team ||
3432
vaccination_record&.team

app/models/consent_form.rb

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -353,28 +353,32 @@ def reason_notes_must_be_provided?
353353
reason.in?(Consent::REASON_FOR_REFUSAL_REQUIRES_NOTES)
354354
end
355355

356-
def original_session
357-
# The session that the consent form was filled out for.
358-
@original_session ||=
359-
Session
360-
.joins(:programmes)
361-
.where(programmes:)
362-
.preload(:programmes)
363-
.find_by(academic_year:, location:, team:)
364-
end
365-
366-
def actual_session
367-
# The session that the patient is expected to be seen in.
368-
@actual_session ||=
369-
(location_is_clinic? && original_session) ||
370-
(
371-
school &&
372-
school
373-
.sessions
374-
.has_programmes(programmes)
375-
.includes(:session_dates)
376-
.find_by(academic_year:)
377-
) || team.generic_clinic_session(academic_year:)
356+
def session
357+
# This tries to find the most approriate session for this consent form.
358+
# It's used when generating links to patients in a session, or when
359+
# deciding which dates to show in an email. Under the hood, patients
360+
# belong to locations, not sessions.
361+
#
362+
# Although unlikely to happen in production, there can be a scenario
363+
# where multiple sessions at the same location offer the same programmes.
364+
# In this case, we have to make a guess about which is the most relevant
365+
# session.
366+
367+
@session ||=
368+
begin
369+
session_location = school || location
370+
371+
sessions_to_search =
372+
Session.has_programmes(programmes).where(
373+
academic_year:,
374+
location: session_location,
375+
team:
376+
)
377+
378+
sessions_to_search.find { !it.completed? } ||
379+
sessions_to_search.first ||
380+
team.generic_clinic_session(academic_year:)
381+
end
378382
end
379383

380384
def find_or_create_parent_with_relationship_to!(patient:)

spec/features/parental_consent_clinic_spec.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
then_i_can_check_my_answers
2828

2929
when_i_submit_the_consent_form
30-
then_i_see_a_confirmation_page
30+
then_i_see_a_confirmation_page_in_school
3131

3232
when_the_nurse_checks_the_school_moves
3333
then_the_nurse_should_see_one_move
@@ -57,7 +57,7 @@
5757
then_i_can_check_my_answers
5858

5959
when_i_submit_the_consent_form
60-
then_i_see_a_confirmation_page
60+
then_i_see_a_confirmation_page_in_clinic
6161

6262
when_the_nurse_checks_the_school_moves
6363
then_the_nurse_should_see_one_move
@@ -87,7 +87,7 @@
8787
then_i_can_check_my_answers
8888

8989
when_i_submit_the_consent_form
90-
then_i_see_a_confirmation_page
90+
then_i_see_a_confirmation_page_in_clinic
9191

9292
when_the_nurse_checks_the_school_moves
9393
then_the_nurse_should_see_no_moves
@@ -229,9 +229,15 @@ def when_i_submit_the_consent_form
229229
click_on "Confirm"
230230
end
231231

232-
def then_i_see_a_confirmation_page
233-
# TODO: "will get their HPV vaccination at the clinic"
232+
def then_i_see_a_confirmation_page_in_school
233+
expect(page).to have_content("is due to get the HPV vaccination at school")
234+
235+
perform_enqueued_jobs # match consent form with patient
236+
end
237+
238+
def then_i_see_a_confirmation_page_in_clinic
234239
expect(page).to have_content("is due to get the HPV vaccination")
240+
expect(page).not_to have_content("at school")
235241

236242
perform_enqueued_jobs # match consent form with patient
237243
end

spec/features/parental_consent_school_session_completed_spec.rb

Lines changed: 23 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,21 @@
99
given_an_hpv_programme_is_underway
1010

1111
when_i_go_to_the_consent_form
12-
when_i_fill_in_my_childs_name_and_birthday
13-
14-
when_i_give_consent
15-
and_i_answer_no_to_all_the_medical_questions
16-
then_i_can_check_my_answers
17-
18-
when_i_submit_the_consent_form
19-
then_i_get_a_confirmation_email
12+
and_i_fill_in_my_childs_name_and_birthday
13+
and_i_try_to_give_consent
14+
then_i_see_that_consent_is_closed
2015
end
2116

2217
def given_an_hpv_programme_is_underway
2318
@programme = create(:programme, :hpv)
2419
@team = create(:team, :with_one_nurse, programmes: [@programme])
2520

26-
subteam = create(:subteam, team: @team)
21+
@subteam = create(:subteam, team: @team)
2722

28-
@scheduled_school = create(:school, :secondary, name: "School 1", subteam:)
29-
@completed_school = create(:school, :secondary, name: "School 2", subteam:)
23+
@scheduled_school =
24+
create(:school, :secondary, name: "School 1", subteam: @subteam)
25+
@completed_school =
26+
create(:school, :secondary, name: "School 2", subteam: @subteam)
3027

3128
@scheduled_session =
3229
create(
@@ -46,29 +43,7 @@ def given_an_hpv_programme_is_underway
4643
location: @completed_school
4744
)
4845

49-
@child = create(:patient, session: @scheduled_session)
50-
end
51-
52-
def when_a_nurse_checks_consent_responses
53-
sign_in @team.users.first
54-
visit "/dashboard"
55-
56-
click_on "Programmes", match: :first
57-
click_on "HPV"
58-
within ".app-secondary-navigation" do
59-
click_on "Sessions"
60-
end
61-
click_on "Pilot School"
62-
click_on "Consent"
63-
end
64-
65-
def then_there_should_be_no_consent_for_my_child
66-
expect(page).to have_content("No response")
67-
68-
check "No response"
69-
click_on "Update results"
70-
71-
expect(page).to have_content(@child.full_name)
46+
@patient = create(:patient, session: @scheduled_session)
7247
end
7348

7449
def when_i_go_to_the_consent_form
@@ -78,76 +53,34 @@ def when_i_go_to_the_consent_form
7853
)
7954
end
8055

81-
def when_i_give_consent
82-
choose "No, they go to a different school"
83-
click_on "Continue"
84-
85-
select @completed_school.name
86-
click_on "Continue"
87-
88-
expect(page).to have_content("About you")
89-
fill_in "Full name", with: "Jane #{@child.family_name}"
90-
choose "Mum" # Your relationship to the child
91-
fill_in "Email address", with: "jane@example.com"
92-
fill_in "Phone number", with: "07123456789"
93-
check "Tick this box if you’d like to get updates by text message"
94-
click_on "Continue"
95-
96-
expect(page).to have_content("Phone contact method")
97-
choose "I do not have specific needs"
98-
click_on "Continue"
99-
100-
expect(page).to have_content("Do you agree")
101-
choose "Yes, I agree"
102-
click_on "Continue"
103-
104-
expect(page).to have_content("Home address")
105-
fill_in "Address line 1", with: "1 Test Street"
106-
fill_in "Address line 2 (optional)", with: "2nd Floor"
107-
fill_in "Town or city", with: "Testville"
108-
fill_in "Postcode", with: "TE1 1ST"
109-
click_on "Continue"
110-
end
111-
112-
def when_i_fill_in_my_childs_name_and_birthday
56+
def and_i_fill_in_my_childs_name_and_birthday
11357
click_on "Start now"
11458

11559
expect(page).to have_content("What is your child’s name?")
116-
fill_in "First name", with: @child.given_name
117-
fill_in "Last name", with: @child.family_name
60+
fill_in "First name", with: @patient.given_name
61+
fill_in "Last name", with: @patient.family_name
11862
choose "No" # Do they use a different name in school?
11963
click_on "Continue"
12064

12165
expect(page).to have_content("What is your child’s date of birth?")
122-
fill_in "Day", with: @child.date_of_birth.day
123-
fill_in "Month", with: @child.date_of_birth.month
124-
fill_in "Year", with: @child.date_of_birth.year
66+
fill_in "Day", with: @patient.date_of_birth.day
67+
fill_in "Month", with: @patient.date_of_birth.month
68+
fill_in "Year", with: @patient.date_of_birth.year
12569
click_on "Continue"
12670
end
12771

128-
def and_i_answer_no_to_all_the_medical_questions
129-
until page.has_content?("Check and confirm")
130-
choose "No"
131-
click_on "Continue"
132-
end
133-
end
134-
135-
def then_i_can_check_my_answers
136-
expect(page).to have_content("Check and confirm")
137-
expect(page).to have_content(
138-
"Child’s name#{@child.full_name(context: :parents)}"
139-
)
140-
end
72+
def and_i_try_to_give_consent
73+
choose "No, they go to a different school"
74+
click_on "Continue"
14175

142-
def when_i_submit_the_consent_form
143-
click_on "Confirm"
76+
select @completed_school.name
77+
click_on "Continue"
14478
end
14579

146-
def then_i_get_a_confirmation_email
80+
def then_i_see_that_consent_is_closed
81+
expect(page).to have_content("The deadline for responding has passed")
14782
expect(page).to have_content(
148-
"#{@child.full_name(context: :parents)} is due to get the HPV vaccination at school"
83+
"Contact #{@subteam.email} to book a clinic appointment."
14984
)
150-
151-
expect_email_to("jane@example.com", :consent_confirmation_clinic)
15285
end
15386
end

0 commit comments

Comments
 (0)