Browse Source

Allow deleting location

pull/2285/head
Kat 2 years ago
parent
commit
b93a989cb4
  1. 9
      app/controllers/locations_controller.rb
  2. 7
      app/models/location.rb
  3. 1
      config/locales/en.yml
  4. 5
      db/migrate/20240301125651_add_discarded_at_column.rb
  5. 1
      db/schema.rb
  6. 62
      spec/requests/locations_controller_spec.rb

9
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

7
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

1
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:

5
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

1
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

62
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

Loading…
Cancel
Save