Skip to content

Commit

Permalink
Split contract migration into two migrators
Browse files Browse the repository at this point in the history
  • Loading branch information
leandroalemao committed Oct 22, 2024
1 parent 07b008f commit 13c4255
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 44 deletions.
8 changes: 8 additions & 0 deletions app/services/migration/migrators/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ def find_schedule_id!(ecf_id:)
schedule_ids_by_ecf_id[ecf_id] || raise(ActiveRecord::RecordNotFound, "Couldn't find Schedule")
end

def find_contract_template_id!(ecf_id:)
contract_template_ids_by_ecf_id[ecf_id] || raise(ActiveRecord::RecordNotFound, "Couldn't find Contract Template")
end

def course_groups_by_schedule_type(ecf_type)
case ecf_type
when "Finance::Schedule::NPQLeadership"
Expand Down Expand Up @@ -188,6 +192,10 @@ def school_ids_by_urn
@school_ids_by_urn ||= ::School.pluck(:urn, :id).to_h
end

def contract_template_ids_by_ecf_id
@contract_template_ids_by_ecf_id ||= ::ContractTemplate.pluck(:ecf_id, :id).to_h
end

def itt_provider_ids_by_legal_name_and_operating_name
@itt_provider_ids_by_legal_name_and_operating_name ||= begin
providers = ::IttProvider.including_disabled
Expand Down
52 changes: 12 additions & 40 deletions app/services/migration/migrators/contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,6 @@ module Migration::Migrators
class Contract < Base
INFRA_WORKER_COUNT = 1

SHARED_ATTRIBUTES = %w[
created_at
updated_at
service_fee_percentage
output_payment_percentage
per_participant
number_of_payment_periods
recruitment_target
service_fee_installments
targeted_delivery_funding_per_participant
monthly_service_fee
special_course
].freeze

class << self
def record_count
ecf_contracts.count
Expand All @@ -30,7 +16,7 @@ def ecf_contracts
end

def dependencies
%i[course statement]
%i[course statement contract_template]
end

def number_of_workers
Expand All @@ -48,36 +34,22 @@ def records_per_worker

def call
migrate(self.class.ecf_contracts) do |ecf_contract|
ApplicationRecord.transaction do
course_id = find_course_id!(identifier: ecf_contract.course_identifier)

contract_template = ::ContractTemplate.find_or_initialize_by(ecf_id: ecf_contract.id)
unless contract_template.persisted?
contract_template.update!(ecf_contract.attributes.slice(*SHARED_ATTRIBUTES))
end

statements_by_id = ::Statement.where(ecf_id: ecf_statements(ecf_contract).pluck(:id)).index_by(&:id)
statements_by_id.each_key do |statement_id|
contract = ::Contract.find_by(
statement_id:,
course_id:,
)

next if contract.present?

::Contract.create!(
statement_id:,
course_id:,
contract_template:,
)
end
course_id = find_course_id!(identifier: ecf_contract.course_identifier)
contract_template_id = find_contract_template_id!(ecf_id: ecf_contract.id)
statements_by_id = ::Statement.where(ecf_id: ecf_statements(ecf_contract).pluck(:id)).index_by(&:id)

statements_by_id.each_key do |statement_id|
contract = ::Contract.find_or_initialize_by(
statement_id:,
course_id:,
)
contract.update!(contract_template_id:)
end
end
end

def ecf_statements(ecf_contract)
@ecf_statements ||= {}
@ecf_statements["#{ecf_contract.npq_lead_provider.cpd_lead_provider},#{ecf_contract.cohort},#{ecf_contract.version}"] ||= Migration::Ecf::Finance::Statement.where(
Migration::Ecf::Finance::Statement.where(
cpd_lead_provider: ecf_contract.npq_lead_provider.cpd_lead_provider,
cohort: ecf_contract.cohort,
contract_version: ecf_contract.version,
Expand Down
38 changes: 38 additions & 0 deletions app/services/migration/migrators/contract_template.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module Migration::Migrators
class ContractTemplate < Base
SHARED_ATTRIBUTES = %w[
created_at
updated_at
service_fee_percentage
output_payment_percentage
per_participant
number_of_payment_periods
recruitment_target
service_fee_installments
targeted_delivery_funding_per_participant
monthly_service_fee
special_course
].freeze

class << self
def record_count
ecf_contracts.count
end

def model
:contract_template
end

def ecf_contracts
Migration::Ecf::NpqContract.includes(:cohort, npq_lead_provider: [:cpd_lead_provider])
end
end

def call
migrate(self.class.ecf_contracts) do |ecf_contract|
contract_template = ::ContractTemplate.find_or_initialize_by(ecf_id: ecf_contract.id)
contract_template.update!(ecf_contract.attributes.slice(*SHARED_ATTRIBUTES))
end
end
end
end
6 changes: 2 additions & 4 deletions spec/services/migration/migrators/contract_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "rails_helper"

RSpec.describe Migration::Migrators::Contract do
it_behaves_like "a migrator", :contract, %i[course statement] do
it_behaves_like "a migrator", :contract, %i[course statement contract_template] do
let(:records_per_worker_divider) { 2 }

def create_ecf_resource
Expand Down Expand Up @@ -38,6 +38,7 @@ def create_npq_resource(ecf_resource)
lead_provider:,
cohort:,
)
create(:contract_template, ecf_id: ecf_resource.id)
end

def setup_failure_state
Expand Down Expand Up @@ -83,9 +84,6 @@ def setup_failure_state
contract_template = contract.contract_template
expect(contract_template.ecf_id).to eq(ecf_resource1.id)

attrs = ecf_resource1.attributes.slice(*described_class::SHARED_ATTRIBUTES)
expect(contract_template).to have_attributes(attrs)

expect(Contract.where(contract_template:).count).to eq(1)
end
end
Expand Down
48 changes: 48 additions & 0 deletions spec/services/migration/migrators/contract_template_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require "rails_helper"

RSpec.describe Migration::Migrators::ContractTemplate do
it_behaves_like "a migrator", :contract_template, [] do
def create_ecf_resource
cohort = create(:ecf_migration_cohort)
npq_lead_provider = create(:ecf_migration_npq_lead_provider)
course = create(:ecf_migration_npq_course)
version = rand(1..9)

create(
:ecf_migration_statement,
cpd_lead_provider: npq_lead_provider.cpd_lead_provider,
cohort:,
contract_version: "0.0.#{version}",
)
create(
:ecf_migration_npq_contract,
npq_lead_provider:,
cohort:,
course_identifier: course.identifier,
version: "0.0.#{version}",
created_at: rand(1..100).days.ago,
updated_at: rand(1..100).days.ago,
)
end

def create_npq_resource(ecf_resource); end

def setup_failure_state
# NPQ contract in ECF with a negative recruitment_target
create(
:ecf_migration_npq_contract,
recruitment_target: -300,
)
end

describe "#call" do
it "sets the created ContractTemplate attributes correctly" do
instance.call

contract_template = ContractTemplate.find_by!(ecf_id: ecf_resource1.id)
attrs = ecf_resource1.attributes.slice(*described_class::SHARED_ATTRIBUTES)
expect(contract_template).to have_attributes(attrs)
end
end
end
end

0 comments on commit 13c4255

Please sign in to comment.