diff --git a/app/services/imports/sales_logs_import_service.rb b/app/services/imports/sales_logs_import_service.rb index 7d10f52ca..c57f2d16a 100644 --- a/app/services/imports/sales_logs_import_service.rb +++ b/app/services/imports/sales_logs_import_service.rb @@ -1,5 +1,7 @@ module Imports class SalesLogsImportService < LogsImportService + include CollectionTimeHelper + def initialize(storage_service, logger = Rails.logger, allow_updates: false) @logs_with_discrepancies = Set.new @logs_overridden = Set.new @@ -124,7 +126,7 @@ module Imports attributes["mortgagelenderother"] = mortgage_lender_other(xml_doc, attributes) attributes["postcode_full"] = parse_postcode(string_or_nil(xml_doc, "Q14Postcode")) attributes["pcodenk"] = 0 if attributes["postcode_full"].present? # known if given - attributes["soctenant"] = 0 if attributes["ownershipsch"] == 1 + attributes["soctenant"] = 0 if set_soctenant_fields?(attributes) attributes["previous_la_known"] = 1 if attributes["prevloc"].present? if attributes["la"].present? @@ -584,6 +586,12 @@ module Imports end end + def set_soctenant_fields?(attributes) + return false if attributes["ownershipsch"] != 1 + + %w[socprevten frombeds fromprop].any? { |field| attributes[field].present? } || collection_start_year_for_date(attributes["saledate"]) < 2023 + end + def set_default_values(attributes) attributes["armedforcesspouse"] ||= 7 attributes["hhregres"] ||= 8 @@ -600,8 +608,8 @@ module Imports attributes["pcodenk"] ||= 1 attributes["prevten"] ||= 0 attributes["extrabor"] ||= 3 if attributes["mortgageused"] == 1 - attributes["socprevten"] ||= 10 if attributes["ownershipsch"] == 1 - attributes["fromprop"] ||= 0 if attributes["ownershipsch"] == 1 + attributes["socprevten"] ||= 10 if set_soctenant_fields?(attributes) + attributes["fromprop"] ||= 0 if set_soctenant_fields?(attributes) attributes["mortgagelender"] ||= 0 if attributes["mortgageused"] == 1 # buyer 1 characteristics diff --git a/spec/services/imports/sales_logs_import_service_spec.rb b/spec/services/imports/sales_logs_import_service_spec.rb index 6d067dbd4..615201f3a 100644 --- a/spec/services/imports/sales_logs_import_service_spec.rb +++ b/spec/services/imports/sales_logs_import_service_spec.rb @@ -409,6 +409,42 @@ RSpec.describe Imports::SalesLogsImportService do sales_log = SalesLog.find_by(old_id: sales_log_id) expect(sales_log.proplen_asked).to eq(1) end + + context "when setting soctenant fields" do + it "does not set soctenant value if none of the soctenant questions are answered" do + sales_log_xml.at_xpath("//xmlns:Q20Bedrooms").content = nil + sales_log_xml.at_xpath("//xmlns:PrevRentType").content = nil + sales_log_xml.at_xpath("//xmlns:Q21PropertyType").content = nil + + expect(logger).not_to receive(:error) + expect(logger).not_to receive(:warn) + expect(logger).not_to receive(:info) + expect { sales_log_service.send(:create_log, sales_log_xml) } + .to change(SalesLog, :count).by(1) + sales_log = SalesLog.find_by(old_id: sales_log_id) + expect(sales_log.soctenant).to eq(nil) + expect(sales_log.frombeds).to eq(nil) + expect(sales_log.fromprop).to eq(nil) + expect(sales_log.socprevten).to eq(nil) + end + + it "sets soctenant to don't know if any of the soctenant questions are answered" do + sales_log_xml.at_xpath("//xmlns:Q20Bedrooms").content = "2" + sales_log_xml.at_xpath("//xmlns:PrevRentType").content = nil + sales_log_xml.at_xpath("//xmlns:Q21PropertyType").content = nil + + expect(logger).not_to receive(:error) + expect(logger).not_to receive(:warn) + expect(logger).not_to receive(:info) + expect { sales_log_service.send(:create_log, sales_log_xml) } + .to change(SalesLog, :count).by(1) + sales_log = SalesLog.find_by(old_id: sales_log_id) + expect(sales_log.soctenant).to eq(0) + expect(sales_log.frombeds).to eq(2) + expect(sales_log.fromprop).to eq(0) + expect(sales_log.socprevten).to eq(10) + end + end end context "with discounted sale type" do @@ -2091,6 +2127,7 @@ RSpec.describe Imports::SalesLogsImportService do sales_log = SalesLog.find_by(old_id: sales_log_id) expect(sales_log&.fromprop).to be(0) + expect(sales_log&.soctenant).to be(0) end end @@ -2107,6 +2144,7 @@ RSpec.describe Imports::SalesLogsImportService do sales_log = SalesLog.find_by(old_id: sales_log_id) expect(sales_log&.socprevten).to be(10) + expect(sales_log&.soctenant).to be(0) end end