Skip to content

Commit a007392

Browse files
committed
Simplify patient and session lookups
This simplifies the lookups for patients and sessions by removing the duplicate scopes between patient locations and patients, instead using the scopes on the patient only, and making sure to filter out patients who shouldn't belong in the session before we need to query for this. Jira-Issue: MAV-1822
1 parent 3ab3483 commit a007392

35 files changed

+604
-656
lines changed

app/components/app_programme_session_table_component.rb

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

33
class AppProgrammeSessionTableComponent < ViewComponent::Base
4-
def initialize(sessions, programme:)
4+
def initialize(sessions, programme:, academic_year:)
55
@sessions = sessions
66
@programme = programme
7+
@academic_year = academic_year
78
end
89

910
private
1011

11-
attr_reader :sessions, :programme
12+
attr_reader :sessions, :programme, :academic_year
1213

1314
delegate :govuk_table, to: :helpers
1415

1516
def cohort_count(session:)
16-
format_number(patient_locations(session:).count)
17+
format_number(patients(session:).count)
1718
end
1819

1920
def no_response_scope(session:)
20-
patient_locations(session:).has_consent_status(:no_response, programme:)
21+
patients(session:).has_consent_status(
22+
:no_response,
23+
programme:,
24+
academic_year:
25+
)
2126
end
2227

2328
def no_response_count(session:)
@@ -27,13 +32,17 @@ def no_response_count(session:)
2732
def no_response_percentage(session:)
2833
format_percentage(
2934
no_response_scope(session:).count,
30-
patient_locations(session:).count
35+
patients(session:).count
3136
)
3237
end
3338

3439
def triage_needed_count(session:)
3540
format_number(
36-
patient_locations(session:).has_triage_status(:required, programme:).count
41+
patients(session:).has_triage_status(
42+
:required,
43+
programme:,
44+
academic_year:
45+
).count
3746
)
3847
end
3948

@@ -48,12 +57,22 @@ def vaccinated_count(session:)
4857
def vaccinated_percentage(session:)
4958
format_percentage(
5059
vaccinated_scope(session:).count,
51-
patient_locations(session:).count
60+
patients(session:).count
61+
)
62+
end
63+
64+
def patients(session:)
65+
@patients ||= {}
66+
@patients[session] = session.patients.where(
67+
birth_academic_year: birth_academic_years(session:)
5268
)
5369
end
5470

55-
def patient_locations(session:)
56-
session.patient_locations.joins(:patient).appear_in_programmes([programme])
71+
def birth_academic_years(session:)
72+
@birth_academic_years ||= {}
73+
@birth_academic_years[session] = session.programme_birth_academic_years[
74+
programme
75+
]
5776
end
5877

5978
def format_number(count) = count.to_s

app/components/app_session_actions_component.rb

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ def render? = rows.any?
1919
delegate :govuk_summary_list, to: :helpers
2020
delegate :academic_year, :programmes, to: :session
2121

22-
def patient_locations
23-
session.patient_locations.joins(:patient).appear_in_programmes(programmes)
24-
end
22+
def patients = session.patients
2523

