Skip to content

Commit

Permalink
Enqueue PatientUpdateFromPDSJob jobs
Browse files Browse the repository at this point in the history
When importing patients, if we have an NHS number for the patient
already then we can enqueue these jobs to check in PDS that the patient
doesn't have any flags set on them such as restricted, or to check that
they're not deceased.

To simplify some of the feature tests, I've removed a call to run
enqueued jobs, which works because the files we're uploading aren't big
enough to trigger the import running in the background.
  • Loading branch information
thomasleese committed Oct 18, 2024
1 parent d395c4a commit 145cfcc
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 25 deletions.
2 changes: 1 addition & 1 deletion app/jobs/patient_update_from_pds_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class PatientUpdateFromPDSJob < ApplicationJob
include NHSAPIConcurrencyConcern

queue_as :patients
queue_as :imports

def perform(patient)
raise MissingNHSNumber if patient.nhs_number.nil?
Expand Down
12 changes: 8 additions & 4 deletions app/models/concerns/csv_importable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,16 @@ def record!
update_columns(recorded_at: Time.zone.now, status: :recorded, **counts)
end

look_up_missing_nhs_numbers
update_from_pds
end

def look_up_missing_nhs_numbers
patients.without_nhs_number.find_each do |patient|
PatientNHSNumberLookupJob.perform_later(patient)
def update_from_pds
patients.find_each do |patient|
if patient.nhs_number.nil?
PatientNHSNumberLookupJob.perform_later(patient)
else
PatientUpdateFromPDSJob.perform_later(patient)
end
end
end

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

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

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

def and_requests_can_be_made_to_pds
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 All @@ -45,7 +37,6 @@ def and_patients_have_been_imported
attach_file("cohort_import[csv]", "spec/fixtures/cohort_import/valid.csv")
click_on "Continue"

perform_enqueued_jobs
expect(@team.cohorts.flat_map(&:patients).size).to eq(3)
expect(@team.cohorts.flat_map(&:patients).flat_map(&:parents).size).to eq(3)
end
Expand All @@ -62,7 +53,6 @@ def and_vaccination_records_have_been_imported
)
click_on "Continue"

perform_enqueued_jobs
expect(VaccinationRecord.count).to eq(11)
end

Expand Down
1 change: 0 additions & 1 deletion spec/features/e2e_journey_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ def when_i_upload_the_cohort_import_containing_one_child
click_on "Import child records"
attach_file "cohort_import[csv]", csv_file.path
click_on "Continue"
perform_enqueued_jobs
visit programme_cohort_import_path(@programme, CohortImport.last)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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_pds
and_an_hpv_programme_is_underway
and_an_existing_patient_record_exists

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

def and_requests_can_be_made_to_pds
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 Expand Up @@ -153,7 +145,6 @@ def and_i_upload_a_file_with_duplicate_records
"spec/fixtures/immunisation_import/valid_hpv.csv"
)
click_on "Continue"
perform_enqueued_jobs
click_link ImmunisationImport.last.created_at.to_fs(:long), match: :first
end

Expand Down
7 changes: 7 additions & 0 deletions spec/models/class_import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,13 @@
).once.on_queue(:imports)
end

it "enqueues jobs to update from PDS" do
expect { record! }.to have_enqueued_job(PatientUpdateFromPDSJob)
.exactly(3)
.times
.on_queue(:imports)
end

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

it "enqueues jobs to update from PDS" do
expect { record! }.to have_enqueued_job(
PatientUpdateFromPDSJob
).twice.on_queue(:imports)
end

context "with an existing patient matching the name" do
before do
create(
Expand Down
14 changes: 14 additions & 0 deletions spec/models/immunisation_import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@
PatientNHSNumberLookupJob
).once.on_queue(:imports)
end

it "enqueues jobs to update from PDS" do
expect { record! }.to have_enqueued_job(PatientUpdateFromPDSJob)
.exactly(6)
.times
.on_queue(:imports)
end
end

context "with valid HPV rows" do
Expand Down Expand Up @@ -259,6 +266,13 @@
PatientNHSNumberLookupJob
).once.on_queue(:imports)
end

it "enqueues jobs to update from PDS" do
expect { record! }.to have_enqueued_job(PatientUpdateFromPDSJob)
.exactly(9)
.times
.on_queue(:imports)
end
end

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

0 comments on commit 145cfcc

Please sign in to comment.