Skip to content

Commit 9f0eb01

Browse files
authored
Merge pull request #6647 from NHSDigital/patient-status-updater-improvements
Patient status updater improvements
2 parents 43a00eb + 50b02d6 commit 9f0eb01

3 files changed

Lines changed: 32 additions & 39 deletions

File tree

app/lib/patient_status_updater.rb

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,15 @@ def update_registration_statuses!
9292
merge_patient_scope(Patient::RegistrationStatus)
9393
.joins(session: :team_location)
9494
.where(team_location: { academic_year: academic_years })
95-
.includes(:attendance_records, :patient, :session, :vaccination_records)
96-
.find_in_batches do |batch|
95+
.in_batches do |relation|
96+
batch =
97+
relation.includes(
98+
:attendance_records,
99+
:patient,
100+
:session,
101+
:vaccination_records
102+
).to_a
103+
97104
batch.each(&:assign_status)
98105

99106
Patient::RegistrationStatus.import!(
@@ -106,23 +113,6 @@ def update_registration_statuses!
106113
end
107114
end
108115

109-
def patient_statuses_to_import
110-
@patient_statuses_to_import ||=
111-
(patient_scope || Patient.all)
112-
.pluck(:id, :birth_academic_year)
113-
.flat_map do |patient_id, birth_academic_year|
114-
academic_years.flat_map do |academic_year|
115-
year_group = birth_academic_year.to_year_group(academic_year:)
116-
117-
programme_types_per_year_group
118-
.fetch(year_group, [])
119-
.map do |programme_type|
120-
[patient_id, programme_type, academic_year]
121-
end
122-
end
123-
end
124-
end
125-
126116
def programme_statuses_to_import
127117
@programme_statuses_to_import ||=
128118
(patient_scope || Patient.all)
@@ -161,19 +151,6 @@ def patient_location_statuses_to_import
161151
end
162152
end
163153

164-
def programme_types_per_year_group
165-
@programme_types_per_year_group ||=
166-
Location::ProgrammeYearGroup
167-
.joins(:location_year_group)
168-
.where(location_year_group: { academic_year: academic_years })
169-
.distinct
170-
.pluck(:programme_type, :"location_year_group.value")
171-
.each_with_object({}) do |(programme_type, year_group), hash|
172-
hash[year_group] ||= []
173-
hash[year_group] << programme_type
174-
end
175-
end
176-
177154
def programme_types_per_session_id_and_year_group
178155
@programme_types_per_session_id_and_year_group ||=
179156
Session::ProgrammeYearGroup

app/models/immunisation_import.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,6 @@ def process!
8989
end
9090

9191
post_commit!
92-
UpdatePatientsFromPDS.call(patients, queue: :imports)
93-
94-
TeamCachedCounts.new(team).reset_import_issues!
9592
end
9693

9794
private
@@ -214,18 +211,22 @@ def postprocess_rows!
214211
.find_each do |vaccination_record|
215212
NextDoseTriageFactory.call(vaccination_record:)
216213
end
214+
end
217215

216+
def post_commit!
218217
PatientTeamUpdater.call(patient_scope: patients)
219218
PatientStatusUpdater.call(patient_scope: Patient.where(id: patients.ids))
220-
end
221219

222-
def post_commit!
223220
vaccination_records.sync_all_to_nhs_immunisations_api
224221

225222
vaccination_records
226223
.includes(:patient, :team, :subteam)
227224
.find_each do |vaccination_record|
228225
AlreadyHadNotificationSender.call(vaccination_record:)
229226
end
227+
228+
UpdatePatientsFromPDS.call(patients, queue: :imports)
229+
230+
TeamCachedCounts.new(team).reset_import_issues!
230231
end
231232
end

spec/models/immunisation_import_spec.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,15 +524,30 @@
524524
create(
525525
:immunisation_import,
526526
team:,
527-
vaccination_records: [vaccination_record]
527+
vaccination_records: [vaccination_record],
528+
patients: [create(:patient)]
528529
)
529530
end
530531
let(:session) { create(:session, location: school, programmes:) }
531532
let(:vaccination_record) do
532533
create(:vaccination_record, programme: programmes.first, session:)
533534
end
534535

535-
before { Flipper.enable(:imms_api_sync_job) }
536+
it "calls the PatientTeamUpdater with imported patients" do
537+
expect(PatientTeamUpdater).to receive(:call).with(
538+
patient_scope: immunisation_import.patients
539+
)
540+
541+
immunisation_import.send :post_commit!
542+
end
543+
544+
it "calls the PatientStatusUpdater with imported patients" do
545+
expect(PatientStatusUpdater).to receive(:call).with(
546+
patient_scope: Patient.where(id: immunisation_import.patients.ids)
547+
)
548+
549+
immunisation_import.send :post_commit!
550+
end
536551

537552
it "syncs the flu vaccination record to the NHS Immunisations API" do
538553
expect { immunisation_import.send :post_commit! }.to enqueue_sidekiq_job(

0 commit comments

Comments
 (0)