From 78b5055a185fa7d34042b00da725d6d5b85aecb2 Mon Sep 17 00:00:00 2001 From: Kat Date: Thu, 10 Nov 2022 16:55:59 +0000 Subject: [PATCH] update lettings logs --- app/controllers/locations_controller.rb | 2 +- app/models/lettings_log.rb | 1 + spec/requests/locations_controller_spec.rb | 14 +++++++++++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index 87a3ea566..82f96e9ca 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -164,7 +164,7 @@ private def confirm_deactivation if @location.update(deactivation_date: params[:location][:deactivation_date]) - # update the logs + @location.lettings_logs.filter_by_before_startdate( params[:location][:deactivation_date]).update(location: nil) flash[:notice] = "#{@location.name} has been deactivated" end redirect_to scheme_locations_path(@scheme) diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index f09fd1ebd..76b43e511 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -38,6 +38,7 @@ class LettingsLog < Log scope :filter_by_propcode, ->(propcode) { where("propcode ILIKE ?", "%#{propcode}%") } scope :filter_by_postcode, ->(postcode_full) { where("REPLACE(postcode_full, ' ', '') ILIKE ?", "%#{postcode_full.delete(' ')}%") } scope :filter_by_location_postcode, ->(postcode_full) { left_joins(:location).where("REPLACE(locations.postcode, ' ', '') ILIKE ?", "%#{postcode_full.delete(' ')}%") } + scope :filter_by_before_startdate, ->(date) { left_joins(:location).where("lettings_logs.startdate >= ?", date) } scope :search_by, lambda { |param| filter_by_location_postcode(param) .or(filter_by_tenant_code(param)) diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index fc624cbd0..0895c54e6 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -1239,6 +1239,8 @@ 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!(:lettings_log_to_deactivate) { FactoryBot.create(:lettings_log, scheme:, location:, startdate: Time.utc(2022, 10, 11) ) } + let!(:active_lettings_log) { FactoryBot.create(:lettings_log, scheme:, location:, startdate: Time.utc(2022, 10, 9) ) } let(:startdate) { Time.utc(2021, 1, 2) } let(:deactivation_date) { Time.utc(2022, 10, 10) } @@ -1267,7 +1269,7 @@ RSpec.describe LocationsController, type: :request do end context "when confirming deactivation" do - let(:params) { { location: { deactivation_date: Time.utc(2022, 10, 10), confirm: true } } } + let(:params) { { location: { deactivation_date: deactivation_date, confirm: true } } } it "updates existing location with valid deactivation date and renders location page" do follow_redirect! @@ -1276,6 +1278,16 @@ RSpec.describe LocationsController, type: :request do location.reload expect(location.deactivation_date).to eq(deactivation_date) end + + it "updates lettings logs with a startdate later than deactivation_date" do + lettings_log_to_deactivate.reload + expect(lettings_log_to_deactivate.location).to eq(nil) + end + + it "does not update lettings logs with a startdate earlier than deactivation_date" do + active_lettings_log.reload + expect(active_lettings_log.location).to eq(location) + end end context "when the date is not selected" do