From a234a0c6f5c239ab72af53297677a36870a10536 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Thu, 18 May 2023 17:19:34 +0100 Subject: [PATCH] Update import validations and confirm locations (#1646) * Clear period if the organisation doesn't charge for that rent period * Clear layear if it's invalid for renewal * remove void and mrcdates * Remove shortfall if larger than rent * Confirm location after creating --- app/models/validations/date_validations.rb | 2 +- .../validations/financial_validations.rb | 4 +- .../validations/household_validations.rb | 2 +- .../imports/lettings_logs_import_service.rb | 4 + .../imports/scheme_location_import_service.rb | 3 +- .../lettings_logs_import_service_spec.rb | 163 ++++++++++++++++++ 6 files changed, 173 insertions(+), 5 deletions(-) diff --git a/app/models/validations/date_validations.rb b/app/models/validations/date_validations.rb index 518f0361f..b83b143cc 100644 --- a/app/models/validations/date_validations.rb +++ b/app/models/validations/date_validations.rb @@ -26,7 +26,7 @@ module Validations::DateValidations end if record["voiddate"].present? && record["mrcdate"].present? && record["mrcdate"].to_date < record["voiddate"].to_date - record.errors.add :voiddate, I18n.t("validations.property.void_date.after_mrcdate") + record.errors.add :voiddate, :after_mrcdate, message: I18n.t("validations.property.void_date.after_mrcdate") record.errors.add :mrcdate, I18n.t("validations.property.mrcdate.before_void_date") end end diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index 7ec730b97..a0b528bde 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -92,7 +92,7 @@ module Validations::FinancialValidations def validate_rent_amount(record) if record.wtshortfall if record.wrent && (record.wtshortfall > record.wrent) - record.errors.add :tshortfall, I18n.t("validations.financial.tshortfall.more_than_rent") + record.errors.add :tshortfall, :more_than_rent, message: I18n.t("validations.financial.tshortfall.more_than_rent") record.errors.add :brent, I18n.t("validations.financial.rent.less_than_shortfall") elsif record.wtshortfall < 0.01 record.errors.add :tshortfall, :must_be_positive, message: I18n.t("validations.financial.tshortfall.must_be_positive") @@ -117,7 +117,7 @@ module Validations::FinancialValidations def validate_rent_period(record) if record.managing_organisation.present? && record.managing_organisation.rent_periods.present? && record.period && !record.managing_organisation.rent_periods.include?(record.period) - record.errors.add :period, I18n.t( + record.errors.add :period, :wrong_rent_period, message: I18n.t( "validations.financial.rent_period.invalid_for_org", org_name: record.managing_organisation.name, rent_period: record.form.get_question("period", record).label_from_value(record.period).downcase, diff --git a/app/models/validations/household_validations.rb b/app/models/validations/household_validations.rb index 47d094be8..cd0ebd9ac 100644 --- a/app/models/validations/household_validations.rb +++ b/app/models/validations/household_validations.rb @@ -116,7 +116,7 @@ module Validations::HouseholdValidations return unless record.layear && record.renewal if record.is_renewal? && record.layear == 1 - record.errors.add :layear, I18n.t("validations.household.renewal_just_moved_to_area.layear") + record.errors.add :layear, :renewal_just_moved, message: I18n.t("validations.household.renewal_just_moved_to_area.layear") record.errors.add :renewal, I18n.t("validations.household.renewal_just_moved_to_area.renewal") end end diff --git a/app/services/imports/lettings_logs_import_service.rb b/app/services/imports/lettings_logs_import_service.rb index 1d63d5d78..bd758ab45 100644 --- a/app/services/imports/lettings_logs_import_service.rb +++ b/app/services/imports/lettings_logs_import_service.rb @@ -324,6 +324,10 @@ module Imports %i[location_id not_active] => %w[location_id scheme_id], %i[tcharge under_10] => charges_attributes, %i[brent over_hard_max] => charges_attributes, + %i[period wrong_rent_period] => %w[period], + %i[layear renewal_just_moved] => %w[layear], + %i[voiddate after_mrcdate] => %w[voiddate mrcdate majorrepairs], + %i[tshortfall more_than_rent] => %w[tshortfall tshortfall_known], } (2..8).each do |person| diff --git a/app/services/imports/scheme_location_import_service.rb b/app/services/imports/scheme_location_import_service.rb index 93093e6da..e01cd1106 100644 --- a/app/services/imports/scheme_location_import_service.rb +++ b/app/services/imports/scheme_location_import_service.rb @@ -111,7 +111,8 @@ module Imports if attributes["end_date"] location.location_deactivation_periods.create!(deactivation_date: attributes["end_date"]) end - location + confirm_scheme_or_location(location) + location.save! && location rescue ActiveRecord::RecordNotUnique @logger.warn("Location is already present with legacy ID #{attributes['location_old_id']}, skipping") rescue ActiveRecord::RecordInvalid diff --git a/spec/services/imports/lettings_logs_import_service_spec.rb b/spec/services/imports/lettings_logs_import_service_spec.rb index 07e863982..13c1bbe49 100644 --- a/spec/services/imports/lettings_logs_import_service_spec.rb +++ b/spec/services/imports/lettings_logs_import_service_spec.rb @@ -1263,6 +1263,169 @@ RSpec.describe Imports::LettingsLogsImportService do end end + context "when the organisation does not charge rent in certain period" do + let(:lettings_log_id) { "0b4a68df-30cc-474a-93c0-a56ce8fdad3b" } + let(:lettings_log_file) { open_file(fixture_directory, lettings_log_id) } + let(:lettings_log_xml) { Nokogiri::XML(lettings_log_file) } + + before do + FormHandler.instance.use_fake_forms! + create(:organisation_rent_period, organisation: managing_organisation, rent_period: 2) + + lettings_log_xml.at_xpath("//xmlns:DAY").content = "10" + lettings_log_xml.at_xpath("//xmlns:MONTH").content = "10" + lettings_log_xml.at_xpath("//xmlns:YEAR").content = "2022" + lettings_log_xml.at_xpath("//xmlns:Q17").content = "1" + lettings_log_xml.at_xpath("//xmlns:Q18c").content = "" + lettings_log_xml.at_xpath("//xmlns:Q18ai").content = "" + lettings_log_xml.at_xpath("//xmlns:Q18aiii").content = "" + lettings_log_xml.at_xpath("//xmlns:Q18aiv").content = "" + lettings_log_xml.at_xpath("//xmlns:Q18av").content = "" + lettings_log_xml.at_xpath("//xmlns:Q2b").content = "" + lettings_log_xml.at_xpath("//xmlns:_2cYears").content = "" + lettings_log_xml.at_xpath("//xmlns:P1Nat").content = "" + lettings_log_xml.at_xpath("//xmlns:_1cschemecode").content = "" + lettings_log_xml.at_xpath("//xmlns:_1cmangroupcode").content = "" + lettings_log_xml.at_xpath("//xmlns:Q25").content = "" + end + + after do + FormHandler.instance.use_real_forms! + Singleton.__init__(FormHandler) + end + + it "intercepts the relevant validation error" do + expect(logger).to receive(:warn).with("Log 0b4a68df-30cc-474a-93c0-a56ce8fdad3b: Removing period with error: DLUHC does not charge rent weekly for 52 weeks") + expect { lettings_log_service.send(:create_log, lettings_log_xml) } + .not_to raise_error + end + + it "clears out the referral answer" do + allow(logger).to receive(:warn) + lettings_log_service.send(:create_log, lettings_log_xml) + lettings_log = LettingsLog.find_by(old_id: lettings_log_id) + + expect(lettings_log).not_to be_nil + expect(lettings_log.period).to be_nil + end + end + + context "when this is a renewal and layear is just moved to local authority" do + let(:lettings_log_id) { "0ead17cb-1668-442d-898c-0d52879ff592" } + let(:lettings_log_file) { open_file(fixture_directory, lettings_log_id) } + let(:lettings_log_xml) { Nokogiri::XML(lettings_log_file) } + + before do + lettings_log_xml.at_xpath("//xmlns:DAY").content = "10" + lettings_log_xml.at_xpath("//xmlns:MONTH").content = "10" + lettings_log_xml.at_xpath("//xmlns:YEAR").content = "2022" + lettings_log_xml.at_xpath("//xmlns:Q27").content = "14" + lettings_log_xml.at_xpath("//xmlns:Q12c").content = "1" + lettings_log_xml.at_xpath("//xmlns:Q26").content = "1" + lettings_log_xml.at_xpath("//xmlns:MRCDAY").content = "" + lettings_log_xml.at_xpath("//xmlns:MRCMONTH").content = "" + lettings_log_xml.at_xpath("//xmlns:MRCYEAR").content = "" + lettings_log_xml.at_xpath("//xmlns:VDAY").content = "10" + lettings_log_xml.at_xpath("//xmlns:VMONTH").content = "10" + lettings_log_xml.at_xpath("//xmlns:VYEAR").content = "2022" + lettings_log_xml.at_xpath("//xmlns:P1Nat").content = "" + lettings_log_xml.at_xpath("//xmlns:Q9a").content = "" + lettings_log_xml.at_xpath("//xmlns:Q11").content = "32" + lettings_log_xml.at_xpath("//xmlns:Q16").content = "1" + end + + it "intercepts the relevant validation error" do + expect(logger).to receive(:warn).with(/Removing layear with error: The household cannot have just moved to the local authority area if this letting is a renewal/) + expect { lettings_log_service.send(:create_log, lettings_log_xml) } + .not_to raise_error + end + + it "clears out the layear answer" do + allow(logger).to receive(:warn) + + lettings_log_service.send(:create_log, lettings_log_xml) + lettings_log = LettingsLog.find_by(old_id: lettings_log_id) + + expect(lettings_log).not_to be_nil + expect(lettings_log.layear).to be_nil + end + end + + context "when this is a void date is after major repairs day" do + let(:lettings_log_id) { "0ead17cb-1668-442d-898c-0d52879ff592" } + let(:lettings_log_file) { open_file(fixture_directory, lettings_log_id) } + let(:lettings_log_xml) { Nokogiri::XML(lettings_log_file) } + + before do + lettings_log_xml.at_xpath("//xmlns:DAY").content = "10" + lettings_log_xml.at_xpath("//xmlns:MONTH").content = "10" + lettings_log_xml.at_xpath("//xmlns:YEAR").content = "2022" + lettings_log_xml.at_xpath("//xmlns:MRCDAY").content = "9" + lettings_log_xml.at_xpath("//xmlns:MRCMONTH").content = "9" + lettings_log_xml.at_xpath("//xmlns:MRCYEAR").content = "2021" + lettings_log_xml.at_xpath("//xmlns:VDAY").content = "10" + lettings_log_xml.at_xpath("//xmlns:VMONTH").content = "10" + lettings_log_xml.at_xpath("//xmlns:VYEAR").content = "2021" + lettings_log_xml.at_xpath("//xmlns:P1Nat").content = "" + end + + it "intercepts the relevant validation error" do + expect(logger).to receive(:warn).with(/Removing voiddate with error: Void date must be before the major repairs date if provided/) + expect(logger).to receive(:warn).with(/Removing mrcdate with error: Void date must be before the major repairs date if provided/) + expect(logger).to receive(:warn).with(/Removing majorrepairs with error: Void date must be before the major repairs date if provided/) + expect { lettings_log_service.send(:create_log, lettings_log_xml) } + .not_to raise_error + end + + it "clears out the voiddate and mrcdate answers" do + allow(logger).to receive(:warn) + + lettings_log_service.send(:create_log, lettings_log_xml) + lettings_log = LettingsLog.find_by(old_id: lettings_log_id) + + expect(lettings_log).not_to be_nil + expect(lettings_log.voiddate).to be_nil + expect(lettings_log.mrcdate).to be_nil + end + end + + context "when shortfall is more than basic rent" do + let(:lettings_log_id) { "0ead17cb-1668-442d-898c-0d52879ff592" } + let(:lettings_log_file) { open_file(fixture_directory, lettings_log_id) } + let(:lettings_log_xml) { Nokogiri::XML(lettings_log_file) } + + before do + lettings_log_xml.at_xpath("//xmlns:DAY").content = "10" + lettings_log_xml.at_xpath("//xmlns:MONTH").content = "10" + lettings_log_xml.at_xpath("//xmlns:YEAR").content = "2022" + lettings_log_xml.at_xpath("//xmlns:P1Nat").content = "" + lettings_log_xml.at_xpath("//xmlns:Q18dyes").content = "200" + lettings_log_xml.at_xpath("//xmlns:Q18ai").content = "20" + lettings_log_xml.at_xpath("//xmlns:Q18aii").content = "20" + lettings_log_xml.at_xpath("//xmlns:Q18aiii").content = "40" + lettings_log_xml.at_xpath("//xmlns:Q18aiv").content = "20" + lettings_log_xml.at_xpath("//xmlns:Q18av").content = "100" + end + + it "intercepts the relevant validation error" do + expect(logger).to receive(:warn).with(/Removing tshortfall with error: Enter a value less less than the basic rent amount/) + expect(logger).to receive(:warn).with(/Removing tshortfall_known with error: Enter a value less less than the basic rent amount/) + expect { lettings_log_service.send(:create_log, lettings_log_xml) } + .not_to raise_error + end + + it "clears out the voiddate and mrcdate answers" do + allow(logger).to receive(:warn) + + lettings_log_service.send(:create_log, lettings_log_xml) + lettings_log = LettingsLog.find_by(old_id: lettings_log_id) + + expect(lettings_log).not_to be_nil + expect(lettings_log.tshortfall).to be_nil + expect(lettings_log.tshortfall_known).to be_nil + end + end + context "and the earnings is not a whole number" do let(:lettings_log_id) { "00d2343e-d5fa-4c89-8400-ec3854b0f2b4" } let(:lettings_log_file) { open_file(fixture_directory, lettings_log_id) }