Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't always remove patients from sessions #2018

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions app/models/class_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion app/models/consent_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions app/models/immunisation_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 9 additions & 2 deletions app/models/patient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
2 changes: 2 additions & 0 deletions app/models/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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] },
Expand Down
14 changes: 14 additions & 0 deletions spec/models/class_import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading