Skip to content

Commit 755f3a1

Browse files
authored
Merge pull request #4673 from nhsuk/fix-original-session
Improve how session is chosen for consent form
2 parents b12c1eb + c5a5879 commit 755f3a1

10 files changed

Lines changed: 100 additions & 151 deletions

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: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class ConsentForms::BaseController < ApplicationController
1111
prepend_before_action :set_session
1212
prepend_before_action :set_consent_form
1313
before_action :authenticate_consent_form_user!
14+
before_action :check_if_past_deadline!
1415
before_action :set_privacy_policy_url
1516

1617
private
@@ -30,7 +31,7 @@ def set_session
3031
if params[:session_slug]
3132
@session = Session.find_by!(slug: params[:session_slug])
3233
elsif @consent_form.present?
33-
@session = @consent_form.original_session
34+
@session = @consent_form.session
3435
end
3536
end
3637

@@ -61,12 +62,6 @@ def set_subteam
6162
end
6263
end
6364

64-
def authenticate_consent_form_user!
65-
unless session[:consent_form_id] == @consent_form.id
66-
redirect_to @header_path
67-
end
68-
end
69-
7065
def set_header_path
7166
@header_path =
7267
start_parent_interface_consent_forms_path(
@@ -99,5 +94,20 @@ def set_service_guide_url
9994
def set_privacy_policy_url
10095
@privacy_policy_url = @team.privacy_policy_url
10196
end
97+
98+
def authenticate_consent_form_user!
99+
unless session[:consent_form_id] == @consent_form.id
100+
redirect_to @header_path
101+
end
102+
end
103+
104+
def check_if_past_deadline!
105+
return if @session.open_for_consent?
106+
107+
redirect_to deadline_passed_parent_interface_consent_forms_path(
108+
@session.slug,
109+
@programmes.map(&:type).join("-")
110+
)
111+
end
102112
end
103113
end

app/controllers/parent_interface/consent_forms_controller.rb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ class ConsentFormsController < ConsentForms::BaseController
77
skip_before_action :set_consent_form, only: %i[start create deadline_passed]
88
skip_before_action :authenticate_consent_form_user!,
99
only: %i[start create deadline_passed]
10+
skip_before_action :check_if_past_deadline!, only: :deadline_passed
1011

11-
before_action :clear_session_edit_variables, only: %i[confirm]
12-
before_action :check_if_past_deadline, except: %i[deadline_passed]
12+
before_action :clear_session_edit_variables, only: :confirm
1313

1414
def start
1515
end
@@ -65,12 +65,5 @@ def record
6565
def clear_session_edit_variables
6666
session.delete(:follow_up_changes_start_page)
6767
end
68-
69-
def check_if_past_deadline
70-
return if @session.open_for_consent?
71-
redirect_to action: :deadline_passed,
72-
programme_types: @programmes.map(&:type).join("-"),
73-
session_slug: @session.slug
74-
end
7568
end
7669
end

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
@@ -67,11 +67,16 @@ def match_with_exact_nhs_number
6767
@consent_form.match_with_patient!(patient, current_user: nil)
6868
end
6969

70-
def session_patients
71-
@session_patients ||=
72-
@consent_form
73-
.original_session
74-
.patients
70+
def location_patients
71+
@location_patients ||=
72+
Patient
73+
.joins(:patient_locations)
74+
.where(
75+
patient_locations: {
76+
location: @consent_form.location,
77+
academic_year: @consent_form.academic_year
78+
}
79+
)
7580
.includes(:school, :parents)
7681
.match_existing(nhs_number: nil, **query)
7782
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

0 commit comments

Comments
 (0)