Skip to content

Commit 08ed628

Browse files
committed
Refactor ready_for_vaccinator?
This removes the usage of `ready_for_vaccinator?` with a database query on the registration status to avoid unnecessary queries to the database.
1 parent 9083748 commit 08ed628

File tree

5 files changed

+40
-71
lines changed

5 files changed

+40
-71
lines changed

app/components/app_session_actions_component.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,19 @@ def ready_for_vaccinator_row
119119

120120
counts_by_programme =
121121
session.programmes.index_with do |programme|
122-
patient_sessions.count { it.ready_for_vaccinator?(programme:) }
122+
patient_sessions
123+
.where(
124+
PatientSession::RegistrationStatus
125+
.for_patient_session
126+
.where(status: %w[attending completed])
127+
.arel
128+
.exists
129+
)
130+
.count do |patient_session|
131+
patient_session.patient.consent_given_and_safe_to_vaccinate?(
132+
programme:
133+
)
134+
end
123135
end
124136

125137
return nil if counts_by_programme.values.all?(&:zero?)

app/controllers/sessions/record_controller.rb

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,30 @@ class Sessions::RecordController < ApplicationController
1616

1717
def show
1818
scope =
19-
@session.patient_sessions.preload_for_status.in_programmes(
20-
@session.programmes
21-
)
19+
@session
20+
.patient_sessions
21+
.preload_for_status
22+
.in_programmes(@session.programmes)
23+
.where(
24+
PatientSession::RegistrationStatus
25+
.for_patient_session
26+
.where(status: %w[attending completed])
27+
.arel
28+
.exists
29+
)
30+
31+
scope = @form.apply(scope)
2232

2333
patient_sessions =
24-
@form.apply(scope) do |filtered_scope|
25-
filtered_scope.select { it.ready_for_vaccinator? }
34+
scope.select do |patient_session|
35+
patient_session.programmes.any? do |programme|
36+
patient_session.patient.consent_given_and_safe_to_vaccinate?(
37+
programme:
38+
)
39+
end
2640
end
2741

28-
if patient_sessions.is_a?(Array)
29-
@pagy, @patient_sessions = pagy_array(patient_sessions)
30-
else
31-
@pagy, @patient_sessions = pagy(patient_sessions)
32-
end
42+
@pagy, @patient_sessions = pagy_array(patient_sessions)
3343

3444
render layout: "full"
3545
end

app/forms/search_form.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ def apply(scope, programme: nil)
4040

4141
scope = scope.search_by_nhs_number(nil) if missing_nhs_number.present?
4242

43-
scope = scope.order_by_name
44-
45-
scope = yield(scope) if block_given?
46-
4743
if (status = consent_status).present?
4844
scope =
4945
scope.where(
@@ -91,6 +87,6 @@ def apply(scope, programme: nil)
9187
)
9288
end
9389

94-
scope
90+
scope.order_by_name
9591
end
9692
end

app/models/patient_session.rb

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,6 @@ def todays_attendance
148148
end
149149
end
150150

151-
def ready_for_vaccinator?(programme: nil)
152-
if registration_status.nil? || registration_status.unknown? ||
153-
registration_status.not_attending?
154-
return false
155-
end
156-
157-
programmes_to_check = programme ? [programme] : programmes
158-
159-
programmes_to_check.any? do
160-
patient.consent_given_and_safe_to_vaccinate?(programme: it)
161-
end
162-
end
163-
164151
def next_activity(programme:)
165152
return :report if patient.vaccination_status(programme:).vaccinated?
166153

@@ -176,6 +163,11 @@ def next_activity(programme:)
176163
end
177164

178165
def outstanding_programmes
166+
if registration_status.nil? || registration_status.unknown? ||
167+
registration_status.not_attending?
168+
return []
169+
end
170+
179171
# If this patient hasn't been seen yet by a nurse for any of the programmes,
180172
# we don't want to show the banner.
181173
all_programmes_none_yet =
@@ -185,7 +177,7 @@ def outstanding_programmes
185177

186178
programmes.select do |programme|
187179
vaccination_status(programme:).none_yet? &&
188-
ready_for_vaccinator?(programme:)
180+
patient.consent_given_and_safe_to_vaccinate?(programme:)
189181
end
190182
end
191183
end

spec/models/patient_session_spec.rb

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -69,47 +69,6 @@
6969
end
7070
end
7171

72-
describe "#ready_for_vaccinator?" do
73-
subject(:ready_for_vaccinator?) { patient_session.ready_for_vaccinator? }
74-
75-
it { should be(false) }
76-
77-
context "when attending the session" do
78-
let(:patient_session) do
79-
create(:patient_session, :in_attendance, session:)
80-
end
81-
82-
it { should be(false) }
83-
end
84-
85-
context "when attending the session and consent given and triaged as safe to vaccinate" do
86-
let(:patient_session) do
87-
create(
88-
:patient_session,
89-
:in_attendance,
90-
:consent_given_triage_not_needed,
91-
session:
92-
)
93-
end
94-
95-
it { should be(true) }
96-
97-
context "when already vaccinated" do
98-
let(:patient_session) do
99-
create(
100-
:patient_session,
101-
:in_attendance,
102-
:consent_given_triage_not_needed,
103-
:vaccinated,
104-
session:
105-
)
106-
end
107-
108-
it { should be(false) }
109-
end
110-
end
111-
end
112-
11372
describe "#next_activity" do
11473
subject { patient_session.next_activity(programme:) }
11574

0 commit comments

Comments
 (0)