Skip to content

Commit ab61229

Browse files
authored
Merge pull request #6154 from NHSDigital/filter-invited-to-clinic
Add ability to filter patients on whether they've been invited to a clinic
2 parents 6dd1a60 + f8eb152 commit ab61229

8 files changed

Lines changed: 295 additions & 108 deletions

File tree

app/components/app_patient_search_form_component.html.erb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@
2121
<% end %>
2222
<% end %>
2323

24+
<% if show_invited_to_clinic %>
25+
<%= f.govuk_check_boxes_fieldset :clinic_invitations,
26+
multiple: false,
27+
legend: { text: "Clinic invitations", size: "s" },
28+
small: true do %>
29+
<%= f.govuk_check_box :invited_to_clinic,
30+
1, 0,
31+
multiple: false,
32+
checked: form.invited_to_clinic,
33+
label: { text: "Invited to clinic" } %>
34+
<% end %>
35+
<% end %>
36+
2437
<% if show_vaccine_criteria && has_vaccine_criteria? %>
2538
<% programmes.each do |programme| %>
2639
<% if (programme_vaccine_criteria = vaccine_criteria_for(programme:)).present? %>

app/components/app_patient_search_form_component.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def initialize(
1313
heading_level: 3,
1414
show_aged_out_of_programmes: false,
1515
show_archived_records: true,
16+
show_invited_to_clinic: false,
1617
show_vaccine_criteria: false
1718
)
1819
@form = form
@@ -28,6 +29,7 @@ def initialize(
2829
@show_aged_out_of_programmes = show_aged_out_of_programmes
2930
@show_archived_records = show_archived_records
3031
@show_vaccine_criteria = show_vaccine_criteria
32+
@show_invited_to_clinic = show_invited_to_clinic
3133
end
3234

3335
private
@@ -44,6 +46,7 @@ def initialize(
4446
:heading_level,
4547
:show_archived_records,
4648
:show_aged_out_of_programmes,
49+
:show_invited_to_clinic,
4750
:show_vaccine_criteria
4851

4952
delegate :format_year_group,

app/controllers/concerns/patient_search_form_concern.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def patient_search_form_params
2626
:date_of_birth_day,
2727
:date_of_birth_month,
2828
:date_of_birth_year,
29+
:invited_to_clinic,
2930
:missing_nhs_number,
3031
:patient_specific_direction_status,
3132
:programme_status_group,

app/forms/patient_search_form.rb

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class PatientSearchForm < SearchForm
88
attribute :date_of_birth_day, :integer
99
attribute :date_of_birth_month, :integer
1010
attribute :date_of_birth_year, :integer
11+
attribute :invited_to_clinic, :boolean
1112
attribute :missing_nhs_number, :boolean
1213
attribute :patient_specific_direction_status, :string
1314
attribute :programme_status_group, :string
@@ -50,17 +51,18 @@ def programmes
5051
end
5152

5253
def apply(scope)
53-
scope = filter_name(scope)
54-
scope = filter_year_groups(scope)
5554
scope = filter_aged_out_of_programmes(scope)
5655
scope = filter_archived(scope)
5756
scope = filter_date_of_birth_year(scope)
57+
scope = filter_invited_to_clinic(scope)
58+
scope = filter_name(scope)
5859
scope = filter_nhs_number(scope)
60+
scope = filter_patient_specific_direction_status(scope)
61+
scope = filter_programme_statuses(scope)
5962
scope = filter_programmes(scope)
6063
scope = filter_registration_status(scope)
61-
scope = filter_programme_statuses(scope)
6264
scope = filter_vaccine_criteria(scope)
63-
scope = filter_patient_specific_direction_status(scope)
65+
scope = filter_year_groups(scope)
6466

6567
scope.order_by_name
6668
end
@@ -79,18 +81,6 @@ def academic_year
7981

8082
def team = session&.team || current_user.selected_team
8183

82-
def filter_name(scope)
83-
q.present? ? scope.search_by_name_or_nhs_number(q) : scope
84-
end
85-
86-
def filter_year_groups(scope)
87-
if year_groups.present?
88-
scope.search_by_year_groups(year_groups, academic_year:)
89-
else
90-
scope
91-
end
92-
end
93-
9484
def filter_aged_out_of_programmes(scope)
9585
return scope if team.has_national_reporting_access?
9686

@@ -133,22 +123,22 @@ def filter_date_of_birth_year(scope)
133123
scope
134124
end
135125

136-
def filter_nhs_number(scope)
137-
missing_nhs_number.present? ? scope.search_by_nhs_number(nil) : scope
138-
end
139-
140-
def filter_programmes(scope)
141-
if programme_types.present?
142-
if session
143-
scope.appear_in_programmes(programmes, session:)
144-
else
145-
scope.appear_in_programmes(programmes, academic_year:)
146-
end
126+
def filter_invited_to_clinic(scope)
127+
if invited_to_clinic
128+
scope.has_clinic_notification(team:, academic_year:, programmes:)
147129
else
148130
scope
149131
end
150132
end
151133

134+
def filter_name(scope)
135+
q.present? ? scope.search_by_name_or_nhs_number(q) : scope
136+
end
137+
138+
def filter_nhs_number(scope)
139+
missing_nhs_number.present? ? scope.search_by_nhs_number(nil) : scope
140+
end
141+
152142
def filter_patient_specific_direction_status(scope)
153143
return scope if (status = patient_specific_direction_status&.to_sym).blank?
154144

@@ -208,6 +198,18 @@ def filter_programme_statuses(scope)
208198
or_scope
209199
end
210200

201+
def filter_programmes(scope)
202+
if programme_types.present?
203+
if session
204+
scope.appear_in_programmes(programmes, session:)
205+
else
206+
scope.appear_in_programmes(programmes, academic_year:)
207+
end
208+
else
209+
scope
210+
end
211+
end
212+
211213
def filter_registration_status(scope)
212214
if (status = registration_status&.to_sym).present?
213215
scope.has_registration_status(status, session:)
@@ -246,4 +248,12 @@ def filter_vaccine_criteria(scope)
246248

247249
or_scope
248250
end
251+
252+
def filter_year_groups(scope)
253+
if year_groups.present?
254+
scope.search_by_year_groups(year_groups, academic_year:)
255+
else
256+
scope
257+
end
258+
end
249259
end

app/models/patient.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,22 @@ class Patient < ApplicationRecord
352352
)
353353
end
354354

355+
scope :has_clinic_notification,
356+
->(team:, academic_year:, programmes: []) do
357+
clinic_notification_scope =
358+
ClinicNotification
359+
.select("1")
360+
.where("patient_id = patients.id")
361+
.where(team:, academic_year:)
362+
363+
if programmes.present?
364+
clinic_notification_scope =
365+
clinic_notification_scope.has_any_programmes_of(programmes)
366+
end
367+
368+
where(clinic_notification_scope.arel.exists)
369+
end
370+
355371
scope :with_patient_specific_direction,
356372
->(programme:, academic_year:, team:) do
357373
where(

app/views/schools/patients.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
programmes: current_team.programmes,
2121
programme_statuses: @programme_statuses,
2222
year_groups: @location.year_groups,
23+
show_invited_to_clinic: true,
2324
) %>
2425
</div>
2526

0 commit comments

Comments
 (0)