Browse Source

Add CSV task to update organisation profit status and group member fields

CLDC-3857-Add-new-questions-to-organisation-setup
Manny Dinssa 3 days ago
parent
commit
aa8e60cb6c
  1. 5
      app/models/organisation.rb
  2. 29
      lib/tasks/update_organisations_group_profit_status.rake
  3. 2
      spec/fixtures/files/organisations_group_profit_status_invalid.csv
  4. 2
      spec/fixtures/files/organisations_group_profit_status_valid.csv
  5. 40
      spec/lib/tasks/update_organisations_group_profit_status_spec.rb

5
app/models/organisation.rb

@ -63,6 +63,8 @@ class Organisation < ApplicationRecord
enum :profit_status, PROFIT_STATUS
attribute :group_member, :boolean
attr_accessor :skip_group_member_validation
before_save :clear_group_member_fields_if_not_group_member
alias_method :la?, :LA?
@ -70,8 +72,7 @@ class Organisation < ApplicationRecord
validates :name, presence: { message: I18n.t("validations.organisation.name_missing") }
validates :name, uniqueness: { case_sensitive: false, message: I18n.t("validations.organisation.name_not_unique") }
validates :provider_type, presence: { message: I18n.t("validations.organisation.provider_type_missing") }
validates :group_member_id, presence: { message: I18n.t("validations.organisation.group_missing") }, if: :group_member?
validates :group_member_id, presence: { message: I18n.t("validations.organisation.group_missing") }, if: -> { group_member? && !skip_group_member_validation }
validate :validate_profit_status
def self.find_by_id_on_multiple_fields(id)

29
lib/tasks/update_organisations_group_profit_status.rake

@ -0,0 +1,29 @@
namespace :data_update do
desc "Update organisations with data from a CSV file"
task :update_organisations, [:csv_path] => :environment do |_task, args|
require "csv"
csv_path = args[:csv_path]
unless csv_path
Rails.logger.error "Please provide the path to the CSV file. Example: rake data_update:update_organisations[csv_path]"
exit
end
CSV.foreach(csv_path, headers: true) do |row|
organisation = Organisation.find_by(id: row["id"].to_i)
if organisation
organisation.skip_group_member_validation = true
organisation.update!(
profit_status: row["profit_status"].to_i,
group_member: true,
group: row["group"].to_i,
)
Rails.logger.info "Updated ORG#{row['id']}"
else
Rails.logger.warn "Organisation with ID #{row['id']} not found"
end
end
Rails.logger.info "Organisation update task completed"
end
end

2
spec/fixtures/files/organisations_group_profit_status_invalid.csv vendored

@ -0,0 +1,2 @@
id,profit_status,group
2000,2,4
1 id profit_status group
2 2000 2 4

2
spec/fixtures/files/organisations_group_profit_status_valid.csv vendored

@ -0,0 +1,2 @@
id,profit_status,group
234,3,2
1 id profit_status group
2 234 3 2

40
spec/lib/tasks/update_organisations_group_profit_status_spec.rb

@ -0,0 +1,40 @@
require "rails_helper"
require "rake"
RSpec.describe "data_update:update_organisations", type: :task do
let(:task) { Rake::Task["data_update:update_organisations"] }
before do
Rake.application.rake_require("tasks/update_organisations_group_profit_status")
Rake::Task.define_task(:environment)
task.reenable
end
context "when the CSV file is valid" do
let!(:organisation) { create(:organisation, id: 234) }
let(:csv_path) { Rails.root.join("spec/fixtures/files/organisations_group_profit_status_valid.csv") }
it "updates the organisation fields" do
expect {
task.invoke(csv_path.to_s)
}.to change { organisation.reload.profit_status }.to("local_authority")
.and change { organisation.reload.group }.to(2)
end
end
context "when the organisation is not found" do
let(:csv_path) { Rails.root.join("spec/fixtures/files/organisations_group_profit_status_invalid.csv") }
it "logs a warning" do
expect(Rails.logger).to receive(:warn).with("Organisation with ID 2000 not found")
task.invoke(csv_path.to_s)
end
end
context "when the CSV path is not provided" do
it "logs an error and exits" do
expect(Rails.logger).to receive(:error).with("Please provide the path to the CSV file. Example: rake data_update:update_organisations[csv_path]")
expect { task.invoke }.to raise_error(SystemExit)
end
end
end
Loading…
Cancel
Save