diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index 208394942..41d24688c 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -50,10 +50,23 @@ class SchemesController < ApplicationController redirect_to scheme_details_path(@scheme) end - def reactivate + def new_reactivation render "toggle_active", locals: { action: "reactivate" } end + def reactivate + @scheme.run_reactivation_validations! + @scheme.reactivation_date = reactivation_date + @scheme.reactivation_date_type = params[:scheme][:reactivation_date_type] + + if @scheme.valid? && @scheme.location_deactivation_periods.deactivations_without_reactivation.update!(reactivation_date:) + flash[:notice] = reactivate_success_notice + redirect_to scheme_details_path(@scheme) + else + render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity + end + end + def new @scheme = Scheme.new end @@ -303,24 +316,36 @@ private end end + def reactivate_success_notice + "#{@scheme.service_name} has been reactivated" + end + def deactivation_date + toggle_date("deactivation_date") + end + + def reactivation_date + toggle_date("reactivation_date") + end + + def toggle_date(key) if params[:scheme].blank? return - elsif params[:scheme][:deactivation_date_type] == "default" + elsif params[:scheme]["#{key}_type".to_sym] == "default" return FormHandler.instance.current_collection_start_date - elsif params[:scheme][:deactivation_date].present? - return params[:scheme][:deactivation_date] + elsif params[:scheme][key.to_sym].present? + return params[:scheme][key.to_sym] end - day = params[:scheme]["deactivation_date(3i)"] - month = params[:scheme]["deactivation_date(2i)"] - year = params[:scheme]["deactivation_date(1i)"] + day = params[:scheme]["#{key}(3i)"] + month = params[:scheme]["#{key}(2i)"] + year = params[:scheme]["#{key}(1i)"] return nil if [day, month, year].any?(&:blank?) Time.zone.local(year.to_i, month.to_i, day.to_i) if Date.valid_date?(year.to_i, month.to_i, day.to_i) end def update_affected_logs - @scheme.lettings_logs.filter_by_before_startdate(deactivation_date.to_time).update!(location: nil, scheme: nil) + @scheme.lettings_logs.filter_by_before_startdate(deactivation_date.to_time).update!(scheme: nil, location: nil) end end diff --git a/app/helpers/toggle_active_scheme_helper.rb b/app/helpers/toggle_active_scheme_helper.rb new file mode 100644 index 000000000..93b9299b5 --- /dev/null +++ b/app/helpers/toggle_active_scheme_helper.rb @@ -0,0 +1,17 @@ +module ToggleActiveSchemeHelper + def toggle_scheme_form_path(action, scheme_id) + if action == "deactivate" + scheme_new_deactivation_path(scheme_id:) + else + scheme_reactivate_path(scheme_id:) + end + end + + def date_type_question(action) + action == "deactivate" ? :deactivation_date_type : :reactivation_date_type + end + + def date_question(action) + action == "deactivate" ? :deactivation_date : :reactivation_date + end +end diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 3dfe1ff55..b0e7ade3d 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -20,10 +20,11 @@ class Scheme < ApplicationRecord validate :validate_confirmed validate :deactivation_date_errors + validate :reactivation_date_errors auto_strip_attributes :service_name - attr_accessor :deactivation_date_type, :deactivation_date, :run_deactivation_validations + attr_accessor :deactivation_date_type, :deactivation_date, :run_deactivation_validations, :reactivation_date_type, :reactivation_date, :run_reactivation_validations SENSITIVE = { No: 0, @@ -252,4 +253,25 @@ class Scheme < ApplicationRecord end end end + + def run_reactivation_validations! + @run_reactivation_validations = true + end + + def reactivation_date_errors + return unless @run_reactivation_validations + + if reactivation_date.blank? + if reactivation_date_type.blank? + errors.add(:reactivation_date_type, message: I18n.t("validations.scheme.reactivation_date.not_selected")) + elsif reactivation_date_type == "other" + errors.add(:reactivation_date, message: I18n.t("validations.scheme.reactivation_date.invalid")) + end + else + collection_start_date = FormHandler.instance.current_collection_start_date + unless reactivation_date.between?(collection_start_date, Time.zone.local(2200, 1, 1)) + errors.add(:reactivation_date, message: I18n.t("validations.scheme.reactivation_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date))) + end + end + end end diff --git a/app/views/schemes/deactivate_confirm.html.erb b/app/views/schemes/deactivate_confirm.html.erb index 45f7d4341..295e0f7f9 100644 --- a/app/views/schemes/deactivate_confirm.html.erb +++ b/app/views/schemes/deactivate_confirm.html.erb @@ -8,7 +8,7 @@ <%= @scheme.service_name %> This change will affect <%= @scheme.lettings_logs.count %> logs - <%= govuk_warning_text text: I18n.t("warnings.scheme.deactivation.review_logs") %> + <%= govuk_warning_text text: I18n.t("warnings.scheme.deactivate.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 %> diff --git a/app/views/schemes/toggle_active.html.erb b/app/views/schemes/toggle_active.html.erb index 11dd2f276..f0bbd63d4 100644 --- a/app/views/schemes/toggle_active.html.erb +++ b/app/views/schemes/toggle_active.html.erb @@ -6,24 +6,24 @@ href: scheme_details_path(@scheme), ) %> <% end %> -<%= form_with model: @scheme, url: scheme_new_deactivation_path(@scheme), method: "patch", local: true do |f| %> +<%= form_with model: @scheme, url: toggle_scheme_form_path(action, @scheme.id), method: "patch", local: true do |f| %>