diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index 48ac8e8a1..7da934cb2 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -24,14 +24,14 @@ class SchemesController < ApplicationController def deactivate if params[:scheme].blank? render "toggle_active", locals: { action: "deactivate" } - elsif params[:scheme][:confirm].present? && params[:scheme][:deactivation_date].present? - confirm_deactivation else - @scheme.run_validations = true + @scheme.run_deactivation_validations = true @scheme.deactivation_date = deactivation_date @scheme.deactivation_date_type = params[:scheme][:deactivation_date_type] - if @scheme.valid? - render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: } + if params[:scheme][:confirm].present? + confirm_deactivation + elsif @scheme.valid? + render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: @scheme.deactivation_date, deactivation_date_type: @scheme.deactivation_date_type } else render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity end @@ -294,13 +294,13 @@ private collection_start_date = FormHandler.instance.current_collection_start_date return collection_start_date if params[:scheme][:deactivation_date_type] == "default" - return nil if params[:scheme][:deactivation_date_type].blank? + return params[:scheme][:deactivation_date] if params[:scheme][:deactivation_date].present? day = params[:scheme]["deactivation_date(3i)"] month = params[:scheme]["deactivation_date(2i)"] year = params[:scheme]["deactivation_date(1i)"] return nil if [day, month, year].any?(&:blank?) - Date.new(year.to_i, month.to_i, day.to_i) if Date.valid_date?(year.to_i, month.to_i, day.to_i) + Time.utc(year.to_i, month.to_i, day.to_i) if Date.valid_date?(year.to_i, month.to_i, day.to_i) end end diff --git a/app/models/scheme.rb b/app/models/scheme.rb index f5e7d43ba..6d1cdbf64 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -22,7 +22,7 @@ class Scheme < ApplicationRecord auto_strip_attributes :service_name - attr_accessor :deactivation_date_type, :run_validations + attr_accessor :deactivation_date_type, :run_deactivation_validations SENSITIVE = { No: 0, @@ -226,7 +226,7 @@ class Scheme < ApplicationRecord end def deactivation_date_errors - return unless :run_validations + return unless run_deactivation_validations == true collection_start_date = FormHandler.instance.current_collection_start_date if deactivation_date_type.blank? diff --git a/app/views/schemes/toggle_active_confirm.html.erb b/app/views/schemes/toggle_active_confirm.html.erb index 596245294..bb07bf5b2 100644 --- a/app/views/schemes/toggle_active_confirm.html.erb +++ b/app/views/schemes/toggle_active_confirm.html.erb @@ -11,6 +11,7 @@ <%= govuk_warning_text text: I18n.t("warnings.scheme.deactivation.review_logs") %> <%= f.hidden_field :confirm, value: true %> <%= f.hidden_field :deactivation_date, value: deactivation_date %> + <%= f.hidden_field :deactivation_date_type, value: deactivation_date_type %>
<%= f.govuk_submit "#{action.humanize} this scheme" %> <%= govuk_button_link_to "Cancel", scheme_details_path(@scheme), html: { method: :get }, secondary: true %>