From 3de82539fae9632e857286310676ec5cd4888d4a Mon Sep 17 00:00:00 2001 From: Thomas Leese Date: Thu, 12 Dec 2024 12:07:58 +0000 Subject: [PATCH] Add patients back to sessions if valid 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. --- app/models/patient.rb | 18 ++++++++++++- app/models/school_move.rb | 2 +- .../patient_nhs_number_lookup_job_spec.rb | 24 ++++++++++-------- spec/models/patient_spec.rb | 25 +++++++++++++++++++ 4 files changed, 57 insertions(+), 12 deletions(-) diff --git a/app/models/patient.rb b/app/models/patient.rb index 802fe6212..0e3e8a3ec 100644 --- a/app/models/patient.rb +++ b/app/models/patient.rb @@ -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? @@ -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 diff --git a/app/models/school_move.rb b/app/models/school_move.rb index 49f6ddcaa..36144dc47 100644 --- a/app/models/school_move.rb +++ b/app/models/school_move.rb @@ -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 diff --git a/spec/jobs/patient_nhs_number_lookup_job_spec.rb b/spec/jobs/patient_nhs_number_lookup_job_spec.rb index b92cff367..221b07be4 100644 --- a/spec/jobs/patient_nhs_number_lookup_job_spec.rb +++ b/spec/jobs/patient_nhs_number_lookup_job_spec.rb @@ -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 @@ -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" } @@ -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:) @@ -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, @@ -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 diff --git a/spec/models/patient_spec.rb b/spec/models/patient_spec.rb index 08090b7ec..8dc4f8020 100644 --- a/spec/models/patient_spec.rb +++ b/spec/models/patient_spec.rb @@ -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