From a514a999b2bd186ce29560cc7cd3ce850929fb34 Mon Sep 17 00:00:00 2001 From: Kat Date: Fri, 26 Jan 2024 14:53:43 +0000 Subject: [PATCH] Log and not update old logs --- app/models/lettings_log.rb | 1 + .../update_schemes_from_csv_service.rb | 10 ++++++++-- .../update_schemes_and_locations_from_csv_spec.rb | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index cc8c7d58b..d5a374903 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -65,6 +65,7 @@ class LettingsLog < Log .or(filter_by_id(param)) } scope :after_date, ->(date) { where("lettings_logs.startdate >= ?", date) } + scope :before_date, ->(date) { where("lettings_logs.startdate < ?", date) } scope :unresolved, -> { where(unresolved: true) } scope :age1_answered, -> { where.not(age1: nil).or(where(age1_known: 1)) } scope :tcharge_answered, -> { where.not(tcharge: nil).or(where(household_charge: 1)).or(where(is_carehome: 1)) } 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 5aaa03956..3eef32fdc 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 @@ -89,7 +89,9 @@ private if organisation.present? && (organisation.child_organisations.include?(current_organisation) || organisation.parent_organisations.include?(current_organisation)) scheme["owning_organisation_id"] = organisation.id Rails.logger.info("Updating scheme #{original_attributes['scheme_code']} with owning_organisation: #{organisation.name}") - LettingsLog.where(scheme_id: scheme.id).update!(location: nil, scheme: nil, unresolved: true) + editable_from_date = FormHandler.instance.earliest_open_for_editing_collection_start_date + LettingsLog.where(scheme_id: scheme.id).after_date(editable_from_date).update!(location: nil, scheme: nil, unresolved: true) + LettingsLog.where(scheme_id: scheme.id).where(startdate: nil).update!(location: nil, scheme: nil, unresolved: true) 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") end @@ -98,7 +100,11 @@ private def save_scheme(scheme, original_attributes) scheme.save! Rails.logger.info("Saved scheme #{original_attributes['scheme_code']}.") - LettingsLog.where(scheme_id: scheme.id).update_all(values_updated_at: Time.zone.now) + exportable_from_date = FormHandler.instance.previous_collection_start_date + LettingsLog.where(scheme_id: scheme.id).after_date(exportable_from_date).update_all(values_updated_at: Time.zone.now) + LettingsLog.where(scheme_id: scheme.id).where(startdate: nil).update_all(values_updated_at: Time.zone.now) + logs_not_to_export = LettingsLog.where(scheme_id: scheme.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 scheme #{original_attributes['scheme_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 c7e532368..c77e176f7 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 @@ -71,8 +71,14 @@ RSpec.describe "bulk_update" do let!(:lettings_log) { FactoryBot.create(:lettings_log, :sh, scheme: schemes[0], location:, values_updated_at: nil, owning_organisation: schemes[0].owning_organisation) } let!(:lettings_log_2) { FactoryBot.create(:lettings_log, :sh, scheme: schemes[1], location: location_2, values_updated_at: nil, owning_organisation: schemes[1].owning_organisation) } let!(:lettings_log_3) { FactoryBot.create(:lettings_log, :sh, scheme: schemes[2], location: location_3, values_updated_at: nil, owning_organisation: schemes[2].owning_organisation) } + let!(:closed_collection_lettings_log) { FactoryBot.create(:lettings_log, :sh, scheme: schemes[0], location:, values_updated_at: nil, owning_organisation: schemes[0].owning_organisation) } + let!(:archived_closed_collection_lettings_log) { FactoryBot.create(:lettings_log, :sh, scheme: schemes[0], location:, values_updated_at: nil, owning_organisation: schemes[0].owning_organisation) } 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_schemes.csv") .and_return(StringIO.new(replace_entity_ids(schemes[0], schemes[1], schemes[2], File.open("./spec/fixtures/files/original_schemes.csv").read))) @@ -174,6 +180,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 @@ -193,6 +205,7 @@ RSpec.describe "bulk_update" do expect(Rails.logger).to receive(:info).with("Cannot update scheme S#{schemes[0].id} with created_at as it it not a permitted field") expect(Rails.logger).to receive(:info).with("Cannot update scheme S#{schemes[0].id} with active_dates as it it not a permitted field") expect(Rails.logger).to receive(:info).with("Saved scheme S#{schemes[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 scheme S#{schemes[1].id}.") @@ -245,6 +258,7 @@ RSpec.describe "bulk_update" do expect(Rails.logger).to receive(:info).with("Cannot update scheme S#{schemes[0].id} with created_at as it it not a permitted field") expect(Rails.logger).to receive(:info).with("Cannot update scheme S#{schemes[0].id} with active_dates as it it not a permitted field") expect(Rails.logger).to receive(:info).with("Saved scheme S#{schemes[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 scheme S#{schemes[1].id}.")