Browse Source

Update affected logs count and skip the confirmation page if no logs are affected for schemes

pull/1018/head
Kat 4 years ago
parent
commit
d7b8f1d5bb
  1. 4
      app/controllers/schemes_controller.rb
  2. 2
      app/views/schemes/deactivate_confirm.html.erb
  3. 36
      spec/requests/schemes_controller_spec.rb

4
app/controllers/schemes_controller.rb

@ -39,9 +39,13 @@ class SchemesController < ApplicationController
end end
def deactivate_confirm def deactivate_confirm
if @scheme.lettings_logs.filter_by_before_startdate(params[:deactivation_date]).count.zero?
deactivate
else
@deactivation_date = params[:deactivation_date] @deactivation_date = params[:deactivation_date]
@deactivation_date_type = params[:deactivation_date_type] @deactivation_date_type = params[:deactivation_date_type]
end end
end
def deactivate def deactivate
if @scheme.scheme_deactivation_periods.create!(deactivation_date: params[:deactivation_date]) && reset_location_and_scheme_for_logs! if @scheme.scheme_deactivation_periods.create!(deactivation_date: params[:deactivation_date]) && reset_location_and_scheme_for_logs!

2
app/views/schemes/deactivate_confirm.html.erb

@ -6,7 +6,7 @@
<% end %> <% end %>
<h1 class="govuk-heading-l"> <h1 class="govuk-heading-l">
<span class="govuk-caption-l"><%= @scheme.service_name %></span> <span class="govuk-caption-l"><%= @scheme.service_name %></span>
This change will affect <%= @scheme.lettings_logs.count %> logs This change will affect <%= @scheme.lettings_logs.filter_by_before_startdate(@deactivation_date).count %> logs
</h1> </h1>
<%= govuk_warning_text text: I18n.t("warnings.scheme.deactivation.review_logs") %> <%= govuk_warning_text text: I18n.t("warnings.scheme.deactivation.review_logs") %>
<%= f.hidden_field :confirm, value: true %> <%= f.hidden_field :confirm, value: true %>

36
spec/requests/schemes_controller_spec.rb

@ -1773,10 +1773,12 @@ RSpec.describe SchemesController, type: :request do
let(:deactivation_date) { Time.utc(2022, 10, 10) } let(:deactivation_date) { Time.utc(2022, 10, 10) }
let!(:lettings_log) { FactoryBot.create(:lettings_log, :sh, location:, scheme:, startdate:, owning_organisation: user.organisation) } let!(:lettings_log) { FactoryBot.create(:lettings_log, :sh, location:, scheme:, startdate:, owning_organisation: user.organisation) }
let(:startdate) { Time.utc(2022, 10, 11) } let(:startdate) { Time.utc(2022, 10, 11) }
let(:setup_schemes) { nil }
before do before do
Timecop.freeze(Time.utc(2022, 10, 10)) Timecop.freeze(Time.utc(2022, 10, 10))
sign_in user sign_in user
setup_schemes
patch "/schemes/#{scheme.id}/new-deactivation", params: patch "/schemes/#{scheme.id}/new-deactivation", params:
end end
@ -1787,6 +1789,7 @@ RSpec.describe SchemesController, type: :request do
context "with default date" do context "with default date" do
let(:params) { { scheme_deactivation_period: { deactivation_date_type: "default", deactivation_date: } } } let(:params) { { scheme_deactivation_period: { deactivation_date_type: "default", deactivation_date: } } }
context "and affected logs" do
it "redirects to the confirmation page" do it "redirects to the confirmation page" do
follow_redirect! follow_redirect!
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
@ -1794,9 +1797,26 @@ RSpec.describe SchemesController, type: :request do
end end
end end
context "and no affected logs" do
let(:setup_schemes) { scheme.lettings_logs.update(scheme: nil) }
it "redirects to the location page and updates the deactivation period" do
follow_redirect!
follow_redirect!
follow_redirect!
expect(response).to have_http_status(:ok)
expect(page).to have_css(".govuk-notification-banner.govuk-notification-banner--success")
scheme.reload
expect(scheme.scheme_deactivation_periods.count).to eq(1)
expect(scheme.scheme_deactivation_periods.first.deactivation_date).to eq(Time.zone.local(2022, 4, 1))
end
end
end
context "with other date" do context "with other date" do
let(:params) { { scheme_deactivation_period: { deactivation_date_type: "other", "deactivation_date(3i)": "10", "deactivation_date(2i)": "10", "deactivation_date(1i)": "2022" } } } let(:params) { { scheme_deactivation_period: { deactivation_date_type: "other", "deactivation_date(3i)": "10", "deactivation_date(2i)": "10", "deactivation_date(1i)": "2022" } } }
context "and affected logs" do
it "redirects to the confirmation page" do it "redirects to the confirmation page" do
follow_redirect! follow_redirect!
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
@ -1804,6 +1824,22 @@ RSpec.describe SchemesController, type: :request do
end end
end end
context "and no affected logs" do
let(:setup_schemes) { scheme.lettings_logs.update(scheme: nil) }
it "redirects to the location page and updates the deactivation period" do
follow_redirect!
follow_redirect!
follow_redirect!
expect(response).to have_http_status(:ok)
expect(page).to have_css(".govuk-notification-banner.govuk-notification-banner--success")
scheme.reload
expect(scheme.scheme_deactivation_periods.count).to eq(1)
expect(scheme.scheme_deactivation_periods.first.deactivation_date).to eq(Time.zone.local(2022, 10, 10))
end
end
end
context "when confirming deactivation" do context "when confirming deactivation" do
let(:params) { { deactivation_date:, confirm: true, deactivation_date_type: "other" } } let(:params) { { deactivation_date:, confirm: true, deactivation_date_type: "other" } }

Loading…
Cancel
Save