From 62d511bc75b02e0e93255eb848a185b0ef587ec4 Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Thu, 10 Nov 2022 10:48:39 +0000 Subject: [PATCH] feat: wip errors refactoring --- app/controllers/schemes_controller.rb | 52 +++++++++---------- app/views/schemes/toggle_active.html.erb | 2 +- .../schemes/toggle_active_confirm.html.erb | 2 +- config/locales/en.yml | 6 +++ 4 files changed, 34 insertions(+), 28 deletions(-) diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index b1db9064f..d249d129d 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -22,32 +22,20 @@ class SchemesController < ApplicationController end def deactivate - if params[:scheme] && params[:scheme][:confirm] && deactivation_date.present? - if @scheme.update!(deactivation_date:) + deactivation_date_value = deactivation_date + + if @scheme.errors.present? + render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity + elsif deactivation_date_value.blank? + render "toggle_active", locals: { action: "deactivate" } + elsif params[:scheme][:confirm].present? + if @scheme.update(deactivation_date: deactivation_date_value) # update the logs flash[:notice] = "#{@scheme.service_name} has been deactivated" end redirect_to scheme_details_path(@scheme) - elsif deactivation_date.present? - if deactivation_date == "other" - if params[:scheme]["deactivation_date(1i)"].blank? && params[:scheme]["deactivation_date(2i)"].blank? && params[:scheme]["deactivation_date(3i)"].blank? - @scheme.errors.add(:deactivation_date, message: "Enter a date") - elsif params[:scheme]["deactivation_date(3i)"].blank? - @scheme.errors.add(:deactivation_date, message: "Enter the day") - elsif params[:scheme]["deactivation_date(2i)"].blank? - @scheme.errors.add(:deactivation_date, message: "Enter the month") - elsif params[:scheme]["deactivation_date(1i)"].blank? - @scheme.errors.add(:deactivation_date, message: "Enter the year") - end - render "toggle_active", locals: { action: "deactivate", deactivation_date: }, status: :unprocessable_entity - else - render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: } - end - elsif params[:scheme] - @scheme.errors.add(:deactivation_date, message: "Select one of the options") - render "toggle_active", locals: { action: "deactivate", deactivation_date: }, status: :unprocessable_entity else - render "toggle_active", locals: { action: "deactivate" } + render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: deactivation_date_value } end end @@ -158,12 +146,24 @@ class SchemesController < ApplicationController end def deactivation_date - if params[:scheme].blank? - nil - elsif params[:scheme][:deactivation_date] == "other" && params[:scheme]["deactivation_date(1i)"].present? && params[:scheme]["deactivation_date(2i)"].present? && params[:scheme]["deactivation_date(3i)"].present? - Time.utc(params[:scheme]["deactivation_date(1i)"].to_i, params[:scheme]["deactivation_date(2i)"].to_i, params[:scheme]["deactivation_date(3i)"].to_i) + return if params[:scheme].blank? + return @scheme.errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.not_selected")) if params[:scheme][:deactivation_date].blank? + return params[:scheme][:deactivation_date] unless params[:scheme][:deactivation_date] == "other" + + day = params[:scheme]["deactivation_date(3i)"] + month = params[:scheme]["deactivation_date(2i)"] + year = params[:scheme]["deactivation_date(1i)"] + + if [day, month, year].any?(&:blank?) + { day:, month:, year: }.each do |period, value| + @scheme.errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.not_entered", period: period.to_s)) if value.blank? + end + elsif !Date.valid_date?(year.to_i, month.to_i, day.to_i) + @scheme.errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.invalid")) + elsif !year.to_i.between?(2000, 2200) + @scheme.errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.invalid")) else - params[:scheme][:deactivation_date] + Date.new(year.to_i, month.to_i, day.to_i) end end diff --git a/app/views/schemes/toggle_active.html.erb b/app/views/schemes/toggle_active.html.erb index d06066363..0d5972d99 100644 --- a/app/views/schemes/toggle_active.html.erb +++ b/app/views/schemes/toggle_active.html.erb @@ -22,7 +22,7 @@ "other", label: { text: "For tenancies starting after a certain date" }, **basic_conditional_html_attributes({"deactivation_date" => ["other"]}, "scheme") do %> - <%= f.govuk_date_field :deactivation_date_field, + <%= f.govuk_date_field :deactivation_date, legend: { text: "Date", size: "m" }, hint: { text: "For example, 27 3 2008" }, width: 20 %> diff --git a/app/views/schemes/toggle_active_confirm.html.erb b/app/views/schemes/toggle_active_confirm.html.erb index f4025378c..af052e890 100644 --- a/app/views/schemes/toggle_active_confirm.html.erb +++ b/app/views/schemes/toggle_active_confirm.html.erb @@ -7,7 +7,7 @@ <%= "This change will affect SOME logs" %> <%= govuk_warning_text text: "Your data providers will need to review these logs and answer a few questions again. We’ll email each log creator with a list of logs that need updating." %> - <%= f.hidden_field :confirm %> + <%= f.hidden_field :confirm, :value => true %> <%= f.hidden_field :deactivation_date, :value => deactivation_date %>
<%= f.govuk_submit "Deactivate this scheme" %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 55ea3a0f7..4f4aab23d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -311,6 +311,12 @@ en: declaration: missing: "You must show the DLUHC privacy notice to the tenant before you can submit this log." + scheme: + deactivation_date: + not_selected: "Select one of the options" + not_entered: "Enter a %{period}" + invalid: "Enter a valid date" + soft_validations: net_income: title_text: "Net income is outside the expected range based on the lead tenant’s working situation"