Skip to content

Commit

Permalink
Add patients back to sessions if valid
Browse files Browse the repository at this point in the history
If we look up a patient in PDS and they're now marked as valid (and
previously were invalid), we should add them to any upcoming sessions if
approriate. This uses a `SchoolMove` instance to handle this to apply
the same logic used elsewhere in the service.
  • Loading branch information
thomasleese committed Dec 13, 2024
1 parent d8bd0d1 commit 0be39ac
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 12 deletions.
18 changes: 17 additions & 1 deletion app/models/patient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,10 @@ def update_from_pds!(pds_patient)

# If we've got a response from DPS we know the patient is valid,
# otherwise PDS will return a 404 status.
self.invalidated_at = nil
if invalidated?
self.invalidated_at = nil
add_to_upcoming_sessions
end

if pds_patient.restricted
self.restricted_at = Time.current unless restricted?
Expand Down Expand Up @@ -403,4 +406,17 @@ def clear_upcoming_sessions
&:destroy_if_safe!
)
end

def add_to_upcoming_sessions
school_move =
if school
SchoolMove.new(patient: self, school:)
elsif (organisation = cohort&.organisation)
SchoolMove.new(patient: self, home_educated:, organisation:)
end

return if school_move.nil?

school_move.confirm!
end
end
2 changes: 1 addition & 1 deletion app/models/school_move.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def confirm!(move_to_school: nil)
ActiveRecord::Base.transaction do
update_patient!
update_sessions!(move_to_school:)
SchoolMove.where(patient:).destroy_all
SchoolMove.where(patient:).destroy_all if persisted?
end
end

Expand Down
24 changes: 14 additions & 10 deletions spec/jobs/patient_nhs_number_lookup_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
describe PatientNHSNumberLookupJob do
subject(:perform_now) { described_class.perform_now(patient) }

let(:programme) { create(:programme) }

before { create(:gp_practice, ods_code: "H81109") }

context "with an NHS number already" do
Expand Down Expand Up @@ -34,6 +36,14 @@
)
end

context "without a match" do
let(:response_file) { "pds/search-patients-no-results-response.json" }

it "doesn't change the NHS number" do
expect { perform_now }.not_to change(patient, :nhs_number)
end
end

context "with a match" do
let(:response_file) { "pds/search-patients-response.json" }

Expand All @@ -52,20 +62,11 @@
end
end

context "without a match" do
let(:response_file) { "pds/search-patients-no-results-response.json" }

it "doesn't change the NHS number" do
expect { perform_now }.not_to change(patient, :nhs_number)
end
end

context "with a match and the patient already exists" do
let(:response_file) { "pds/search-patients-response.json" }

let!(:existing_patient) { create(:patient, nhs_number: "9449306168") }

let(:programme) { create(:programme) }
let(:patient_session) { create(:patient_session, patient:, programme:) }
let(:gillick_assessment) do
create(:gillick_assessment, :competent, patient_session:)
Expand Down Expand Up @@ -135,6 +136,8 @@
end

context "with an NHS number already but invalidated" do
let(:organisation) { create(:organisation, programmes: [programme]) }

let(:patient) do
create(
:patient,
Expand All @@ -143,7 +146,8 @@
family_name: "Smith",
date_of_birth: Date.new(2014, 2, 18),
address_postcode: "SW11 1AA",
invalidated_at: Time.current
invalidated_at: Time.current,
organisation:
)
end

Expand Down
25 changes: 25 additions & 0 deletions spec/models/patient_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,31 @@
update_from_pds!
end
end

context "when the patient is currently invalidated" do
let(:patient) do
create(:patient, :invalidated, school:, nhs_number: "0123456789")
end

let(:programme) { create(:programme) }
let(:organisation) { create(:organisation, programmes: [programme]) }
let(:school) { create(:school, organisation:) }
let(:session) do
create(:session, location: school, organisation:, programme:)
end

it "marks the patient as not invalidated" do
expect { update_from_pds! }.to change(patient, :invalidated?).from(
true
).to(false)
end

it "adds the patient to upcoming sessions" do
expect(session.patients).not_to include(patient)
update_from_pds!
expect(session.reload.patients).to include(patient)
end
end
end

describe "#invalidate!" do
Expand Down

0 comments on commit 0be39ac

Please sign in to comment.