From 2787815a9d0854f7483a2a0868b332ab0db9c22f Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Mon, 30 Jan 2023 09:54:40 +0000 Subject: [PATCH] CLDC-878 Validate basic monthly rent (#1228) * Validate basic monthly rent * Make the test description clearer --- app/models/sales_log.rb | 4 ++ .../sales/sale_information_validations.rb | 9 +++ config/locales/en.yml | 2 + .../sale_information_validations_spec.rb | 57 +++++++++++++++++++ 4 files changed, 72 insertions(+) diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index aa3eb1c09..a9e8b807e 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -211,4 +211,8 @@ class SalesLog < Log def old_persons_shared_ownership? type == 24 end + + def shared_owhership_scheme? + ownershipsch == 1 + end end diff --git a/app/models/validations/sales/sale_information_validations.rb b/app/models/validations/sales/sale_information_validations.rb index c42c42ccc..53d76fd48 100644 --- a/app/models/validations/sales/sale_information_validations.rb +++ b/app/models/validations/sales/sale_information_validations.rb @@ -54,4 +54,13 @@ module Validations::Sales::SaleInformationValidations end end end + + def validate_basic_monthly_rent(record) + return unless record.mrent && record.ownershipsch && record.type + + if record.shared_owhership_scheme? && !record.old_persons_shared_ownership? && record.mrent > 9999 + record.errors.add :mrent, I18n.t("validations.sale_information.monthly_rent.higher_than_expected") + record.errors.add :type, I18n.t("validations.sale_information.monthly_rent.higher_than_expected") + end + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 3dd612e8e..67a819e52 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -426,6 +426,8 @@ en: previous_property_type: property_type_bedsit: "A bedsit can not have more than 1 bedroom" discounted_ownership_value: "Mortgage, deposit, and grant total must equal £%{value_with_discount}" + monthly_rent: + higher_than_expected: "Basic monthly rent must be between £0 and £9,999" soft_validations: net_income: diff --git a/spec/models/validations/sales/sale_information_validations_spec.rb b/spec/models/validations/sales/sale_information_validations_spec.rb index d2921e8f0..8783efd09 100644 --- a/spec/models/validations/sales/sale_information_validations_spec.rb +++ b/spec/models/validations/sales/sale_information_validations_spec.rb @@ -371,4 +371,61 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end end end + + describe "#validate_basic_monthly_rent" do + context "when within permitted bounds" do + let(:record) { build(:sales_log, mrent: 9998, ownershipsch: 1, type: 2) } + + it "does not add an error" do + sale_information_validator.validate_basic_monthly_rent(record) + + expect(record.errors[:mrent]).not_to be_present + expect(record.errors[:type]).not_to be_present + end + end + + context "when the rent is blank" do + let(:record) { build(:sales_log, mrent: nil, ownershipsch: 1, type: 2) } + + it "does not add an error" do + sale_information_validator.validate_basic_monthly_rent(record) + + expect(record.errors[:mrent]).not_to be_present + expect(record.errors[:type]).not_to be_present + end + end + + context "when the type is old persons shared ownership" do + let(:record) { build(:sales_log, mrent: 100_000, ownershipsch: 1, type: 24) } + + it "does not add an error" do + sale_information_validator.validate_basic_monthly_rent(record) + + expect(record.errors[:mrent]).not_to be_present + expect(record.errors[:type]).not_to be_present + end + end + + context "when the type is blank" do + let(:record) { build(:sales_log, mrent: 100_000, ownershipsch: 1, type: nil) } + + it "does not add an error" do + sale_information_validator.validate_basic_monthly_rent(record) + + expect(record.errors[:mrent]).not_to be_present + expect(record.errors[:type]).not_to be_present + end + end + + context "when higher than upper bound" do + let(:record) { build(:sales_log, mrent: 100_000, ownershipsch: 1, type: 2) } + + it "adds an error" do + sale_information_validator.validate_basic_monthly_rent(record) + + expect(record.errors[:mrent]).to include(I18n.t("validations.sale_information.monthly_rent.higher_than_expected")) + expect(record.errors[:type]).to include(I18n.t("validations.sale_information.monthly_rent.higher_than_expected")) + end + end + end end