Browse Source

CLDC-2420 Update deactivating soon label (#1701)

* Display active status is location/scheme deactivation is in more than 6 months

* Override existing location deactivation period with new deactivation

* Override existing scheme deactivation period with new deactivation

* add reactivate to policy

* Change status tag method

* Update instance double in test

* Update deactivates_in_a_long_time? method

* Uncoment a test
pull/1713/head
kosiakkatrina 2 years ago committed by GitHub
parent
commit
8c23cf6550
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      app/controllers/locations_controller.rb
  2. 8
      app/controllers/schemes_controller.rb
  3. 2
      app/helpers/locations_helper.rb
  4. 4
      app/helpers/schemes_helper.rb
  5. 6
      app/helpers/tag_helper.rb
  6. 4
      app/models/location.rb
  7. 2
      app/models/location_deactivation_period.rb
  8. 4
      app/models/scheme.rb
  9. 2
      app/models/scheme_deactivation_period.rb
  10. 2
      app/policies/scheme_policy.rb
  11. 2
      app/views/locations/index.html.erb
  12. 2
      app/views/locations/show.html.erb
  13. 2
      app/views/schemes/_scheme_list.html.erb
  14. 80
      spec/requests/locations_controller_spec.rb
  15. 73
      spec/requests/schemes_controller_spec.rb
  16. 1
      spec/views/locations/show.html.erb_spec.rb

8
app/controllers/locations_controller.rb

@ -149,7 +149,11 @@ class LocationsController < ApplicationController
def show; end
def new_deactivation
@location_deactivation_period = LocationDeactivationPeriod.new
@location_deactivation_period = if @location.deactivates_in_a_long_time?
@location.open_deactivation || LocationDeactivationPeriod.new
else
LocationDeactivationPeriod.new
end
if params[:location_deactivation_period].blank?
render "toggle_active", locals: { action: "deactivate" }
@ -176,7 +180,7 @@ class LocationsController < ApplicationController
end
def deactivate
if @location.location_deactivation_periods.create!(deactivation_date: params[:deactivation_date])
if @location.open_deactivation&.update!(deactivation_date: params[:deactivation_date]) || @location.location_deactivation_periods.create!(deactivation_date: params[:deactivation_date])
logs = reset_location_and_scheme_for_logs!
flash[:notice] = deactivate_success_notice

8
app/controllers/schemes_controller.rb

@ -27,7 +27,11 @@ class SchemesController < ApplicationController
end
def new_deactivation
@scheme_deactivation_period = SchemeDeactivationPeriod.new
@scheme_deactivation_period = if @scheme.deactivates_in_a_long_time?
@scheme.open_deactivation || SchemeDeactivationPeriod.new
else
SchemeDeactivationPeriod.new
end
if params[:scheme_deactivation_period].blank?
render "toggle_active", locals: { action: "deactivate" }
@ -54,7 +58,7 @@ class SchemesController < ApplicationController
end
def deactivate
if @scheme.scheme_deactivation_periods.create!(deactivation_date: params[:deactivation_date])
if @scheme.open_deactivation&.update!(deactivation_date: params[:deactivation_date]) || @scheme.scheme_deactivation_periods.create!(deactivation_date: params[:deactivation_date])
logs = reset_location_and_scheme_for_logs!
flash[:notice] = deactivate_success_notice

2
app/helpers/locations_helper.rb

@ -69,7 +69,7 @@ module LocationsHelper
end
def toggle_location_link(location)
return govuk_button_link_to "Deactivate this location", scheme_location_new_deactivation_path(location.scheme, location), warning: true if location.active?
return govuk_button_link_to "Deactivate this location", scheme_location_new_deactivation_path(location.scheme, location), warning: true if location.active? || location.deactivates_in_a_long_time?
return govuk_button_link_to "Reactivate this location", scheme_location_new_reactivation_path(location.scheme, location) if location.deactivated?
end

4
app/helpers/schemes_helper.rb

@ -14,7 +14,7 @@ module SchemesHelper
{ name: "Level of support given", value: scheme.support_type },
{ name: "Intended length of stay", value: scheme.intended_stay },
{ name: "Availability", value: scheme_availability(scheme) },
{ name: "Status", value: status_tag(scheme.status) },
{ name: "Status", value: status_tag_from_resource(scheme) },
]
if user.data_coordinator?
@ -40,7 +40,7 @@ module SchemesHelper
end
def toggle_scheme_link(scheme)
return govuk_button_link_to "Deactivate this scheme", scheme_new_deactivation_path(scheme), warning: true if scheme.active?
return govuk_button_link_to "Deactivate this scheme", scheme_new_deactivation_path(scheme), warning: true if scheme.active? || scheme.deactivates_in_a_long_time?
return govuk_button_link_to "Reactivate this scheme", scheme_new_reactivation_path(scheme) if scheme.deactivated?
end

