From 4c02c6bdca8fe36a0bbc0a95b253820ce1e71f9a Mon Sep 17 00:00:00 2001 From: Kat Date: Thu, 17 Nov 2022 16:33:57 +0000 Subject: [PATCH] Add reactivation errors --- app/controllers/locations_controller.rb | 11 +++-- app/models/location.rb | 24 +++++++++- spec/requests/locations_controller_spec.rb | 56 +++++++++++++++++++++- 3 files changed, 85 insertions(+), 6 deletions(-) diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index 8d46ca92f..0ff78bfe3 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -54,10 +54,16 @@ class LocationsController < ApplicationController end def reactivate - if @location.location_deactivation_periods.deactivations_without_reactivation.update!(reactivation_date:) + @location.run_reactivation_validations! + @location.reactivation_date = reactivation_date + @location.reactivation_date_type = params[:location][:reactivation_date_type] + + if @location.valid? && @location.location_deactivation_periods.deactivations_without_reactivation.update!(reactivation_date:) flash[:notice] = reactivate_success_notice + redirect_to scheme_location_path(@scheme, @location) + else + render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity end - redirect_to scheme_location_path(@scheme, @location) end def create @@ -206,7 +212,6 @@ private def deactivation_date toggle_date("deactivation_date") - end def reactivation_date diff --git a/app/models/location.rb b/app/models/location.rb index 25fc05fd9..ee7bdef38 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -1,6 +1,7 @@ class Location < ApplicationRecord validate :validate_postcode validate :deactivation_date_errors + validate :reactivation_date_errors validates :units, :type_of_unit, :mobility_type, presence: true belongs_to :scheme has_many :lettings_logs, class_name: "LettingsLog" @@ -12,7 +13,7 @@ class Location < ApplicationRecord auto_strip_attributes :name - attr_accessor :add_another_location, :deactivation_date_type, :deactivation_date, :run_deactivation_validations + attr_accessor :add_another_location, :deactivation_date_type, :deactivation_date, :run_deactivation_validations, :reactivation_date_type, :reactivation_date, :run_reactivation_validations scope :search_by_postcode, ->(postcode) { where("REPLACE(postcode, ' ', '') ILIKE ?", "%#{postcode.delete(' ')}%") } scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") } @@ -411,6 +412,27 @@ class Location < ApplicationRecord 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.location.reactivation_date.not_selected")) + elsif reactivation_date_type == "other" + errors.add(:reactivation_date, message: I18n.t("validations.location.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.location.reactivation_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date))) + end + end + end + private PIO = PostcodeService.new diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index 965df1468..cfce33b8d 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -1443,7 +1443,6 @@ RSpec.describe LocationsController, type: :request do end end - describe "#reactivate" do context "when not signed in" do it "redirects to the sign in page" do @@ -1471,7 +1470,6 @@ RSpec.describe LocationsController, type: :request do let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) } let!(:location) { FactoryBot.create(:location, scheme:) } let(:deactivation_date) { Time.zone.local(2022, 10, 10) } - let!(:lettings_log) { FactoryBot.create(:lettings_log, :sh, location:, scheme:, startdate:, owning_organisation: user.organisation) } let(:startdate) { Time.utc(2022, 10, 11) } before do @@ -1522,6 +1520,60 @@ RSpec.describe LocationsController, type: :request do expect(location.location_deactivation_periods.first.reactivation_date).to eq(Time.zone.local(2022, 10, 14)) end end + + context "when the date is not selected" do + let(:params) { { location: { "reactivation_date": "" } } } + + it "displays the new page with an error message" do + expect(response).to have_http_status(:unprocessable_entity) + expect(page).to have_content(I18n.t("validations.location.reactivation_date.not_selected")) + end + end + + context "when invalid date is entered" do + let(:params) { { location: { reactivation_date_type: "other", "reactivation_date(3i)": "10", "reactivation_date(2i)": "44", "reactivation_date(1i)": "2022" } } } + + it "displays the new page with an error message" do + expect(response).to have_http_status(:unprocessable_entity) + expect(page).to have_content(I18n.t("validations.location.reactivation_date.invalid")) + end + end + + context "when the date is entered is before the beginning of current collection window" do + let(:params) { { location: { reactivation_date_type: "other", "reactivation_date(3i)": "10", "reactivation_date(2i)": "4", "reactivation_date(1i)": "2020" } } } + + it "displays the new page with an error message" do + expect(response).to have_http_status(:unprocessable_entity) + expect(page).to have_content(I18n.t("validations.location.reactivation_date.out_of_range", date: "1 April 2022")) + end + end + + context "when the day is not entered" do + let(:params) { { location: { reactivation_date_type: "other", "reactivation_date(3i)": "", "reactivation_date(2i)": "2", "reactivation_date(1i)": "2022" } } } + + it "displays page with an error message" do + expect(response).to have_http_status(:unprocessable_entity) + expect(page).to have_content(I18n.t("validations.location.reactivation_date.invalid")) + end + end + + context "when the month is not entered" do + let(:params) { { location: { reactivation_date_type: "other", "reactivation_date(3i)": "2", "reactivation_date(2i)": "", "reactivation_date(1i)": "2022" } } } + + it "displays page with an error message" do + expect(response).to have_http_status(:unprocessable_entity) + expect(page).to have_content(I18n.t("validations.location.reactivation_date.invalid")) + end + end + + context "when the year is not entered" do + let(:params) { { location: { reactivation_date_type: "other", "reactivation_date(3i)": "2", "reactivation_date(2i)": "2", "reactivation_date(1i)": "" } } } + + it "displays page with an error message" do + expect(response).to have_http_status(:unprocessable_entity) + expect(page).to have_content(I18n.t("validations.location.reactivation_date.invalid")) + end + end end end end