2624
def rows
2725
@rows ||= [
@@ -35,7 +33,7 @@ def rows
3533
end
3634

3735
def no_nhs_number_row
38-
count = patient_locations.merge(Patient.without_nhs_number).count
36+
count = patients.without_nhs_number.count
3937
href = session_patients_path(session, missing_nhs_number: true)
4038

4139
generate_row(:children_without_nhs_number, count:, href:)
@@ -44,7 +42,11 @@ def no_nhs_number_row
4442
def no_consent_response_row
4543
status = "no_response"
4644
count =
47-
patient_locations.has_consent_status(status, programme: programmes).count
45+
patients.has_consent_status(
46+
status,
47+
programme: programmes,
48+
academic_year:
49+
).count
4850
href = session_consent_path(session, consent_statuses: [status])
4951
actions = [
5052
{
@@ -58,7 +60,11 @@ def no_consent_response_row
5860
def conflicting_consent_row
5961
status = "conflicts"
6062
count =
61-
patient_locations.has_consent_status(status, programme: programmes).count
63+
patients.has_consent_status(
64+
status,
65+
programme: programmes,
66+
academic_year:
67+
).count
6268
href = session_consent_path(session, consent_statuses: [status])
6369

6470
generate_row(:children_with_conflicting_consent_response, count:, href:)
@@ -67,7 +73,11 @@ def conflicting_consent_row
6773
def triage_required_row
6874
status = "required"
6975
count =
70-
patient_locations.has_triage_status(status, programme: programmes).count
76+
patients.has_triage_status(
77+
status,
78+
programme: programmes,
79+
academic_year:
80+
).count
7181
href = session_triage_path(session, triage_status: status)
7282

7383
generate_row(:children_requiring_triage, count:, href:)
@@ -77,7 +87,7 @@ def register_attendance_row
7787
return nil unless session.requires_registration? && session.today?
7888

7989
status = "unknown"
80-
count = patient_locations.has_registration_status(status, session:).count
90+
count = patients.has_registration_status(status, session:).count
8191
href = session_register_path(session, register_status: status)
8292

8393
generate_row(:children_to_register, count:, href:)
@@ -88,13 +98,11 @@ def ready_for_vaccinator_row
8898

8999
counts_by_programme =
90100
session.programmes.index_with do |programme|
91-
patient_locations
101+
patients
92102
.has_registration_status(%w[attending completed], session:)
93-
.includes(
94-
patient: %i[consent_statuses triage_statuses vaccination_statuses]
95-
)
96-
.count do |patient_location|
97-
patient_location.patient.consent_given_and_safe_to_vaccinate?(
103+
.includes(:consent_statuses, :triage_statuses, :vaccination_statuses)
104+
.count do |patient|
105+
patient.consent_given_and_safe_to_vaccinate?(
98106
programme:,
99107
academic_year:
100108
)

app/components/app_session_details_summary_component.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ def call
1414
attr_reader :session
1515

1616
delegate :govuk_summary_list, to: :helpers
17-
delegate :programmes, to: :session
17+
delegate :programmes, :academic_year, to: :session
1818

19-
def patient_locations
20-
session.patient_locations.joins(:patient).appear_in_programmes(programmes)
21-
end
19+
def patients = session.patients
2220

2321
def cohort_row
24-
count = patient_locations.count
22+
count = patients.count
2523

2624
{ key: { text: "Cohort" }, value: { text: I18n.t("children", count:) } }
2725
end
@@ -30,7 +28,11 @@ def consent_refused_row
3028
status = "refused"
3129

3230
count =
33-
patient_locations.has_consent_status(status, programme: programmes).count
31+
patients.has_consent_status(
32+
status,
33+
programme: programmes,
34+
academic_year:
35+
).count
3436

3537
href = session_consent_path(session, consent_statuses: [status])
3638

app/components/app_session_needs_review_warning_component.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ def warning_href
3232

3333
def warning_counts
3434
@warning_counts ||= {
35-
children_without_nhs_number:
36-
patient_locations.merge(Patient.without_nhs_number).count
35+
children_without_nhs_number: patients.without_nhs_number.count
3736
}
3837
end
3938

@@ -42,10 +41,5 @@ def make_row_from_warning(warning)
4241
link_to(t(warning, count: warning_counts[warning]), warning_href[warning])
4342
end
4443

45-
def patient_locations
46-
@session
47-
.patient_locations
48-
.joins(:patient)
49-
.appear_in_programmes(@session.programmes)
50-
end
44+
def patients = @session.patients
5145
end

app/controllers/programmes/base_controller.rb

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,11 @@ def set_academic_year
2222

2323
def patient_ids
2424
@patient_ids ||=
25-
PatientLocation
26-
.distinct
27-
.joins(:patient)
28-
.joins_sessions
29-
.where("sessions.id IN (?)", session_ids)
30-
.appear_in_programmes([@programme])
31-
.not_archived(team: current_team)
32-
.pluck(:patient_id)
33-
end
34-
35-
def session_ids
36-
@session_ids ||=
3725
current_team
38-
.sessions
39-
.where(academic_year: @academic_year)
40-
.has_programmes([@programme])
26+
.patients
27+
.appear_in_programmes([@programme], academic_year: @academic_year)
28+
.not_archived(team: current_team)
4129
.pluck(:id)
30+
.uniq
4231
end
4332
end

app/controllers/sessions/consent_controller.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,16 @@ def show
2727

2828
scope =
2929
@session
30-
.patient_locations
31-
.includes(patient: [:consent_statuses, { notes: :created_by }])
30+
.patients
31+
.includes(:consent_statuses, { notes: :created_by })
3232
.has_consent_status(
3333
statuses_except_not_required,
34-
programme: @form.programmes
34+
programme: @form.programmes,
35+
academic_year: @session.academic_year
3536
)
3637

37-
patient_locations = @form.apply(scope)
38-
@pagy, @patient_locations = pagy(patient_locations)
38+
patients = @form.apply(scope)
39+
@pagy, @patients = pagy(patients)
3940
end
4041

4142
private

app/controllers/sessions/patient_specific_directions_controller.rb

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,18 @@ class Sessions::PatientSpecificDirectionsController < ApplicationController
99
before_action :set_patient_search_form
1010

1111
def show
12-
scope =
13-
@session.patient_locations.includes(
14-
patient: {
15-
patient_specific_directions: :programme
16-
}
17-
)
18-
@eligible_for_bulk_psd_count = patient_locations_allowed_psd.count
19-
patient_locations = @form.apply(scope)
20-
@pagy, @patient_locations = pagy(patient_locations)
12+
scope = @session.patients.includes(patient_specific_directions: :programme)
13+
14+
@eligible_for_bulk_psd_count = patients_allowed_psd.count
15+
16+
patients = @form.apply(scope)
17+
@pagy, @patients = pagy(patients)
2118

2219
render layout: "full"
2320
end
2421

2522
def new
26-
@eligible_for_bulk_psd_count = patient_locations_allowed_psd.count
23+
@eligible_for_bulk_psd_count = patients_allowed_psd.count
2724
end
2825

2926
def create
@@ -56,12 +53,12 @@ def set_vaccine
5653
end
5754

5855
def psds_to_create
59-
patient_locations_allowed_psd.map do |patient_location|
56+
patients_allowed_psd.map do |patient|
6057
PatientSpecificDirection.new(
6158
academic_year: @session.academic_year,
6259
created_by: current_user,
6360
delivery_site: "nose",
64-
patient_id: patient_location.patient_id,
61+
patient:,
6562
programme: @programme,
6663
team: current_team,
6764
vaccine: @vaccine,
@@ -70,18 +67,24 @@ def psds_to_create
7067
end
7168
end
7269

73-
def patient_locations_allowed_psd
74-
@patient_locations_allowed_psd ||=
70+
def patients_allowed_psd
71+
@patients_allowed_psd ||=
7572
@session
76-
.patient_locations
73+
.patients
7774
.has_consent_status(
7875
"given",
7976
programme: @programme,
77+
academic_year: @session.academic_year,
8078
vaccine_method: "nasal"
8179
)
82-
.has_triage_status("not_required", programme: @programme)
80+
.has_triage_status(
81+
"not_required",
82+
programme: @programme,
83+
academic_year: @session.academic_year
84+
)
8385
.without_patient_specific_direction(
8486
programme: @programme,
87+
academic_year: @session.academic_year,
8588
team: current_team
8689
)
8790
end

app/controllers/sessions/patients_controller.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ def show
1212
@statuses = Patient::VaccinationStatus.statuses.keys
1313

1414
scope =
15-
@session.patient_locations.includes(
16-
patient: [:vaccination_statuses, { notes: :created_by }]
17-
)
15+
@session.patients.includes(:vaccination_statuses, notes: :created_by)
1816

19-
patient_locations = @form.apply(scope)
20-
@pagy, @patient_locations = pagy(patient_locations)
17+
patients = @form.apply(scope)
18+
@pagy, @patients = pagy(patients)
2119
end
2220

2321
private

0 commit comments

Comments
 (0)