6
app/helpers/tag_helper.rb

@ -34,4 +34,10 @@ module TagHelper
text: TEXT[status.to_sym],
)
end
def status_tag_from_resource(resource, classes = [])
status = resource.status
status = :active if resource.deactivates_in_a_long_time?
status_tag(status, classes)
end
end

4
app/models/location.rb

@ -108,6 +108,10 @@ class Location < ApplicationRecord
status == :reactivating_soon
end
def deactivates_in_a_long_time?
status_at(6.months.from_now) == :deactivating_soon
end
def validate_postcode
if !postcode&.match(POSTCODE_REGEXP)
error_message = I18n.t("validations.postcode")

2
app/models/location_deactivation_period.rb

@ -4,7 +4,7 @@ class LocationDeactivationPeriodValidator < ActiveModel::Validator
def validate(record)
location = record.location
recent_deactivation = location.location_deactivation_periods.deactivations_without_reactivation.first
if recent_deactivation.present?
if recent_deactivation.present? && recent_deactivation.deactivation_date <= 6.months.from_now
validate_reactivation(record, recent_deactivation, location)
else
validate_deactivation(record, location)

4
app/models/scheme.rb

@ -242,4 +242,8 @@ class Scheme < ApplicationRecord
def deactivated?
status == :deactivated
end
def deactivates_in_a_long_time?
status_at(6.months.from_now) == :deactivating_soon
end
end

2
app/models/scheme_deactivation_period.rb

@ -4,7 +4,7 @@ class SchemeDeactivationPeriodValidator < ActiveModel::Validator
def validate(record)
scheme = record.scheme
recent_deactivation = scheme.scheme_deactivation_periods.deactivations_without_reactivation.first
if recent_deactivation.present?
if recent_deactivation.present? && recent_deactivation.deactivation_date <= 6.months.from_now
validate_reactivation(record, recent_deactivation, scheme)
else
validate_deactivation(record, scheme)

2
app/policies/scheme_policy.rb

@ -47,7 +47,9 @@ class SchemePolicy
confirm_secondary_client_group?
secondary_client_group?
new_deactivation?
new_reactivation?
deactivate?
reactivate?
details?
support?
deactivate_confirm?

2
app/views/locations/index.html.erb

@ -54,7 +54,7 @@
end), { class: "govuk-!-font-weight-bold" }, wrapper_tag: "div")) %>
<% row.cell(text: location.name) %>
<% row.cell(text: location.id) %>
<% row.cell(text: status_tag(location.status)) %>
<% row.cell(text: status_tag_from_resource(location)) %>
<% end %>
<% end %>
<% end %>

2
app/views/locations/show.html.erb

@ -15,7 +15,7 @@
<% display_location_attributes(@location).each do |attr| %>
<%= summary_list.row do |row| %>
<% row.key { attr[:name] } %>
<% row.value { attr[:attribute].eql?("status") ? status_tag(attr[:value]) : details_html(attr) } %>
<% row.value { attr[:attribute].eql?("status") ? status_tag_from_resource(@location) : details_html(attr) } %>
<% if LocationPolicy.new(current_user, @location).update? %>
<% row.action(text: "Change", href: scheme_location_name_path(@scheme, @location, referrer: "details")) if attr[:attribute] == "name" %>
<% end %>

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

@ -17,7 +17,7 @@
<% row.cell(text: simple_format(scheme_cell(scheme), { class: "govuk-!-font-weight-bold" }, wrapper_tag: "div")) %>
<% row.cell(text: scheme.id_to_display) %>
<% row.cell(text: scheme.locations&.count) %>
<% row.cell(text: status_tag(scheme.status)) %>
<% row.cell(text: status_tag_from_resource(scheme)) %>
<% end %>
<% end %>
<% end %>

80
spec/requests/locations_controller_spec.rb

