Skip to content

Commit 24ef810

Browse files
authored
Avoid N+1 issue in vaccintion criteria (#3236)
This avoids an N+1 issue when checking whether a patient is vaccinated by checking only if `session_id` is present rather than checking if `session` is present. I've also tided up a few other places where we do this check in a scope for consistency.
2 parents fb75772 + 684a271 commit 24ef810

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

app/jobs/vaccination_confirmations_job.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def perform
1919
.kept
2020
.where("created_at >= ?", since)
2121
.where(confirmation_sent_at: nil)
22-
.where.not(session_id: nil)
22+
.recorded_in_service
2323
.select { _1.academic_year == academic_year }
2424
.each do |vaccation_record|
2525
send_vaccination_confirmation(vaccation_record)

app/lib/vaccinated_criteria.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def call
2222
administered_records.any? do
2323
(
2424
it.dose_sequence == programme.vaccinated_dose_sequence ||
25-
(it.dose_sequence.nil? && it.session.present?)
25+
(it.dose_sequence.nil? && it.recorded_in_service?)
2626
) && patient.age(now: it.performed_at) >= 10
2727
end
2828
else

app/models/vaccination_record.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class VaccinationRecord < ApplicationRecord
9292
has_one :organisation, through: :session
9393
has_one :team, through: :session
9494

95-
scope :recorded_in_service, -> { where.not(session: nil) }
95+
scope :recorded_in_service, -> { where.not(session_id: nil) }
9696
scope :unexported, -> { where.missing(:dps_exports) }
9797

9898
scope :with_pending_changes,
@@ -163,6 +163,10 @@ def confirmation_sent?
163163
confirmation_sent_at != nil
164164
end
165165

166+
def recorded_in_service?
167+
session_id != nil
168+
end
169+
166170
def dose_volume_ml
167171
# TODO: this will need to be revisited once it's possible to record half-doses
168172
# e.g. for the flu programme where a child refuses the second half of the dose

0 commit comments

Comments
 (0)