From c32b4d41e61659f26c23a91a144b08c1b0a6566d Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 29 Jan 2024 11:28:28 +0000 Subject: [PATCH] Do not export old logs with changed location --- .../update_locations_from_csv_service.rb | 6 +++++- .../update_schemes_and_locations_from_csv_spec.rb | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) 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 bf87c0134..86935d210 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 @@ -114,7 +114,11 @@ private def save_location(location, original_attributes) location.save! Rails.logger.info("Saved location #{original_attributes['location_code']}.") - LettingsLog.where(location_id: location.id).update_all(values_updated_at: Time.zone.now) + exportable_from_date = FormHandler.instance.previous_collection_start_date + LettingsLog.where(location_id: location.id).after_date(exportable_from_date).update_all(values_updated_at: Time.zone.now) + LettingsLog.where(location_id: location.id).where(startdate: nil).update_all(values_updated_at: Time.zone.now) + logs_not_to_export = LettingsLog.where(location_id: location.id).before_date(exportable_from_date) + Rails.logger.info("Will not export log #{logs_not_to_export.map(&:id).join(',')} as it is before the exportable date") if logs_not_to_export.any? rescue ActiveRecord::RecordInvalid => e Rails.logger.error("Cannot update location #{original_attributes['location_code']}. #{e.message}") end 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 300e55cd9..766a20bb6 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 @@ -334,8 +334,14 @@ RSpec.describe "bulk_update" do let!(:lettings_log) { FactoryBot.create(:lettings_log, :sh, location: locations[0], scheme:, values_updated_at: nil) } let!(:lettings_log_2) { FactoryBot.create(:lettings_log, :sh, location: locations[1], scheme:, values_updated_at: nil) } let!(:lettings_log_3) { FactoryBot.create(:lettings_log, :sh, location: locations[2], scheme:, values_updated_at: nil) } + let!(:closed_collection_lettings_log) { FactoryBot.create(:lettings_log, :sh, location: locations[0], scheme:, values_updated_at: nil) } + let!(:archived_closed_collection_lettings_log) { FactoryBot.create(:lettings_log, :sh, location: locations[0], scheme:, values_updated_at: nil) } before do + closed_collection_lettings_log.startdate = Time.zone.local(2022, 4, 1) + closed_collection_lettings_log.save!(validate: false) + archived_closed_collection_lettings_log.startdate = Time.zone.local(2021, 4, 1) + archived_closed_collection_lettings_log.save!(validate: false) allow(storage_service).to receive(:get_file_io) .with("original_locations.csv") .and_return(StringIO.new(replace_entity_ids_for_locations(locations[0], locations[1], locations[2], scheme, scheme, scheme, File.open("./spec/fixtures/files/original_locations.csv").read))) @@ -395,6 +401,12 @@ RSpec.describe "bulk_update" do lettings_log_3.reload expect(lettings_log_3.values_updated_at).to eq(nil) + + closed_collection_lettings_log.reload + expect(closed_collection_lettings_log.values_updated_at).not_to eq(nil) + + archived_closed_collection_lettings_log.reload + expect(archived_closed_collection_lettings_log.values_updated_at).to eq(nil) end it "logs the progress of the update" do @@ -408,6 +420,7 @@ RSpec.describe "bulk_update" do 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}.") + expect(Rails.logger).to receive(:info).with("Will not export log #{archived_closed_collection_lettings_log.id} as it is before the exportable date") expect(Rails.logger).to receive(:info).with("No changes to location #{locations[1].id}.") expect(Rails.logger).to receive(:info).with("Cannot update location #{locations[2].id} with postcode: SWAAA. Enter a postcode in the correct format, for example AA1 1AA")