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 86935d210..d66391f59 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 @@ -115,11 +115,30 @@ private location.save! Rails.logger.info("Saved location #{original_attributes['location_code']}.") 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_to_export = LettingsLog.where(location_id: location.id).after_date(exportable_from_date) + if original_attributes["location_admin_district"] != location.location_admin_district + clear_invalid_rent_fields(logs_to_export) + else + logs_to_export.update_all(values_updated_at: Time.zone.now) + end + 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 + + def clear_invalid_rent_fields(logs) + logs.each do |log| + log.validate + if log.errors["brent"].any? + log.brent = nil + log.scharge = nil + log.pscharge = nil + log.supcharg = nil + end + log.values_updated_at = Time.zone.now + log.save!(validate: false) + end + end end diff --git a/spec/factories/lettings_log.rb b/spec/factories/lettings_log.rb index f0493fc2d..925847ce1 100644 --- a/spec/factories/lettings_log.rb +++ b/spec/factories/lettings_log.rb @@ -183,7 +183,7 @@ FactoryBot.define do end trait :sh do needstype { 2 } - sheltered { 0 } + sheltered { 3 } household_charge { 0 } end trait :sheltered_housing do diff --git a/spec/fixtures/files/original_locations.csv b/spec/fixtures/files/original_locations.csv index 13370ec13..88e308b21 100644 --- a/spec/fixtures/files/original_locations.csv +++ b/spec/fixtures/files/original_locations.csv @@ -1,5 +1,5 @@ scheme_code,location_code,location_postcode,location_name,location_status,location_local_authority,location_units,location_type_of_unit,location_mobility_type,location_active_dates -{scheme_id1},{id1},SW1A 2AA,Downing Street,active,Westminster,20,Self-contained house,Fitted with equipment and adaptations,"Active from 1 April 2022" -{scheme_id2},{id2},SW1A 2AA,Downing Street,active,Westminster,20,Self-contained house,Fitted with equipment and adaptations,"Active from 1 April 2022" -{scheme_id3},{id3},SW1A 2AA,Downing Street,active,Westminster,20,Self-contained house,Fitted with equipment and adaptations,"Active from 1 April 2022" -1,SWrong_id,SW1A 2AA,Downing Street,active,Westminster,20,Self-contained house,Fitted with equipment and adaptations,"Active from 1 April 2022" +{scheme_id1},{id1},SW1A 2AA,Downing Street,active,Wigan,20,Self-contained house,Fitted with equipment and adaptations,"Active from 1 April 2022" +{scheme_id2},{id2},SW1A 2AA,Downing Street,active,Wigan,20,Self-contained house,Fitted with equipment and adaptations,"Active from 1 April 2022" +{scheme_id3},{id3},SW1A 2AA,Downing Street,active,Wigan,20,Self-contained house,Fitted with equipment and adaptations,"Active from 1 April 2022" +1,SWrong_id,SW1A 2AA,Downing Street,active,Wigan,20,Self-contained house,Fitted with equipment and adaptations,"Active from 1 April 2022" diff --git a/spec/fixtures/files/updated_locations.csv b/spec/fixtures/files/updated_locations.csv index 7f5237fab..a04c51683 100644 --- a/spec/fixtures/files/updated_locations.csv +++ b/spec/fixtures/files/updated_locations.csv @@ -1,6 +1,6 @@ scheme_code,location_code,location_postcode,location_name,location_status,location_local_authority,location_units,location_type_of_unit,location_mobility_type,location_active_dates {scheme_id1},{id1},B11BB,Updated name,deactivating_soon,Westminster,10,Bungalow,Wheelchair-user standard,"Active from 1 April 2028" -{scheme_id2},{id2},SW1A 2AA,Downing Street,active,Westminster,20,Self-contained house,Fitted with equipment and adaptations,"Active from 1 April 2022" +{scheme_id2},{id2},SW1A 2AA,Downing Street,active,Wigan,20,Self-contained house,Fitted with equipment and adaptations,"Active from 1 April 2022" {scheme_id3},{id3},SWAAA,,deactivating_soon,Westminst,,elf-contained house,55,"Active from 1 April 2022" x,Wrong_id,SWAAA,,deactivating_soon,Westminst,,elf-contained house,55,"Active from 1 April 2022" x,SWrong_id,SWAAA,,deactivating_soon,Westminst,,elf-contained house,55,"Active from 1 April 2022" 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 b77fa8aa4..ae2a8864c 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 @@ -26,7 +26,7 @@ RSpec.describe "bulk_update" do WebMock.stub_request(:get, /api\.postcodes\.io/) .to_return(status: 200, body: "{\"status\":404,\"error\":\"Postcode not found\"}", headers: {}) WebMock.stub_request(:get, /api\.postcodes\.io\/postcodes\/B11BB/) - .to_return(status: 200, body: '{"status":200,"result":{"admin_district":"Westminster","codes":{"admin_district":"E08000035"}}}', headers: {}) + .to_return(status: 200, body: '{"status":200,"result":{"admin_district":"Westminster","codes":{"admin_district":"E09000033"}}}', headers: {}) end describe ":update_schemes_from_csv", type: :task do @@ -324,18 +324,18 @@ RSpec.describe "bulk_update" do type_of_unit: "Self-contained house", units: 20, mobility_type: "Fitted with equipment and adaptations", - location_code: "E09000033", - location_admin_district: "Westminster", + location_code: "E08000010", + location_admin_district: "Wigan", startdate: Time.zone.local(2022, 4, 1), confirmed: true, updated_at: Time.zone.local(2022, 3, 1), scheme:) end - 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!(:lettings_log_4) { FactoryBot.create(:lettings_log, :sh, location: locations[0], scheme:, values_updated_at: nil) } - let!(:lettings_log_5) { FactoryBot.create(:lettings_log, :sh, location: locations[0], scheme:, values_updated_at: nil) } + let!(:lettings_log) { FactoryBot.create(:lettings_log, :sh, location: locations[0], scheme:, values_updated_at: nil, startdate: Time.zone.local(2023, 4, 4)) } + let!(:lettings_log_2) { FactoryBot.create(:lettings_log, :sh, location: locations[1], scheme:, values_updated_at: nil, startdate: Time.zone.local(2023, 4, 4)) } + let!(:lettings_log_3) { FactoryBot.create(:lettings_log, :sh, location: locations[2], scheme:, values_updated_at: nil, startdate: Time.zone.local(2023, 4, 4)) } + let!(:lettings_log_4) { FactoryBot.create(:lettings_log, :sh, location: locations[0], scheme:, values_updated_at: nil, startdate: Time.zone.local(2023, 4, 4)) } + let!(:lettings_log_5) { FactoryBot.create(:lettings_log, :sh, location: locations[0], scheme:, values_updated_at: nil, startdate: Time.zone.local(2023, 4, 4)) } before do allow(storage_service).to receive(:get_file_io) @@ -355,7 +355,7 @@ RSpec.describe "bulk_update" do expect(locations[0].type_of_unit).to eq("Bungalow") expect(locations[0].units).to eq(10) expect(locations[0].mobility_type).to eq("Wheelchair-user standard") - expect(locations[0].location_code).to eq("E08000035") + expect(locations[0].location_code).to eq("E09000033") expect(locations[0].location_admin_district).to eq("Westminster") expect(locations[0].scheme).to eq(different_scheme) end @@ -368,8 +368,8 @@ RSpec.describe "bulk_update" do expect(locations[1].type_of_unit).to eq("Self-contained house") expect(locations[1].units).to eq(20) expect(locations[1].mobility_type).to eq("Fitted with equipment and adaptations") - expect(locations[1].location_code).to eq("E09000033") - expect(locations[1].location_admin_district).to eq("Westminster") + expect(locations[1].location_code).to eq("E08000010") + expect(locations[1].location_admin_district).to eq("Wigan") expect(locations[1].scheme).to eq(scheme) end @@ -381,8 +381,8 @@ RSpec.describe "bulk_update" do expect(locations[2].type_of_unit).to eq("Self-contained house") expect(locations[2].units).to eq(20) expect(locations[2].mobility_type).to eq("Fitted with equipment and adaptations") - expect(locations[2].location_code).to eq("E09000033") - expect(locations[2].location_admin_district).to eq("Westminster") + expect(locations[2].location_code).to eq("E08000010") + expect(locations[2].location_admin_district).to eq("Wigan") expect(locations[2].scheme).to eq(scheme) end @@ -418,6 +418,7 @@ RSpec.describe "bulk_update" do 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") @@ -455,6 +456,112 @@ RSpec.describe "bulk_update" do it "raises an error when no updated path is given" do expect { task.invoke(original_locations_csv_path, nil) }.to raise_error(RuntimeError, "Usage: rake bulk_update:update_locations_from_csv['original_file_name','updated_file_name']") end + + context "when updating LA" do + before do + LaRentRange.create!( + ranges_rent_id: "1", + la: "E09000033", + beds: 0, + lettype: 8, + soft_min: 12.41, + soft_max: 89.54, + hard_min: 9.87, + hard_max: 100.99, + start_year: 2023, + ) + end + + context "when new LA does not trigger hard rent ranges validations" do + let!(:lettings_log) do + FactoryBot.create(:lettings_log, + :completed, + :sh, + location: locations[0], + scheme:, + values_updated_at: nil, + owning_organisation: scheme.owning_organisation, + brent: 80, + scharge: 50, + pscharge: 50, + supcharg: 50, + beds: 4, + lettype: 1, + period: 1) + 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) + lettings_log.reload + expect(lettings_log.status).to eq("completed") + expect(lettings_log.brent).to eq(80) + expect(lettings_log.scharge).to eq(50) + expect(lettings_log.pscharge).to eq(50) + expect(lettings_log.supcharg).to eq(50) + end + end + + context "when new LA triggers hard rent ranges validation" do + let!(:lettings_log) do + FactoryBot.create(:lettings_log, + :completed, + :sh, + location: locations[0], + scheme:, + values_updated_at: nil, + owning_organisation: scheme.owning_organisation, + brent: 200, + scharge: 50, + pscharge: 50, + supcharg: 50, + beds: 4, + lettype: 1, + period: 1) + end + + it "clears the charges values" do + expect(lettings_log.status).to eq("completed") + task.invoke(original_locations_csv_path, updated_locations_csv_path) + lettings_log.reload + expect(lettings_log.status).to eq("in_progress") + expect(lettings_log.brent).to be_nil + expect(lettings_log.scharge).to be_nil + expect(lettings_log.pscharge).to be_nil + expect(lettings_log.supcharg).to be_nil + end + end + + context "when new LA triggers soft rent ranges validations" do + let!(:lettings_log) do + FactoryBot.create(:lettings_log, + :completed, + :sh, + location: locations[0], + scheme:, + values_updated_at: nil, + owning_organisation: scheme.owning_organisation, + brent: 100, + scharge: 50, + pscharge: 50, + supcharg: 50, + beds: 4, + lettype: 1, + period: 1) + end + + it "does not clear the charges values and marks the log in progress" do + expect(lettings_log.status).to eq("completed") + task.invoke(original_locations_csv_path, updated_locations_csv_path) + lettings_log.reload + expect(lettings_log.status).to eq("in_progress") + expect(lettings_log.brent).to eq(100) + expect(lettings_log.scharge).to eq(50) + expect(lettings_log.pscharge).to eq(50) + expect(lettings_log.supcharg).to eq(50) + end + end + end end end end