@ -1557,6 +1557,37 @@ RSpec.describe LocationsController, type: :request do
expect(lettings_log.unresolved).to eq(nil)
end
end
context "and there already is a deactivation period" do
let(:add_deactivations) { create(:location_deactivation_period, deactivation_date: Time.zone.local(2023, 6, 5), reactivation_date: nil, location:) }
before do
patch "/schemes/#{scheme.id}/locations/#{location.id}/deactivate", params:
end
it "updates existing location with valid deactivation date and renders location page" do
follow_redirect!
expect(response).to have_http_status(:ok)
expect(page).to have_css(".govuk-notification-banner.govuk-notification-banner--success")
location.reload
expect(location.location_deactivation_periods.count).to eq(1)
expect(location.location_deactivation_periods.first.deactivation_date).to eq(deactivation_date)
end
it "clears the location and scheme answers" do
expect(lettings_log.location).to eq(location)
expect(lettings_log.scheme).to eq(scheme)
lettings_log.reload
expect(lettings_log.location).to eq(nil)
expect(lettings_log.scheme).to eq(nil)
end
it "marks log as needing attention" do
expect(lettings_log.unresolved).to eq(nil)
lettings_log.reload
expect(lettings_log.unresolved).to eq(true)
end
end
end
context "when the date is not selected" do
@ -1623,6 +1654,34 @@ RSpec.describe LocationsController, type: :request do
expect(page).to have_content(I18n.t("validations.location.deactivation.during_deactivated_period"))
end
end
context "when there is an earlier open deactivation" do
let(:deactivation_date) { Time.zone.local(2022, 10, 10) }
let(:params) { { location_deactivation_period: { deactivation_date_type: "other", "deactivation_date(3i)": "8", "deactivation_date(2i)": "9", "deactivation_date(1i)": "2023" } } }
let(:add_deactivations) { create(:location_deactivation_period, deactivation_date: Time.zone.local(2023, 6, 5), reactivation_date: nil, location:) }
it "redirects to the location page and updates the existing deactivation period" do
follow_redirect!
follow_redirect!
expect(response).to have_http_status(:ok)
expect(page).to have_css(".govuk-notification-banner.govuk-notification-banner--success")
location.reload
expect(location.location_deactivation_periods.count).to eq(1)
expect(location.location_deactivation_periods.first.deactivation_date).to eq(Time.zone.local(2023, 9, 8))
end
end
context "when there is a later open deactivation" do
let(:deactivation_date) { Time.zone.local(2022, 10, 10) }
let(:params) { { location_deactivation_period: { deactivation_date_type: "other", "deactivation_date(3i)": "8", "deactivation_date(2i)": "9", "deactivation_date(1i)": "2022" } } }
let(:add_deactivations) { create(:location_deactivation_period, deactivation_date: Time.zone.local(2023, 6, 5), reactivation_date: nil, location:) }
it "redirects to the confirmation page" do
follow_redirect!
expect(response).to have_http_status(:ok)
expect(page).to have_content("This change will affect 1 logs")
end
end
end
end
@ -1692,6 +1751,19 @@ RSpec.describe LocationsController, type: :request do
expect(response).to have_http_status(:ok)
expect(page).not_to have_link("Reactivate this location")
expect(page).not_to have_link("Deactivate this location")
expect(page).to have_content("Deactivating soon")
end
end
context "with location that's deactivating in more than 6 months" do
let(:location_deactivation_period) { create(:location_deactivation_period, deactivation_date: Time.zone.local(2023, 6, 12), location:) }
it "does render toggle location link" do
expect(response).to have_http_status(:ok)
expect(page).not_to have_link("Reactivate this location")
expect(page).to have_link("Deactivate this location")
expect(response.body).not_to include("<strong class=\"govuk-tag govuk-tag--yellow\">Deactivating soon</strong>")
expect(response.body).to include("<strong class=\"govuk-tag govuk-tag--green\">Active</strong>")
end
end
@ -1758,10 +1830,10 @@ RSpec.describe LocationsController, type: :request do
let(:scheme) { create(:scheme, owning_organisation: user.organisation) }
let(:location) { create(:location, scheme:) }
let(:deactivation_date) { Time.zone.local(2022, 4, 1) }
let(:startdate) { Time.utc(2022, 10, 11) }
let(:startdate) { Time.utc(2022, 9, 11) }
before do
Timecop.freeze(Time.utc(2022, 10, 10))
Timecop.freeze(Time.utc(2022, 9, 10))
sign_in user
create(:location_deactivation_period, deactivation_date:, location:)
location.save!
@ -1792,7 +1864,7 @@ RSpec.describe LocationsController, type: :request do
end
context "with other date" do
let(:params) { { location_deactivation_period: { reactivation_date_type: "other", "reactivation_date(3i)": "10", "reactivation_date(2i)": "10", "reactivation_date(1i)": "2022" } } }
let(:params) { { location_deactivation_period: { reactivation_date_type: "other", "reactivation_date(3i)": "10", "reactivation_date(2i)": "9", "reactivation_date(1i)": "2022" } } }
it "redirects to the location page and displays a success banner" do
expect(response).to redirect_to("/schemes/#{scheme.id}/locations/#{location.id}")
@ -1805,7 +1877,7 @@ RSpec.describe LocationsController, type: :request do
follow_redirect!
location.reload
expect(location.location_deactivation_periods.count).to eq(1)
expect(location.location_deactivation_periods.first.reactivation_date).to eq(Time.zone.local(2022, 10, 10))
expect(location.location_deactivation_periods.first.reactivation_date).to eq(Time.zone.local(2022, 9, 10))
end
end

