diff --git a/app/services/data_stage/process_school_changes.rb b/app/services/data_stage/process_school_changes.rb index f6e05fdf5f..bdfe5ebde5 100644 --- a/app/services/data_stage/process_school_changes.rb +++ b/app/services/data_stage/process_school_changes.rb @@ -98,7 +98,8 @@ def move_assets_from!(school:, successor:) school_cohort.update!(school: successor) school_cohort.ecf_participant_profiles.each do |profile| RectifyParticipantSchool.call(participant_profile: profile, - school: successor, + from_school: school, + to_school: successor, transfer_pupil_premium_and_sparsity: false) end end diff --git a/app/services/rectify_participant_school.rb b/app/services/rectify_participant_school.rb index f6bee5e675..068de60680 100644 --- a/app/services/rectify_participant_school.rb +++ b/app/services/rectify_participant_school.rb @@ -1,11 +1,12 @@ # frozen_string_literal: true class RectifyParticipantSchool < BaseService - attr_reader :participant_profile, :school, :transfer_pupil_premium_and_sparsity + attr_reader :participant_profile, :from_school, :to_school, :transfer_pupil_premium_and_sparsity, :cohort, :school_cohort - def initialize(participant_profile:, school:, transfer_pupil_premium_and_sparsity: true) + def initialize(participant_profile:, from_school:, to_school:, transfer_pupil_premium_and_sparsity: true) @participant_profile = participant_profile - @school = school + @from_school = from_school + @to_school = to_school @transfer_pupil_premium_and_sparsity = transfer_pupil_premium_and_sparsity end @@ -14,22 +15,35 @@ def initialize(participant_profile:, school:, transfer_pupil_premium_and_sparsit # that have been added to the wrong school by mistake or in a GIAS school closure/reopen # scenario. def call - cohort = participant_profile.school_cohort.cohort - school_cohort = school.school_cohorts.find_by(cohort:) + @cohort = participant_profile.school_cohort.cohort + @school_cohort = to_school.school_cohorts.find_by(cohort:) return if school_cohort.blank? ActiveRecord::Base.transaction do - participant_profile.teacher_profile.update!(school:) - attrs = { - school_cohort:, - } + rectify_teacher_profile + rectify_mentor_pools if participant_profile.mentor? + rectify_participant_profile + end + end + +private + + def rectify_mentor_pools + from_school.school_mentors.find_by(participant_profile:)&.update!(school: to_school) + end - if transfer_pupil_premium_and_sparsity - attrs[:sparsity_uplift] = school.sparsity_uplift?(cohort.start_year) - attrs[:pupil_premium_uplift] = school.pupil_premium_uplift?(cohort.start_year) - end + def rectify_participant_profile + attrs = { school_cohort: } - participant_profile.update!(attrs) + if transfer_pupil_premium_and_sparsity + attrs[:sparsity_uplift] = to_school.sparsity_uplift?(cohort.start_year) + attrs[:pupil_premium_uplift] = to_school.pupil_premium_uplift?(cohort.start_year) end + + participant_profile.update!(attrs) + end + + def rectify_teacher_profile + participant_profile.teacher_profile.update!(school: to_school) end end diff --git a/spec/services/rectify_participant_school_spec.rb b/spec/services/rectify_participant_school_spec.rb index 1ebfc4414c..46de1ad487 100644 --- a/spec/services/rectify_participant_school_spec.rb +++ b/spec/services/rectify_participant_school_spec.rb @@ -6,13 +6,22 @@ subject(:service) { described_class } let(:cohort) { Cohort.current || create(:cohort, :current) } let(:participant_profile) { create(:ect_participant_profile, cohort:) } + let(:participant_identity) { participant_profile.participant_identity } + let(:old_school) { participant_profile.school } let(:new_school) { create(:school, name: "Big Shiny School", urn: "123000") } let!(:school_cohort) { create(:school_cohort, cohort:, school: new_school) } let(:transfer_uplift) { true } describe ".call" do before do - service.call(participant_profile:, school: new_school, transfer_pupil_premium_and_sparsity: transfer_uplift) + if participant_profile.mentor? + old_school.school_mentors.create!(participant_profile:, preferred_identity: participant_identity) + end + + service.call(participant_profile:, + from_school: old_school, + to_school: new_school, + transfer_pupil_premium_and_sparsity: transfer_uplift) participant_profile.reload end @@ -65,5 +74,14 @@ expect(participant_profile).not_to be_sparsity_uplift end end + + context "when the participant is a mentor" do + let(:participant_profile) { create(:mentor_participant_profile) } + + it "update both schools' mentor pool" do + expect(old_school.mentor_profiles).not_to include(participant_profile) + expect(new_school.mentor_profiles).to include(participant_profile) + end + end end end