diff --git a/app/models/derived_variables/lettings_log_variables.rb b/app/models/derived_variables/lettings_log_variables.rb index 230b2a1dc..ed10b6d72 100644 --- a/app/models/derived_variables/lettings_log_variables.rb +++ b/app/models/derived_variables/lettings_log_variables.rb @@ -176,7 +176,7 @@ module DerivedVariables::LettingsLogVariables if !form.start_year_2026_or_later? && is_supported_housing? reset_address_fields! - elsif form.start_year_2026_or_later? && location_changed? + elsif form.start_year_2026_or_later? && location_changed? && persisted? reset_address_fields! self.la = nil end diff --git a/spec/features/form/check_answers_page_lettings_logs_spec.rb b/spec/features/form/check_answers_page_lettings_logs_spec.rb index 62f9fad52..3b6bc69a3 100644 --- a/spec/features/form/check_answers_page_lettings_logs_spec.rb +++ b/spec/features/form/check_answers_page_lettings_logs_spec.rb @@ -18,6 +18,7 @@ RSpec.describe "Lettings Log Check Answers Page" do needstype: 2, scheme:, location:, + postcode_full: location.postcode, ) end let(:empty_lettings_log) do diff --git a/spec/models/lettings_log_derived_fields_spec.rb b/spec/models/lettings_log_derived_fields_spec.rb index c418c13cf..e15f6980e 100644 --- a/spec/models/lettings_log_derived_fields_spec.rb +++ b/spec/models/lettings_log_derived_fields_spec.rb @@ -1583,6 +1583,39 @@ RSpec.describe LettingsLog, type: :model do end end end + + context "when it is 2026 and the log is new, with a location assigned for the first time (e.g. bulk upload)", metadata: { year: 26 } do + let(:startdate) { collection_start_date_for_year(2026) } + let(:location_a) { create(:location) } + let(:log) { build(:lettings_log, startdate:, assigned_to: user) } + let(:la) { "E09000003" } + + around do |example| + Timecop.freeze(collection_start_date_for_year(2026)) do + Singleton.__init__(FormHandler) + example.run + end + end + + before do + allow(location_a).to receive(:lookup_postcode!).and_return(nil) + + log.assign_attributes(location: location_a, uprn:, uprn_known:, uprn_confirmed:, address_line1:, address_line2:, town_or_city:, county:, postcode_full:, la:, manual_address_entry_selected:) + end + + it "does not reset the address fields" do + expect { log.set_derived_fields! } + .to not_change { log.read_attribute(:uprn) } + .and not_change { log.read_attribute(:uprn_known) } + .and not_change { log.read_attribute(:uprn_confirmed) } + .and not_change { log.read_attribute(:address_line1) } + .and not_change { log.read_attribute(:address_line2) } + .and not_change { log.read_attribute(:town_or_city) } + .and not_change { log.read_attribute(:county) } + .and(not_change { log.read_attribute(:postcode_full) }) + .and(not_change { log.read_attribute(:la) }) + end + end end describe "address field behaviour for confidential schemes", metadata: { year: 26 } do diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index 779b7be4a..166011ecb 100644 --- a/spec/models/lettings_log_spec.rb +++ b/spec/models/lettings_log_spec.rb @@ -1965,8 +1965,8 @@ RSpec.describe LettingsLog do context "when there is a duplicate supported housing log" do let(:scheme) { create(:scheme, owning_organisation: organisation) } let(:location) { create(:location, scheme:) } - let!(:supported_housing_log) { create(:lettings_log, :duplicate, needstype: 2, location:, scheme:, owning_organisation: organisation) } - let!(:duplicate_supported_housing_log) { create(:lettings_log, :duplicate, needstype: 2, location:, scheme:, owning_organisation: organisation) } + let!(:supported_housing_log) { create(:lettings_log, :duplicate, needstype: 2, location:, scheme:, owning_organisation: organisation, postcode_full: location.postcode) } + let!(:duplicate_supported_housing_log) { create(:lettings_log, :duplicate, needstype: 2, location:, scheme:, owning_organisation: organisation, postcode_full: location.postcode) } it "returns the log as a duplicate" do expect(duplicate_sets).to contain_exactly(