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