Browse Source

Update affected logs with deactivated location.

pull/996/head
Kat 4 years ago
parent
commit
9102271b2f
  1. 6
      app/controllers/locations_controller.rb
  2. 1
      app/models/lettings_log.rb
  3. 37
      spec/requests/locations_controller_spec.rb

6
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: params[:location][:deactivation_date]) && update_affected_logs
flash[:notice] = deactivate_success_notice
end
redirect_to scheme_location_path(@scheme, @location)
@ -189,6 +189,10 @@ private
end
end
def update_affected_logs
@location.lettings_logs.filter_by_before_startdate(params[:location][:deactivation_date]).update!(location: nil)
end
def deactivation_date
if params[:location].blank?
return

1
app/models/lettings_log.rb

@ -45,6 +45,7 @@ class LettingsLog < Log
.or(filter_by_postcode(param))
.or(filter_by_id(param))
}
scope :filter_by_before_startdate, ->(date) { left_joins(:location).where("lettings_logs.startdate >= ?", date) }
AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at].freeze
OPTIONAL_FIELDS = %w[first_time_property_let_as_social_housing tenancycode propcode].freeze

37
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, :completed, location:, tenancylength: nil, startdate:) }
let(:startdate) { Time.utc(2022, 10, 11) }
before do
Timecop.freeze(Time.utc(2022, 10, 10))
@ -1282,7 +1283,26 @@ 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_deactivations.count).to eq(1)
expect(location.location_deactivations.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)
lettings_log.reload
expect(lettings_log.location).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)
lettings_log.reload
expect(lettings_log.location).to eq(location)
end
end
end
@ -1368,19 +1388,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_deactivations << location_deactivation }
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 +1408,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) { LocationDeactivation.create(deactivation_date: Time.utc(2022, 10, 9)) }
it "renders reactivate this location" do
expect(response).to have_http_status(:ok)
@ -1399,8 +1417,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) { LocationDeactivation.create(deactivation_date: Time.utc(2022, 10, 12)) }
it "renders reactivate this location" do
expect(response).to have_http_status(:ok)

Loading…
Cancel
Save