From 306dd14ce0d59273679494197073127a5a5c7ac0 Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 2 Oct 2023 13:05:42 +0100 Subject: [PATCH] Write wrong uprn logs to csv --- .../csv/missing_addresses_csv_service.rb | 89 ++++++++++- lib/tasks/send_missing_addresses_csv.rake | 19 ++- .../files/missing_lettings_logs_addresses.csv | 4 +- ...ing_lettings_logs_addresses_all_issues.csv | 4 + ...ttings_logs_addresses_and_town_or_city.csv | 3 - .../missing_lettings_logs_town_or_city.csv | 4 +- .../missing_lettings_logs_wrong_uprn.csv | 2 + .../files/missing_sales_logs_addresses.csv | 4 +- ...issing_sales_logs_addresses_all_issues.csv | 4 + ..._sales_logs_addresses_and_town_or_city.csv | 3 - .../files/missing_sales_logs_town_or_city.csv | 4 +- .../files/missing_sales_logs_wrong_uprn.csv | 2 + .../tasks/send_missing_addresses_csv_spec.rb | 89 +++++++++++ .../csv/missing_addresses_csv_service_spec.rb | 138 +++++++++++++++--- 14 files changed, 323 insertions(+), 46 deletions(-) create mode 100644 spec/fixtures/files/missing_lettings_logs_addresses_all_issues.csv delete mode 100644 spec/fixtures/files/missing_lettings_logs_addresses_and_town_or_city.csv create mode 100644 spec/fixtures/files/missing_lettings_logs_wrong_uprn.csv create mode 100644 spec/fixtures/files/missing_sales_logs_addresses_all_issues.csv delete mode 100644 spec/fixtures/files/missing_sales_logs_addresses_and_town_or_city.csv create mode 100644 spec/fixtures/files/missing_sales_logs_wrong_uprn.csv diff --git a/app/services/csv/missing_addresses_csv_service.rb b/app/services/csv/missing_addresses_csv_service.rb index d07f70e0e..28ca72878 100644 --- a/app/services/csv/missing_addresses_csv_service.rb +++ b/app/services/csv/missing_addresses_csv_service.rb @@ -5,12 +5,30 @@ module Csv end def create_missing_lettings_addresses_csv - logs_with_missing_addresses = @organisation.managed_lettings_logs.imported.filter_by_year(2023).where(needstype: 1, address_line1: nil, town_or_city: nil, uprn_known: [0, nil]).where.not(old_form_id: nil) - logs_with_missing_town_or_city = @organisation.managed_lettings_logs.imported.filter_by_year(2023).where(needstype: 1, town_or_city: nil, uprn_known: [0, nil]).where.not(old_form_id: nil).where.not(address_line1: nil) - return if logs_with_missing_addresses.empty? && logs_with_missing_town_or_city.empty? + logs_with_missing_addresses = @organisation.managed_lettings_logs + .imported + .filter_by_year(2023) + .where(needstype: 1, address_line1: nil, town_or_city: nil, uprn_known: [0, nil]) + .where.not(old_form_id: nil) + + logs_with_missing_town_or_city = @organisation.managed_lettings_logs + .imported + .filter_by_year(2023) + .where(needstype: 1, town_or_city: nil, uprn_known: [0, nil]) + .where.not(old_form_id: nil) + .where.not(address_line1: nil) + + logs_with_wrong_uprn = @organisation.managed_lettings_logs + .imported + .filter_by_year(2023) + .where(needstype: 1) + .where.not(uprn: nil) + .where("uprn = propcode OR uprn = tenancycode or town_or_city = 'Bristol'") + + return if logs_with_missing_addresses.empty? && logs_with_missing_town_or_city.empty? && logs_with_wrong_uprn.empty? CSV.generate(headers: true) do |csv| - csv << ["Issue type", "Lettings log ID", "Tenancy start date", "Tenant code", "Property code", "Log owner", "Owning organisation name", "Managing organisation name", "Address line 1", "Address line 2", "Town or City", "County", "Postcode", "Local authority"] + csv << ["Issue type", "Lettings log ID", "Tenancy start date", "Tenant code", "Property code", "Log owner", "Owning organisation name", "Managing organisation name", "UPRN", "Address line 1", "Address line 2", "Town or City", "County", "Postcode", "Local authority"] logs_with_missing_addresses.each do |log| csv << ["Full address required", @@ -21,6 +39,7 @@ module Csv log.created_by&.email, log.owning_organisation&.name, log.managing_organisation&.name, + log.uprn, log.address_line1, log.address_line2, log.town_or_city, @@ -38,6 +57,25 @@ module Csv log.created_by&.email, log.owning_organisation&.name, log.managing_organisation&.name, + log.uprn, + log.address_line1, + log.address_line2, + log.town_or_city, + log.county, + log.postcode_full, + log.la] + end + + logs_with_wrong_uprn.each do |log| + csv << ["UPRN issues", + log.id, + log.startdate&.to_date, + log.tenancycode, + log.propcode, + log.created_by&.email, + log.owning_organisation&.name, + log.managing_organisation&.name, + log.uprn, log.address_line1, log.address_line2, log.town_or_city, @@ -49,12 +87,29 @@ module Csv end def create_missing_sales_addresses_csv - logs_with_missing_addresses = @organisation.sales_logs.imported.filter_by_year(2023).where(address_line1: nil, town_or_city: nil, uprn_known: [0, nil]).where.not(old_form_id: nil) - logs_with_missing_town_or_city = @organisation.sales_logs.imported.filter_by_year(2023).where(town_or_city: nil, uprn_known: [0, nil]).where.not(old_form_id: nil).where.not(address_line1: nil) - return if logs_with_missing_addresses.empty? && logs_with_missing_town_or_city.empty? + logs_with_missing_addresses = @organisation.sales_logs + .imported + .filter_by_year(2023) + .where(address_line1: nil, town_or_city: nil, uprn_known: [0, nil]) + .where.not(old_form_id: nil) + + logs_with_missing_town_or_city = @organisation.sales_logs + .imported + .filter_by_year(2023) + .where(town_or_city: nil, uprn_known: [0, nil]) + .where.not(old_form_id: nil) + .where.not(address_line1: nil) + + logs_with_wrong_uprn = @organisation.sales_logs + .imported + .filter_by_year(2023) + .where.not(uprn: nil) + .where("uprn = purchid or town_or_city = 'Bristol'") + + return if logs_with_missing_addresses.empty? && logs_with_missing_town_or_city.empty? && logs_with_wrong_uprn.empty? CSV.generate(headers: true) do |csv| - csv << ["Issue type", "Sales log ID", "Sale completion date", "Purchaser code", "Log owner", "Owning organisation name", "Address line 1", "Address line 2", "Town or City", "County", "Postcode", "Local authority"] + csv << ["Issue type", "Sales log ID", "Sale completion date", "Purchaser code", "Log owner", "Owning organisation name", "UPRN", "Address line 1", "Address line 2", "Town or City", "County", "Postcode", "Local authority"] logs_with_missing_addresses.each do |log| csv << ["Full address required", @@ -63,6 +118,7 @@ module Csv log.purchid, log.created_by&.email, log.owning_organisation&.name, + log.uprn, log.address_line1, log.address_line2, log.town_or_city, @@ -78,6 +134,23 @@ module Csv log.purchid, log.created_by&.email, log.owning_organisation&.name, + log.uprn, + log.address_line1, + log.address_line2, + log.town_or_city, + log.county, + log.postcode_full, + log.la] + end + + logs_with_wrong_uprn.each do |log| + csv << ["UPRN issues", + log.id, + log.saledate&.to_date, + log.purchid, + log.created_by&.email, + log.owning_organisation&.name, + log.uprn, log.address_line1, log.address_line2, log.town_or_city, diff --git a/lib/tasks/send_missing_addresses_csv.rake b/lib/tasks/send_missing_addresses_csv.rake index 67a8c54e5..a187275e3 100644 --- a/lib/tasks/send_missing_addresses_csv.rake +++ b/lib/tasks/send_missing_addresses_csv.rake @@ -19,12 +19,21 @@ namespace :correct_addresses do .where.not(old_form_id: nil) .where.not(address_line1: nil).count - next unless logs_impacted_by_missing_address >= MISSING_ADDRESSES_THRESHOLD || logs_impacted_by_missing_town_or_city >= MISSING_ADDRESSES_THRESHOLD + logs_impacted_by_uprn_issue = organisation.managed_lettings_logs + .imported + .filter_by_year(2023) + .where(needstype: 1) + .where.not(uprn: nil) + .where("uprn = propcode OR uprn = tenancycode or town_or_city = 'Bristol'") - data_coordinators = organisation.users.where(role: 2).filter_by_active - users_to_contact = data_coordinators.any? ? data_coordinators : organisation.users.filter_by_active - EmailMissingAddressesCsvJob.perform_later(users_to_contact.map(&:id), organisation, "lettings") - Rails.logger.info("Sending missing addresses CSV for #{organisation.name} to #{users_to_contact.map(&:email).join(', ')}") + if logs_impacted_by_missing_address >= MISSING_ADDRESSES_THRESHOLD || logs_impacted_by_missing_town_or_city >= MISSING_ADDRESSES_THRESHOLD || logs_impacted_by_uprn_issue.any? + data_coordinators = organisation.users.where(role: 2).filter_by_active + users_to_contact = data_coordinators.any? ? data_coordinators : organisation.users.filter_by_active + EmailMissingAddressesCsvJob.perform_later(users_to_contact.map(&:id), organisation, "lettings") + Rails.logger.info("Sending missing addresses CSV for #{organisation.name} to #{users_to_contact.map(&:email).join(', ')}") + else + Rails.logger.info("Missing addresses below threshold for #{organisation.name}") + end end end end diff --git a/spec/fixtures/files/missing_lettings_logs_addresses.csv b/spec/fixtures/files/missing_lettings_logs_addresses.csv index 09b903bb5..243641c25 100644 --- a/spec/fixtures/files/missing_lettings_logs_addresses.csv +++ b/spec/fixtures/files/missing_lettings_logs_addresses.csv @@ -1,2 +1,2 @@ -Issue type,Lettings log ID,Tenancy start date,Tenant code,Property code,Log owner,Owning organisation name,Managing organisation name,Address line 1,Address line 2,Town or City,County,Postcode,Local authority -Full address required,{id},2023-04-05,tenancycode,propcode,testy@example.com,Address test,Address test,,,,,, +Issue type,Lettings log ID,Tenancy start date,Tenant code,Property code,Log owner,Owning organisation name,Managing organisation name,UPRN,Address line 1,Address line 2,Town or City,County,Postcode,Local authority +Full address required,{id},2023-04-05,tenancycode,propcode,testy@example.com,Address org,Address org,,,,,,, diff --git a/spec/fixtures/files/missing_lettings_logs_addresses_all_issues.csv b/spec/fixtures/files/missing_lettings_logs_addresses_all_issues.csv new file mode 100644 index 000000000..5a748ad9f --- /dev/null +++ b/spec/fixtures/files/missing_lettings_logs_addresses_all_issues.csv @@ -0,0 +1,4 @@ +Issue type,Lettings log ID,Tenancy start date,Tenant code,Property code,Log owner,Owning organisation name,Managing organisation name,UPRN,Address line 1,Address line 2,Town or City,County,Postcode,Local authority +Full address required,{id},2023-04-05,tenancycode,propcode,testy@example.com,Address org,Address org,,,,,,, +Missing town or city,{id},2023-04-05,tenancycode,propcode,testy@example.com,Address org,Address org,,existing address,,,,, +UPRN issues,{id},2023-04-05,tenancycode,propcode,testy@example.com,Address org,Address org,123,Some Place,,Bristol,,BS1 1AD, diff --git a/spec/fixtures/files/missing_lettings_logs_addresses_and_town_or_city.csv b/spec/fixtures/files/missing_lettings_logs_addresses_and_town_or_city.csv deleted file mode 100644 index cb1ad5f6d..000000000 --- a/spec/fixtures/files/missing_lettings_logs_addresses_and_town_or_city.csv +++ /dev/null @@ -1,3 +0,0 @@ -Issue type,Lettings log ID,Tenancy start date,Tenant code,Property code,Log owner,Owning organisation name,Managing organisation name,Address line 1,Address line 2,Town or City,County,Postcode,Local authority -Full address required,{id},2023-04-05,tenancycode,propcode,testy@example.com,Address test,Address test,,,,,, -Missing town or city,{id},2023-04-05,tenancycode,propcode,testy@example.com,Address test,Address test,existing address,,,,, diff --git a/spec/fixtures/files/missing_lettings_logs_town_or_city.csv b/spec/fixtures/files/missing_lettings_logs_town_or_city.csv index c97bc76cf..d5c554289 100644 --- a/spec/fixtures/files/missing_lettings_logs_town_or_city.csv +++ b/spec/fixtures/files/missing_lettings_logs_town_or_city.csv @@ -1,2 +1,2 @@ -Issue type,Lettings log ID,Tenancy start date,Tenant code,Property code,Log owner,Owning organisation name,Managing organisation name,Address line 1,Address line 2,Town or City,County,Postcode,Local authority -Missing town or city,{id},2023-04-05,tenancycode,propcode,testy@example.com,Address test,Address test,existing address,,,,, +Issue type,Lettings log ID,Tenancy start date,Tenant code,Property code,Log owner,Owning organisation name,Managing organisation name,UPRN,Address line 1,Address line 2,Town or City,County,Postcode,Local authority +Missing town or city,{id},2023-04-05,tenancycode,propcode,testy@example.com,Address org,Address org,,existing address,,,,, diff --git a/spec/fixtures/files/missing_lettings_logs_wrong_uprn.csv b/spec/fixtures/files/missing_lettings_logs_wrong_uprn.csv new file mode 100644 index 000000000..bd7d78c40 --- /dev/null +++ b/spec/fixtures/files/missing_lettings_logs_wrong_uprn.csv @@ -0,0 +1,2 @@ +Issue type,Lettings log ID,Tenancy start date,Tenant code,Property code,Log owner,Owning organisation name,Managing organisation name,UPRN,Address line 1,Address line 2,Town or City,County,Postcode,Local authority +UPRN issues,{id},2023-04-05,tenancycode,12,testy@example.com,Address org,Address org,12,Some Place,,Newcastle,,EC1N 2TD, diff --git a/spec/fixtures/files/missing_sales_logs_addresses.csv b/spec/fixtures/files/missing_sales_logs_addresses.csv index cccbc3a1a..a26fbdfe4 100644 --- a/spec/fixtures/files/missing_sales_logs_addresses.csv +++ b/spec/fixtures/files/missing_sales_logs_addresses.csv @@ -1,2 +1,2 @@ -Issue type,Sales log ID,Sale completion date,Purchaser code,Log owner,Owning organisation name,Address line 1,Address line 2,Town or City,County,Postcode,Local authority -Full address required,{id},2023-04-05,purchaser code,testy@example.com,Address test,,,,,, +Issue type,Sales log ID,Sale completion date,Purchaser code,Log owner,Owning organisation name,UPRN,Address line 1,Address line 2,Town or City,County,Postcode,Local authority +Full address required,{id},2023-04-05,purchaser code,testy@example.com,Address org,,,,,,, diff --git a/spec/fixtures/files/missing_sales_logs_addresses_all_issues.csv b/spec/fixtures/files/missing_sales_logs_addresses_all_issues.csv new file mode 100644 index 000000000..0ed0b43f1 --- /dev/null +++ b/spec/fixtures/files/missing_sales_logs_addresses_all_issues.csv @@ -0,0 +1,4 @@ +Issue type,Sales log ID,Sale completion date,Purchaser code,Log owner,Owning organisation name,UPRN,Address line 1,Address line 2,Town or City,County,Postcode,Local authority +Full address required,{id},2023-04-05,purchaser code,testy@example.com,Address org,,,,,,, +Missing town or city,{id},2023-04-05,purchaser code,testy@example.com,Address org,,existing address line 1,,,,, +UPRN issues,{id},2023-04-05,purchaser code,testy@example.com,Address org,123,Some Place,,Bristol,,BS1 1AD, diff --git a/spec/fixtures/files/missing_sales_logs_addresses_and_town_or_city.csv b/spec/fixtures/files/missing_sales_logs_addresses_and_town_or_city.csv deleted file mode 100644 index cfa6aad69..000000000 --- a/spec/fixtures/files/missing_sales_logs_addresses_and_town_or_city.csv +++ /dev/null @@ -1,3 +0,0 @@ -Issue type,Sales log ID,Sale completion date,Purchaser code,Log owner,Owning organisation name,Address line 1,Address line 2,Town or City,County,Postcode,Local authority -Full address required,{id},2023-04-05,purchaser code,testy@example.com,Address test,,,,,, -Missing town or city,{id},2023-04-05,purchaser code,testy@example.com,Address test,existing address line 1,,,,, diff --git a/spec/fixtures/files/missing_sales_logs_town_or_city.csv b/spec/fixtures/files/missing_sales_logs_town_or_city.csv index 13394d0e1..ab1913f69 100644 --- a/spec/fixtures/files/missing_sales_logs_town_or_city.csv +++ b/spec/fixtures/files/missing_sales_logs_town_or_city.csv @@ -1,2 +1,2 @@ -Issue type,Sales log ID,Sale completion date,Purchaser code,Log owner,Owning organisation name,Address line 1,Address line 2,Town or City,County,Postcode,Local authority -Missing town or city,{id},2023-04-05,purchaser code,testy@example.com,Address test,existing address line 1,,,,, +Issue type,Sales log ID,Sale completion date,Purchaser code,Log owner,Owning organisation name,UPRN,Address line 1,Address line 2,Town or City,County,Postcode,Local authority +Missing town or city,{id},2023-04-05,purchaser code,testy@example.com,Address org,,existing address line 1,,,,, diff --git a/spec/fixtures/files/missing_sales_logs_wrong_uprn.csv b/spec/fixtures/files/missing_sales_logs_wrong_uprn.csv new file mode 100644 index 000000000..3f8e6fe19 --- /dev/null +++ b/spec/fixtures/files/missing_sales_logs_wrong_uprn.csv @@ -0,0 +1,2 @@ +Issue type,Sales log ID,Sale completion date,Purchaser code,Log owner,Owning organisation name,UPRN,Address line 1,Address line 2,Town or City,County,Postcode,Local authority +UPRN issues,{id},2023-04-05,12,testy@example.com,Address org,12,Some Place,,Newcastle,,EC1N 2TD, diff --git a/spec/lib/tasks/send_missing_addresses_csv_spec.rb b/spec/lib/tasks/send_missing_addresses_csv_spec.rb index dbba071b5..ccc975d97 100644 --- a/spec/lib/tasks/send_missing_addresses_csv_spec.rb +++ b/spec/lib/tasks/send_missing_addresses_csv_spec.rb @@ -10,6 +10,36 @@ RSpec.describe "correct_addresses" do Rake.application.rake_require("tasks/send_missing_addresses_csv") Rake::Task.define_task(:environment) task.reenable + + body_1 = { + results: [ + { + DPA: { + "POSTCODE": "BS1 1AD", + "POST_TOWN": "Bristol", + "ORGANISATION_NAME": "Some place", + }, + }, + ], + }.to_json + + body_2 = { + results: [ + { + DPA: { + "POSTCODE": "EC1N 2TD", + "POST_TOWN": "Newcastle", + "ORGANISATION_NAME": "Some place", + }, + }, + ], + }.to_json + + stub_request(:get, "https://api.os.uk/search/places/v1/uprn?key=OS_DATA_KEY&uprn=123") + .to_return(status: 200, body: body_1, headers: {}) + + stub_request(:get, "https://api.os.uk/search/places/v1/uprn?key=OS_DATA_KEY&uprn=12") + .to_return(status: 200, body: body_2, headers: {}) end context "when the rake task is run" do @@ -118,6 +148,65 @@ RSpec.describe "correct_addresses" do expect { task.invoke }.not_to enqueue_job(EmailMissingAddressesCsvJob) end end + + context "when org has more than 5 wrong uprn and data coordinators" do + let!(:data_coordinator) { create(:user, :data_coordinator, organisation:, email: "data_coordinator1@example.com") } + let!(:data_coordinator2) { create(:user, :data_coordinator, organisation:, email: "data_coordinator2@example.com") } + + before do + create(:user, :data_provider, organisation:, email: "data_provider1@example.com") + create_list(:lettings_log, 7, :imported, startdate: Time.zone.local(2023, 9, 9), uprn: "123", town_or_city: "Bristol", needstype: 1, owning_organisation: organisation, managing_organisation: organisation, created_by: organisation.users.first) + end + + it "enqueues the job with correct organisations" do + expect { task.invoke }.to enqueue_job(EmailMissingAddressesCsvJob).with(include(data_coordinator.id, data_coordinator2.id), organisation, "lettings") + end + + it "prints out the jobs enqueued" do + expect(Rails.logger).to receive(:info).with(nil) + expect(Rails.logger).to receive(:info).with("Sending missing addresses CSV for test organisation to data_coordinator1@example.com, data_coordinator2@example.com") + task.invoke + end + end + + context "when org has 5 wrong uprn and data providers only" do + let!(:data_provider) { create(:user, :data_provider, organisation:, email: "data_provider3@example.com") } + let!(:data_provider2) { create(:user, :data_provider, organisation:, email: "data_provider4@example.com") } + + before do + create_list(:lettings_log, 5, :imported, startdate: Time.zone.local(2023, 9, 9), uprn: "12", propcode: "12", needstype: 1, owning_organisation: organisation, managing_organisation: organisation, created_by: organisation.users.first) + end + + it "enqueues the job with correct organisations" do + expect { task.invoke }.to enqueue_job(EmailMissingAddressesCsvJob).with(include(data_provider.id, data_provider2.id), organisation, "lettings") + end + + it "prints out the jobs enqueued" do + expect(Rails.logger).to receive(:info).with(nil) + expect(Rails.logger).to receive(:info).with("Sending missing addresses CSV for test organisation to data_provider3@example.com, data_provider4@example.com") + task.invoke + end + end + + context "when org has less than 5 wrong uprn" do + let!(:data_provider) { create(:user, :data_provider, organisation:, email: "data_provider3@example.com") } + let!(:data_provider2) { create(:user, :data_provider, organisation:, email: "data_provider4@example.com") } + + before do + create_list(:lettings_log, 3, :imported, startdate: Time.zone.local(2023, 9, 9), uprn: "123", town_or_city: "Bristol", needstype: 1, owning_organisation: organisation, managing_organisation: organisation, created_by: organisation.users.first) + create_list(:lettings_log, 2, :imported, startdate: Time.zone.local(2023, 9, 9), uprn: "12", tenancycode: "12", needstype: 1, owning_organisation: organisation, managing_organisation: organisation, created_by: organisation.users.first) + end + + it "enqueues the job with correct organisations" do + expect { task.invoke }.to enqueue_job(EmailMissingAddressesCsvJob).with(include(data_provider.id, data_provider2.id), organisation, "lettings") + end + + it "prints out the jobs enqueued" do + expect(Rails.logger).to receive(:info).with(nil) + expect(Rails.logger).to receive(:info).with("Sending missing addresses CSV for test organisation to data_provider3@example.com, data_provider4@example.com") + task.invoke + end + end end end end diff --git a/spec/services/csv/missing_addresses_csv_service_spec.rb b/spec/services/csv/missing_addresses_csv_service_spec.rb index f7c76d933..97eba9853 100644 --- a/spec/services/csv/missing_addresses_csv_service_spec.rb +++ b/spec/services/csv/missing_addresses_csv_service_spec.rb @@ -1,10 +1,42 @@ require "rails_helper" RSpec.describe Csv::MissingAddressesCsvService do - let(:organisation) { create(:organisation, name: "Address test") } + let(:organisation) { create(:organisation, name: "Address org") } let(:user) { create(:user, organisation:, email: "testy@example.com") } let(:service) { described_class.new(organisation) } + before do + body_1 = { + results: [ + { + DPA: { + "POSTCODE": "BS1 1AD", + "POST_TOWN": "Bristol", + "ORGANISATION_NAME": "Some place", + }, + }, + ], + }.to_json + + body_2 = { + results: [ + { + DPA: { + "POSTCODE": "EC1N 2TD", + "POST_TOWN": "Newcastle", + "ORGANISATION_NAME": "Some place", + }, + }, + ], + }.to_json + + stub_request(:get, "https://api.os.uk/search/places/v1/uprn?key=OS_DATA_KEY&uprn=123") + .to_return(status: 200, body: body_1, headers: {}) + + stub_request(:get, "https://api.os.uk/search/places/v1/uprn?key=OS_DATA_KEY&uprn=12") + .to_return(status: 200, body: body_2, headers: {}) + end + def replace_entity_ids(lettings_log, export_template) export_template.sub!(/\{id\}/, lettings_log.id.to_s) end @@ -26,7 +58,7 @@ RSpec.describe Csv::MissingAddressesCsvService do uprn_known: 0) end - let!(:lettings_log_2) do + let!(:lettings_log_missing_town) do create(:lettings_log, tenancycode: "tenancycode", propcode: "propcode", @@ -42,10 +74,25 @@ RSpec.describe Csv::MissingAddressesCsvService do uprn_known: 0) end - context "when the organisation has logs with missing addresses and logs with missing town or city" do + let!(:lettings_log_wrong_uprn) do + create(:lettings_log, + tenancycode: "tenancycode", + propcode: "propcode", + startdate: Time.zone.local(2023, 4, 5), + created_by: user, + owning_organisation: organisation, + managing_organisation: organisation, + uprn: "123", + uprn_known: 1, + old_id: "oldest_id", + needstype: 1) + end + + context "when the organisation has logs with missing addresses, missing town or city and wrong uprn" do it "returns a csv with relevant logs" do - expected_content = replace_entity_ids(lettings_log, File.open("spec/fixtures/files/missing_lettings_logs_addresses_and_town_or_city.csv").read) - expected_content = replace_entity_ids(lettings_log_2, expected_content) + expected_content = replace_entity_ids(lettings_log, File.open("spec/fixtures/files/missing_lettings_logs_addresses_all_issues.csv").read) + expected_content = replace_entity_ids(lettings_log_missing_town, expected_content) + expected_content = replace_entity_ids(lettings_log_wrong_uprn, expected_content) csv = service.create_missing_lettings_addresses_csv expect(csv).to eq(expected_content) end @@ -53,7 +100,8 @@ RSpec.describe Csv::MissingAddressesCsvService do context "when the organisation has logs with missing addresses only" do before do - lettings_log_2.update!(town_or_city: "towncity") + lettings_log_missing_town.update!(town_or_city: "towncity") + lettings_log_wrong_uprn.update!(uprn: "12") end it "returns a csv with relevant logs" do @@ -66,10 +114,25 @@ RSpec.describe Csv::MissingAddressesCsvService do context "when the organisation has logs with missing town or city only" do before do lettings_log.update!(address_line1: "existing address", town_or_city: "towncity") + lettings_log_wrong_uprn.update!(uprn: "12") end it "returns a csv with relevant logs" do - expected_content = replace_entity_ids(lettings_log_2, File.open("spec/fixtures/files/missing_lettings_logs_town_or_city.csv").read) + expected_content = replace_entity_ids(lettings_log_missing_town, File.open("spec/fixtures/files/missing_lettings_logs_town_or_city.csv").read) + csv = service.create_missing_lettings_addresses_csv + expect(csv).to eq(expected_content) + end + end + + context "when the organisation has logs with wrong uprn only" do + before do + lettings_log.update!(address_line1: "existing address", town_or_city: "towncity") + lettings_log_missing_town.update!(town_or_city: "towncity") + lettings_log_wrong_uprn.update!(uprn: "12", propcode: "12") + end + + it "returns a csv with relevant logs" do + expected_content = replace_entity_ids(lettings_log_wrong_uprn, File.open("spec/fixtures/files/missing_lettings_logs_wrong_uprn.csv").read) csv = service.create_missing_lettings_addresses_csv expect(csv).to eq(expected_content) end @@ -78,7 +141,8 @@ RSpec.describe Csv::MissingAddressesCsvService do context "when the organisation only has supported housing logs with missing addresses or town or city" do before do lettings_log.update!(needstype: 2) - lettings_log_2.update!(needstype: 2) + lettings_log_missing_town.update!(needstype: 2) + lettings_log_wrong_uprn.update!(needstype: 2) end it "returns nil" do @@ -89,7 +153,8 @@ RSpec.describe Csv::MissingAddressesCsvService do context "when the organisation only has logs with missing addresses or town or city from 2022" do before do lettings_log.update!(startdate: Time.zone.local(2022, 4, 5)) - lettings_log_2.update!(startdate: Time.zone.local(2022, 4, 5)) + lettings_log_missing_town.update!(startdate: Time.zone.local(2022, 4, 5)) + lettings_log_wrong_uprn.update!(startdate: Time.zone.local(2022, 4, 5)) end it "returns nil" do @@ -97,10 +162,11 @@ RSpec.describe Csv::MissingAddressesCsvService do end end - context "when the organisation has any address and town or city fields filled in" do + context "when the organisation has any address and town or city fields filled in or correct uprn" do before do lettings_log.update!(address_line1: "address_line1", town_or_city: "towncity") - lettings_log_2.update!(address_line1: "address_line1", town_or_city: "towncity") + lettings_log_missing_town.update!(address_line1: "address_line1", town_or_city: "towncity") + lettings_log_wrong_uprn.update!(uprn: "12") end it "returns nil" do @@ -123,7 +189,7 @@ RSpec.describe Csv::MissingAddressesCsvService do uprn_known: 0) end - let!(:sales_log_2) do + let!(:sales_log_missing_town) do create(:sales_log, purchid: "purchaser code", saledate: Time.zone.local(2023, 4, 5), @@ -136,10 +202,26 @@ RSpec.describe Csv::MissingAddressesCsvService do uprn_known: 0) end - context "when the organisation has logs with missing addresses and town or city" do + let!(:sales_log_wrong_uprn) do + create(:sales_log, + :completed, + purchid: "purchaser code", + saledate: Time.zone.local(2023, 4, 5), + created_by: user, + owning_organisation: organisation, + uprn: "123", + town_or_city: "Bristol", + old_id: "oldest_id", + uprn_known: 1, + uprn_confirmed: 1, + la: nil) + end + + context "when the organisation has logs with missing addresses, town or city and wrong uprn" do it "returns a csv with relevant logs" do - expected_content = replace_entity_ids(sales_log, File.open("spec/fixtures/files/missing_sales_logs_addresses_and_town_or_city.csv").read) - expected_content = replace_entity_ids(sales_log_2, expected_content) + expected_content = replace_entity_ids(sales_log, File.open("spec/fixtures/files/missing_sales_logs_addresses_all_issues.csv").read) + expected_content = replace_entity_ids(sales_log_missing_town, expected_content) + expected_content = replace_entity_ids(sales_log_wrong_uprn, expected_content) csv = service.create_missing_sales_addresses_csv expect(csv).to eq(expected_content) end @@ -147,7 +229,8 @@ RSpec.describe Csv::MissingAddressesCsvService do context "when the organisation has logs with missing addresses" do before do - sales_log_2.update!(town_or_city: "towncity") + sales_log_missing_town.update!(town_or_city: "towncity") + sales_log_wrong_uprn.update!(uprn: "12") end it "returns a csv with relevant logs" do @@ -160,10 +243,25 @@ RSpec.describe Csv::MissingAddressesCsvService do context "when the organisation has logs with missing town_or_city only" do before do sales_log.update!(address_line1: "address", town_or_city: "towncity") + sales_log_wrong_uprn.update!(uprn: "12") + end + + it "returns a csv with relevant logs" do + expected_content = replace_entity_ids(sales_log_missing_town, File.open("spec/fixtures/files/missing_sales_logs_town_or_city.csv").read) + csv = service.create_missing_sales_addresses_csv + expect(csv).to eq(expected_content) + end + end + + context "when the organisation has logs with wrong uprn only" do + before do + sales_log.update!(address_line1: "address", town_or_city: "towncity") + sales_log_missing_town.update!(town_or_city: "towncity") + sales_log_wrong_uprn.update!(uprn: "12", purchid: "12") end it "returns a csv with relevant logs" do - expected_content = replace_entity_ids(sales_log_2, File.open("spec/fixtures/files/missing_sales_logs_town_or_city.csv").read) + expected_content = replace_entity_ids(sales_log_wrong_uprn, File.open("spec/fixtures/files/missing_sales_logs_wrong_uprn.csv").read) csv = service.create_missing_sales_addresses_csv expect(csv).to eq(expected_content) end @@ -172,7 +270,8 @@ RSpec.describe Csv::MissingAddressesCsvService do context "when the organisation only has logs with missing addresses from 2022" do before do sales_log.update!(saledate: Time.zone.local(2022, 4, 5)) - sales_log_2.update!(saledate: Time.zone.local(2022, 4, 5)) + sales_log_missing_town.update!(saledate: Time.zone.local(2022, 4, 5)) + sales_log_wrong_uprn.update!(saledate: Time.zone.local(2022, 4, 5)) end it "returns nil" do @@ -183,7 +282,8 @@ RSpec.describe Csv::MissingAddressesCsvService do context "when the organisation has address fields filled in" do before do sales_log.update!(town_or_city: "town", address_line1: "line1") - sales_log_2.update!(town_or_city: "town") + sales_log_missing_town.update!(town_or_city: "town") + sales_log_wrong_uprn.update!(uprn: "12") end it "returns nil" do