-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Script to backfill Grant payment certificate dates for live data
In #1283 we added a new date field to the Conversion Grant payment certificate received task. Some Conversion projects are already completed in production and therefore cannot be edited by users. We need a way to backfill these projects with their relevant dates. A spreadsheet has been supplied with URNs of the academies, and the date the grant payment certificate was received. Once this script is deployed, we can run the associated rake task with this CSV (uploaded to Azure) and update the relevant projects.
- Loading branch information
Laura Porter
committed
Jan 11, 2024
1 parent
feaebc5
commit af3d2a1
Showing
6 changed files
with
154 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
27 changes: 27 additions & 0 deletions
27
app/services/import/grant_certificate_date_importer_service.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,27 @@ | ||
require "csv" | ||
|
||
class Import::GrantCertificateDateImporterService | ||
def call(csv_path) | ||
CSV.foreach(csv_path, headers: true, header_converters: :symbol) do |row| | ||
urn = row[:urn] | ||
next unless urn | ||
|
||
project = Project.find_by(academy_urn: urn.to_i) | ||
unless project | ||
puts "Unable to find project with academy_urn: #{urn}" | ||
next | ||
end | ||
|
||
tasks_data = project.tasks_data | ||
next unless tasks_data | ||
|
||
date_string = row[:date].to_s | ||
date = Date.strptime(date_string, "%d/%m/%Y") | ||
tasks_data.receive_grant_payment_certificate_date_received = date | ||
tasks_data.save(validate: false) | ||
puts "Updated grant payment certificate date for project with academy_urn: #{urn}" | ||
rescue Date::Error | ||
puts "Date for URN #{urn} is not valid" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace :project_data do | ||
desc ">> Update dates in the Conversion Grant payment certificate task" | ||
task :grant_payment_certificate_dates, [:csv_path] => :environment do |_task, args| | ||
abort I18n.t("tasks.grant_payment_certificate_importer.import.error") if args[:csv_path].nil? | ||
|
||
csv_path = Rails.root.join(args[:csv_path]) | ||
|
||
Import::GrantCertificateDateImporterService.new.call(csv_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe "rake project_data:grant_payment_certificate_dates", type: :task do | ||
subject { Rake::Task["project_data:grant_payment_certificate_dates"] } | ||
|
||
let(:csv_path) { "/csv/grant_payment_certificate_dates.csv" } | ||
|
||
before do | ||
allow_any_instance_of(Import::GrantCertificateDateImporterService).to receive(:call) | ||
end | ||
|
||
it "calls GrantCertificateDateImporterService service with the supplied path" do | ||
expect_any_instance_of(Import::GrantCertificateDateImporterService).to receive(:call).with(Pathname.new(csv_path)).once | ||
|
||
subject.invoke(csv_path) | ||
end | ||
|
||
it "errors if no path is supplied" do | ||
expect { subject.execute } | ||
.to raise_error(SystemExit, I18n.t("tasks.grant_payment_certificate_importer.import.error")) | ||
end | ||
end |
90 changes: 90 additions & 0 deletions
90
spec/services/import/grant_certificate_date_importer_service_spec.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,90 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Import::GrantCertificateDateImporterService do | ||
let(:csv_path) { "/csv/grant_certificate_dates.csv" } | ||
let(:date_importer) { Import::GrantCertificateDateImporterService.new } | ||
let(:date_csv) do | ||
<<~CSV | ||
URN,Date | ||
149962,09/12/2023 | ||
150075,17/10/2023 | ||
CSV | ||
end | ||
|
||
before { allow(File).to receive(:open).with(csv_path, any_args).and_return(date_csv) } | ||
|
||
before do | ||
mock_all_academies_api_responses | ||
end | ||
|
||
describe "#call" do | ||
subject(:call_date_importer) { date_importer.call(csv_path) } | ||
|
||
context "when dates are all valid and URNs are all present" do | ||
let(:tasks_data_1) { create(:conversion_tasks_data, receive_grant_payment_certificate_date_received: nil) } | ||
let(:tasks_data_2) { create(:conversion_tasks_data, receive_grant_payment_certificate_date_received: nil) } | ||
let!(:project_1) { create(:conversion_project, academy_urn: 149962, tasks_data: tasks_data_1) } | ||
let!(:project_2) { create(:conversion_project, academy_urn: 150075, tasks_data: tasks_data_2) } | ||
|
||
it "updates all the project tasks_data" do | ||
call_date_importer | ||
|
||
expect(project_1.tasks_data.reload.receive_grant_payment_certificate_date_received).to eq(Date.new(2023, 12, 9)) | ||
expect(project_2.tasks_data.reload.receive_grant_payment_certificate_date_received).to eq(Date.new(2023, 10, 17)) | ||
end | ||
end | ||
|
||
context "when a URN is not valid" do | ||
let(:tasks_data_1) { create(:conversion_tasks_data, receive_grant_payment_certificate_date_received: nil) } | ||
let!(:project_1) { create(:conversion_project, academy_urn: 149962, tasks_data: tasks_data_1) } | ||
|
||
it "updates all other rows" do | ||
call_date_importer | ||
|
||
expect(project_1.tasks_data.reload.receive_grant_payment_certificate_date_received).to eq(Date.new(2023, 12, 9)) | ||
end | ||
end | ||
|
||
context "when a date is not valid" do | ||
let(:tasks_data_1) { create(:conversion_tasks_data, receive_grant_payment_certificate_date_received: nil) } | ||
let(:tasks_data_2) { create(:conversion_tasks_data, receive_grant_payment_certificate_date_received: nil) } | ||
let!(:project_1) { create(:conversion_project, academy_urn: 149962, tasks_data: tasks_data_1) } | ||
let!(:project_2) { create(:conversion_project, academy_urn: 150075, tasks_data: tasks_data_2) } | ||
let(:date_csv) do | ||
<<~CSV | ||
URN,Date | ||
149962,09/12/2023 | ||
150075,17/18/2023 | ||
CSV | ||
end | ||
|
||
it "updates all other rows" do | ||
call_date_importer | ||
|
||
expect(project_1.tasks_data.reload.receive_grant_payment_certificate_date_received).to eq(Date.new(2023, 12, 9)) | ||
expect(project_2.tasks_data.reload.receive_grant_payment_certificate_date_received).to be_nil | ||
end | ||
end | ||
|
||
context "when a date is zero" do | ||
let(:tasks_data_1) { create(:conversion_tasks_data, receive_grant_payment_certificate_date_received: nil) } | ||
let(:tasks_data_2) { create(:conversion_tasks_data, receive_grant_payment_certificate_date_received: nil) } | ||
let!(:project_1) { create(:conversion_project, academy_urn: 149962, tasks_data: tasks_data_1) } | ||
let!(:project_2) { create(:conversion_project, academy_urn: 150075, tasks_data: tasks_data_2) } | ||
let(:date_csv) do | ||
<<~CSV | ||
URN,Date | ||
149962,09/12/2023 | ||
150075,0 | ||
CSV | ||
end | ||
|
||
it "updates all other rows" do | ||
call_date_importer | ||
|
||
expect(project_1.tasks_data.reload.receive_grant_payment_certificate_date_received).to eq(Date.new(2023, 12, 9)) | ||
expect(project_2.tasks_data.reload.receive_grant_payment_certificate_date_received).to be_nil | ||
end | ||
end | ||
end | ||
end |