Skip to content

Commit

Permalink
Add patient to sessions when importing vaccinations
Browse files Browse the repository at this point in the history
This fixes a regression that was added in
a01aa7f which meant that when importing
vaccination records for the current academic year (where a session must
exist) we weren't adding the patients to that session if they weren't
already in the session.

Although the data model allows patients to be vaccinated and doesn't
require them to be in the session at that point in time, in this case
it's important that the patients are visible in the session so their
status is represented accurately.
  • Loading branch information
thomasleese committed Feb 14, 2025
1 parent 6d965f4 commit 93378bd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
10 changes: 9 additions & 1 deletion app/models/immunisation_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,18 @@ def process_row(row)
@vaccination_records_batch ||= Set.new
@batches_batch ||= Set.new
@patients_batch ||= Set.new
@patient_sessions_batch ||= Set.new

@vaccination_records_batch.add(vaccination_record)
if vaccination_record.administered?
@batches_batch.add(vaccination_record.batch)
end
@patients_batch.add(vaccination_record.patient)

if (patient_session = row.to_patient_session)
@patient_sessions_batch.add(patient_session)
end

count_column_to_increment
end

Expand All @@ -91,13 +96,16 @@ def bulk_import(rows: 100)
# We need to convert the batch to an array as `import` modifies the
# objects to add IDs to any new records.
vaccination_records = @vaccination_records_batch.to_a
patient_sessions = @patient_sessions_batch.to_a

VaccinationRecord.import(vaccination_records, on_duplicate_key_update: :all)
PatientSession.import(patient_sessions, on_duplicate_key_ignore: :all)

[
[:vaccination_records, vaccination_records],
[:batches, @batches_batch],
[:patients, @patients_batch]
[:patients, @patients_batch],
[:patient_sessions, patient_sessions.select { it.id.present? }]
].each do |association, collection|
link_records_by_type(association, collection)
collection.clear
Expand Down
6 changes: 6 additions & 0 deletions app/models/immunisation_import_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ def to_vaccination_record
vaccination_record
end

def to_patient_session
return if patient.nil? || session.nil?

PatientSession.new(patient:, session:)
end

def patient
return unless valid?

Expand Down
36 changes: 36 additions & 0 deletions spec/models/immunisation_import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
.and change(immunisation_import.vaccination_records, :count).by(11)
.and change(immunisation_import.patients, :count).by(11)
.and change(immunisation_import.batches, :count).by(4)
.and not_change(immunisation_import.patient_sessions, :count)

# Second import should not duplicate the vaccination records if they're
# identical.
Expand Down Expand Up @@ -177,6 +178,23 @@
.times
.on_queue(:imports)
end

it "adds patients to the session if it exists" do
# must match a session in valid_flu.csv
location = Location.school.find_by!(urn: "120026")
create(
:session,
location:,
organisation:,
programme:,
date: Date.new(2024, 5, 14)
)

expect { process! }.to change(
immunisation_import.patient_sessions,
:count
).by(8)
end
end

context "with valid HPV rows" do
Expand All @@ -190,6 +208,7 @@
.and change(immunisation_import.vaccination_records, :count).by(11)
.and change(immunisation_import.patients, :count).by(10)
.and change(immunisation_import.batches, :count).by(9)
.and not_change(immunisation_import.patient_sessions, :count)

# Second import should not duplicate the vaccination records if they're
# identical.
Expand Down Expand Up @@ -230,6 +249,23 @@
.times
.on_queue(:imports)
end

it "adds patients to the session if it exists" do
# must match a session in valid_hpv.csv
location = Location.school.find_by!(urn: "110158")
create(
:session,
location:,
organisation:,
programme:,
date: Date.new(2024, 5, 14)
)

expect { process! }.to change(
immunisation_import.patient_sessions,
:count
).by(6)
end
end

context "with an existing patient matching the name" do
Expand Down

0 comments on commit 93378bd

Please sign in to comment.