From b93a989cb4af53ade72c52930814a1f917642ef9 Mon Sep 17 00:00:00 2001 From: Kat Date: Fri, 1 Mar 2024 13:11:53 +0000 Subject: [PATCH] Allow deleting location --- app/controllers/locations_controller.rb | 9 ++- app/models/location.rb | 7 +++ config/locales/en.yml | 1 + .../20240301125651_add_discarded_at_column.rb | 5 ++ db/schema.rb | 1 + spec/requests/locations_controller_spec.rb | 62 +++++++++++++++++++ 6 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20240301125651_add_discarded_at_column.rb diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index 2a254bfa3..2b75da1a0 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -14,8 +14,8 @@ class LocationsController < ApplicationController def index authorize @scheme - @pagy, @locations = pagy(filter_manager.filtered_locations(@scheme.locations, search_term, session_filters)) - @total_count = @scheme.locations.size + @pagy, @locations = pagy(filter_manager.filtered_locations(@scheme.locations.visible, search_term, session_filters)) + @total_count = @scheme.locations.visible.size @searched = search_term.presence @filter_type = "scheme_locations" end @@ -230,7 +230,10 @@ class LocationsController < ApplicationController end end - def delete; end + def delete + @location.discard! + redirect_to scheme_locations_path(@scheme), notice: I18n.t("notification.location_deleted", postcode: @location.postcode) + end private diff --git a/app/models/location.rb b/app/models/location.rb index bfe9f77a4..43285bfbb 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -79,6 +79,8 @@ class Location < ApplicationRecord .where.not(id: activating_soon.pluck(:id)) } + scope :visible, -> { where(discarded_at: nil) } + LOCAL_AUTHORITIES = LocalAuthority.all.map { |la| [la.name, la.code] }.to_h enum local_authorities: LOCAL_AUTHORITIES @@ -138,6 +140,7 @@ class Location < ApplicationRecord end def status_at(date) + return :deleted if discarded_at.present? return :incomplete unless confirmed return :deactivated if open_deactivation&.deactivation_date.present? && date >= open_deactivation.deactivation_date return :deactivating_soon if open_deactivation&.deactivation_date.present? && date < open_deactivation.deactivation_date @@ -190,6 +193,10 @@ class Location < ApplicationRecord LocalAuthority.where(id: [la.id] + la.linked_local_authority_ids) end + def discard! + update!(discarded_at: Time.zone.now) + end + private PIO = PostcodeService.new diff --git a/config/locales/en.yml b/config/locales/en.yml index e2934273f..640dc6902 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -196,6 +196,7 @@ en: duplicate_sets: one: "There is %{count} set of duplicate logs" other: "There are %{count} sets of duplicate logs" + location_deleted: "%{postcode} has been deleted." validations: organisation: diff --git a/db/migrate/20240301125651_add_discarded_at_column.rb b/db/migrate/20240301125651_add_discarded_at_column.rb new file mode 100644 index 000000000..ed752d650 --- /dev/null +++ b/db/migrate/20240301125651_add_discarded_at_column.rb @@ -0,0 +1,5 @@ +class AddDiscardedAtColumn < ActiveRecord::Migration[7.0] + def change + add_column :locations, :discarded_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index 5c754b39c..e1e788d56 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -374,6 +374,7 @@ ActiveRecord::Schema[7.0].define(version: 2024_03_19_122706) do t.string "location_admin_district" t.boolean "confirmed" t.boolean "is_la_inferred" + t.datetime "discarded_at" t.index ["old_id"], name: "index_locations_on_old_id", unique: true t.index ["scheme_id"], name: "index_locations_on_scheme_id" end diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index f4db5bb71..6e35e03fb 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -2110,4 +2110,66 @@ RSpec.describe LocationsController, type: :request do end end end + + describe "#delete" do + let(:scheme) { create(:scheme, owning_organisation: user.organisation) } + let(:location) { create(:location, scheme:, name: "Location to delete", created_at: Time.zone.local(2022, 4, 1)) } + + before do + delete "/schemes/#{scheme.id}/locations/#{location.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") + end + end + + context "when signed in" do + before do + allow(user).to receive(:need_two_factor_authentication?).and_return(false) + sign_in user + delete "/schemes/#{scheme.id}/locations/#{location.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 + end + + context "with a data coordinator user" do + let(:user) { create(:user, :data_coordinator) } + + it "returns 401 unauthorized" do + expect(response).to have_http_status(:unauthorized) + end + end + + context "with a support user user" do + let(:user) { create(:user, :support) } + + it "deletes the location" do + location.reload + expect(location.status).to eq(:deleted) + expect(location.discarded_at).not_to be nil + end + + it "redirects to the scheme locations list and displays a notice that the location has been deleted" do + expect(response).to redirect_to scheme_locations_path(scheme) + follow_redirect! + expect(page).to have_selector(".govuk-notification-banner--success") + expect(page).to have_selector(".govuk-notification-banner--success", text: "has been deleted.") + end + + it "does not display the deleted location" do + expect(response).to redirect_to scheme_locations_path(scheme) + follow_redirect! + expect(page).not_to have_content("Location to delete") + end + end + end + end end