From 7951ba107e8b0ad0ed2ee9527c7f8466979aaff9 Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Thu, 16 Mar 2023 17:12:09 +0000 Subject: [PATCH] feat: respond to pr comments --- .../sales/financial_validations.rb | 58 +++++++++---------- config/locales/en.yml | 2 +- 2 files changed, 27 insertions(+), 33 deletions(-) diff --git a/app/models/validations/sales/financial_validations.rb b/app/models/validations/sales/financial_validations.rb index f10a4542e..50dbdf297 100644 --- a/app/models/validations/sales/financial_validations.rb +++ b/app/models/validations/sales/financial_validations.rb @@ -88,20 +88,15 @@ module Validations::Sales::FinancialValidations def validate_equity_in_range_for_year_and_type(record) return unless record.type && record.equity && record.collection_start_year - if record.collection_start_year == 2022 - ranges = EQUITY_RANGES_FOR_TYPES_BY_COLLECTION_START_YEAR[2022] - elsif record.collection_start_year >= 2023 - ranges = EQUITY_RANGES_FOR_TYPES_BY_COLLECTION_START_YEAR[2023] - end - - if ranges.key?(record.type) - range = ranges[record.type] - if record.equity < range["min"] - record.errors.add :type, I18n.t("validations.financial.equity.under_min", min_equity: range["min"]) - record.errors.add :equity, I18n.t("validations.financial.equity.under_min", min_equity: range["min"]) - elsif record.equity > range["max"] - record.errors.add :type, I18n.t("validations.financial.equity.over_max") - record.errors.add :equity, I18n.t("validations.financial.equity.over_max") + ranges = EQUITY_RANGES_BY_YEAR.fetch(record.collection_start_year, DEFAULT_EQUITY_RANGES) + + if range = ranges[record.type] + if record.equity < range.min + record.errors.add :type, I18n.t("validations.financial.equity.under_min", min_equity: range.min) + record.errors.add :equity, I18n.t("validations.financial.equity.under_min", min_equity: range.min) + elsif record.equity > range.max + record.errors.add :type, I18n.t("validations.financial.equity.over_max", max_equity: range.max) + record.errors.add :equity, I18n.t("validations.financial.equity.over_max", max_equity: range.max) end end end @@ -116,25 +111,24 @@ private economic_status == 9 end - EQUITY_RANGES_FOR_TYPES_BY_COLLECTION_START_YEAR = { + EQUITY_RANGES_BY_YEAR = { 2022 => { - 2 => { "min" => 25, "max" => 75 }, - 30 => { "min" => 10, "max" => 75 }, - 18 => { "min" => 25, "max" => 75 }, - 16 => { "min" => 10, "max" => 75 }, - 24 => { "min" => 25, "max" => 75 }, - # 28 => ranges don't apply - 31 => { "min" => 0, "max" => 75 }, - }, - 2023 => { - 2 => { "min" => 10, "max" => 75 }, - 30 => { "min" => 25, "max" => 75 }, - 18 => { "min" => 25, "max" => 75 }, - 16 => { "min" => 10, "max" => 75 }, - 24 => { "min" => 25, "max" => 75 }, - # 28 => ranges don't apply - 31 => { "min" => 0, "max" => 75 }, - 32 => { "min" => 0, "max" => 75 }, + 2 => 25..75, + 30 => 10..75, + 18 => 25..75, + 16 => 10..75, + 24 => 25..75, + 31 => 0..75, }, }.freeze + + DEFAULT_EQUITY_RANGES = { + 2 => 10..75, + 30 => 25..75, + 18 => 25..75, + 16 => 10..75, + 24 => 25..75, + 31 => 0..75, + 32 => 0..75, + }.freeze end diff --git a/config/locales/en.yml b/config/locales/en.yml index 6074038c5..d140ad617 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -307,7 +307,7 @@ en: percentage_bought_must_be_at_least_threshold: "The minimum increase in equity while staircasing is %{threshold}%" equity: under_min: "The minimum initial equity stake for this type of shared ownership sale is %{min_equity}%" - over_max: "The maximum initial equity stake is 75%" + over_max: "The maximum initial equity stake is %{max_equity}%" household: reasonpref: