diff --git a/app/controllers/organisations_controller.rb b/app/controllers/organisations_controller.rb index a35e6c292..6191f2129 100644 --- a/app/controllers/organisations_controller.rb +++ b/app/controllers/organisations_controller.rb @@ -153,6 +153,8 @@ class OrganisationsController < ApplicationController end def delete + authorize @organisation + @organisation.discard! redirect_to organisations_path, notice: I18n.t("notification.organisation_deleted", name: @organisation.name) end diff --git a/app/helpers/organisations_helper.rb b/app/helpers/organisations_helper.rb index 7d37b9289..a557f26d0 100644 --- a/app/helpers/organisations_helper.rb +++ b/app/helpers/organisations_helper.rb @@ -44,4 +44,8 @@ module OrganisationsHelper result[period_code] = period_value.merge(checked: checked_periods.nil? || checked_periods.include?(period_code)) end end + + def delete_organisation_link(organisation) + govuk_button_link_to "Delete this organisation", delete_confirmation_organisation_path(organisation), warning: true + end end diff --git a/app/policies/organisation_policy.rb b/app/policies/organisation_policy.rb index 3e0c9a946..c17d69785 100644 --- a/app/policies/organisation_policy.rb +++ b/app/policies/organisation_policy.rb @@ -13,4 +13,12 @@ class OrganisationPolicy def reactivate? user.support? && organisation.status == :deactivated end + + def delete_confirmation? + delete? + end + + def delete? + user.support? && (organisation.status == :deactivated || organisation.status == :merged) + end end diff --git a/app/views/organisations/show.html.erb b/app/views/organisations/show.html.erb index 26a20926e..214b988f0 100644 --- a/app/views/organisations/show.html.erb +++ b/app/views/organisations/show.html.erb @@ -52,3 +52,7 @@ <%= govuk_button_link_to "Reactivate this organisation", reactivate_organisation_path(@organisation) %> <% end %> + +<% if OrganisationPolicy.new(current_user, @organisation).delete? %> + <%= delete_organisation_link(@organisation) %> +<% end %> diff --git a/spec/requests/organisations_controller_spec.rb b/spec/requests/organisations_controller_spec.rb index 5f5b94bec..dc8c08b52 100644 --- a/spec/requests/organisations_controller_spec.rb +++ b/spec/requests/organisations_controller_spec.rb @@ -1523,6 +1523,45 @@ RSpec.describe OrganisationsController, type: :request do end end + describe "#show" do + let(:organisation) { create(:organisation) } + + before do + get "/organisations/#{organisation.id}", headers:, params: {} + end + + context "with an active organisation" do + it "does not render delete this organisation" do + follow_redirect! + expect(response).to have_http_status(:ok) + expect(page).not_to have_link("Delete this organisation", href: "/organisations/#{organisation.id}/delete-confirmation") + end + end + + context "with an inactive organisation" do + let(:organisation) { create(:organisation, active: false) } + + it "renders delete this organisation" do + follow_redirect! + expect(response).to have_http_status(:ok) + expect(page).to have_link("Delete this organisation", href: "/organisations/#{organisation.id}/delete-confirmation") + end + end + + context "with merged organisation" do + before do + organisation.update!(merge_date: Time.zone.yesterday) + get "/organisations/#{organisation.id}", headers:, params: {} + end + + it "renders delete this organisation" do + follow_redirect! + expect(response).to have_http_status(:ok) + expect(page).to have_link("Delete this organisation", href: "/organisations/#{organisation.id}/delete-confirmation") + end + end + end + describe "#update" do let(:params) { { id: organisation.id, organisation: { active:, rent_periods: [], all_rent_periods: [] } } } @@ -1709,7 +1748,7 @@ RSpec.describe OrganisationsController, type: :request do end describe "#delete" do - let(:organisation) { create(:organisation) } + let(:organisation) { create(:organisation, active: false) } before do delete "/organisations/#{organisation.id}/delete"