Skip to content

Commit

Permalink
Handle importing multiple doses in same file
Browse files Browse the repository at this point in the history
If the same patient appears multiple times in a single file we currently
get an error because we try and create multiple patients for the same
NHS number, and other personal details.

To fix this we can looking for duplicate patients each time we process a
row, ensuring that if a patient was created in a previous row, we would
now match with it and avoid creating a second patient.
  • Loading branch information
thomasleese committed Oct 7, 2024
1 parent c68fd76 commit 3aea12e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
15 changes: 7 additions & 8 deletions app/models/immunisation_import_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,13 @@ def date_of_birth_in_a_valid_year_group
end

def find_existing_patients
@find_existing_patients ||=
Patient.find_existing(
nhs_number: patient_nhs_number,
first_name: patient_first_name,
last_name: patient_last_name,
date_of_birth: patient_date_of_birth,
address_postcode: patient_postcode
)
Patient.find_existing(
nhs_number: patient_nhs_number,
first_name: patient_first_name,
last_name: patient_last_name,
date_of_birth: patient_date_of_birth,
address_postcode: patient_postcode
)
end

def zero_or_one_existing_patient
Expand Down
4 changes: 2 additions & 2 deletions spec/features/dev_reset_team_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def and_vaccination_records_have_been_imported
)

click_on "Upload records"
expect(VaccinationRecord.count).to eq(7)
expect(VaccinationRecord.count).to eq(8)
end

def then_all_associated_data_is_deleted_when_i_reset_the_team
Expand All @@ -71,7 +71,7 @@ def then_all_associated_data_is_deleted_when_i_reset_the_team
.by(-10)
.and(change(Cohort, :count).by(-2))
.and(change(Parent, :count).by(-3))
.and(change(VaccinationRecord, :count).by(-7))
.and(change(VaccinationRecord, :count).by(-8))
.and(change(ImmunisationImport, :count).by(-1))
.and(change(CohortImport, :count).by(-1))
)
Expand Down
2 changes: 1 addition & 1 deletion spec/features/import_vaccination_records_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def and_i_upload_a_valid_file
end

def then_i_should_see_the_success_heading
expect(page).to have_content("7 new vaccination records")
expect(page).to have_content("8 new vaccination records")
end

def then_i_should_see_the_vaccination_records
Expand Down
14 changes: 7 additions & 7 deletions spec/models/immunisation_import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@
.and change(immunisation_import.vaccination_records, :count).by(8)
.and change(immunisation_import.locations, :count).by(1)
.and change(immunisation_import.patients, :count).by(7)
.and change(immunisation_import.sessions, :count).by(2)
.and change(immunisation_import.patient_sessions, :count).by(7)
.and change(immunisation_import.batches, :count).by(5)
.and change(immunisation_import.sessions, :count).by(3)
.and change(immunisation_import.patient_sessions, :count).by(8)
.and change(immunisation_import.batches, :count).by(6)

# Second import should not duplicate the vaccination records if they're
# identical.
Expand All @@ -202,7 +202,7 @@
# stree-ignore
expect { process! }
.to change(immunisation_import, :exact_duplicate_record_count).to(0)
.and change(immunisation_import, :new_record_count).to(7)
.and change(immunisation_import, :new_record_count).to(8)
.and change(immunisation_import, :not_administered_record_count).to(0)
end

Expand All @@ -211,13 +211,13 @@
csv.rewind

process!
expect(immunisation_import.exact_duplicate_record_count).to eq(7)
expect(immunisation_import.exact_duplicate_record_count).to eq(8)
end

it "creates a new session for each date" do
process!

expect(immunisation_import.sessions.count).to eq(2)
expect(immunisation_import.sessions.count).to eq(3)

session = immunisation_import.sessions.first
expect(session.dates.map(&:value)).to contain_exactly(
Expand Down Expand Up @@ -318,7 +318,7 @@

it "activates the patient sessions" do
expect { record! }.to change(PatientSession.active, :count).from(0).to(
7
8
)
end
end
Expand Down

0 comments on commit 3aea12e

Please sign in to comment.