diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index 797e79544..34e1e718c 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -21,7 +21,15 @@ class LocationsController < ApplicationController def show; end def deactivate - render "toggle_active", locals: { action: "deactivate" } + if params[:deactivation_date].blank? + render "toggle_active", locals: { action: "deactivate" } + elsif (params[:confirm]) + # update the deactivation_date + # update the logs + # redirect to location page + else + render "toggle_active_confirm", locals: {action: "deactivate", deactivation_date: params[:deactivation_date]} + end end def create @@ -128,7 +136,7 @@ private end def authenticate_action! - if %w[new edit update create index edit_name edit_local_authority].include?(action_name) && !((current_user.organisation == @scheme&.owning_organisation) || current_user.support?) + if %w[new edit update create index edit_name edit_local_authority deactivate].include?(action_name) && !((current_user.organisation == @scheme&.owning_organisation) || current_user.support?) render_not_found and return end end diff --git a/app/views/locations/toggle_active.html.erb b/app/views/locations/toggle_active.html.erb index 8bb425350..6f2d391c1 100644 --- a/app/views/locations/toggle_active.html.erb +++ b/app/views/locations/toggle_active.html.erb @@ -9,10 +9,9 @@ <% end %> -<%= form_for(@location, method: :patch, url: location_deactivate_path(@location)) do |f| %> +<%= form_with url: location_deactivate_path(@location), method: "patch", local: true do |f| %>
- <%= f.govuk_error_summary %> <%= f.govuk_radio_buttons_fieldset :deactivation_date, legend: { text: "When should this change apply?"}, caption: { text: "Deactivate #{@location.postcode}"}, diff --git a/app/views/locations/toggle_active_confirm.html.erb b/app/views/locations/toggle_active_confirm.html.erb new file mode 100644 index 000000000..d005ce289 --- /dev/null +++ b/app/views/locations/toggle_active_confirm.html.erb @@ -0,0 +1,17 @@ +<%= form_with url: location_deactivate_path(@location), method: "patch", local: true do |f| %> + <% content_for :before_content do %> + <%= govuk_back_link(href: :back) %> + <% end %> +

+ <%= @scheme.service_name %> + <%= "This change will affect SOME logs" %> +

+ <%= govuk_warning_text text: "Your data providers will need to review these logs and answer a few questions again. We’ll email each log creator with a list of logs that need updating." %> + <%= f.hidden_field :confirm, :value => true %> + <%= f.hidden_field :deactivation_date, :value => deactivation_date %> +
+ <%= f.govuk_submit "Deactivate this scheme" %> + <%= govuk_button_link_to "Cancel", location_path, html: { method: :get }, secondary: true %> +
+ <% end %> + \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index ad088fe11..22650185d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -54,6 +54,7 @@ Rails.application.routes.draw do get "edit-name", to: "locations#edit_name" get "edit-local-authority", to: "locations#edit_local_authority" get "deactivate", to: "locations#deactivate" + patch "deactivate", to: "locations#deactivate" end end diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index 96b3d1b21..0cd0d6466 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -1212,4 +1212,59 @@ RSpec.describe LocationsController, type: :request do end end end + + describe "#deactivate" do + context "when not signed in" do + it "redirects to the sign in page" do + patch "/schemes/1/locations/1/deactivate" + expect(response).to redirect_to("/account/sign-in") + end + end + + context "when signed in as a data provider" do + let(:user) { FactoryBot.create(:user) } + + before do + sign_in user + patch "/schemes/1/locations/1/deactivate" + end + + it "returns 401 unauthorized" do + request + expect(response).to have_http_status(:unauthorized) + end + end + + context "when signed in as a data coordinator" do + let(:user) { FactoryBot.create(:user, :data_coordinator) } + let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) } + let!(:location) { FactoryBot.create(:location, scheme:) } + let(:startdate) { Time.utc(2021, 1, 2) } + let(:deactivation_date) { Time.new(2022, 10, 10) } + + before do + Timecop.freeze(Time.utc(2022, 10, 10)) + sign_in user + patch "/schemes/#{scheme.id}/locations/#{location.id}/deactivate", params: + end + + context "default date" do + let(:params) { { deactivation_date: deactivation_date } } + + it "renders the confirmation page" do + expect(response).to have_http_status(:ok) + expect(page).to have_content("This change will affect SOME logs") + end + end + + context "other date" do + let(:params) { { deactivation_date: "other", "deactivation_date(3i)": "10", "deactivation_date(2i)": "10", "deactivation_date(1i)": "2022"} } + + it "renders the confirmation page" do + expect(response).to have_http_status(:ok) + expect(page).to have_content("This change will affect SOME logs") + end + end + end + end end