Skip to content
Merged
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
14 changes: 14 additions & 0 deletions lib/tasks/schools.rake
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ namespace :schools do

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

unless PatientSession
.joins(:patient, :session)
.where(
patient: {
school: old_loc
},
session: {
location: old_loc
}
)
.all?(&:safe_to_destroy?)
raise "Some patient sessions at #{old_loc.urn} are not safe to destroy. Cannot complete transfer."
end

if !new_loc.team_id.nil? && new_loc.team_id != old_loc.team_id
raise "#{new_loc.urn} belongs to #{new_loc.team.name}. Could not complete transfer."
end
Expand Down
25 changes: 25 additions & 0 deletions spec/lib/tasks/schools_rake_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,31 @@
expect(school_move.school).to eq(target_school)
end

context "when some patient sessions are not safe to destroy" do
let!(:patient_session) { create(:patient_session, patient:, session:) } # rubocop:disable RSpec/LetSetup

before do
# rubocop:disable RSpec/AnyInstance
allow_any_instance_of(PatientSession).to receive(
:safe_to_destroy?
).and_return(false)
# rubocop:enable RSpec/AnyInstance
end

it "raises an error and does not transfer records" do
expect { invoke }.to raise_error(
RuntimeError,
/Some patient sessions at #{source_school.urn} are not safe to destroy/
)

expect(patient.reload.school).to eq(source_school)
expect(consent_form.reload.school).to eq(source_school)
expect(consent_form.reload.location).to eq(source_school)
expect(session.reload.location).to eq(source_school)
expect(school_move.reload.school).to eq(source_school)
end
end

context "when source school ID is invalid" do
let(:source_urn) { "999999" }

Expand Down