From 482dfca1e8b5554561c727706bb5a474ea6e4ad0 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Mon, 12 Dec 2022 14:17:59 +0000 Subject: [PATCH] Cldc 1771 Blank invalidated unresolved log fields (#1078) * Add a test to clear the voiddate for unresolved logs * feat: add reset_voiddate method * feat: only reset voiddate if log is unresolved * test: check voiddate not cleared if log is resolved * feat: remove redundant check for startdate and voiddate being non-nil * feat: make reset_voiddate into a bang method * test: add tests for clearing mrcdate * feat: create reset_mcrdate! method * refactor * reset rent values when rent validation triggers * refactor tests * Test for checking that other validations aren't impacted Co-authored-by: Sam Seed --- app/models/lettings_log.rb | 23 +++++++ spec/models/lettings_log_spec.rb | 111 +++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index 610ddbd75..46d8b0759 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -17,6 +17,8 @@ end class LettingsLog < Log include Validations::SoftValidations include DerivedVariables::LettingsLogVariables + include Validations::DateValidations + include Validations::FinancialValidations has_paper_trail @@ -538,6 +540,26 @@ private end end + def reset_invalid_unresolved_log_fields! + return unless unresolved? + + validate_property_void_date(self) + self.voiddate = nil if errors[:voiddate].present? + + validate_property_major_repairs(self) + self.mrcdate = nil if errors[:mrcdate].present? + + validate_rent_range(self) + if errors[:brent].present? + self.brent = nil + self.scharge = nil + self.pscharge = nil + self.supcharg = nil + end + + errors.clear + end + def reset_scheme return unless scheme && owning_organisation @@ -547,6 +569,7 @@ private def reset_invalidated_dependent_fields! super + reset_invalid_unresolved_log_fields! reset_created_by reset_scheme reset_derived_questions diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index 15936b767..a4704a12c 100644 --- a/spec/models/lettings_log_spec.rb +++ b/spec/models/lettings_log_spec.rb @@ -1855,6 +1855,43 @@ RSpec.describe LettingsLog do end describe "resetting invalidated fields" do + let(:scheme) { FactoryBot.create(:scheme, owning_organisation: created_by_user.organisation) } + let(:location) { FactoryBot.create(:location, location_code: "E07000223", scheme:) } + let(:lettings_log) do + FactoryBot.create( + :lettings_log, + renewal: 0, + rsnvac: 5, + first_time_property_let_as_social_housing: 0, + startdate: Time.zone.tomorrow, + voiddate: Time.zone.today, + mrcdate: Time.zone.today, + rent_type: 2, + needstype: 2, + period: 1, + beds: 1, + brent: 7.17, + scharge: 1, + pscharge: 1, + supcharg: 1, + created_by: created_by_user, + ) + end + + before do + LaRentRange.create!( + ranges_rent_id: "1", + la: "E07000223", + beds: 0, + lettype: 8, + soft_min: 12.41, + soft_max: 89.54, + hard_min: 10.87, + hard_max: 100.99, + start_year: lettings_log.startdate&.year, + ) + end + context "when a question that has already been answered, no longer has met dependencies" do let(:lettings_log) { FactoryBot.create(:lettings_log, :in_progress, cbl: 1, preg_occ: 2, wchair: 1) } @@ -2003,6 +2040,80 @@ RSpec.describe LettingsLog do end end end + + context "when the log is unresolved" do + before do + lettings_log.update!(unresolved: true) + end + + context "and the new startdate triggers void date validation" do + it "clears void date value" do + lettings_log.update!(startdate: Time.zone.yesterday) + lettings_log.reload + expect(lettings_log.startdate).to eq(Time.zone.yesterday) + expect(lettings_log.voiddate).to eq(nil) + end + + it "does not impact other validations" do + expect { lettings_log.update!(startdate: Time.zone.yesterday, first_time_property_let_as_social_housing: 0, rsnvac: 16) } + .to raise_error(ActiveRecord::RecordInvalid, /Enter a reason for vacancy that is not 'first let' if unit has been previously let as social housing/) + end + end + + context "and the new startdate triggers major repairs date validation" do + it "clears major repairs date value" do + lettings_log.update!(startdate: Time.zone.yesterday) + lettings_log.reload + expect(lettings_log.startdate).to eq(Time.zone.yesterday) + expect(lettings_log.mrcdate).to eq(nil) + end + end + + context "and the new location triggers the rent range validation" do + it "clears rent values" do + lettings_log.update!(location:, scheme:) + lettings_log.reload + expect(lettings_log.location).to eq(location) + expect(lettings_log.brent).to eq(nil) + expect(lettings_log.scharge).to eq(nil) + expect(lettings_log.pscharge).to eq(nil) + expect(lettings_log.supcharg).to eq(nil) + end + + it "does not impact other validations" do + expect { lettings_log.update!(location:, scheme:, first_time_property_let_as_social_housing: 0, rsnvac: 16) } + .to raise_error(ActiveRecord::RecordInvalid, /Enter a reason for vacancy that is not 'first let' if unit has been previously let as social housing/) + end + end + end + + context "when the log is resolved" do + context "and the new startdate triggers void date validation" do + it "doesn't clear void date value" do + expect { lettings_log.update!(startdate: Time.zone.yesterday) }.to raise_error(ActiveRecord::RecordInvalid, /Enter a void date that is before the tenancy start date/) + expect(lettings_log.startdate).to eq(Time.zone.yesterday) + expect(lettings_log.voiddate).to eq(Time.zone.today) + end + end + + context "and the new startdate triggers major repairs date validation" do + it "doesn't clear major repairs date value" do + expect { lettings_log.update!(startdate: Time.zone.yesterday) }.to raise_error(ActiveRecord::RecordInvalid, /Enter a major repairs date that is before the tenancy start date/) + expect(lettings_log.startdate).to eq(Time.zone.yesterday) + expect(lettings_log.mrcdate).to eq(Time.zone.today) + end + end + + context "and the new location triggers brent validation" do + it "doesn't clear rent values" do + expect { lettings_log.update!(location:, scheme:) }.to raise_error(ActiveRecord::RecordInvalid, /Rent is below the absolute minimum expected/) + expect(lettings_log.brent).to eq(7.17) + expect(lettings_log.scharge).to eq(1) + expect(lettings_log.pscharge).to eq(1) + expect(lettings_log.supcharg).to eq(1) + end + end + end end describe "tshortfall_unknown?" do