Skip to content
Open
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
33 changes: 1 addition & 32 deletions app/migration/ecf1_teacher_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def self.build_ect_data(participant_profile:)
payments_frozen_cohort_start_year: participant_profile.previous_payments_frozen_cohort_start_year,
states: build_profile_states(participant_profile:),
induction_records:,
mentor_at_school_periods: build_mentor_at_school_periods(induction_records:)
mentor_at_school_periods: SchoolMentorsForECT.new(induction_records:).mentor_at_school_periods
)
end

Expand Down Expand Up @@ -89,33 +89,6 @@ def self.build_induction_record(induction_record:)
)
end

def self.build_mentor_at_school_periods(induction_records:)
urns = induction_records.map(&:school).map(&:urn).uniq
mentor_profile_ids = induction_records.map(&:mentor_profile_id).compact.uniq

# raise("No mentor_profile_ids!!") if mentor_profile_ids.empty?

mentors = ::MentorAtSchoolPeriod.joins(:school, :teacher)
.where(schools: { urn: urns },
teachers: { api_mentor_training_record_id: mentor_profile_ids })
.to_a
mentors.map do |mentor_at_school_period|
build_mentor_at_school_period(mentor_at_school_period:)
end
end

def self.build_mentor_at_school_period(mentor_at_school_period:)
MentorAtSchoolPeriod.new(
mentor_at_school_period_id: mentor_at_school_period.id,
started_on: mentor_at_school_period.started_on,
finished_on: mentor_at_school_period.finished_on,
created_at: mentor_at_school_period.created_at,
updated_at: mentor_at_school_period.updated_at,
school: build_school_data(mentor_at_school_period.school),
teacher: build_teacher_data(mentor_at_school_period.teacher)
)
end

def self.build_school_data(school)
Types::SchoolData.new(urn: school.urn, name: school.name, school_type_name: school_type_name_for(school))
end
Expand All @@ -128,10 +101,6 @@ def self.school_type_name_for(school)
end
end

def self.build_teacher_data(teacher)
Types::TeacherData.new(trn: teacher.trn, api_mentor_training_record_id: teacher.api_mentor_training_record_id)
end

def self.build_mentor_data(participant_profile:)
Mentor.new(
participant_profile_id: participant_profile.id,
Expand Down
42 changes: 42 additions & 0 deletions app/migration/school_mentors_for_ect.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class SchoolMentorsForECT
def initialize(induction_records:)
@induction_records = induction_records
end

def mentor_at_school_periods
find_mentor_at_school_periods_for_ect
end

private

attr_reader :induction_records

def find_mentor_at_school_periods_for_ect
schools = induction_records.map(&:school).uniq
mentor_profile_ids = induction_records.map(&:mentor_profile_id).compact.uniq

mentor_at_school_periods = ::MentorAtSchoolPeriod.joins(:school, :teacher)
.where(schools: { urn: schools.map(&:urn) },
teachers: { api_mentor_training_record_id: mentor_profile_ids })

mentor_at_school_periods.map do |mentor_at_school_period|
build_mentor_at_school_period(mentor_at_school_period:, schools:)
end
end

def build_mentor_at_school_period(mentor_at_school_period:, schools:)
ECF1TeacherHistory::MentorAtSchoolPeriod.new(
mentor_at_school_period_id: mentor_at_school_period.id,
started_on: mentor_at_school_period.started_on,
finished_on: mentor_at_school_period.finished_on,
created_at: mentor_at_school_period.created_at,
updated_at: mentor_at_school_period.updated_at,
school: schools.find { |s| s.urn == mentor_at_school_period.school.urn.to_s },
teacher: build_teacher_data(mentor_at_school_period.teacher)
)
end

def build_teacher_data(teacher)
Types::TeacherData.new(trn: teacher.trn, api_mentor_training_record_id: teacher.api_mentor_training_record_id)
end
end
2 changes: 1 addition & 1 deletion spec/migration/ecf1_teacher_history_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
expect(historic_period.finished_on).to eq(mentor_at_school_period&.finished_on)
expect(historic_period.created_at).to be_within(1.second).of(mentor_at_school_period.created_at)
expect(historic_period.updated_at).to be_within(1.second).of(mentor_at_school_period.updated_at)
expect(historic_period.school.urn).to eq(mentor_at_school_period.school.urn)
expect(historic_period.school.urn).to eq(mentor_at_school_period.school.urn.to_s)
expect(historic_period.teacher.trn).to eq(mentor_at_school_period.teacher.trn)
expect(historic_period.teacher.api_mentor_training_record_id).to eq(mentor_at_school_period.teacher.api_mentor_training_record_id)
end
Expand Down
43 changes: 43 additions & 0 deletions spec/migration/school_mentors_for_ect_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
RSpec.describe SchoolMentorsForECT do
subject(:service) { described_class.new(induction_records:) }

let(:ect_profile) { FactoryBot.create(:migration_participant_profile, :ect) }
let(:school_cohort) { ect_profile.school_cohort }
let(:induction_programme) { FactoryBot.create(:migration_induction_programme, :provider_led, school_cohort:) }

let!(:induction_records) do
FactoryBot.create_list(:migration_induction_record, 2, :with_mentor, participant_profile: ect_profile)
end

let!(:mentor_at_school_periods) do
school = FactoryBot.create(:school, urn: school_cohort.school.urn.to_i)
period_1 = FactoryBot.create(:mentor_at_school_period,
api_mentor_training_record_id: induction_records.first.mentor_profile_id,
school:)
period_2 = FactoryBot.create(:mentor_at_school_period,
api_mentor_training_record_id: induction_records.last.mentor_profile_id,
school:)
[period_1, period_2]
end

describe "#mentor_at_school_periods" do
it "builds the right number" do
expect(service.mentor_at_school_periods.count).to eq mentor_at_school_periods.count
end

it "populates the right attributes" do
aggregate_failures "ECT mentor at school periods results" do
mentor_at_school_periods.each do |mentor_at_school_period|
historic_period = service.mentor_at_school_periods.find { |hp| hp.mentor_at_school_period_id == mentor_at_school_period.id }
expect(historic_period.started_on).to eq(mentor_at_school_period.started_on)
expect(historic_period.finished_on).to eq(mentor_at_school_period&.finished_on)
expect(historic_period.created_at).to be_within(1.second).of(mentor_at_school_period.created_at)
expect(historic_period.updated_at).to be_within(1.second).of(mentor_at_school_period.updated_at)
expect(historic_period.school.urn).to eq(mentor_at_school_period.school.urn.to_s)
expect(historic_period.teacher.trn).to eq(mentor_at_school_period.teacher.trn)
expect(historic_period.teacher.api_mentor_training_record_id).to eq(mentor_at_school_period.teacher.api_mentor_training_record_id)
end
end
end
end
end