diff --git a/app/models/organisation.rb b/app/models/organisation.rb index 0dac3d530..6e38040f9 100644 --- a/app/models/organisation.rb +++ b/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) diff --git a/lib/tasks/update_organisations_group_profit_status.rake b/lib/tasks/update_organisations_group_profit_status.rake new file mode 100644 index 000000000..fb0718608 --- /dev/null +++ b/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 diff --git a/spec/fixtures/files/organisations_group_profit_status_invalid.csv b/spec/fixtures/files/organisations_group_profit_status_invalid.csv new file mode 100644 index 000000000..648f9ecde --- /dev/null +++ b/spec/fixtures/files/organisations_group_profit_status_invalid.csv @@ -0,0 +1,2 @@ +id,profit_status,group +2000,2,4 diff --git a/spec/fixtures/files/organisations_group_profit_status_valid.csv b/spec/fixtures/files/organisations_group_profit_status_valid.csv new file mode 100644 index 000000000..c8a977db0 --- /dev/null +++ b/spec/fixtures/files/organisations_group_profit_status_valid.csv @@ -0,0 +1,2 @@ +id,profit_status,group +234,3,2 diff --git a/spec/lib/tasks/update_organisations_group_profit_status_spec.rb b/spec/lib/tasks/update_organisations_group_profit_status_spec.rb new file mode 100644 index 000000000..c2ab4a93a --- /dev/null +++ b/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