Browse Source

Log and not update old logs

pull/2144/head
Kat 2 years ago
parent
commit
a514a999b2
  1. 1
      app/models/lettings_log.rb
  2. 10
      app/services/bulk_update_from_csv/update_schemes_from_csv_service.rb
  3. 14
      spec/lib/tasks/update_schemes_and_locations_from_csv_spec.rb

1
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)) }

10
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

14
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}.")

Loading…
Cancel
Save