From 0daf251ad5ba640120a572728f5514fb3e953aa0 Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 27 Feb 2024 08:52:17 +0000 Subject: [PATCH] Clear scheme/location if location scheme changes --- .../update_locations_from_csv_service.rb | 9 +++++++ .../update_schemes_from_csv_service.rb | 4 +-- ...ate_schemes_and_locations_from_csv_spec.rb | 25 +++++++++++++++++-- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/app/services/bulk_update_from_csv/update_locations_from_csv_service.rb b/app/services/bulk_update_from_csv/update_locations_from_csv_service.rb index 5a3ce18ab..5d01578d1 100644 --- a/app/services/bulk_update_from_csv/update_locations_from_csv_service.rb +++ b/app/services/bulk_update_from_csv/update_locations_from_csv_service.rb @@ -110,6 +110,15 @@ private else location["scheme_id"] = scheme.id Rails.logger.info("Updating location #{original_attributes['location_code']} with scheme: S#{scheme.id}") + editable_from_date = FormHandler.instance.earliest_open_for_editing_collection_start_date + + editable_logs_with_startdate = LettingsLog.where(location_id: location.id).after_date(editable_from_date) + editable_logs_with_startdate.update!(location: nil, scheme: nil, values_updated_at: Time.zone.now) + Rails.logger.info("Cleared location and scheme for logs with startdate and location #{location.id}. Log IDs: #{editable_logs_with_startdate.map(&:id).join(', ')}") + + logs_without_start_date = LettingsLog.where(scheme_id: scheme.id).where(startdate: nil) + logs_without_start_date.update!(location: nil, scheme: nil, values_updated_at: Time.zone.now) + Rails.logger.info("Cleared location and scheme for logs without startdate and location #{location.id}. Log IDs: #{logs_without_start_date.map(&:id).join(', ')}") end else Rails.logger.info("Cannot update location #{original_attributes['location_code']} with scheme_code: #{value}. Scheme with id #{value} is not in the database") diff --git a/app/services/bulk_update_from_csv/update_schemes_from_csv_service.rb b/app/services/bulk_update_from_csv/update_schemes_from_csv_service.rb index 92cddbfc9..4017cd6fe 100644 --- a/app/services/bulk_update_from_csv/update_schemes_from_csv_service.rb +++ b/app/services/bulk_update_from_csv/update_schemes_from_csv_service.rb @@ -96,11 +96,11 @@ private editable_logs_with_startdate = LettingsLog.where(scheme_id: scheme.id).after_date(editable_from_date) editable_logs_with_startdate.update!(location: nil, scheme: nil) - Rails.logger.info("Updated logs with startdate for scheme S#{scheme.id}. Log IDs: #{editable_logs_with_startdate.map(&:id).join(', ')}") + Rails.logger.info("Cleared location and scheme for logs with startdate and scheme S#{scheme.id}. Log IDs: #{editable_logs_with_startdate.map(&:id).join(', ')}") logs_without_start_date = LettingsLog.where(scheme_id: scheme.id).where(startdate: nil) logs_without_start_date.update!(location: nil, scheme: nil) - Rails.logger.info("Updated logs without startdate for scheme S#{scheme.id}. Log IDs: #{logs_without_start_date.map(&:id).join(', ')}") + Rails.logger.info("Cleared location and scheme for logs without startdate and scheme S#{scheme.id}. Log IDs: #{logs_without_start_date.map(&:id).join(', ')}") end else Rails.logger.info("Cannot update scheme #{original_attributes['scheme_code']} with owning_organisation: #{value}. Organisation with name #{value} is not in the database or is not related to current organisation") diff --git a/spec/lib/tasks/update_schemes_and_locations_from_csv_spec.rb b/spec/lib/tasks/update_schemes_and_locations_from_csv_spec.rb index 8642f50a6..075283dd5 100644 --- a/spec/lib/tasks/update_schemes_and_locations_from_csv_spec.rb +++ b/spec/lib/tasks/update_schemes_and_locations_from_csv_spec.rb @@ -204,8 +204,8 @@ RSpec.describe "bulk_update" do expect(Rails.logger).to receive(:info).with("Updating scheme S#{schemes[0].id} with service_name: Updated test name") expect(Rails.logger).to receive(:info).with("Updating scheme S#{schemes[0].id} with sensitive: No") expect(Rails.logger).to receive(:info).with("Updating scheme S#{schemes[0].id} with scheme_type: Direct Access Hostel") - expect(Rails.logger).to receive(:info).with("Updated logs with startdate for scheme S#{schemes[0].id}. Log IDs: ") - expect(Rails.logger).to receive(:info).with("Updated logs without startdate for scheme S#{schemes[0].id}. Log IDs: #{lettings_log.id}, #{lettings_log_4.id}, #{lettings_log_5.id}") + expect(Rails.logger).to receive(:info).with("Cleared location and scheme for logs with startdate and scheme S#{schemes[0].id}. Log IDs: ") + expect(Rails.logger).to receive(:info).with("Cleared location and scheme for logs without startdate and scheme S#{schemes[0].id}. Log IDs: #{lettings_log.id}, #{lettings_log_4.id}, #{lettings_log_5.id}") expect(Rails.logger).to receive(:info).with("Updating scheme S#{schemes[0].id} with arrangement_type: Another registered stock owner") expect(Rails.logger).to receive(:info).with("Updating scheme S#{schemes[0].id} with primary_client_group: People with drug problems") expect(Rails.logger).to receive(:info).with("Updating scheme S#{schemes[0].id} with has_other_client_group: No") @@ -386,6 +386,13 @@ RSpec.describe "bulk_update" do expect(locations[2].scheme).to eq(scheme) end + it "does not update the scheme id if the new scheme is not in the same or related organisation as the old scheme" do + different_scheme.update!(owning_organisation: FactoryBot.create(:organisation)) + task.invoke(original_locations_csv_path, updated_locations_csv_path) + locations[0].reload + expect(locations[0].scheme).not_to eq(different_scheme) + end + it "only re-exports the logs for the locations that have been updated" do lettings_log_4.startdate = Time.zone.local(2022, 4, 1) lettings_log_4.save!(validate: false) @@ -416,6 +423,8 @@ RSpec.describe "bulk_update" do lettings_log_5.startdate = Time.zone.local(2021, 4, 1) lettings_log_5.save!(validate: false) + expect(Rails.logger).to receive(:info).with("Cleared location and scheme for logs with startdate and location #{locations[0].id}. Log IDs: #{lettings_log.id}") + expect(Rails.logger).to receive(:info).with("Cleared location and scheme for logs without startdate and location #{locations[0].id}. Log IDs: ") expect(Rails.logger).to receive(:info).with("Updating location #{locations[0].id} with postcode: B11BB") expect(Rails.logger).to receive(:info).with("Updating location #{locations[0].id} with name: Updated name") expect(Rails.logger).to receive(:info).with("Updating location #{locations[0].id} with location_code: E09000033") @@ -490,6 +499,12 @@ RSpec.describe "bulk_update" do period: 1) end + before do + allow(storage_service).to receive(:get_file_io) + .with("updated_locations.csv") + .and_return(StringIO.new(replace_entity_ids_for_locations(locations[0], locations[1], locations[2], scheme, scheme, { id: "non existent scheme id" }, File.open("./spec/fixtures/files/updated_locations.csv").read))) + end + it "does not clear the charges values" do expect(lettings_log.status).to eq("completed") task.invoke(original_locations_csv_path, updated_locations_csv_path) @@ -520,6 +535,12 @@ RSpec.describe "bulk_update" do period: 1) end + before do + allow(storage_service).to receive(:get_file_io) + .with("updated_locations.csv") + .and_return(StringIO.new(replace_entity_ids_for_locations(locations[0], locations[1], locations[2], scheme, scheme, { id: "non existent scheme id" }, File.open("./spec/fixtures/files/updated_locations.csv").read))) + end + it "clears the charges values" do expect(lettings_log.status).to eq("completed") task.invoke(original_locations_csv_path, updated_locations_csv_path)