diff --git a/app/helpers/locations_helper.rb b/app/helpers/locations_helper.rb index f718e9576..f963c7040 100644 --- a/app/helpers/locations_helper.rb +++ b/app/helpers/locations_helper.rb @@ -73,6 +73,10 @@ module LocationsHelper return govuk_button_link_to "Reactivate this location", scheme_location_new_reactivation_path(location.scheme, location) if location.deactivated? end + def delete_location_link(location) + govuk_button_link_to "Delete this location", scheme_location_delete_confirmation_path(location.scheme, location), warning: true + end + def location_creation_success_notice(location) if location.confirmed "#{location.postcode} #{location.startdate.blank? || location.startdate.before?(Time.zone.now) ? 'has been' : 'will be'} added to this scheme" diff --git a/app/policies/location_policy.rb b/app/policies/location_policy.rb index 5371df19e..0f653b55d 100644 --- a/app/policies/location_policy.rb +++ b/app/policies/location_policy.rb @@ -27,11 +27,11 @@ class LocationPolicy end def delete_confirmation? - user.support? + delete? end def delete? - user.support? + user.support? && (location.status == :incomplete || location.status == :deactivated) end %w[ diff --git a/app/views/locations/show.html.erb b/app/views/locations/show.html.erb index 9b11e4052..f7d77f16b 100644 --- a/app/views/locations/show.html.erb +++ b/app/views/locations/show.html.erb @@ -45,3 +45,7 @@ <% if LocationPolicy.new(current_user, @location).deactivate? %> <%= toggle_location_link(@location) %> <% end %> + +<% if LocationPolicy.new(current_user, @location).delete? %> + <%= delete_location_link(@location) %> +<% end %> diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index 6e35e03fb..cc238015f 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -1825,6 +1825,11 @@ RSpec.describe LocationsController, type: :request do expect(response).to have_http_status(:ok) expect(page).to have_link("Reactivate this location", href: "/schemes/#{scheme.id}/locations/#{location.id}/new-reactivation") end + + it "does not render delete this location" do + expect(response).to have_http_status(:ok) + expect(page).not_to have_link("Delete this location", href: "/schemes/#{scheme.id}/locations/#{location.id}/delete-confirmation") + end end context "with location that's deactivating soon" do @@ -1883,6 +1888,86 @@ RSpec.describe LocationsController, type: :request do end end end + + context "when signed in as a support user" do + let(:user) { create(:user, :support) } + let(:scheme) { create(:scheme) } + let(:location) { create(:location, scheme:) } + let(:add_deactivations) { location.location_deactivation_periods << location_deactivation_period } + + before do + Timecop.freeze(Time.utc(2022, 10, 10)) + allow(user).to receive(:need_two_factor_authentication?).and_return(false) + sign_in user + add_deactivations + location.save! + get "/schemes/#{scheme.id}/locations/#{location.id}" + end + + after do + Timecop.unfreeze + end + + context "with active location" do + let(:add_deactivations) {} + + it "does not render delete this location" do + expect(response).to have_http_status(:ok) + expect(page).not_to have_link("Delete this location", href: "/schemes/#{scheme.id}/locations/#{location.id}/delete-confirmation") + end + end + + context "with deactivated location" do + let(:location_deactivation_period) { create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 9), location:) } + + it "renders delete this location" do + expect(response).to have_http_status(:ok) + expect(page).to have_link("Delete this location", href: "/schemes/#{scheme.id}/locations/#{location.id}/delete-confirmation") + end + end + + context "with incomplete location" do + let(:add_deactivations) {} + + before do + location.update!(units: nil) + get "/schemes/#{scheme.id}/locations/#{location.id}" + end + + it "renders delete this location" do + expect(location.reload.status).to eq(:incomplete) + expect(response).to have_http_status(:ok) + expect(page).to have_link("Delete this location", href: "/schemes/#{scheme.id}/locations/#{location.id}/delete-confirmation") + end + end + + context "with location that's deactivating soon" do + let(:location_deactivation_period) { create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 12), location:) } + + it "does not render delete this location" do + expect(response).to have_http_status(:ok) + expect(page).not_to have_link("Delete this location", href: "/schemes/#{scheme.id}/locations/#{location.id}/delete-confirmation") + end + end + + context "with location that's deactivating in more than 6 months" do + let(:location_deactivation_period) { create(:location_deactivation_period, deactivation_date: Time.zone.local(2023, 6, 12), location:) } + + it "does not render delete this location" do + expect(response).to have_http_status(:ok) + expect(page).not_to have_link("Delete this location", href: "/schemes/#{scheme.id}/locations/#{location.id}/delete-confirmation") + end + end + + context "with location that's reactivating soon" do + let(:location_deactivation_period) { create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 4, 12), reactivation_date: Time.zone.local(2022, 10, 12), location:) } + + it "does not render delete this location" do + expect(response).to have_http_status(:ok) + expect(page).not_to have_link("Delete this location", href: "/schemes/#{scheme.id}/locations/#{location.id}/delete-confirmation") + end + end + end end describe "#reactivate" do @@ -2057,11 +2142,18 @@ RSpec.describe LocationsController, type: :request do context "when signed in" do before do + Timecop.freeze(Time.utc(2022, 10, 10)) + location.location_deactivation_periods << create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 9), location:) + location.save! allow(user).to receive(:need_two_factor_authentication?).and_return(false) sign_in user get "/schemes/#{scheme.id}/locations/#{location.id}/delete-confirmation" end + after do + Timecop.unfreeze + end + context "with a data provider user" do let(:user) { create(:user) } @@ -2116,9 +2208,16 @@ RSpec.describe LocationsController, type: :request do let(:location) { create(:location, scheme:, name: "Location to delete", created_at: Time.zone.local(2022, 4, 1)) } before do + Timecop.freeze(Time.utc(2022, 10, 10)) + location.location_deactivation_periods << create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 9), location:) + location.save! delete "/schemes/#{scheme.id}/locations/#{location.id}/delete" end + after do + Timecop.unfreeze + end + context "when not signed in" do it "redirects to the sign in page" do expect(response).to redirect_to("/account/sign-in")