From 07e7d4f685dee36778a64ab333976f71e1439bec Mon Sep 17 00:00:00 2001 From: Sam Seed Date: Mon, 13 Mar 2023 12:04:37 +0000 Subject: [PATCH] feat: add validation to shared ownership type as well --- .../sales/financial_validations.rb | 1 + config/locales/en.yml | 2 ++ .../sales/financial_validations_spec.rb | 20 +++++++++++++++++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/app/models/validations/sales/financial_validations.rb b/app/models/validations/sales/financial_validations.rb index 7a5ab648a..a59e975ec 100644 --- a/app/models/validations/sales/financial_validations.rb +++ b/app/models/validations/sales/financial_validations.rb @@ -63,6 +63,7 @@ module Validations::Sales::FinancialValidations if threshold && record.stairbought < threshold record.errors.add :stairbought, I18n.t("validations.financial.staircasing.percentage_bought_must_be_at_least_threshold", threshold:) + record.errors.add :type, I18n.t("validations.setup.type.percentage_bought_must_be_at_least_threshold", threshold:) end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 045397650..e17c0ab9b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -159,6 +159,8 @@ en: Enter a date within the %{current_start_year_short}/%{current_end_year_short} financial year, which is between %{current_start_year_long} and %{current_end_year_long} previous_and_current_financial_year: "Enter a date within the %{previous_start_year_short}/%{previous_end_year_short} or %{previous_end_year_short}/%{current_end_year_short} financial years, which is between %{previous_start_year_long} and %{current_end_year_long}" + type: + percentage_bought_must_be_at_least_threshold: "The minimum increase in equity while staircasing is %{threshold}% for this shared ownership type" startdate: later_than_14_days_after: "The tenancy start date must not be later than 14 days from today’s date" diff --git a/spec/models/validations/sales/financial_validations_spec.rb b/spec/models/validations/sales/financial_validations_spec.rb index d3446bdee..55d6e3c52 100644 --- a/spec/models/validations/sales/financial_validations_spec.rb +++ b/spec/models/validations/sales/financial_validations_spec.rb @@ -163,27 +163,31 @@ RSpec.describe Validations::Sales::FinancialValidations do describe "#validate_percentage_bought_at_least_threshold" do let(:record) { FactoryBot.create(:sales_log) } - it "adds an error to stairbought if the percentage bought is less than the threshold (which depends on the shared ownership type)" do + it "adds an error to stairbought and type if the percentage bought is less than the threshold (which is 1% by default, but higher for some shared ownership types)" do record.stairbought = 9 record.type = 2 financial_validator.validate_percentage_bought_at_least_threshold(record) expect(record.errors["stairbought"]).to eq(["The minimum increase in equity while staircasing is 10%"]) + expect(record.errors["type"]).to eq(["The minimum increase in equity while staircasing is 10% for this shared ownership type"]) record.errors.clear record.type = 16 financial_validator.validate_percentage_bought_at_least_threshold(record) expect(record.errors["stairbought"]).to eq(["The minimum increase in equity while staircasing is 10%"]) + expect(record.errors["type"]).to eq(["The minimum increase in equity while staircasing is 10% for this shared ownership type"]) record.errors.clear record.type = 18 financial_validator.validate_percentage_bought_at_least_threshold(record) expect(record.errors["stairbought"]).to eq(["The minimum increase in equity while staircasing is 10%"]) + expect(record.errors["type"]).to eq(["The minimum increase in equity while staircasing is 10% for this shared ownership type"]) record.errors.clear record.type = 24 financial_validator.validate_percentage_bought_at_least_threshold(record) expect(record.errors["stairbought"]).to eq(["The minimum increase in equity while staircasing is 10%"]) + expect(record.errors["type"]).to eq(["The minimum increase in equity while staircasing is 10% for this shared ownership type"]) record.errors.clear record.stairbought = 0 @@ -191,45 +195,53 @@ RSpec.describe Validations::Sales::FinancialValidations do record.type = 28 financial_validator.validate_percentage_bought_at_least_threshold(record) expect(record.errors["stairbought"]).to eq(["The minimum increase in equity while staircasing is 1%"]) + expect(record.errors["type"]).to eq(["The minimum increase in equity while staircasing is 1% for this shared ownership type"]) record.errors.clear record.type = 30 financial_validator.validate_percentage_bought_at_least_threshold(record) expect(record.errors["stairbought"]).to eq(["The minimum increase in equity while staircasing is 1%"]) + expect(record.errors["type"]).to eq(["The minimum increase in equity while staircasing is 1% for this shared ownership type"]) record.errors.clear record.type = 31 financial_validator.validate_percentage_bought_at_least_threshold(record) expect(record.errors["stairbought"]).to eq(["The minimum increase in equity while staircasing is 1%"]) + expect(record.errors["type"]).to eq(["The minimum increase in equity while staircasing is 1% for this shared ownership type"]) record.errors.clear record.type = 32 financial_validator.validate_percentage_bought_at_least_threshold(record) expect(record.errors["stairbought"]).to eq(["The minimum increase in equity while staircasing is 1%"]) + expect(record.errors["type"]).to eq(["The minimum increase in equity while staircasing is 1% for this shared ownership type"]) record.errors.clear end - it "doesn't add an error to stairbought if the percentage bought is greater than or equal to the threshold (which depends on the shared ownership type)" do + it "doesn't add an error to stairbought and type if the percentage bought is less than the threshold (which is 1% by default, but higher for some shared ownership types)" do record.stairbought = 10 record.type = 2 financial_validator.validate_percentage_bought_at_least_threshold(record) expect(record.errors["stairbought"]).to be_empty + expect(record.errors["type"]).to be_empty record.errors.clear record.type = 16 financial_validator.validate_percentage_bought_at_least_threshold(record) expect(record.errors["stairbought"]).to be_empty + expect(record.errors["type"]).to be_empty record.errors.clear record.type = 18 financial_validator.validate_percentage_bought_at_least_threshold(record) expect(record.errors["stairbought"]).to be_empty + expect(record.errors["type"]).to be_empty record.errors.clear record.type = 24 financial_validator.validate_percentage_bought_at_least_threshold(record) expect(record.errors["stairbought"]).to be_empty + expect(record.errors["type"]).to be_empty record.errors.clear record.stairbought = 1 @@ -237,21 +249,25 @@ RSpec.describe Validations::Sales::FinancialValidations do record.type = 28 financial_validator.validate_percentage_bought_at_least_threshold(record) expect(record.errors["stairbought"]).to be_empty + expect(record.errors["type"]).to be_empty record.errors.clear record.type = 30 financial_validator.validate_percentage_bought_at_least_threshold(record) expect(record.errors["stairbought"]).to be_empty + expect(record.errors["type"]).to be_empty record.errors.clear record.type = 31 financial_validator.validate_percentage_bought_at_least_threshold(record) expect(record.errors["stairbought"]).to be_empty + expect(record.errors["type"]).to be_empty record.errors.clear record.type = 32 financial_validator.validate_percentage_bought_at_least_threshold(record) expect(record.errors["stairbought"]).to be_empty + expect(record.errors["type"]).to be_empty record.errors.clear end end