From bc133a8bd0b79e154978ab69c8a919a3532d63d2 Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 27 Feb 2024 11:59:49 +0000 Subject: [PATCH] Clear location/scheme in remaining exportable logs --- .../update_locations_from_csv_service.rb | 12 ++++++++---- .../update_schemes_and_locations_from_csv_spec.rb | 3 ++- 2 files changed, 10 insertions(+), 5 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 1cb1bef63..748199c8c 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 @@ -111,14 +111,18 @@ private 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(', ')}") + editable_logs = LettingsLog.where(location_id: location.id).after_date(editable_from_date) + editable_logs.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.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(', ')}") + + exportable_from_date = FormHandler.instance.previous_collection_start_date + remaining_logs_to_export = LettingsLog.where(location_id: location.id).after_date(exportable_from_date) + remaining_logs_to_export.update_all(location_id: nil, scheme_id: nil, values_updated_at: Time.zone.now) + Rails.logger.info("Cleared location and scheme for non editable logs with location #{location.id}. Log IDs: #{remaining_logs_to_export.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/spec/lib/tasks/update_schemes_and_locations_from_csv_spec.rb b/spec/lib/tasks/update_schemes_and_locations_from_csv_spec.rb index 76e9b4cb2..9d10e9524 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 @@ -423,15 +423,16 @@ 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("Updating location #{locations[0].id} with scheme: S#{different_scheme.id}") 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("Cleared location and scheme for non editable logs with 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") expect(Rails.logger).to receive(:info).with("Updating location #{locations[0].id} with type_of_unit: Bungalow") expect(Rails.logger).to receive(:info).with("Updating location #{locations[0].id} with units: 10") expect(Rails.logger).to receive(:info).with("Updating location #{locations[0].id} with mobility_type: Wheelchair-user standard") - expect(Rails.logger).to receive(:info).with("Updating location #{locations[0].id} with scheme: S#{different_scheme.id}") expect(Rails.logger).to receive(:info).with("Cannot update location #{locations[0].id} with status as it it not a permitted field") expect(Rails.logger).to receive(:info).with("Cannot update location #{locations[0].id} with active_dates as it it not a permitted field") expect(Rails.logger).to receive(:info).with("Saved location #{locations[0].id}.")