From 92029ea2a09d135b84e843ac3ec2fe97c6ec3233 Mon Sep 17 00:00:00 2001 From: SamSeed-Softwire <63662292+SamSeed-Softwire@users.noreply.github.com> Date: Tue, 14 Feb 2023 10:41:40 +0000 Subject: [PATCH] CLDC-859 Sales validation - bedsits cannot have more than 1 bed (#1188) * feat: add sales validation to check bedsits have <=1 beds * test: sales validation to check bedsits have <=1 beds * fix: typo in property validation method name * feat: add same bedsit validation to number of bedrooms question * test: fix typo propert -> property in property validations tests * test: add test for validate_property_number_of_bedrooms (sales) * feat: update wording for 'number of bedrooms' validation when bedsit * test: condense sales property validations tests * test: strengthen sales property validations error addition test * refactor: simplify sales property validations into one method * fix: update error message content to match Beth's choices * chore: lint * test: add requests test for invalid sales log params when posting * refactor: use if rather than unless... not in property validation * refactor: write method is_bedsit? on SalesLog * test: check bedsit error not added if proptype or beds is nil * lint: use update! not update * feat: update validation messages to improve readability * fix: provide valid date in request test for invalid proptype/beds * feat: make is_bedsit and validate_bedsit_number_of_beds more readable --- app/models/sales_log.rb | 4 +++ .../validations/sales/property_validations.rb | 9 +++++ config/locales/en.yml | 3 ++ .../sales/property_validations_spec.rb | 36 +++++++++++++++++++ spec/requests/sales_logs_controller_spec.rb | 25 +++++++++++++ 5 files changed, 77 insertions(+) diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index 463381559..add706f7c 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -223,6 +223,10 @@ class SalesLog < Log type == 24 end + def is_bedsit? + proptype == 2 + end + def shared_ownership_scheme? ownershipsch == 1 end diff --git a/app/models/validations/sales/property_validations.rb b/app/models/validations/sales/property_validations.rb index 879e37ff1..83f86ec8f 100644 --- a/app/models/validations/sales/property_validations.rb +++ b/app/models/validations/sales/property_validations.rb @@ -7,4 +7,13 @@ module Validations::Sales::PropertyValidations record.errors.add :ppostcode_full, I18n.t("validations.property.postcode.must_match_previous") end end + + def validate_bedsit_number_of_beds(record) + return unless record.proptype.present? && record.beds.present? + + if record.is_bedsit? && record.beds > 1 + record.errors.add :proptype, I18n.t("validations.property.proptype.bedsits_have_max_one_bedroom") + record.errors.add :beds, I18n.t("validations.property.beds.bedsits_have_max_one_bedroom") + end + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 0a4ae34de..98e40dfb3 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -202,6 +202,9 @@ en: beds: non_positive: "Number of bedrooms has to be greater than 0" over_max: "Number of bedrooms cannot be more than 12" + bedsits_have_max_one_bedroom: "Number of bedrooms must be 1 if the property is a bedsit" + proptype: + bedsits_have_max_one_bedroom: "Answer cannot be 'Bedsit' if the property has 2 or more bedrooms" postcode: must_match_previous: "Buyer's last accommodation and discounted ownership postcodes must match" diff --git a/spec/models/validations/sales/property_validations_spec.rb b/spec/models/validations/sales/property_validations_spec.rb index fc3fdd1a8..180f16021 100644 --- a/spec/models/validations/sales/property_validations_spec.rb +++ b/spec/models/validations/sales/property_validations_spec.rb @@ -48,4 +48,40 @@ RSpec.describe Validations::Sales::PropertyValidations do end end end + + describe "#validate_property_unit_type" do + context "when number of bedrooms is 1" do + let(:record) { FactoryBot.build(:sales_log, beds: 1, proptype: 2) } + + it "does not add an error if it's a bedsit" do + property_validator.validate_bedsit_number_of_beds(record) + expect(record.errors).not_to be_present + end + end + + context "when number of bedrooms is > 1" do + let(:record) { FactoryBot.build(:sales_log, beds: 2, proptype: 2) } + + it "does add an error if it's a bedsit" do + property_validator.validate_bedsit_number_of_beds(record) + expect(record.errors.added?(:proptype, "Answer cannot be 'Bedsit' if the property has 2 or more bedrooms")).to be true + expect(record.errors.added?(:beds, "Number of bedrooms must be 1 if the property is a bedsit")).to be true + end + + it "does not add an error if proptype is undefined" do + record.update!(proptype: nil) + property_validator.validate_bedsit_number_of_beds(record) + expect(record.errors).not_to be_present + end + end + + context "when number of bedrooms is undefined" do + let(:record) { FactoryBot.build(:sales_log, beds: nil, proptype: 2) } + + it "does not add an error if it's a bedsit" do + property_validator.validate_bedsit_number_of_beds(record) + expect(record.errors).not_to be_present + end + end + end end diff --git a/spec/requests/sales_logs_controller_spec.rb b/spec/requests/sales_logs_controller_spec.rb index 925e3f1f3..e45fee440 100644 --- a/spec/requests/sales_logs_controller_spec.rb +++ b/spec/requests/sales_logs_controller_spec.rb @@ -61,6 +61,31 @@ RSpec.describe SalesLogsController, type: :request do expect(response).to have_http_status(:unauthorized) end end + + context "with a request containing invalid json parameters" do + let(:params) do + { + "saledate": Date.new(2022, 4, 1), + "purchid": "1", + "ownershipsch": 1, + "type": 2, + "jointpur": 1, + "jointmore": 1, + "beds": 2, + "proptype": 2, + } + end + + before do + post "/sales-logs", headers:, params: params.to_json + end + + it "validates sales log parameters" do + json_response = JSON.parse(response.body) + expect(response).to have_http_status(:unprocessable_entity) + expect(json_response["errors"]).to match_array([["beds", ["Number of bedrooms must be 1 if the property is a bedsit"]], ["proptype", ["Answer cannot be 'Bedsit' if the property has 2 or more bedrooms"]]]) + end + end end context "when UI" do