73
spec/requests/schemes_controller_spec.rb

@ -330,6 +330,18 @@ RSpec.describe SchemesController, type: :request do
expect(page).not_to have_link("Deactivate this scheme")
end
end
context "with scheme that's deactivating in more than 6 months" do
let(:scheme_deactivation_period) { create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 5, 12), scheme:) }
it "does not render toggle scheme link" do
expect(response).to have_http_status(:ok)
expect(page).not_to have_link("Reactivate this scheme")
expect(page).to have_link("Deactivate this scheme")
expect(response.body).not_to include("<strong class=\"govuk-tag govuk-tag--yellow\">Deactivating soon</strong>")
expect(response.body).to include("<strong class=\"govuk-tag govuk-tag--green\">Active</strong>")
end
end
end
context "when coordinator attempts to see scheme belonging to a parent organisation" do
@ -1999,6 +2011,38 @@ RSpec.describe SchemesController, type: :request do
end
end
context "and there already is a deactivation period" do
let(:add_deactivations) { create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 6, 5), reactivation_date: nil, scheme:) }
before do
create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 6, 5), reactivation_date: nil, scheme:)
patch "/schemes/#{scheme.id}/deactivate", params:
end
it "updates existing scheme with valid deactivation date and renders scheme page" do
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(deactivation_date)
end
it "clears the scheme and scheme answers" do
expect(lettings_log.scheme).to eq(scheme)
lettings_log.reload
expect(lettings_log.scheme).to eq(nil)
expect(lettings_log.scheme).to eq(nil)
end
it "marks log as needing attention" do
expect(lettings_log.unresolved).to eq(nil)
lettings_log.reload
expect(lettings_log.unresolved).to eq(true)
end
end
context "and the users need to be notified" do
it "sends E-mails to the creators of affected logs with counts" do
expect {
@ -2061,6 +2105,35 @@ RSpec.describe SchemesController, type: :request do
expect(page).to have_content(I18n.t("validations.scheme.toggle_date.invalid"))
end
end
context "when there is an earlier open deactivation" do
let(:deactivation_date) { Time.zone.local(2022, 10, 10) }
let(:params) { { scheme_deactivation_period: { deactivation_date_type: "other", "deactivation_date(3i)": "8", "deactivation_date(2i)": "9", "deactivation_date(1i)": "2023" } } }
let(:add_deactivations) { create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 6, 5), reactivation_date: nil, scheme:) }
it "redirects to the scheme page and updates the existing 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(2023, 9, 8))
end
end
context "when there is a later open deactivation" do
let(:deactivation_date) { Time.zone.local(2022, 10, 10) }
let(:params) { { scheme_deactivation_period: { deactivation_date_type: "other", "deactivation_date(3i)": "8", "deactivation_date(2i)": "9", "deactivation_date(1i)": "2022" } } }
let(:add_deactivations) { create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 6, 5), reactivation_date: nil, scheme:) }
it "redirects to the confirmation page" do
follow_redirect!
expect(response).to have_http_status(:ok)
expect(page).to have_content("This change will affect 1 logs")
end
end
end
end
end

1
spec/views/locations/show.html.erb_spec.rb

@ -40,6 +40,7 @@ RSpec.describe "locations/show.html.erb" do
status: :active,
active?: true,
scheme:,
deactivates_in_a_long_time?: false,
)
end

Loading…
Cancel
Save