Skip to content

Commit

Permalink
Look up NHS numbers after importing
Browse files Browse the repository at this point in the history
When importing patients, from either of the three imports (cohorts,
classes and immunisations), we should look up the NHS number of any
records for which we don't have the NHS number available, by using the
`PDSLookupJob`.
  • Loading branch information
thomasleese committed Oct 16, 2024
1 parent 9635bf5 commit eb68186
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 5 deletions.
1 change: 0 additions & 1 deletion app/models/class_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class ClassImport < PatientImport

has_and_belongs_to_many :parent_relationships
has_and_belongs_to_many :parents
has_and_belongs_to_many :patients

private

Expand Down
1 change: 0 additions & 1 deletion app/models/cohort_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class CohortImport < PatientImport

has_and_belongs_to_many :parent_relationships
has_and_belongs_to_many :parents
has_and_belongs_to_many :patients

private

Expand Down
10 changes: 10 additions & 0 deletions app/models/concerns/csv_importable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ module CSVImportable
class_name: "User",
foreign_key: :uploaded_by_user_id

has_and_belongs_to_many :patients

scope :csv_not_removed, -> { where(csv_removed_at: nil) }

enum :status,
Expand Down Expand Up @@ -101,11 +103,19 @@ def record!

update_columns(recorded_at: Time.zone.now, status: :recorded, **counts)
end

look_up_missing_nhs_numbers
end

def postprocess_rows!
end

def look_up_missing_nhs_numbers
patients.without_nhs_number.find_each do |patient|
PDSLookupJob.perform_later(patient)
end
end

def remove!
return if csv_removed?
update!(csv_data: nil, csv_removed_at: Time.zone.now)
Expand Down
1 change: 0 additions & 1 deletion app/models/immunisation_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class ImmunisationImport < ApplicationRecord
has_and_belongs_to_many :batches
has_and_belongs_to_many :locations
has_and_belongs_to_many :patient_sessions
has_and_belongs_to_many :patients
has_and_belongs_to_many :sessions
has_and_belongs_to_many :vaccination_records

Expand Down
2 changes: 2 additions & 0 deletions app/models/patient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class Patient < ApplicationRecord
# https://www.datadictionary.nhs.uk/attributes/person_gender_code.html
enum :gender_code, { not_known: 0, male: 1, female: 2, not_specified: 9 }

scope :without_nhs_number, -> { where(nhs_number: [nil, ""]) }

validates :given_name, :family_name, :date_of_birth, presence: true

validates :nhs_number,
Expand Down
8 changes: 8 additions & 0 deletions spec/features/dev_reset_team_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

scenario "Resetting a team deletes all associated data" do
given_an_example_programme_exists
and_requests_can_be_made_to_dps
and_patients_have_been_imported
and_vaccination_records_have_been_imported

Expand All @@ -27,6 +28,13 @@ def given_an_example_programme_exists
@user = @team.users.first
end

def and_requests_can_be_made_to_dps
stub_request(
:get,
"https://sandbox.api.service.nhs.uk/personal-demographics/FHIR/R4/Patient"
).with(query: hash_including({})).to_return_json(body: { total: 0 })
end

def and_patients_have_been_imported
sign_in @user
visit "/dashboard"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
describe "Immunisation imports duplicates" do
scenario "User reviews and selects between duplicate records" do
given_i_am_signed_in
and_requests_can_be_made_to_dps
and_an_hpv_programme_is_underway
and_an_existing_patient_record_exists

Expand Down Expand Up @@ -50,6 +51,13 @@ def given_i_am_signed_in
sign_in @team.users.first
end

def and_requests_can_be_made_to_dps
stub_request(
:get,
"https://sandbox.api.service.nhs.uk/personal-demographics/FHIR/R4/Patient"
).with(query: hash_including({})).to_return_json(body: { total: 0 })
end

def and_an_hpv_programme_is_underway
@programme = create(:programme, :hpv_all_vaccines)
create(:team_programme, team: @team, programme: @programme)
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/cohort_import/valid.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CHILD_SCHOOL_URN,PARENT_1_NAME,PARENT_1_RELATIONSHIP,PARENT_1_EMAIL,PARENT_1_PHONE,PARENT_2_NAME,PARENT_2_RELATIONSHIP,PARENT_2_EMAIL,PARENT_2_PHONE,CHILD_FIRST_NAME,CHILD_LAST_NAME,CHILD_COMMON_NAME,CHILD_DATE_OF_BIRTH,CHILD_ADDRESS_LINE_1,CHILD_ADDRESS_LINE_2,CHILD_TOWN,CHILD_POSTCODE,CHILD_NHS_NUMBER
123456,,,,,,,,,Jennifer,Clarke,Jenny,2010-01-01,10 Downing Street,,London,SW1A 1AA,1234567890
123456,John Smith,Father,[email protected],07412345678,,,,,Jimmy,Smith,Jim,02/01/2010,10 Downing Street,,London,SW1A 1AA,123 456 7891
123456,Jane Doe,Mother,[email protected],07412345679,Richard Doe,Father,[email protected],,Mark,Doe,,2010-01-03,11 Downing Street,,London,SW1A 1AA,1234567892
123456,Jane Doe,Mother,[email protected],07412345679,Richard Doe,Father,[email protected],,Mark,Doe,,2010-01-03,11 Downing Street,,London,SW1A 1AA,
4 changes: 4 additions & 0 deletions spec/models/class_import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@
expect(class_import.exact_duplicate_record_count).to eq(4)
end

it "enqueues jobs to look up missing NHS numbers" do
expect { record! }.to have_enqueued_job(PDSLookupJob).once.on_queue(:pds)
end

context "with an existing patient matching the name" do
before do
create(
Expand Down
6 changes: 5 additions & 1 deletion spec/models/cohort_import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
expect(Patient.second.parent_relationships.first).to be_father

expect(Patient.third).to have_attributes(
nhs_number: "1234567892",
nhs_number: nil,
date_of_birth: Date.new(2010, 1, 3),
full_name: "Mark Doe",
school: location,
Expand Down Expand Up @@ -242,6 +242,10 @@
expect(cohort_import.exact_duplicate_record_count).to eq(3)
end

it "enqueues jobs to look up missing NHS numbers" do
expect { record! }.to have_enqueued_job(PDSLookupJob).once.on_queue(:pds)
end

context "with an existing patient matching the name" do
before do
create(
Expand Down
12 changes: 12 additions & 0 deletions spec/models/immunisation_import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@
0
).to(7)
end

it "enqueues jobs to look up missing NHS numbers" do
expect { record! }.to have_enqueued_job(PDSLookupJob).once.on_queue(
:pds
)
end
end

context "with valid HPV rows" do
Expand Down Expand Up @@ -255,6 +261,12 @@
0
).to(11)
end

it "enqueues jobs to look up missing NHS numbers" do
expect { record! }.to have_enqueued_job(PDSLookupJob).once.on_queue(
:pds
)
end
end

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

0 comments on commit eb68186

Please sign in to comment.