diff --git a/app/services/imports/sales_logs_import_service.rb b/app/services/imports/sales_logs_import_service.rb index a764403a7..0c6962450 100644 --- a/app/services/imports/sales_logs_import_service.rb +++ b/app/services/imports/sales_logs_import_service.rb @@ -95,7 +95,7 @@ module Imports attributes["pregghb"] = 1 if string_or_nil(xml_doc, "PREGHBA") == "Yes" attributes["pregother"] = 1 if string_or_nil(xml_doc, "PREGOTHER") == "Yes" attributes["ppostcode_full"] = compose_postcode(xml_doc, "PPOSTC1", "PPOSTC2") - attributes["prevloc"] = string_or_nil(xml_doc, "Q7ONSLACODE") + attributes["prevloc"] = string_or_nil(xml_doc, "Q7ONSLACode") attributes["ppcodenk"] = previous_postcode_known(xml_doc, attributes["ppostcode_full"], attributes["prevloc"]) # Q7UNKNOWNPOSTCODE check mapping attributes["ppostc1"] = string_or_nil(xml_doc, "PPOSTC1") attributes["ppostc2"] = string_or_nil(xml_doc, "PPOSTC2") @@ -207,7 +207,7 @@ module Imports def check_status_completed(sales_log, previous_status) if previous_status.include?("submitted") && sales_log.status != "completed" - @logger.warn "sales log #{sales_log.id} is not completed" + @logger.warn "sales log #{sales_log.id} is not completed. The following answers are missing: #{missing_answers(sales_log).join(', ')}" @logger.warn "sales log with old id:#{sales_log.old_id} is incomplete but status should be complete" @logs_with_discrepancies << sales_log.old_id end @@ -398,6 +398,7 @@ module Imports def set_default_values(attributes) attributes["mscharge_known"] ||= 0 if attributes["ownershipsch"] == 3 + attributes["mscharge"] ||= 0 if attributes["ownershipsch"] == 3 attributes["armedforcesspouse"] ||= 7 attributes["hhregres"] ||= 8 attributes["disabled"] ||= 3 @@ -405,14 +406,17 @@ module Imports attributes["hb"] ||= 4 attributes["prevown"] ||= 3 attributes["savingsnk"] ||= attributes["savings"].present? ? 0 : 1 + # attributes["noint"] = 1 # not interviewed # buyer 1 characteristics attributes["age1_known"] ||= 1 attributes["sex1"] ||= "R" attributes["ethnic_group"] ||= 17 + attributes["ethnic"] ||= 17 attributes["national"] ||= 13 attributes["ecstat1"] ||= 10 attributes["income1nk"] ||= attributes["income1"].present? ? 0 : 1 + attributes["hholdcount"] ||= 0 # just for testing, might need to change # buyer 2 characteristics if attributes["jointpur"] == 1 @@ -430,5 +434,10 @@ module Imports attributes["relat#{index}"] ||= "R" end end + + def missing_answers(sales_log) + applicable_questions = sales_log.form.subsections.map { |s| s.applicable_questions(sales_log) }.flatten + applicable_questions.filter { |q| q.unanswered?(sales_log) }.map(&:id) + end end end diff --git a/spec/services/imports/sales_logs_import_service_spec.rb b/spec/services/imports/sales_logs_import_service_spec.rb index e262912a7..cbc2d4d21 100644 --- a/spec/services/imports/sales_logs_import_service_spec.rb +++ b/spec/services/imports/sales_logs_import_service_spec.rb @@ -21,8 +21,13 @@ RSpec.describe Imports::SalesLogsImportService do end before do - WebMock.stub_request(:get, /api.postcodes.io\/postcodes\/GL519EX/) - .to_return(status: 200, body: '{"status":200,"result":{"admin_district":"Westminster","codes":{"admin_district":"E09000033"}}}', headers: {}) + { "GL519EX" => "E07000078", + "SW1A2AA" => "E09000033", + "SW1A1AA" => "E09000033", + "SW147QP" => "E09000027", + "B955HZ" => "E07000221" }.each do |postcode, district_code| + WebMock.stub_request(:get, /api.postcodes.io\/postcodes\/#{postcode}/).to_return(status: 200, body: "{\"status\":200,\"result\":{\"admin_district\":\"#{district_code}\",\"codes\":{\"admin_district\":\"#{district_code}\"}}}", headers: {}) + end allow(Organisation).to receive(:find_by).and_return(nil) allow(Organisation).to receive(:find_by).with(old_visible_id: organisation.old_visible_id).and_return(organisation) @@ -154,12 +159,11 @@ RSpec.describe Imports::SalesLogsImportService do let(:sales_log_id) { shared_ownership_sales_log_id } it "successfully creates a completed shared ownership log" do - allow(logger).to receive(:warn).and_return(nil) - sales_log_service.send(:create_log, sales_log_xml) - - sales_log = SalesLog.find_by(old_id: sales_log_id) - applicable_questions = sales_log.form.subsections.map { |s| s.applicable_questions(sales_log) }.flatten - expect(applicable_questions.filter { |q| q.unanswered?(sales_log) }.map(&:id)).to be_empty + 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) end end @@ -167,12 +171,11 @@ RSpec.describe Imports::SalesLogsImportService do let(:sales_log_id) { discounted_ownership_sales_log_id } it "successfully creates a completed discounted ownership log" do - allow(logger).to receive(:warn).and_return(nil) - sales_log_service.send(:create_log, sales_log_xml) - - sales_log = SalesLog.find_by(old_id: sales_log_id) - applicable_questions = sales_log.form.subsections.map { |s| s.applicable_questions(sales_log) }.flatten - expect(applicable_questions.filter { |q| q.unanswered?(sales_log) }.map(&:id)).to be_empty + 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) end end @@ -180,12 +183,11 @@ RSpec.describe Imports::SalesLogsImportService do let(:sales_log_id) { outright_sale_sales_log_id } it "successfully creates a completed outright sale log" do - allow(logger).to receive(:warn).and_return(nil) - sales_log_service.send(:create_log, sales_log_xml) - - sales_log = SalesLog.find_by(old_id: sales_log_id) - applicable_questions = sales_log.form.subsections.map { |s| s.applicable_questions(sales_log) }.flatten - expect(applicable_questions.filter { |q| q.unanswered?(sales_log) }.map(&:id)).to be_empty + 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) end end