Skip to content

Commit b61af56

Browse files
authored
Merge pull request #3622 from nhsuk/school-rollover-rake-task
Add rake task to transfer school data
2 parents 654507a + 4765b98 commit b61af56

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

lib/tasks/schools.rake

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,30 @@ namespace :schools do
9696
year_groups: [8, 9, 10, 11]
9797
)
9898
end
99+
100+
desc "Transfer child records from one school to another."
101+
task :move_patients, %i[old_urn new_urn] => :environment do |_task, args|
102+
old_loc = Location.school.find_by(urn: args[:old_urn])
103+
new_loc = Location.school.find_by(urn: args[:new_urn])
104+
105+
raise "Could not find one or both schools." if old_loc.nil? || new_loc.nil?
106+
107+
if !new_loc.team_id.nil? && new_loc.team_id != old_loc.team_id
108+
raise "#{new_loc.urn} belongs to #{new_loc.team.name}. Could not complete transfer."
109+
end
110+
new_loc.update!(team: old_loc.team)
111+
112+
Session.where(location_id: old_loc.id).update_all(location_id: new_loc.id)
113+
Patient.where(school_id: old_loc.id).update_all(school_id: new_loc.id)
114+
ConsentForm.where(location_id: old_loc.id).update_all(
115+
location_id: new_loc.id
116+
)
117+
ConsentForm.where(school_id: old_loc.id).update_all(school_id: new_loc.id)
118+
SchoolMove.where(school_id: old_loc.id).update_all(school_id: new_loc.id)
119+
Patient
120+
.where(school_id: new_loc.id)
121+
.find_each do |patient|
122+
SchoolMoveLogEntry.create!(patient:, school: new_loc)
123+
end
124+
end
99125
end
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# frozen_string_literal: true
2+
3+
describe "schools:move_patients" do
4+
subject(:invoke) do
5+
Rake::Task["schools:move_patients"].invoke(source_urn, target_urn)
6+
end
7+
8+
let(:organisation) { create(:organisation) }
9+
let(:team) { create(:team, organisation:) }
10+
let(:other_team) { create(:team, organisation:) }
11+
let(:source_school) { create(:school, organisation: organisation, team:) }
12+
let(:target_school) { create(:school, organisation: organisation) }
13+
let(:programmes) { [create(:programme, :hpv)] }
14+
let!(:patient) { create(:patient, school: source_school) }
15+
let!(:session) { create(:session, location: source_school, programmes:) }
16+
let!(:school_move) do
17+
create(:school_move, patient: patient, school: source_school)
18+
end
19+
let!(:consent_form) do
20+
create(
21+
:consent_form,
22+
school: source_school,
23+
location: source_school,
24+
session:
25+
)
26+
end
27+
let(:other_org_school) { create(:school, team: other_team) }
28+
29+
let(:source_urn) { source_school.urn.to_s }
30+
let(:target_urn) { target_school.urn.to_s }
31+
32+
after { Rake.application["schools:move_patients"].reenable }
33+
34+
it "transfers associated records from source to target school" do
35+
expect { invoke }.to change { patient.reload.school }.from(
36+
source_school
37+
).to(target_school).and change { consent_form.reload.school }.from(
38+
source_school
39+
).to(target_school).and change { consent_form.reload.location }.from(
40+
source_school
41+
).to(target_school).and change { session.reload.location }.from(
42+
source_school
43+
).to(target_school).and change {
44+
school_move.reload.school
45+
}.from(source_school).to(target_school)
46+
47+
expect(patient.school).to eq(target_school)
48+
expect(consent_form.school).to eq(target_school)
49+
expect(consent_form.location).to eq(target_school)
50+
expect(session.location).to eq(target_school)
51+
expect(school_move.school).to eq(target_school)
52+
end
53+
54+
context "when source school ID is invalid" do
55+
let(:source_urn) { "999999" }
56+
57+
it "raises an error" do
58+
expect { invoke }.to raise_error(
59+
RuntimeError,
60+
/Could not find one or both schools./
61+
)
62+
end
63+
end
64+
65+
context "when target school ID is invalid" do
66+
let(:target_urn) { "999999" }
67+
68+
it "raises an error" do
69+
expect { invoke }.to raise_error(
70+
RuntimeError,
71+
/Could not find one or both schools./
72+
)
73+
end
74+
end
75+
end

0 commit comments

Comments
 (0)