Skip to content

Commit 3737514

Browse files
committed
Reassign vaccination records, batches and sessions
When splitting the East of England team in to six teams, we need to make sure that the batches, sessions and vaccination records get reassigned to the most approriate team. Jira-Issue: MAV-1664
1 parent 3638868 commit 3737514

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

app/lib/team_migration/east_of_england.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ def process_row(urn, sen, team)
8181

8282
programmes = (sen ? [flu_programme, hpv_programme] : [hpv_programme])
8383
add_school_year_groups(location, programmes, sen:)
84+
85+
location.sessions.find_each do |session|
86+
log("Reassigning session to #{team.workgroup}")
87+
session.update!(team:)
88+
89+
session
90+
.vaccination_records
91+
.includes(:batch)
92+
.find_each do |vaccination_record|
93+
batch = vaccination_record.batch
94+
if batch.team_id != team.id
95+
log("Reassigning batch #{batch.name} to #{team.workgroup}")
96+
batch.update!(team:)
97+
end
98+
end
99+
end
84100
end
85101

86102
def school_rows

spec/lib/team_migration/east_of_england_spec.rb

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,30 @@
1818
)
1919
end
2020

21+
let!(:existing_school) { create(:school, urn: "109464", team: current_team) }
22+
23+
let!(:existing_session) do
24+
create(
25+
:session,
26+
location: existing_school,
27+
team: current_team,
28+
programmes: [hpv_programme]
29+
)
30+
end
31+
32+
let!(:existing_batch) do
33+
create(:batch, team: current_team, vaccine: hpv_programme.vaccines.first)
34+
end
35+
36+
let!(:existing_vaccination_record) do
37+
create(
38+
:vaccination_record,
39+
session: existing_session,
40+
batch: existing_batch,
41+
programme: hpv_programme
42+
)
43+
end
44+
2145
let(:csv_data) { <<-CSV }
2246
URN,SEN,ICB Shortname
2347
109464,,BLMK ICB
@@ -39,7 +63,8 @@
3963

4064
allow(CSV).to receive(:foreach).and_return(parsed_csv_data)
4165

42-
parsed_csv_data.each do |row|
66+
# The first school is `existing_school`.
67+
parsed_csv_data[1..].each do |row|
4368
create(
4469
:school,
4570
urn: row.fetch("URN"),
@@ -93,6 +118,22 @@
93118
expect { current_team.reload }.to raise_error(ActiveRecord::RecordNotFound)
94119
end
95120

121+
it "reassigns batches, sessions and vaccination records" do
122+
expect(existing_school.team).to eq(current_team)
123+
expect(existing_session.team).to eq(current_team)
124+
expect(existing_batch.team).to eq(current_team)
125+
expect(existing_vaccination_record.team).to eq(current_team)
126+
127+
call
128+
new_team =
129+
organisation.teams.find_by(workgroup: "bedslutonmiltonkeynessais")
130+
131+
expect(existing_school.reload.team).to eq(new_team)
132+
expect(existing_session.reload.team).to eq(new_team)
133+
expect(existing_batch.reload.team).to eq(new_team)
134+
expect(existing_vaccination_record.reload.team).to eq(new_team)
135+
end
136+
96137
context "with patients in community clinic" do
97138
let(:current_session) do
98139
current_team.generic_clinic_session(academic_year: AcademicYear.current)

0 commit comments

Comments
 (0)