Skip to content

Commit 0229833

Browse files
committed
Hide patients vaccinated by the same organisation but different team
At the moment if a patient is vaccinated by one team, and there's another team in the same organisation, they will be shown the patient because it's vaccination record has the same ODS code. Instead, for a non-historical vaccination, we shouldn't be looking at the ODS code, instead we should be looking at the team that's attached to the session. Jira-Issue: MAV-1280
1 parent 69d119c commit 0229833

File tree

4 files changed

+59
-11
lines changed

4 files changed

+59
-11
lines changed

app/policies/patient_policy.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@ def resolve
2727
.arel
2828
.exists
2929

30+
vaccination_records_for_patients =
31+
VaccinationRecord.where("vaccination_records.patient_id = patients.id")
32+
3033
vaccination_record_exists =
31-
VaccinationRecord
32-
.where("vaccination_records.patient_id = patients.id")
34+
vaccination_records_for_patients
3335
.where(session: team.sessions)
3436
.or(
35-
VaccinationRecord.where(
36-
"vaccination_records.patient_id = patients.id"
37-
).where(performed_ods_code: organisation.ods_code)
37+
vaccination_records_for_patients.where(
38+
performed_ods_code: organisation.ods_code,
39+
session_id: nil
40+
)
3841
)
3942
.arel
4043
.exists

app/policies/vaccination_record_policy.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ def resolve
3232
.kept
3333
.where(patient: team.patients)
3434
.or(scope.kept.where(session: team.sessions))
35-
.or(scope.kept.where(performed_ods_code: organisation.ods_code))
35+
.or(
36+
scope.kept.where(
37+
performed_ods_code: organisation.ods_code,
38+
session_id: nil
39+
)
40+
)
3641
end
3742
end
3843
end

spec/policies/patient_policy_spec.rb

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
subject { PatientPolicy::Scope.new(user, Patient).resolve }
66

77
let(:programmes) { [create(:programme)] }
8-
let(:team) { create(:team, programmes:) }
9-
let(:another_team) { create(:team, programmes:) }
8+
let(:organisation) { create(:organisation) }
9+
let(:team) { create(:team, organisation:, programmes:) }
10+
let(:another_team) { create(:team, organisation:, programmes:) }
1011
let(:user) { create(:user, team:) }
1112

1213
context "when a patient is archived" do
@@ -97,6 +98,30 @@
9798
it { should contain_exactly(patient_with_move_in_school) }
9899
end
99100

101+
context "when the patient in the org but vaccinated by a different team" do
102+
let(:patient_with_vaccination_record) { create(:patient) }
103+
let(:patient_with_another_vaccination_record) { create(:patient) }
104+
105+
before do
106+
create(
107+
:vaccination_record,
108+
patient: patient_with_vaccination_record,
109+
performed_ods_code: organisation.ods_code,
110+
session: create(:session, team:, programmes:),
111+
programme: programmes.first
112+
)
113+
create(
114+
:vaccination_record,
115+
patient: patient_with_another_vaccination_record,
116+
performed_ods_code: organisation.ods_code,
117+
session: create(:session, team: another_team, programmes:),
118+
programme: programmes.first
119+
)
120+
end
121+
122+
it { should contain_exactly(patient_with_vaccination_record) }
123+
end
124+
100125
context "when the patient not in the org but was vaccinated by them" do
101126
let(:patient_with_vaccination_record) { create(:patient) }
102127
let(:patient_with_another_vaccination_record) { create(:patient) }
@@ -105,7 +130,7 @@
105130
create(
106131
:vaccination_record,
107132
patient: patient_with_vaccination_record,
108-
performed_ods_code: team.organisation.ods_code,
133+
performed_ods_code: organisation.ods_code,
109134
programme: programmes.first
110135
)
111136
create(

spec/policies/vaccination_record_policy_spec.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,21 @@
7373
end
7474

7575
describe "Scope#resolve" do
76-
subject(:resolve) do
76+
subject(:scope) do
7777
VaccinationRecordPolicy::Scope.new(user, VaccinationRecord).resolve
7878
end
7979

8080
let(:programme) { create(:programme) }
81-
let(:team) { create(:team, programmes: [programme]) }
81+
let(:organisation) { create(:organisation) }
82+
83+
let(:team) { create(:team, organisation:, programmes: [programme]) }
84+
let(:other_team) { create(:team, organisation:, programmes: [programme]) }
8285
let(:user) { create(:user, team:) }
8386

8487
let(:session) { create(:session, team:, programmes: [programme]) }
88+
let(:other_session) do
89+
create(:session, team: other_team, programmes: [programme])
90+
end
8591

8692
let(:kept_vaccination_record) do
8793
create(:vaccination_record, session:, programme:)
@@ -90,9 +96,18 @@
9096
create(:vaccination_record, :discarded, session:, programme:)
9197
end
9298
let(:non_team_kept_batch) { create(:vaccination_record, programme:) }
99+
let(:vaccination_record_same_organisation_different_team) do
100+
create(:vaccination_record, session: other_session, programme:)
101+
end
93102

94103
it { should include(kept_vaccination_record) }
95104
it { should_not include(discarded_vaccination_record) }
96105
it { should_not include(non_team_kept_batch) }
106+
107+
it do
108+
expect(scope).not_to include(
109+
vaccination_record_same_organisation_different_team
110+
)
111+
end
97112
end
98113
end

0 commit comments

Comments
 (0)