Skip to content

Commit 42b4a00

Browse files
committed
Fix school site team assignment to use academic year filtering
When creating school sites from parent schools, the controller was using all teams from the parent school regardless of academic year. This fix adds a new `teams_for_academic_year` method to Location that filters teams by academic year, preventing incorrect team assignments across different academic years. This is because a school can belong to one team one year and shift teams the next year. Only the currently assigned teams matter
1 parent c9a022d commit 42b4a00

3 files changed

Lines changed: 67 additions & 8 deletions

File tree

app/controllers/draft_school_sites_controller.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,16 @@ def handle_confirm
9999
@school.save!
100100
academic_year = AcademicYear.pending
101101

102-
parent_school.teams.each do |team|
103-
@school.attach_to_team!(team, academic_year:)
104-
@school.import_year_groups_from_gias!(academic_year:)
105-
@school.import_default_programme_year_groups!(
106-
team.programmes,
107-
academic_year:
108-
)
109-
end
102+
parent_school
103+
.teams_for_academic_year(academic_year)
104+
.each do |team|
105+
@school.attach_to_team!(team, academic_year:)
106+
@school.import_year_groups_from_gias!(academic_year:)
107+
@school.import_default_programme_year_groups!(
108+
team.programmes,
109+
academic_year:
110+
)
111+
end
110112

111113
parent_school.update!(site: "A") if parent_school.site.nil?
112114
end

app/models/location.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ def as_json
238238
)
239239
end
240240

241+
def teams_for_academic_year(academic_year)
242+
team_locations.where(academic_year:).includes(:team).map(&:team)
243+
end
244+
241245
def attach_to_team!(team, academic_year:, subteam: nil)
242246
team_locations
243247
.find_or_initialize_by(team:, academic_year:)

spec/models/location_spec.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,4 +492,57 @@
492492
end
493493
end
494494
end
495+
496+
describe "#teams_for_academic_year" do
497+
subject(:teams_for_academic_year) do
498+
location.teams_for_academic_year(target_year)
499+
end
500+
501+
let(:location) { create(:school) }
502+
let(:team_a) { create(:team) }
503+
let(:team_b) { create(:team) }
504+
let(:team_c) { create(:team) }
505+
let(:current_year) { AcademicYear.current }
506+
let(:next_year) { AcademicYear.current + 1 }
507+
let(:target_year) { current_year }
508+
509+
before do
510+
location.attach_to_team!(team_a, academic_year: current_year)
511+
location.attach_to_team!(team_b, academic_year: current_year)
512+
location.attach_to_team!(team_c, academic_year: next_year)
513+
end
514+
515+
context "when requesting current academic year teams" do
516+
it "returns only teams for the current academic year" do
517+
expect(teams_for_academic_year).to contain_exactly(team_a, team_b)
518+
end
519+
520+
it "does not return teams from other academic years" do
521+
expect(teams_for_academic_year).not_to include(team_c)
522+
end
523+
end
524+
525+
context "when requesting next academic year teams" do
526+
let(:target_year) { next_year }
527+
528+
it "returns only teams for the next academic year" do
529+
expect(teams_for_academic_year).to contain_exactly(team_c)
530+
end
531+
532+
it "does not return teams from other academic years" do
533+
expect(teams_for_academic_year).not_to include(team_a, team_b)
534+
end
535+
end
536+
537+
context "when the location has no teams for the specified year" do
538+
let(:location_without_teams) { create(:school) }
539+
let(:pending_year) { AcademicYear.pending }
540+
541+
it "returns an empty array" do
542+
expect(
543+
location_without_teams.teams_for_academic_year(pending_year)
544+
).to be_empty
545+
end
546+
end
547+
end
495548
end

0 commit comments

Comments
 (0)