Skip to content

Commit 72c1a0f

Browse files
committed
Add vaccinations check to school trasnfer rake task
Add a check in schools:move_patients to check if all patient sessions at the old school are safe to destroy, before performing the transfer. This prevents misuse o the rake task, so no previous records (vacciantions,triages,etc) can be over-written.
1 parent 8429b77 commit 72c1a0f

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

lib/tasks/schools.rake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ namespace :schools do
5252

5353
raise "Could not find one or both schools." if old_loc.nil? || new_loc.nil?
5454

55+
patients = Patient.where(school_id: old_loc.id)
56+
patient_sessions = PatientSession.where(patient_id: patients.select(:id))
57+
unsafe_sessions = patient_sessions.reject(&:safe_to_destroy?)
58+
if unsafe_sessions.any?
59+
raise "Some patient sessions at #{old_loc.urn} are not safe to destroy. Cannot complete transfer."
60+
end
61+
5562
if !new_loc.team_id.nil? && new_loc.team_id != old_loc.team_id
5663
raise "#{new_loc.urn} belongs to #{new_loc.team.name}. Could not complete transfer."
5764
end

spec/lib/tasks/schools_rake_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,31 @@
5151
expect(school_move.school).to eq(target_school)
5252
end
5353

54+
context "when some patient sessions are not safe to destroy" do
55+
let!(:patient_session) { create(:patient_session, patient:, session:) } # rubocop:disable RSpec/LetSetup
56+
57+
before do
58+
# rubocop:disable RSpec/AnyInstance
59+
allow_any_instance_of(PatientSession).to receive(
60+
:safe_to_destroy?
61+
).and_return(false)
62+
# rubocop:enable RSpec/AnyInstance
63+
end
64+
65+
it "raises an error and does not transfer records" do
66+
expect { invoke }.to raise_error(
67+
RuntimeError,
68+
/Some patient sessions at #{source_school.urn} are not safe to destroy/
69+
)
70+
71+
expect(patient.reload.school).to eq(source_school)
72+
expect(consent_form.reload.school).to eq(source_school)
73+
expect(consent_form.reload.location).to eq(source_school)
74+
expect(session.reload.location).to eq(source_school)
75+
expect(school_move.reload.school).to eq(source_school)
76+
end
77+
end
78+
5479
context "when source school ID is invalid" do
5580
let(:source_urn) { "999999" }
5681

0 commit comments

Comments
 (0)