diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index a6cfe475c..f4e48c665 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -20,22 +20,32 @@ class LocationsController < ApplicationController def show; end - def deactivate + def new_deactivation if params[:location].blank? render "toggle_active", locals: { action: "deactivate" } - elsif params[:location][:confirm].present? && params[:location][:deactivation_date].present? - confirm_deactivation else - @location.deactivation_date_errors(params) - if @location.errors.present? - @location.deactivation_date_type = params[:location][:deactivation_date_type] - render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity + @location.run_deactivation_validations = true + @location.deactivation_date = deactivation_date + @location.deactivation_date_type = params[:location][:deactivation_date_type] + if @location.valid? + redirect_to scheme_location_deactivate_confirm_path(@location, deactivation_date: @location.deactivation_date, deactivation_date_type: @location.deactivation_date_type) else - render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: } + render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity end end end + def deactivate_confirm + render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: params[:deactivation_date], deactivation_date_type: params[:deactivation_date_type] } + end + + def deactivate + @location.run_deactivation_validations = true + @location.deactivation_date = deactivation_date + @location.deactivation_date_type = params[:location][:deactivation_date_type] + confirm_deactivation + end + def reactivate render "toggle_active", locals: { action: "reactivate" } end @@ -144,13 +154,13 @@ private end def authenticate_action! - if %w[new edit update create index edit_name edit_local_authority deactivate].include?(action_name) && !((current_user.organisation == @scheme&.owning_organisation) || current_user.support?) + if %w[new edit update create index edit_name edit_local_authority new_deactivation deactivate_confirm deactivate].include?(action_name) && !((current_user.organisation == @scheme&.owning_organisation) || current_user.support?) render_not_found and return end end def location_params - required_params = params.require(:location).permit(:postcode, :name, :units, :type_of_unit, :add_another_location, :startdate, :mobility_type, :location_admin_district, :location_code).merge(scheme_id: @scheme.id) + required_params = params.require(:location).permit(:postcode, :name, :units, :type_of_unit, :add_another_location, :startdate, :mobility_type, :location_admin_district, :location_code, :deactivation_date).merge(scheme_id: @scheme.id) required_params[:postcode] = PostcodeService.clean(required_params[:postcode]) if required_params[:postcode] required_params end @@ -164,23 +174,35 @@ private end def confirm_deactivation - if @location.update(deactivation_date: params[:location][:deactivation_date]) - flash[:notice] = "#{@location.name || @location.postcode} has been deactivated" + if @location.update!(deactivation_date: @location.deactivation_date) + flash[:notice] = success_text end redirect_to scheme_location_path(@scheme, @location) end - def deactivation_date - return if params[:location].blank? + def success_text + case @location.status + when :deactivated + "#{@location.name} has been deactivated" + when :deactivating_soon + "#{@location.name} will deactivate on #{@location.deactivation_date.to_formatted_s(:govuk_date)}" + end + end - collection_start_date = FormHandler.instance.current_collection_start_date - return collection_start_date if params[:location][:deactivation_date_type] == "default" - return params[:location][:deactivation_date] if params[:location][:deactivation_date_type].blank? + def deactivation_date + if params[:location].blank? + return + elsif params[:location][:deactivation_date_type] == "default" + return FormHandler.instance.current_collection_start_date + elsif params[:location][:deactivation_date].present? + return params[:location][:deactivation_date] + end day = params[:location]["deactivation_date(3i)"] month = params[:location]["deactivation_date(2i)"] year = params[:location]["deactivation_date(1i)"] + return nil if [day, month, year].any?(&:blank?) - Date.new(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/location.rb b/app/models/location.rb index a877ce549..1647f8c8e 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -1,5 +1,6 @@ class Location < ApplicationRecord validate :validate_postcode + validate :deactivation_date_errors validates :units, :type_of_unit, :mobility_type, presence: true belongs_to :scheme has_many :lettings_logs, class_name: "LettingsLog" @@ -10,7 +11,7 @@ class Location < ApplicationRecord auto_strip_attributes :name - attr_accessor :add_another_location, :deactivation_date_type + attr_accessor :add_another_location, :deactivation_date_type, :run_deactivation_validations scope :search_by_postcode, ->(postcode) { where("REPLACE(postcode, ' ', '') ILIKE ?", "%#{postcode.delete(' ')}%") } scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") } @@ -383,25 +384,20 @@ class Location < ApplicationRecord status == :active end - def deactivation_date_errors(params) - if params[:location][:deactivation_date].blank? && params[:location][:deactivation_date_type].blank? - errors.add(:deactivation_date_type, message: I18n.t("validations.location.deactivation_date.not_selected")) - end - - if params[:location][:deactivation_date_type] == "other" - day = params[:location]["deactivation_date(3i)"] - month = params[:location]["deactivation_date(2i)"] - year = params[:location]["deactivation_date(1i)"] + def implicit_run_deactivation_validations + deactivation_date.present? || @run_deactivation_validations + end - collection_start_date = FormHandler.instance.current_collection_start_date + def deactivation_date_errors + return unless implicit_run_deactivation_validations - if [day, month, year].any?(&:blank?) - errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.invalid")) - elsif !Date.valid_date?(year.to_i, month.to_i, day.to_i) - errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.invalid")) - elsif !Date.new(year.to_i, month.to_i, day.to_i).between?(collection_start_date, Date.new(2200, 1, 1)) - errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date))) - end + collection_start_date = FormHandler.instance.current_collection_start_date + if deactivation_date_type.blank? + errors.add(:deactivation_date_type, message: I18n.t("validations.location.deactivation_date.not_selected")) + elsif deactivation_date.blank? + errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.invalid")) + elsif !deactivation_date.between?(collection_start_date, Date.new(2200, 1, 1)) + errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date))) end end diff --git a/app/views/locations/show.html.erb b/app/views/locations/show.html.erb index 8b7ae9e0c..52607d582 100644 --- a/app/views/locations/show.html.erb +++ b/app/views/locations/show.html.erb @@ -25,7 +25,7 @@ <% if FeatureToggle.location_toggle_enabled? %> <% if @location.active? %> - <%= govuk_button_link_to "Deactivate this location", scheme_location_deactivate_path(scheme_id: @scheme.id, location_id: @location.id), warning: true %> + <%= govuk_button_link_to "Deactivate this location", scheme_location_new_deactivation_path(scheme_id: @scheme.id, location_id: @location.id), warning: true %> <% else %> <%= govuk_button_link_to "Reactivate this location", scheme_location_reactivate_path(scheme_id: @scheme.id, location_id: @location.id) %> <% end %> diff --git a/app/views/locations/toggle_active.html.erb b/app/views/locations/toggle_active.html.erb index 131cbda43..f5f6e1144 100644 --- a/app/views/locations/toggle_active.html.erb +++ b/app/views/locations/toggle_active.html.erb @@ -8,7 +8,7 @@ ) %> <% end %> -<%= form_with model: @location, url: scheme_location_deactivate_path(scheme_id: @location.scheme.id, location_id: @location.id), method: "patch", local: true do |f| %> +<%= form_with model: @location, url: scheme_location_new_deactivation_path(scheme_id: @location.scheme.id, location_id: @location.id), method: "patch", local: true do |f| %>