From e4c6e42e3dbabb7453535b0a59655d0aa87e7044 Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire <94526761+natdeanlewissoftwire@users.noreply.github.com> Date: Thu, 21 Sep 2023 16:35:05 +0100 Subject: [PATCH] CLDC-2659 complete logs with no location (#1931) * WIP fix * feat: update status before validation to prevent issue in future * feat: reprivatise update_status! * feat: revert before_save -> before_validation * feat: add test * feat: lint * refactor: lint --- .../derived_variables/lettings_log_variables.rb | 2 +- spec/models/lettings_log_spec.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/models/derived_variables/lettings_log_variables.rb b/app/models/derived_variables/lettings_log_variables.rb index 960ecad52..09b3a5fde 100644 --- a/app/models/derived_variables/lettings_log_variables.rb +++ b/app/models/derived_variables/lettings_log_variables.rb @@ -275,7 +275,7 @@ private def reset_scheme_location! self.location = nil if scheme && scheme.locations.active_in_2_weeks.size == 1 - self.location = scheme.locations.first + self.location = scheme.locations.active_in_2_weeks.first end end diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index 74a04f9e6..b73c37f4a 100644 --- a/spec/models/lettings_log_spec.rb +++ b/spec/models/lettings_log_spec.rb @@ -2035,6 +2035,21 @@ RSpec.describe LettingsLog do allow(FormHandler.instance).to receive(:get_form).and_return(real_2021_2022_form) end + describe "when changing a log's scheme and hence calling reset_scheme_location!" do + let(:scheme) { FactoryBot.create(:scheme) } + let(:invalid_location_1) { FactoryBot.create(:location, scheme:, startdate: Time.zone.today + 3.weeks) } + let(:valid_location) { FactoryBot.create(:location, scheme:, startdate: Time.zone.yesterday) } + let(:invalid_location_2) { FactoryBot.create(:location, scheme:, startdate: Time.zone.today + 3.weeks) } + + context "when there is one valid location and many invalid locations in the new scheme" do + let(:log) { create(:lettings_log, scheme: nil, location_id: nil, startdate: Time.zone.today) } + + it "infers that the log is for the valid location" do + expect { log.update!(scheme:) }.to change(log, :location_id).from(nil).to(valid_location.id) + end + end + end + context "and a scheme with a single log is selected" do let(:scheme) { create(:scheme) } let!(:location) { create(:location, scheme:) }