From 538553aba6662bba7c93c267b178d641ec347f49 Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Thu, 19 Oct 2023 14:17:34 +0100 Subject: [PATCH] feat: make error message dynamic --- .../validations/financial_validations.rb | 29 +++++++++++++++---- config/locales/en.yml | 26 ++--------------- 2 files changed, 26 insertions(+), 29 deletions(-) diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index 29c22eb62..e9c708dbe 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -146,7 +146,7 @@ module Validations::FinancialValidations private - CHARGE_MAXIMUMS = { + CHARGE_MAXIMA_PER_WEEK = { scharge: { private_registered_provider: { general_needs: 800, @@ -181,22 +181,39 @@ private PROVIDER_TYPE = { 1 => :local_authority, 2 => :private_registered_provider }.freeze NEEDSTYPE_VALUES = { 2 => :supported_housing, 1 => :general_needs }.freeze + CHARGE_NAMES = { :scharge => "service charge", :pscharge => "personal service charge", :supcharg => "support charge" }.freeze + FREQUENCIES = { + 2 => "every 2 weeks", + 3 => "every 4 weeks", + 4 => "every calendar month", + 9 => "weekly for 46 weeks", + 8 => "weekly for 47 weeks", + 7 => "weekly for 48 weeks", + 6 => "weekly for 49 weeks", + 5 => "weekly for 50 weeks", + 1 => "weekly for 52 weeks", + 10 => "weekly for 53 weeks", + }.freeze def validate_charges(record) return unless record.owning_organisation provider_type = record.owning_organisation.provider_type_before_type_cast %i[scharge pscharge supcharg].each do |charge| - maximum = CHARGE_MAXIMUMS.dig(charge, PROVIDER_TYPE[provider_type], NEEDSTYPE_VALUES[record.needstype]) + maximum_per_week = CHARGE_MAXIMA_PER_WEEK.dig(charge, PROVIDER_TYPE[provider_type], NEEDSTYPE_VALUES[record.needstype]) - if maximum.present? && record[:period].present? && record[charge].present? && !weekly_value_in_range(record, charge, 0.0, maximum) - record.errors.add charge, :outside_the_range, message: I18n.t("validations.financial.rent.#{charge}.#{PROVIDER_TYPE[provider_type]}.#{NEEDSTYPE_VALUES[record.needstype]}") - end + next unless maximum_per_week.present? && record[:period].present? && record[charge].present? && !weekly_value_in_range(record, charge, 0.0, maximum_per_week) + + charge_name = CHARGE_NAMES[charge] + frequency = FREQUENCIES[record[:period]] + letting_type = NEEDSTYPE_VALUES[record.needstype].to_s.humanize(capitalize: false) + provider_type = PROVIDER_TYPE[provider_type].to_s.humanize(capitalize: false) + record.errors.add charge, :outside_the_range, message: I18n.t("validations.financial.rent.out_of_range", charge_name:, maximum_per_week:, frequency:, letting_type:, provider_type:) end end def weekly_value_in_range(record, field, min, max) - record[field].present? && record.weekly_value(record[field]).present? && record.weekly_value(record[field]).between?(min, max) + record.weekly_value(record[field])&.between?(min, max) end def validate_rent_range(record) diff --git a/config/locales/en.yml b/config/locales/en.yml index 2890f3bde..b10fafd93 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -265,7 +265,7 @@ en: owning_organisation: "Enter a date when the owning organisation was active. %{owning_organisation} became inactive on %{owning_organisation_merge_date} and was replaced by %{owning_absorbing_organisation}." managing_organisation: "Enter a date when the managing organisation was active. %{managing_organisation} became inactive on %{managing_organisation_merge_date} and was replaced by %{managing_absorbing_organisation}." different_merge: "Enter a date when the owning and managing organisations were active. %{owning_organisation} became inactive on %{owning_organisation_merge_date} and was replaced by %{owning_absorbing_organisation}. %{managing_organisation} became inactive on %{managing_organisation_merge_date} and was replaced by %{managing_absorbing_organisation}." - invalid_absorbing_organisations_start_date: + invalid_absorbing_organisations_start_date: same_organisation: "Enter a date when the owning and managing organisation was active. %{owning_organisation} became active on %{owning_organisation_available_from}." owning_organisation: "Enter a date when the owning organisation was active. %{owning_organisation} became active on %{owning_organisation_available_from}." managing_organisation: "Enter a date when the managing organisation was active. %{managing_organisation} became active on %{managing_organisation_available_from}." @@ -367,27 +367,7 @@ en: negative_currency: "Enter an amount above 0" rent: less_than_shortfall: "Enter an amount that is more than the shortfall in basic rent" - scharge: - private_registered_provider: - general_needs: "Enter a value for the service charge between £0 and £800 per week if the landlord is a private registered provider and it is a general needs letting" - supported_housing: "Enter a value for the service charge between £0 and £800 per week if the landlord is a private registered provider and it is a supported housing letting" - local_authority: - general_needs: "Enter a value for the service charge between £0 and £500 per week if the landlord is a local authority and it is a general needs letting" - supported_housing: "Enter a value for the service charge between £0 and £500 per week if the landlord is a local authority and it is a supported housing letting" - pscharge: - private_registered_provider: - general_needs: "Enter a value for the personal service charge between £0 and £700 per week if the landlord is a private registered provider and it is a general needs letting" - supported_housing: "Enter a value for the personal service charge between £0 and £700 per week if the landlord is a private registered provider and it is a supported housing letting" - local_authority: - general_needs: "Enter a value for the personal service charge between £0 and £200 per week if the landlord is a local authority and it is a general needs letting" - supported_housing: "Enter a value for the personal service charge between £0 and £200 per week if the landlord is a local authority and it is a supported housing letting" - supcharg: - private_registered_provider: - general_needs: "Enter a value for the support charge between £0 and £800 per week if the landlord is a private registered provider and it is a general needs letting" - supported_housing: "Enter a value for the support charge between £0 and £800 per week if the landlord is a private registered provider and it is a supported housing letting" - local_authority: - general_needs: "Enter a value for the support charge between £0 and £200 per week if the landlord is a local authority and it is a general needs letting" - supported_housing: "Enter a value for the support charge between £0 and £200 per week if the landlord is a local authority and it is a supported housing letting" + out_of_range: "Enter a value for the %{charge_name} between £0 and £%{maximum_per_week} per week. £%{maximum_per_week} is the maximum limit per week for rent and charges paid %{frequency} for %{letting_type} lettings owned by a %{provider_type}." ecstat: over_hard_max: "Net income of %{hard_max} per week is too high given the tenant’s working situation" brent: @@ -722,7 +702,7 @@ Make sure these answers are correct." charges: informative_text: "This is higher than we would expect." hint_text: "Check the following:" - + devise: email: