generated from DFE-Digital/govuk-rails-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updating contract data, fixes 2350 data request (#2066)
- Loading branch information
Showing
5 changed files
with
165 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module OneOff | ||
class UpdateContracts | ||
def self.call(year:, month:, cohort_year:, csv_path:) | ||
csv_file = CSV.read(csv_path, headers: true) | ||
|
||
ActiveRecord::Base.transaction do | ||
csv_file.each do |row| | ||
lead_provider = LeadProvider.find_by!(name: row["provider_name"]) | ||
cohort = Cohort.find_by!(start_year: cohort_year) | ||
course = Course.find_by!(identifier: row["course_identifier"]) | ||
|
||
statements = Statement.where(year:, month:, cohort: cohort, lead_provider: lead_provider) | ||
raise "There should be only one statement present (#{row.to_h})" if statements.count != 1 | ||
|
||
contracts = statements.first.contracts.where(course: course) | ||
raise "There should be only one contract present (#{row.to_h})" if contracts.count != 1 | ||
|
||
contract = contracts.first | ||
old_template = contract.contract_template | ||
new_template = old_template.new_from_existing(per_participant: row["per_participant"]) | ||
new_template.save! | ||
|
||
contract.contract_template = new_template | ||
contract.save! | ||
Rails.logger.info("[UpdateContract] Contract #{contract.id} got template updated: #{old_template.id} to #{new_template.id}") | ||
end | ||
end | ||
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace :one_off do | ||
desc "One off task for ticket 2350 to update contracts" | ||
task :update_contracts, %i[file_path] => :environment do |_t, args| | ||
OneOff::UpdateContracts.call(year: 2024, month: 12, cohort_year: 2024, csv_path: args[:file_path]) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe OneOff::UpdateContracts do | ||
describe ".call" do | ||
let(:year) { 2024 } | ||
let(:month) { 12 } | ||
let(:cohort_year) { 2024 } | ||
let(:csv_file) { Tempfile.new } | ||
let(:csv_path) { csv_file.path } | ||
let(:csv_content) do | ||
<<~CSV | ||
provider_name,course_identifier,per_participant | ||
Provider 1,#{Course::NPQ_SENCO},1000 | ||
Provider 2,#{Course::NPQ_HEADSHIP},2000 | ||
CSV | ||
end | ||
|
||
let(:lead_provider_1) { create(:lead_provider, name: "Provider 1") } | ||
let(:lead_provider_2) { create(:lead_provider, name: "Provider 2") } | ||
let(:cohort) { create(:cohort, start_year: cohort_year) } | ||
let(:course_1) { create(:course, :senco) } | ||
let(:course_2) { create(:course, :headship) } | ||
let(:statement_1) { create(:statement, year: year, month: month, cohort: cohort, lead_provider: lead_provider_1) } | ||
let(:statement_2) { create(:statement, year: year, month: month, cohort: cohort, lead_provider: lead_provider_2) } | ||
let(:contract_template_1) { create(:contract_template, per_participant: 100) } | ||
let(:contract_template_2) { create(:contract_template, per_participant: 200) } | ||
let!(:contract_1) { create(:contract, contract_template: contract_template_1, statement: statement_1, course: course_1) } | ||
let!(:contract_2) { create(:contract, contract_template: contract_template_2, statement: statement_2, course: course_2) } | ||
|
||
before do | ||
csv_file.write(csv_content) | ||
csv_file.rewind | ||
end | ||
|
||
context "when operation is successful" do | ||
it "changes the contract templates" do | ||
OneOff::UpdateContracts.call(year: 2024, month: 12, cohort_year: 2024, csv_path:) | ||
|
||
expect(contract_1.reload.contract_template).not_to eq(contract_template_1) | ||
expect(contract_2.reload.contract_template).not_to eq(contract_template_2) | ||
|
||
expect(contract_1.reload.contract_template.per_participant).to eq(1000.0) | ||
expect(contract_2.reload.contract_template.per_participant).to eq(2000.0) | ||
end | ||
|
||
describe "logs" do | ||
let(:csv_content) do | ||
<<~CSV | ||
provider_name,course_identifier,per_participant | ||
Provider 1,#{Course::NPQ_SENCO},1000 | ||
CSV | ||
end | ||
|
||
let(:expected_message) do | ||
from_id = contract_template_1.id | ||
to_id = contract_1.reload.contract_template.id | ||
|
||
"[UpdateContract] Contract #{contract_1.id} got template updated: #{from_id} to #{to_id}" | ||
end | ||
|
||
it "has proper output" do | ||
allow(Rails.logger).to receive(:info) | ||
|
||
OneOff::UpdateContracts.call(year: 2024, month: 12, cohort_year: 2024, csv_path:) | ||
|
||
expect(Rails.logger) | ||
.to have_received(:info).with(expected_message) | ||
end | ||
end | ||
end | ||
|
||
context "when record is missing from the file" do | ||
let(:csv_content) do | ||
<<~CSV | ||
provider_name,course_identifier,per_participant | ||
Provider 1,#{Course::NPQ_SENCO},1000 | ||
Provider 2,#{Course::NPQ_HEADSHIP},2000 | ||
Provider 3,#{Course::NPQ_HEADSHIP},2000 | ||
CSV | ||
end | ||
|
||
it "makes the whole process unsuccessful" do | ||
expect(contract_1.reload.contract_template.per_participant).not_to eq(1000.0) | ||
expect(contract_2.reload.contract_template.per_participant).not_to eq(2000.0) | ||
end | ||
|
||
it "raises the exception" do | ||
expect { | ||
OneOff::UpdateContracts.call(year: 2024, month: 12, cohort_year: 2024, csv_path:) | ||
}.to raise_error(ActiveRecord::RecordNotFound) | ||
end | ||
end | ||
end | ||
end |