Skip to content

Commit b33a9ed

Browse files
Merge pull request #5239 from nhsuk/improved_patient_archived_check
MAV-2633: Replace (not_)arcived check with patient_teams check
2 parents 6c9066f + dfba49e commit b33a9ed

3 files changed

Lines changed: 57 additions & 15 deletions

File tree

app/models/patient.rb

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,6 @@ class Patient < ApplicationRecord
9696
# https://www.datadictionary.nhs.uk/attributes/person_gender_code.html
9797
enum :gender_code, { not_known: 0, male: 1, female: 2, not_specified: 9 }
9898

99-
scope :joins_archive_reasons,
100-
->(team:) do
101-
joins(
102-
"LEFT OUTER JOIN archive_reasons " \
103-
"ON archive_reasons.patient_id = patients.id " \
104-
"AND archive_reasons.team_id = #{team.id}"
105-
)
106-
end
107-
10899
scope :joins_sessions, -> { joins(:patient_locations).joins(<<-SQL) }
109100
INNER JOIN sessions
110101
ON sessions.location_id = patient_locations.location_id
@@ -122,12 +113,26 @@ class Patient < ApplicationRecord
122113

123114
scope :archived,
124115
->(team:) do
125-
joins_archive_reasons(team:).where("archive_reasons.id IS NOT NULL")
116+
joins(:patient_teams).where(
117+
patient_teams: {
118+
team_id: team.id
119+
}
120+
).where(
121+
"patient_teams.sources @> ARRAY[?]::integer[]",
122+
PatientTeam.sources.fetch("archive_reason")
123+
)
126124
end
127125

128126
scope :not_archived,
129127
->(team:) do
130-
joins_archive_reasons(team:).where("archive_reasons.id IS NULL")
128+
joins(:patient_teams).where(
129+
patient_teams: {
130+
team_id: team.id
131+
}
132+
).where(
133+
"NOT patient_teams.sources @> ARRAY[?]::integer[]",
134+
PatientTeam.sources.fetch("archive_reason")
135+
)
131136
end
132137

133138
scope :with_pending_changes_for_team,

spec/factories/patient_teams.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,33 @@
2222
#
2323
FactoryBot.define do
2424
factory :patient_team do
25+
# Required associations – these will be created automatically if not overridden
2526
patient
2627
team
2728
sources { %i[patient_location] }
29+
30+
trait :patient_location do
31+
sources { %i[patient_location] }
32+
end
33+
34+
trait :archive_reason do
35+
sources { %i[archive_reason] }
36+
end
37+
38+
trait :vaccination_record_session do
39+
sources { %i[vaccination_record_session] }
40+
end
41+
42+
trait :vaccination_record_organisation do
43+
sources { %i[vaccination_record_organisation] }
44+
end
45+
46+
trait :school_move_team do
47+
sources { %i[school_move_team] }
48+
end
49+
50+
trait :school_move_school do
51+
sources { %i[school_move_school] }
52+
end
2853
end
2954
end

spec/models/patient_spec.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,22 @@
7777
let(:team) { create(:team) }
7878

7979
context "without an archive reason" do
80+
before { create(:patient_team, team:, patient:) }
81+
8082
it { should_not include(patient) }
8183
end
8284

8385
context "with an archive reason for the team" do
84-
before { create(:archive_reason, :moved_out_of_area, team:, patient:) }
86+
before { create(:patient_team, :archive_reason, team:, patient:) }
8587

8688
it { should include(patient) }
8789
end
8890

8991
context "with an archive reason for a different team" do
90-
before { create(:archive_reason, :imported_in_error, patient:) }
92+
before do
93+
create(:patient_team, team:, patient:)
94+
create(:patient_team, :archive_reason, team: create(:team), patient:)
95+
end
9196

9297
it { should_not include(patient) }
9398
end
@@ -100,17 +105,22 @@
100105
let(:team) { create(:team) }
101106

102107
context "without an archive reason" do
108+
before { create(:patient_team, team:, patient:) }
109+
103110
it { should include(patient) }
104111
end
105112

106113
context "with an archive reason for the team" do
107-
before { create(:archive_reason, :moved_out_of_area, team:, patient:) }
114+
before { create(:patient_team, :archive_reason, team:, patient:) }
108115

109116
it { should_not include(patient) }
110117
end
111118

112119
context "with an archive reason for a different team" do
113-
before { create(:archive_reason, :imported_in_error, patient:) }
120+
before do
121+
create(:patient_team, team:, patient:)
122+
create(:patient_team, :archive_reason, team: create(:team), patient:)
123+
end
114124

115125
it { should include(patient) }
116126
end
@@ -122,6 +132,8 @@
122132
let(:team) { create(:team) }
123133
let(:patient) { create(:patient) }
124134

135+
before { create(:patient_team, team:, patient:) }
136+
125137
context "without pending changes" do
126138
it { should_not include(patient) }
127139
end

0 commit comments

Comments
 (0)