Skip to content

Commit

Permalink
Don't always remove patients from sessions
Browse files Browse the repository at this point in the history
If the patient session has been interacted with in some way (either
consent received, or triaged, or even vaccinated) we can't safely remove
the patient from the session as it's unclear what state they're in.

https://good-machine.sentry.io/issues/5990842901
  • Loading branch information
thomasleese committed Oct 16, 2024
1 parent af7efcd commit 9d65893
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
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
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
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 @@ -355,6 +355,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

0 comments on commit 9d65893

Please sign in to comment.