From 3051862f01f1fafea8e6c5f9574941db9c8f8884 Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 23 Jan 2023 16:02:16 +0000 Subject: [PATCH] Add previous property bedsit validations --- .../sales/sale_information_validations.rb | 9 ++++++++ config/locales/en.yml | 5 +++++ .../sale_information_validations_spec.rb | 22 +++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/app/models/validations/sales/sale_information_validations.rb b/app/models/validations/sales/sale_information_validations.rb index 11049e8ff..49e39d052 100644 --- a/app/models/validations/sales/sale_information_validations.rb +++ b/app/models/validations/sales/sale_information_validations.rb @@ -14,4 +14,13 @@ module Validations::Sales::SaleInformationValidations record.errors.add :hodate, "Practical completion or handover date must be before exchange date" end end + + def validate_previous_property_unit_type(record) + return unless record.fromprop && record.frombeds + + if record.frombeds != 1 && record.fromprop == 2 + record.errors.add :frombeds, I18n.t("validations.sale_information.previous_property_beds.property_type_bedsit") + record.errors.add :fromprop, I18n.t("validations.sale_information.previous_property_type.property_type_bedsit") + end + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index fbfd0c8e0..73bd8bdf7 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -408,6 +408,11 @@ en: before_deactivation: "This location was deactivated on %{date}. The reactivation date must be on or after deactivation date" deactivation: during_deactivated_period: "The location is already deactivated during this date, please enter a different date" + sale_information: + previous_property_beds: + property_type_bedsit: "Bedsit bedroom maximum 1" + previous_property_type: + property_type_bedsit: "A bedsit can not have more than 1 bedroom" 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 a808d2ba8..fafe2786a 100644 --- a/spec/models/validations/sales/sale_information_validations_spec.rb +++ b/spec/models/validations/sales/sale_information_validations_spec.rb @@ -108,4 +108,26 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end end end + + describe "#validate_previous_property_unit_type" do + context "when number of bedrooms is <= 1" do + let(:record) { FactoryBot.build(:sales_log, frombeds: 1, fromprop: 2) } + + it "does not add an error if it's a bedsit" do + sale_information_validator.validate_previous_property_unit_type(record) + + expect(record.errors).to be_empty + end + end + + context "when number of bedrooms is > 1" do + let(:record) { FactoryBot.build(:sales_log, frombeds: 2, fromprop: 2) } + + it "does add an error if it's a bedsit" do + sale_information_validator.validate_previous_property_unit_type(record) + expect(record.errors["fromprop"]).to include(I18n.t("validations.sale_information.previous_property_type.property_type_bedsit")) + expect(record.errors["frombeds"]).to include(I18n.t("validations.sale_information.previous_property_beds.property_type_bedsit")) + end + end + end end