diff --git a/app/controllers/organisations_controller.rb b/app/controllers/organisations_controller.rb index 22f2f8b6d..a35e6c292 100644 --- a/app/controllers/organisations_controller.rb +++ b/app/controllers/organisations_controller.rb @@ -15,9 +15,9 @@ class OrganisationsController < ApplicationController redirect_to organisation_path(current_user.organisation) unless current_user.support? all_organisations = Organisation.order(:name) - @pagy, @organisations = pagy(filtered_collection(all_organisations, search_term)) + @pagy, @organisations = pagy(filtered_collection(all_organisations.visible, search_term)) @searched = search_term.presence - @total_count = all_organisations.size + @total_count = all_organisations.visible.size end def schemes @@ -152,7 +152,10 @@ class OrganisationsController < ApplicationController end end - def delete; end + def delete + @organisation.discard! + redirect_to organisations_path, notice: I18n.t("notification.organisation_deleted", name: @organisation.name) + end def lettings_logs organisation_logs = LettingsLog.visible.filter_by_organisation(@organisation).filter_by_years_or_nil(FormHandler.instance.years_of_available_lettings_forms) diff --git a/app/models/organisation.rb b/app/models/organisation.rb index 25a3fdd0d..da041cd14 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -17,6 +17,7 @@ class Organisation < ApplicationRecord belongs_to :absorbing_organisation, class_name: "Organisation", optional: true has_many :absorbed_organisations, class_name: "Organisation", foreign_key: "absorbing_organisation_id" + scope :visible, -> { where(discarded_at: nil) } def affiliated_stock_owners ids = [] @@ -143,6 +144,7 @@ class Organisation < ApplicationRecord end def status_at(date) + return :deleted if discarded_at.present? return :merged if merge_date.present? && merge_date < date return :deactivated unless active @@ -178,4 +180,8 @@ class Organisation < ApplicationRecord false end + + def discard! + update!(discarded_at: Time.zone.now) + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 537fe2114..9dcfed8c3 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -202,6 +202,7 @@ en: location_deleted: "%{postcode} has been deleted." scheme_deleted: "%{service_name} has been deleted." user_deleted: "%{name} has been deleted." + organisation_deleted: "%{name} has been deleted." validations: organisation: diff --git a/db/migrate/20240610142812_add_discarded_at_to_organisations.rb b/db/migrate/20240610142812_add_discarded_at_to_organisations.rb new file mode 100644 index 000000000..192e7095c --- /dev/null +++ b/db/migrate/20240610142812_add_discarded_at_to_organisations.rb @@ -0,0 +1,5 @@ +class AddDiscardedAtToOrganisations < ActiveRecord::Migration[7.0] + def change + add_column :organisations, :discarded_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index 1430da65f..66693435f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_05_29_133005) do +ActiveRecord::Schema[7.0].define(version: 2024_06_10_142812) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -492,6 +492,7 @@ ActiveRecord::Schema[7.0].define(version: 2024_05_29_133005) do t.datetime "merge_date" t.bigint "absorbing_organisation_id" t.datetime "available_from" + t.datetime "discarded_at" t.index ["absorbing_organisation_id"], name: "index_organisations_on_absorbing_organisation_id" t.index ["old_visible_id"], name: "index_organisations_on_old_visible_id", unique: true end diff --git a/spec/requests/organisations_controller_spec.rb b/spec/requests/organisations_controller_spec.rb index e01fff221..5f5b94bec 100644 --- a/spec/requests/organisations_controller_spec.rb +++ b/spec/requests/organisations_controller_spec.rb @@ -48,13 +48,27 @@ RSpec.describe OrganisationsController, type: :request do end end - fdescribe "#delete-confirmation" do + describe "#delete-confirmation" do let(:organisation) { create(:organisation) } - + before do get "/organisations/#{organisation.id}/delete-confirmation" end - + + context "when not signed in" do + it "redirects to the sign in page" do + expect(response).to redirect_to("/account/sign-in") + end + end + end + + describe "#delete" do + let(:organisation) { create(:organisation) } + + before do + delete "/organisations/#{organisation.id}/delete" + end + context "when not signed in" do it "redirects to the sign in page" do expect(response).to redirect_to("/account/sign-in") @@ -762,16 +776,32 @@ RSpec.describe OrganisationsController, type: :request do end end - fdescribe "#delete-confirmation" do + describe "#delete-confirmation" do let(:organisation) { user.organisation } - + before do get "/organisations/#{organisation.id}/delete-confirmation" end - + context "with a data provider user" do let(:user) { create(:user) } - + + it "returns 401 unauthorized" do + expect(response).to have_http_status(:unauthorized) + end + end + end + + describe "#delete" do + let(:organisation) { user.organisation } + + before do + delete "/organisations/#{organisation.id}/delete" + end + + context "with a data provider user" do + let(:user) { create(:user) } + it "returns 401 unauthorized" do expect(response).to have_http_status(:unauthorized) end @@ -907,16 +937,32 @@ RSpec.describe OrganisationsController, type: :request do end end - fdescribe "#delete-confirmation" do + describe "#delete-confirmation" do let(:organisation) { user.organisation } - + before do get "/organisations/#{organisation.id}/delete-confirmation" end - + + context "with a data provider user" do + let(:user) { create(:user) } + + it "returns 401 unauthorized" do + expect(response).to have_http_status(:unauthorized) + end + end + end + + describe "#delete" do + let(:organisation) { user.organisation } + + before do + delete "/organisations/#{organisation.id}/delete" + end + context "with a data provider user" do let(:user) { create(:user) } - + it "returns 401 unauthorized" do expect(response).to have_http_status(:unauthorized) end @@ -1627,9 +1673,9 @@ RSpec.describe OrganisationsController, type: :request do end end - fdescribe "#delete-confirmation" do + describe "#delete-confirmation" do let(:organisation) { create(:organisation) } - + before do get "/organisations/#{organisation.id}/delete-confirmation" end @@ -1662,6 +1708,33 @@ RSpec.describe OrganisationsController, type: :request do end end + describe "#delete" do + let(:organisation) { create(:organisation) } + + before do + delete "/organisations/#{organisation.id}/delete" + end + + it "deletes the organisation" do + organisation.reload + expect(organisation.status).to eq(:deleted) + expect(organisation.discarded_at).not_to be nil + end + + it "redirects to the organisations list and displays a notice that the organisation has been deleted" do + expect(response).to redirect_to organisations_path + follow_redirect! + expect(page).to have_selector(".govuk-notification-banner--success") + expect(page).to have_selector(".govuk-notification-banner--success", text: "#{organisation.name} has been deleted.") + end + + it "does not display the deleted organisation" do + expect(response).to redirect_to organisations_path + follow_redirect! + expect(page).not_to have_content("Organisation to delete") + end + end + context "when they view the lettings logs tab" do let(:tenancycode) { "42" }