diff --git a/app/controllers/organisations_controller.rb b/app/controllers/organisations_controller.rb
index 6191f2129..6d9b1f4b5 100644
--- a/app/controllers/organisations_controller.rb
+++ b/app/controllers/organisations_controller.rb
@@ -117,7 +117,6 @@ class OrganisationsController < ApplicationController
end
def update
- selected_rent_periods = rent_period_params[:rent_periods].compact_blank
if (current_user.data_coordinator? && org_params[:active].nil?) || current_user.support?
if @organisation.update(org_params)
case org_params[:active]
@@ -136,11 +135,14 @@ class OrganisationsController < ApplicationController
else
flash[:notice] = I18n.t("organisation.updated")
end
- used_rent_periods = @organisation.lettings_logs.pluck(:period).uniq.compact.map(&:to_s)
- rent_periods_to_delete = rent_period_params[:all_rent_periods] - selected_rent_periods - used_rent_periods
- OrganisationRentPeriod.transaction do
- selected_rent_periods.each { |period| OrganisationRentPeriod.create(organisation: @organisation, rent_period: period) }
- OrganisationRentPeriod.where(organisation: @organisation, rent_period: rent_periods_to_delete).destroy_all
+ if rent_period_params[:rent_periods].present?
+ selected_rent_periods = rent_period_params[:rent_periods].compact_blank
+ used_rent_periods = @organisation.lettings_logs.pluck(:period).uniq.compact.map(&:to_s)
+ rent_periods_to_delete = rent_period_params[:all_rent_periods] - selected_rent_periods - used_rent_periods
+ OrganisationRentPeriod.transaction do
+ selected_rent_periods.each { |period| OrganisationRentPeriod.create(organisation: @organisation, rent_period: period) }
+ OrganisationRentPeriod.where(organisation: @organisation, rent_period: rent_periods_to_delete).destroy_all
+ end
end
redirect_to details_organisation_path(@organisation)
else
diff --git a/app/helpers/organisations_helper.rb b/app/helpers/organisations_helper.rb
index a557f26d0..75d5869fa 100644
--- a/app/helpers/organisations_helper.rb
+++ b/app/helpers/organisations_helper.rb
@@ -19,7 +19,7 @@ module OrganisationsHelper
{ name: "Owns housing stock", value: organisation.holds_own_stock ? "Yes" : "No", editable: false },
{ name: "Rent periods", value: organisation.rent_period_labels, editable: true, format: :bullet },
{ name: "Data Sharing Agreement" },
- { name: "Status", value: status_tag(organisation.status), editable: false },
+ { name: "Status", value: status_tag(organisation.status) + delete_organisation_text(organisation), editable: false },
]
end
@@ -39,6 +39,12 @@ module OrganisationsHelper
end
end
+ def delete_organisation_text(organisation)
+ if organisation.active == false && current_user.support? && !OrganisationPolicy.new(current_user, organisation).delete?
+ "
This organisation was active in an open or editable collection year, and cannot be deleted.
".html_safe
+ end
+ end
+
def rent_periods_with_checked_attr(checked_periods: nil)
RentPeriod.rent_period_mappings.each_with_object({}) do |(period_code, period_value), result|
result[period_code] = period_value.merge(checked: checked_periods.nil? || checked_periods.include?(period_code))
diff --git a/app/models/organisation.rb b/app/models/organisation.rb
index da041cd14..d56dd7afb 100644
--- a/app/models/organisation.rb
+++ b/app/models/organisation.rb
@@ -182,6 +182,8 @@ class Organisation < ApplicationRecord
end
def discard!
+ owned_schemes.each(&:discard!)
+ users.each(&:discard!)
update!(discarded_at: Time.zone.now)
end
end
diff --git a/app/views/organisations/delete_confirmation.html.erb b/app/views/organisations/delete_confirmation.html.erb
index f0efe61bf..7ca97707b 100644
--- a/app/views/organisations/delete_confirmation.html.erb
+++ b/app/views/organisations/delete_confirmation.html.erb
@@ -5,7 +5,7 @@
- Delete <%= @organisation.postcode %>
+ Delete <%= @organisation.name %>
<%= content_for(:title) %>
diff --git a/spec/requests/organisations_controller_spec.rb b/spec/requests/organisations_controller_spec.rb
index dc8c08b52..11c7fbd84 100644
--- a/spec/requests/organisations_controller_spec.rb
+++ b/spec/requests/organisations_controller_spec.rb
@@ -1536,6 +1536,12 @@ RSpec.describe OrganisationsController, type: :request do
expect(response).to have_http_status(:ok)
expect(page).not_to have_link("Delete this organisation", href: "/organisations/#{organisation.id}/delete-confirmation")
end
+
+ it "does not render informative text about deleting the organisation" do
+ follow_redirect!
+ expect(response).to have_http_status(:ok)
+ expect(page).not_to have_content("This organisation was active in an open or editable collection year, and cannot be deleted.")
+ end
end
context "with an inactive organisation" do
@@ -1546,6 +1552,44 @@ RSpec.describe OrganisationsController, type: :request do
expect(response).to have_http_status(:ok)
expect(page).to have_link("Delete this organisation", href: "/organisations/#{organisation.id}/delete-confirmation")
end
+
+ context "and associated lettings logs in editable collection period" do
+ before do
+ create(:lettings_log, owning_organisation: organisation)
+ get "/organisations/#{organisation.id}"
+ end
+
+ 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
+
+ it "adds informative text about deleting the organisation" do
+ follow_redirect!
+ expect(response).to have_http_status(:ok)
+ expect(page).to have_content("This organisation was active in an open or editable collection year, and cannot be deleted.")
+ end
+ end
+
+ context "and associated sales logs in editable collection period" do
+ before do
+ create(:sales_log, owning_organisation: organisation)
+ get "/organisations/#{organisation.id}"
+ end
+
+ 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
+
+ it "adds informative text about deleting the organisation" do
+ follow_redirect!
+ expect(response).to have_http_status(:ok)
+ expect(page).to have_content("This organisation was active in an open or editable collection year, and cannot be deleted.")
+ end
+ end
end
context "with merged organisation" do
@@ -1749,15 +1793,26 @@ RSpec.describe OrganisationsController, type: :request do
describe "#delete" do
let(:organisation) { create(:organisation, active: false) }
+ let!(:org_user) { create(:user, active: false, organisation:) }
+ let!(:scheme) { create(:scheme, owning_organisation: organisation) }
+ let!(:location) { create(:location, scheme:) }
before do
+ scheme.scheme_deactivation_periods << SchemeDeactivationPeriod.new(deactivation_date: Time.zone.yesterday)
+ location.location_deactivation_periods << LocationDeactivationPeriod.new(deactivation_date: Time.zone.yesterday)
delete "/organisations/#{organisation.id}/delete"
end
- it "deletes the organisation" do
+ it "deletes the organisation and related resources" do
organisation.reload
expect(organisation.status).to eq(:deleted)
expect(organisation.discarded_at).not_to be nil
+ expect(location.reload.status).to eq(:deleted)
+ expect(location.discarded_at).not_to be nil
+ expect(scheme.reload.status).to eq(:deleted)
+ expect(scheme.discarded_at).not_to be nil
+ expect(org_user.reload.status).to eq(:deleted)
+ expect(org_user.discarded_at).not_to be nil
end
it "redirects to the organisations list and displays a notice that the organisation has been deleted" do