Skip to content

Commit df6f2db

Browse files
committed
Ensure Patient#teams doesn't return duplicates
If a patient belongs to multiple sessions for the same team (which is normal) then this method returns the same team multiple times. Although this is not really an issue in the way this method is used, it does cause a problem when creating archive reasons when the patient is deceased as we try and create multiple archive reasons for the same team which results in a unique constraint failing. Sentry-Issue: 6902363114
1 parent 755f3a1 commit df6f2db

2 files changed

Lines changed: 52 additions & 17 deletions

File tree

app/models/patient.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -428,12 +428,12 @@ def sessions
428428
end
429429

430430
def teams
431-
Team.left_outer_joins(:sessions).joins(<<-SQL)
432-
INNER JOIN patient_locations
433-
ON patient_locations.patient_id = #{id}
434-
AND patient_locations.location_id = sessions.location_id
435-
AND patient_locations.academic_year = sessions.academic_year
436-
SQL
431+
Team.distinct.joins(:sessions).joins(<<-SQL)
432+
INNER JOIN patient_locations
433+
ON patient_locations.patient_id = #{id}
434+
AND patient_locations.location_id = sessions.location_id
435+
AND patient_locations.academic_year = sessions.academic_year
436+
SQL
437437
end
438438

439439
def archived?(team:)

spec/models/patient_spec.rb

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@
5151
describe Patient do
5252
describe "associations" do
5353
it { should have_many(:archive_reasons) }
54+
55+
describe "#vaccination_records" do
56+
subject(:vaccination_records) { patient.vaccination_records }
57+
58+
let(:patient) { create(:patient) }
59+
let(:programme) { create(:programme) }
60+
let(:kept_vaccination_record) do
61+
create(:vaccination_record, patient:, programme:)
62+
end
63+
let(:discarded_vaccination_record) do
64+
create(:vaccination_record, :discarded, patient:, programme:)
65+
end
66+
67+
it { should include(kept_vaccination_record) }
68+
it { should_not include(discarded_vaccination_record) }
69+
end
5470
end
5571

5672
describe "scopes" do
@@ -464,20 +480,39 @@
464480
it { should normalize(:address_postcode).from(" SW111AA ").to("SW11 1AA") }
465481
end
466482

467-
describe "#vaccination_records" do
468-
subject(:vaccination_records) { patient.vaccination_records }
483+
describe "#teams" do
484+
subject(:teams) { patient.teams }
469485

470486
let(:patient) { create(:patient) }
471-
let(:programme) { create(:programme) }
472-
let(:kept_vaccination_record) do
473-
create(:vaccination_record, patient:, programme:)
474-
end
475-
let(:discarded_vaccination_record) do
476-
create(:vaccination_record, :discarded, patient:, programme:)
477-
end
478487

479-
it { should include(kept_vaccination_record) }
480-
it { should_not include(discarded_vaccination_record) }
488+
it { should be_empty }
489+
490+
context "when a team exists" do
491+
let!(:team) { create(:team) }
492+
493+
it { should be_empty }
494+
495+
context "and the patient belongs to the team" do
496+
let(:session) { create(:session, team:) }
497+
let(:patient) { create(:patient, session:) }
498+
499+
it { should include(team) }
500+
end
501+
502+
context "and the patient belongs to multiple sessions under the same team" do
503+
let(:menacwy_session) do
504+
create(:session, team:, programmes: [create(:programme, :menacwy)])
505+
end
506+
let(:td_ipv_session) do
507+
create(:session, team:, programmes: [create(:programme, :td_ipv)])
508+
end
509+
let(:patient) { create(:patient, session: menacwy_session) }
510+
511+
before { create(:patient_location, patient:, session: td_ipv_session) }
512+
513+
it { should contain_exactly(team) }
514+
end
515+
end
481516
end
482517

483518
describe "#match_existing" do

0 commit comments

Comments
 (0)