Browse Source

git: update branch

CLDC-1671-deactivate-scheme
natdeanlewissoftwire 2 years ago
parent
commit
7fcc1f6f44
  1. 8
      app/controllers/locations_controller.rb
  2. 9
      app/helpers/locations_helper.rb
  3. 10
      app/models/location.rb
  4. 6
      app/models/scheme.rb
  5. 27
      spec/helpers/locations_helper_spec.rb
  6. 68
      spec/models/location_spec.rb
  7. 41
      spec/requests/locations_controller_spec.rb

8
app/controllers/locations_controller.rb

@ -43,7 +43,7 @@ class LocationsController < ApplicationController
def deactivate
@location.run_deactivation_validations!
if @location.update!(deactivation_date:)
if @location.location_deactivation_periods.create!(deactivation_date:) && update_affected_logs
flash[:notice] = deactivate_success_notice
end
redirect_to scheme_location_path(@scheme, @location)
@ -185,10 +185,14 @@ private
when :deactivated
"#{@location.name} has been deactivated"
when :deactivating_soon
"#{@location.name} will deactivate on #{@location.deactivation_date.to_formatted_s(:govuk_date)}"
"#{@location.name} will deactivate on #{deactivation_date.to_time.to_formatted_s(:govuk_date)}"
end
end
def update_affected_logs
@location.lettings_logs.filter_by_before_startdate(deactivation_date.to_time).update!(location: nil, scheme: nil)
end
def deactivation_date
if params[:location].blank?
return

9
app/helpers/locations_helper.rb

@ -41,4 +41,13 @@ module LocationsHelper
base_attributes
end
def location_availability(location)
availability = "Active from #{location.available_from.to_formatted_s(:govuk_date)}"
location.location_deactivation_periods.each do |deactivation|
availability << " to #{(deactivation.deactivation_date - 1.day).to_formatted_s(:govuk_date)}\nDeactivated on #{deactivation.deactivation_date.to_formatted_s(:govuk_date)}"
availability << "\nActive from #{deactivation.reactivation_date.to_formatted_s(:govuk_date)}" if deactivation.reactivation_date.present?
end
availability
end
end

10
app/models/location.rb

@ -4,6 +4,7 @@ class Location < ApplicationRecord
validates :units, :type_of_unit, :mobility_type, presence: true
belongs_to :scheme
has_many :lettings_logs, class_name: "LettingsLog"
has_many :location_deactivation_periods, class_name: "LocationDeactivationPeriod"
has_paper_trail
@ -11,7 +12,7 @@ class Location < ApplicationRecord
auto_strip_attributes :name
attr_accessor :add_another_location, :deactivation_date_type, :run_deactivation_validations
attr_accessor :add_another_location, :deactivation_date_type, :deactivation_date, :run_deactivation_validations
scope :search_by_postcode, ->(postcode) { where("REPLACE(postcode, ' ', '') ILIKE ?", "%#{postcode.delete(' ')}%") }
scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") }
@ -374,8 +375,9 @@ class Location < ApplicationRecord
end
def status
return :active if deactivation_date.blank?
return :deactivating_soon if Time.zone.now < deactivation_date
recent_deactivation = location_deactivation_periods.deactivations_without_reactivation.first
return :active if recent_deactivation.blank?
return :deactivating_soon if Time.zone.now < recent_deactivation.deactivation_date
:deactivated
end
@ -403,7 +405,7 @@ class Location < ApplicationRecord
end
else
collection_start_date = FormHandler.instance.current_collection_start_date
unless deactivation_date.between?(collection_start_date, Date.new(2200, 1, 1))
unless deactivation_date.between?(collection_start_date, Time.zone.local(2200, 1, 1))
errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date)))
end
end

6
app/models/scheme.rb

@ -199,7 +199,6 @@ class Scheme < ApplicationRecord
end
def validate_confirmed
required_attributes = attribute_names - %w[id created_at updated_at old_id old_visible_id confirmed end_date sensitive secondary_client_group total_units has_other_client_group deactivation_date deactivation_date_type]
if confirmed == true
required_attributes.any? do |attribute|
@ -216,6 +215,7 @@ class Scheme < ApplicationRecord
end
def status
return :incomplete if required_attributes.any?(&:blank?)
return :active if deactivation_date.blank?
return :deactivating_soon if Time.zone.now < deactivation_date
@ -250,4 +250,8 @@ class Scheme < ApplicationRecord
end
end
end
def required_attributes
attribute_names - %w[id created_at updated_at old_id old_visible_id confirmed end_date sensitive secondary_client_group total_units has_other_client_group deactivation_date deactivation_date_type]
end
end

27
spec/helpers/locations_helper_spec.rb

@ -59,18 +59,35 @@ RSpec.describe LocationsHelper do
{ name: "Common type of unit", value: location.type_of_unit },
{ name: "Mobility type", value: location.mobility_type },
{ name: "Code", value: location.location_code },
{ name: "Availability", value: "Available from 8 August 2022" },
{ name: "Availability", value: "Active from 8 August 2022" },
{ name: "Status", value: :active },
]
expect(display_location_attributes(location)).to eq(attributes)
end
it "displays created_at as availability date if startdate is not present" do
location.update!(startdate: nil)
availability_attribute = display_location_attributes(location).find { |x| x[:name] == "Availability" }[:value]
context "when viewing availability" do
context "with are no deactivations" do
it "displays created_at as availability date if startdate is not present" do
location.update!(startdate: nil)
availability_attribute = display_location_attributes(location).find { |x| x[:name] == "Availability" }[:value]
expect(availability_attribute).to eq("Available from #{location.created_at.to_formatted_s(:govuk_date)}")
expect(availability_attribute).to eq("Active from #{location.created_at.to_formatted_s(:govuk_date)}")
end
end
context "with previous deactivations" do
before do
location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 8, 10), reactivation_date: Time.zone.local(2022, 9, 1))
location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 9, 15), reactivation_date: nil)
end
it "displays the timeline of availability" do
availability_attribute = display_location_attributes(location).find { |x| x[:name] == "Availability" }[:value]
expect(availability_attribute).to eq("Active from 8 August 2022 to 9 August 2022\nDeactivated on 10 August 2022\nActive from 1 September 2022 to 14 September 2022\nDeactivated on 15 September 2022")
end
end
end
end
end

