Skip to content

Commit 68793c5

Browse files
authored
Merge pull request #5174 from nhsuk/eligible-programmes-exclude-unknown-locations
Fix `eligible_for_programmes` scope to exclude vaccinations with unknown location
2 parents 97ae7b3 + ce1e19f commit 68793c5

4 files changed

Lines changed: 39 additions & 8 deletions

File tree

app/models/patient.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,8 @@ class Patient < ApplicationRecord
397397
scope :eligible_for_programmes,
398398
->(programmes, location:, academic_year:) do
399399
# We exclude patients who were vaccinated in a previous
400-
# academic year, or vaccinated at a different location.
400+
# academic year, or vaccinated at a different location,
401+
# or the location is not known.
401402

402403
not_eligible_criteria =
403404
programmes.map do |programme|
@@ -409,9 +410,10 @@ class Patient < ApplicationRecord
409410
.vaccinated
410411

411412
scope =
412-
vaccinated_statuses
413-
.where(academic_year:)
414-
.where.not(latest_location: location)
413+
vaccinated_statuses.where(academic_year:).where(
414+
"latest_location_id IS NULL OR latest_location_id != ?",
415+
location.id
416+
)
415417

416418
unless programme.seasonal?
417419
scope =

spec/components/app_session_overview_tallies_component_spec.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
let(:hpv_programme) { CachedProgramme.hpv }
77
let(:flu_programme) { CachedProgramme.flu }
88
let(:session) { create(:session, programmes: [hpv_programme, flu_programme]) }
9+
let(:latest_location) { session.location }
910

1011
let(:component) { described_class.new(session) }
1112

@@ -69,7 +70,8 @@
6970
:patient_vaccination_status,
7071
:vaccinated,
7172
patient:,
72-
programme: flu_programme
73+
programme: flu_programme,
74+
latest_location:
7375
)
7476
end
7577

@@ -268,7 +270,8 @@
268270
:patient_vaccination_status,
269271
:vaccinated,
270272
programme: hpv_programme,
271-
patient: patients.second
273+
patient: patients.second,
274+
latest_location:
272275
)
273276
create(
274277
:patient_consent_status,

spec/features/tallying_session_overview_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ def and_one_vaccinated
8585
:patient_vaccination_status,
8686
:vaccinated,
8787
patient: @patients.fifth,
88-
programme: @flu_programme
88+
programme: @flu_programme,
89+
latest_location: @session.location
8990
)
9091
end
9192

spec/lib/stats/session_spec.rb

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
let(:programme) { CachedProgramme.hpv }
88
let(:session) { create(:session, programmes: [programme]) }
9+
let(:latest_location) { session.location }
910

1011
context "with no patients" do
1112
it "returns zero counts for all stats" do
@@ -30,7 +31,13 @@
3031
end
3132

3233
create(:patient, session:, year_group: 9).tap do |patient|
33-
create(:patient_vaccination_status, :vaccinated, patient:, programme:)
34+
create(
35+
:patient_vaccination_status,
36+
:vaccinated,
37+
patient:,
38+
programme:,
39+
latest_location:
40+
)
3441
end
3542

3643
create(:patient, session:, year_group: 9).tap do |patient|
@@ -135,5 +142,23 @@
135142
expect(stats).to include(eligible_children: 0)
136143
end
137144
end
145+
146+
context "patient is vaccinated but the location is unknown" do
147+
before do
148+
create(:patient, session:, year_group: 9).tap do |patient|
149+
create(
150+
:patient_vaccination_status,
151+
:vaccinated,
152+
patient:,
153+
programme:,
154+
latest_location: nil
155+
)
156+
end
157+
end
158+
159+
it "doesn't count the vaccination" do
160+
expect(stats).to include(vaccinated: 0)
161+
end
162+
end
138163
end
139164
end

0 commit comments

Comments
 (0)