generated from DFE-Digital/govuk-rails-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5442 from DFE-Digital/3933-add-mentor-call-off-co…
…ntract-to-ecf-2 [CPDLP-3933] Add Mentor call off contract to ECF
- Loading branch information
Showing
18 changed files
with
368 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
class MentorCallOffContract < ApplicationRecord | ||
belongs_to :lead_provider | ||
belongs_to :cohort | ||
end |
108 changes: 108 additions & 0 deletions
108
app/services/importers/create_mentor_call_off_contract.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# frozen_string_literal: true | ||
|
||
require "csv" | ||
|
||
module Importers | ||
class CreateMentorCallOffContract | ||
def call | ||
logger.info "CreateMentorCallOffContract: Started!" | ||
|
||
if path_to_csv.present? | ||
create_mentor_call_off_contract_from_csv | ||
else | ||
create_default_mentor_call_off_contracts | ||
end | ||
|
||
logger.info "CreateMentorCallOffContract: Finished!" | ||
end | ||
|
||
private | ||
|
||
attr_reader :path_to_csv, :logger | ||
|
||
def initialize(path_to_csv: nil, logger: Rails.logger) | ||
@path_to_csv = path_to_csv | ||
@logger = logger | ||
end | ||
|
||
def create_default_mentor_call_off_contracts | ||
raise "Do not seed default Mentor Call Off Contracts in Production!" if Rails.env.production? | ||
|
||
LeadProvider.find_each do |lead_provider| | ||
[cohort_current, cohort_previous, cohort_next].each do |cohort| | ||
create_mentor_call_off_contract(lead_provider:, cohort:, contract_data: example_contract_data) | ||
end | ||
end | ||
end | ||
|
||
def create_mentor_call_off_contract_from_csv | ||
check_headers! | ||
|
||
ActiveRecord::Base.transaction do | ||
rows.each do |row| | ||
cohort = Cohort.find_by!(start_year: row["cohort-start-year"].to_i) | ||
lead_provider = LeadProvider.joins(:cohorts).find_by!(name: row["lead-provider-name"], cohorts: { start_year: row["cohort-start-year"].to_i }) | ||
|
||
mentor_call_off_contract = MentorCallOffContract.find_by( | ||
lead_provider:, | ||
cohort:, | ||
) | ||
|
||
next if mentor_call_off_contract | ||
|
||
contract_data = build_contract_data(row) | ||
create_mentor_call_off_contract(lead_provider:, cohort:, contract_data:) | ||
end | ||
end | ||
end | ||
|
||
def create_mentor_call_off_contract(lead_provider:, cohort:, contract_data:) | ||
logger.info "CreateMentorCallOffContract: Adding Mentor Call Off Contract for Lead Provider: #{lead_provider.name} in cohort: #{cohort.start_year}" | ||
|
||
MentorCallOffContract.create!( | ||
lead_provider:, | ||
cohort:, | ||
recruitment_target: contract_data[:recruitment_target], | ||
payment_per_participant: contract_data[:payment_per_participant], | ||
) | ||
|
||
logger.info "CreateMentorCallOffContract: Added Mentor Call Off Contract for Lead Provider: #{lead_provider.name} in cohort: #{cohort.start_year} successfully!" | ||
end | ||
|
||
def check_headers! | ||
unless %w[lead-provider-name cohort-start-year recruitment-target payment-per-participant].all? { |header| rows.headers.include?(header) } | ||
raise NameError, "Invalid headers" | ||
end | ||
end | ||
|
||
def rows | ||
@rows ||= CSV.read(path_to_csv, headers: true) | ||
end | ||
|
||
def cohort_current | ||
@cohort_current ||= Cohort.current || FactoryBot.create(:cohort, :current) | ||
end | ||
|
||
def cohort_previous | ||
@cohort_previous ||= Cohort.previous || FactoryBot.create(:cohort, :previous) | ||
end | ||
|
||
def cohort_next | ||
@cohort_next ||= Cohort.next || FactoryBot.create(:cohort, :next) | ||
end | ||
|
||
def example_contract_data | ||
@example_contract_data ||= { | ||
recruitment_target: 4500, | ||
payment_per_participant: 1000.0, | ||
} | ||
end | ||
|
||
def build_contract_data(row) | ||
{ | ||
recruitment_target: row["recruitment-target"], | ||
payment_per_participant: row["payment-per-participant"], | ||
} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
db/migrate/20250114144917_create_mentor_call_off_contracts.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateMentorCallOffContracts < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :mentor_call_off_contracts do |t| | ||
t.belongs_to :lead_provider, null: false, foreign_key: true, type: :uuid, index: true | ||
t.belongs_to :cohort, null: false, foreign_key: true, type: :uuid, index: true | ||
|
||
t.string :version, null: false, default: "0.0.1" | ||
t.integer :recruitment_target | ||
t.decimal :payment_per_participant, default: 1000.00, null: false | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
Importers::CreateCallOffContract.new.call | ||
Importers::CreateMentorCallOffContract.new.call |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :mentor_call_off_contract do | ||
lead_provider { build(:lead_provider, cpd_lead_provider: build(:cpd_lead_provider)) } | ||
cohort { Cohort.current || create(:cohort, :current) } | ||
|
||
recruitment_target { 6000 } | ||
payment_per_participant { 1000.0 } | ||
version { "1.0" } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
lead-provider-name,cohort-start-year,recruitment-target,payment-per-participant | ||
Ambition Institute,2026,4600,1000.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe MentorCallOffContract, type: :model do | ||
let(:mentor_call_off_contract) { create(:mentor_call_off_contract) } | ||
|
||
describe "associations" do | ||
it { is_expected.to belong_to(:cohort) } | ||
it { is_expected.to belong_to(:lead_provider) } | ||
end | ||
end |
Oops, something went wrong.