diff --git a/app/models/class_import.rb b/app/models/class_import.rb index 4e59873b4..caf69065d 100644 --- a/app/models/class_import.rb +++ b/app/models/class_import.rb @@ -56,10 +56,12 @@ def postprocess_rows! unknown_patients = session.patients - patients - unknown_patients.each do |unknown_patient| - unknown_patient.update!(school: nil) - end - - session.patients.delete(unknown_patients) + session + .patient_sessions + .where(patient: unknown_patients) + .find_each do |patient_session| + patient_session.patient.update!(school: nil) + patient_session.destroy! if patient_session.added_to_session? + end end end diff --git a/app/models/consent_form.rb b/app/models/consent_form.rb index 3d5d35dd5..b79735aa6 100644 --- a/app/models/consent_form.rb +++ b/app/models/consent_form.rb @@ -356,7 +356,11 @@ def match_with_patient!(patient) if school && school != patient.school patient.update!(school:) - patient.patient_sessions.find_by(session: scheduled_session)&.destroy! + existing_patient_session = + patient.patient_sessions.find_by(session: scheduled_session) + if existing_patient_session&.added_to_session? + existing_patient_session.destroy! + end upcoming_session = Session diff --git a/app/models/immunisation_import.rb b/app/models/immunisation_import.rb index e08be63b3..b9d1adf78 100644 --- a/app/models/immunisation_import.rb +++ b/app/models/immunisation_import.rb @@ -148,9 +148,12 @@ def postprocess_rows! .includes(:vaccination_records) .select { _1.vaccinated?(programme) } - PatientSession.where( - session: team.sessions.upcoming, - patient: already_vaccinated_patients - ).delete_all + PatientSession + .where( + session: team.sessions.upcoming, + patient: already_vaccinated_patients + ) + .select(&:added_to_session?) + .each(&:destroy!) end end diff --git a/app/models/patient.rb b/app/models/patient.rb index 0c16952d7..ac55cfe71 100644 --- a/app/models/patient.rb +++ b/app/models/patient.rb @@ -186,7 +186,7 @@ def update_from_pds!(pds_patient) self.date_of_death = pds_patient.date_of_death if date_of_death_changed? - upcoming_sessions.clear unless date_of_death.nil? + clear_upcoming_sessions unless date_of_death.nil? self.date_of_death_recorded_at = Time.current end @@ -208,7 +208,7 @@ def invalidate! return if invalidated? ActiveRecord::Base.transaction do - upcoming_sessions.clear + clear_upcoming_sessions update!(invalidated_at: Time.current) end end @@ -235,4 +235,11 @@ def destroy_childless_parents parent.destroy! if parent.parent_relationships.count.zero? end end + + def clear_upcoming_sessions + patient_sessions + .where(session: upcoming_sessions) + .select(&:added_to_session?) + .each(&:destroy!) + end end diff --git a/app/models/session.rb b/app/models/session.rb index c3f3d2ad8..fffade908 100644 --- a/app/models/session.rb +++ b/app/models/session.rb @@ -131,6 +131,7 @@ def create_patient_sessions! required_programmes.all? { |programme| patient.vaccinated?(programme) } end + # First we remove patients from any other upcoming sessions. unvaccinated_patients.each do |patient| sessions_other_than_self = patient.upcoming_sessions.reject { _1 == self } next if sessions_other_than_self.empty? @@ -142,6 +143,7 @@ def create_patient_sessions! .each(&:destroy!) end + # Next we can add the unvaccinated patients to this session. PatientSession.import!( %i[patient_id session_id], unvaccinated_patients.map { [_1.id, id] }, diff --git a/spec/models/class_import_spec.rb b/spec/models/class_import_spec.rb index 23d6ddc51..1e7bf10f9 100644 --- a/spec/models/class_import_spec.rb +++ b/spec/models/class_import_spec.rb @@ -360,6 +360,20 @@ expect { record! }.to change { existing_patient.reload.school }.to(nil) expect(session.reload.patients).not_to include(existing_patient) end + + context "when the patient has been seen" do + let(:existing_patient) do + create(:patient, :consent_given_triage_not_needed, session:) + end + + it "doesn't remove the patient from the session" do + expect(session.patients).to include(existing_patient) + expect { record! }.to change { existing_patient.reload.school }.to( + nil + ) + expect(session.reload.patients).to include(existing_patient) + end + end end end end