Skip to content

Commit ac88722

Browse files
authored
Filter reports by programme (#3231)
When requesting a report, we should only be returning vaccination records for the programme the user requested the report for. At the moment, the programme is being ignored in the CarePlus report and we're instead returning all the vaccination records for the organisation. I've also refactored the code to make it clearer where the vaccination records are coming from.
2 parents b0d020b + 8c465b2 commit ac88722

File tree

7 files changed

+76
-19
lines changed

7 files changed

+76
-19
lines changed

app/lib/reports/careplus_exporter.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ def initialize(organisation:, programme:, start_date:, end_date:)
1010

1111
def call
1212
CSV.generate(headers:, write_headers: true) do |csv|
13-
programme
13+
organisation
1414
.sessions
15-
.where(organisation:)
15+
.has_programme(programme)
1616
.find_each do |session|
1717
patient_sessions_for_session(session).each do |patient_session|
1818
rows(patient_session:).each { |row| csv << row }
@@ -116,19 +116,18 @@ def patient_sessions_for_session(session)
116116
def rows(patient_session:)
117117
patient = patient_session.patient
118118

119-
patient_session.programmes.filter_map do |programme|
120-
vaccination_records =
121-
patient_session.session_outcome.all[programme].select(&:administered?)
119+
vaccination_records =
120+
patient_session.session_outcome.all[programme].select(&:administered?)
122121

123-
if vaccination_records.any?
124-
existing_row(patient:, patient_session:, vaccination_records:)
125-
end
122+
if vaccination_records.any?
123+
[existing_row(patient:, patient_session:, vaccination_records:)]
124+
else
125+
[]
126126
end
127127
end
128128

129129
def existing_row(patient:, patient_session:, vaccination_records:)
130130
first_vaccination = vaccination_records.first
131-
programme = first_vaccination.programme
132131

133132
[
134133
patient.nhs_number,

app/lib/reports/programme_vaccinations_exporter.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,13 @@ def headers
9191

9292
def vaccination_records
9393
scope =
94-
programme
94+
organisation
9595
.vaccination_records
96-
.joins(:organisation)
97-
.where(organisations: { id: organisation.id })
96+
.where(programme:)
9897
.includes(
9998
:batch,
10099
:location,
101100
:performed_by_user,
102-
:programme,
103101
:vaccine,
104102
patient: [:gp_practice, :school, :triages, { consents: :parent }]
105103
)
@@ -146,7 +144,6 @@ def gillick_assessments
146144
def row(vaccination_record:)
147145
location = vaccination_record.location
148146
patient = vaccination_record.patient
149-
programme = vaccination_record.programme
150147
session = vaccination_record.session
151148

152149
consents = patient.consent_outcome.latest[programme]

app/lib/reports/systm_one_exporter.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,11 @@ def headers
7878

7979
def vaccination_records
8080
scope =
81-
programme
81+
organisation
8282
.vaccination_records
83-
.joins(:organisation)
84-
.where(organisations: { id: organisation.id })
85-
.merge(VaccinationRecord.administered)
86-
.includes(:batch, :location, :programme, :vaccine, :patient)
83+
.administered
84+
.where(programme:)
85+
.includes(:batch, :location, :vaccine, :patient)
8786

8887
if start_date.present?
8988
scope =

app/models/organisation.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class Organisation < ApplicationRecord
4848
has_many :programmes, through: :organisation_programmes
4949
has_many :schools, through: :teams
5050
has_many :vaccines, through: :programmes
51+
has_many :vaccination_records, through: :sessions
5152

5253
has_and_belongs_to_many :users
5354

spec/lib/reports/careplus_exporter_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,25 @@
186186
expect(data_rows.first).not_to be_nil
187187
end
188188

189+
it "excludes vaccination records for a different programme outside the date range" do
190+
patient = create(:patient_session, session:).patient
191+
192+
other_programme =
193+
create(
194+
:programme,
195+
type: (Programme.types.values - [programme.type]).sample
196+
)
197+
198+
create(
199+
:vaccination_record,
200+
programme: other_programme,
201+
patient:,
202+
session:
203+
)
204+
205+
expect(data_rows.first).to be_nil
206+
end
207+
189208
context "with a session in a different organisation" do
190209
let(:session) { create(:session, programmes:, location:) }
191210

spec/lib/reports/programme_vaccinations_exporter_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,28 @@
208208
it { should be_empty }
209209
end
210210

211+
context "with a vaccination for a different programme" do
212+
let(:patient) { create(:patient_session, session:).patient }
213+
214+
let(:other_programme) do
215+
create(
216+
:programme,
217+
type: (Programme.types.values - programmes.map(&:type)).sample
218+
)
219+
end
220+
221+
let(:vaccination_record) do
222+
create(
223+
:vaccination_record,
224+
programme: other_programme,
225+
patient:,
226+
session:
227+
)
228+
end
229+
230+
it { should be_blank }
231+
end
232+
211233
context "with a vaccinated patient that was updated in the date range" do
212234
let(:patient) { create(:patient_session, session:).patient }
213235
let(:start_date) { 1.day.ago }

spec/lib/reports/systm_one_exporter_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,26 @@
113113
it { should_not be_blank }
114114
end
115115

116+
context "with vaccination records for a different programme" do
117+
let(:other_programme) do
118+
create(
119+
:programme,
120+
type: (Programme.types.values - [programme.type]).sample
121+
)
122+
end
123+
124+
let(:vaccination_record) do
125+
create(
126+
:vaccination_record,
127+
programme: other_programme,
128+
patient:,
129+
session:
130+
)
131+
end
132+
133+
it { should be_blank }
134+
end
135+
116136
context "with a session in a different organisation" do
117137
let(:programme) do
118138
create(:programme, :hpv, organisations: [other_organisation])

0 commit comments

Comments
 (0)