Skip to content

Commit e2c09b8

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 e2c09b8

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

lib/tasks/schools.rake

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

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

55+
unless PatientSession
56+
.joins(:patient, :session)
57+
.where(
58+
patient: {
59+
school: old_loc
60+
},
61+
session: {
62+
location: old_loc
63+
}
64+
)
65+
.all?(&:safe_to_destroy?)
66+
raise "Some patient sessions at #{old_loc.urn} are not safe to destroy. Cannot complete transfer."
67+
end
68+
5569
if !new_loc.team_id.nil? && new_loc.team_id != old_loc.team_id
5670
raise "#{new_loc.urn} belongs to #{new_loc.team.name}. Could not complete transfer."
5771
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)