68
spec/models/location_spec.rb

@ -119,32 +119,56 @@ RSpec.describe Location, type: :model do
Timecop.freeze(2022, 6, 7)
end
it "returns active if the location is not deactivated" do
location.deactivation_date = nil
location.deactivation_date_type = nil
location.save!
expect(location.status).to eq(:active)
end
context "when there have not been any previous deactivations" do
it "returns active if the location has no deactivation records" do
expect(location.status).to eq(:active)
end
it "returns deactivating soon if deactivation_date is in the future" do
location.deactivation_date = Time.zone.local(2022, 8, 8)
location.deactivation_date_type = "other"
location.save!
expect(location.status).to eq(:deactivating_soon)
end
it "returns deactivating soon if deactivation_date is in the future" do
location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 8, 8))
location.save!
expect(location.status).to eq(:deactivating_soon)
end
it "returns deactivated if deactivation_date is in the past" do
location.deactivation_date = Time.zone.local(2022, 4, 8)
location.deactivation_date_type = "other"
location.save!
expect(location.status).to eq(:deactivated)
it "returns deactivated if deactivation_date is in the past" do
location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 6))
location.save!
expect(location.status).to eq(:deactivated)
end
it "returns deactivated if deactivation_date is today" do
location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7))
location.save!
expect(location.status).to eq(:deactivated)
end
end
it "returns deactivated if deactivation_date is today" do
location.deactivation_date = Time.zone.local(2022, 6, 7)
location.deactivation_date_type = "other"
location.save!
expect(location.status).to eq(:deactivated)
context "when there have been previous deactivations" do
before do
location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 6, 5))
end
it "returns active if the location has no relevant deactivation records" do
expect(location.status).to eq(:active)
end
it "returns deactivating soon if deactivation_date is in the future" do
location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 8, 8))
location.save!
expect(location.status).to eq(:deactivating_soon)
end
it "returns deactivated if deactivation_date is in the past" do
location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 6))
location.save!
expect(location.status).to eq(:deactivated)
end
it "returns deactivated if deactivation_date is today" do
location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7))
location.save!
expect(location.status).to eq(:deactivated)
end
end
end

41
spec/requests/locations_controller_spec.rb

@ -1239,8 +1239,9 @@ RSpec.describe LocationsController, type: :request 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.utc(2022, 10, 10) }
let!(:lettings_log) { FactoryBot.create(:lettings_log, :sh, location:, scheme:, startdate:, owning_organisation: user.organisation) }
let(:startdate) { Time.utc(2022, 10, 11) }
before do
Timecop.freeze(Time.utc(2022, 10, 10))
@ -1282,7 +1283,30 @@ RSpec.describe LocationsController, type: :request do
expect(response).to have_http_status(:ok)
expect(page).to have_css(".govuk-notification-banner.govuk-notification-banner--success")
location.reload
expect(location.deactivation_date).to eq(deactivation_date)
expect(location.location_deactivation_periods.count).to eq(1)
expect(location.location_deactivation_periods.first.deactivation_date).to eq(deactivation_date)
end
context "and a log startdate is after location deactivation date" do
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
end
context "and a log startdate is before location deactivation date" do
let(:startdate) { Time.utc(2022, 10, 9) }
it "does not update the log" do
expect(lettings_log.location).to eq(location)
expect(lettings_log.scheme).to eq(scheme)
lettings_log.reload
expect(lettings_log.location).to eq(location)
expect(lettings_log.scheme).to eq(scheme)
end
end
end
@ -1368,19 +1392,18 @@ RSpec.describe LocationsController, type: :request do
let(:user) { FactoryBot.create(:user, :data_coordinator) }
let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) }
let!(:location) { FactoryBot.create(:location, scheme:) }
let(:add_deactivations) { location.location_deactivation_periods << location_deactivation_period }
before do
Timecop.freeze(Time.utc(2022, 10, 10))
sign_in user
location.deactivation_date = deactivation_date
location.deactivation_date_type = deactivation_date_type
add_deactivations
location.save!
get "/schemes/#{scheme.id}/locations/#{location.id}"
end
context "with active location" do
let(:deactivation_date) { nil }
let(:deactivation_date_type) { nil }
let(:add_deactivations) {}
it "renders deactivate this location" do
expect(response).to have_http_status(:ok)
@ -1389,8 +1412,7 @@ RSpec.describe LocationsController, type: :request do
end
context "with deactivated location" do
let(:deactivation_date) { Time.utc(2022, 10, 9) }
let(:deactivation_date_type) { "other" }
let(:location_deactivation_period) { FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 9)) }
it "renders reactivate this location" do
expect(response).to have_http_status(:ok)
@ -1399,8 +1421,7 @@ RSpec.describe LocationsController, type: :request do
end
context "with location that's deactivating soon" do
let(:deactivation_date) { Time.utc(2022, 10, 12) }
let(:deactivation_date_type) { "other" }
let(:location_deactivation_period) { FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 12)) }
it "renders reactivate this location" do
expect(response).to have_http_status(:ok)

Loading…
Cancel
Save