Browse Source

Write wrong uprn logs to csv

pull/1953/head
Kat 3 years ago
parent
commit
306dd14ce0
  1. 89
      app/services/csv/missing_addresses_csv_service.rb
  2. 19
      lib/tasks/send_missing_addresses_csv.rake
  3. 4
      spec/fixtures/files/missing_lettings_logs_addresses.csv
  4. 4
      spec/fixtures/files/missing_lettings_logs_addresses_all_issues.csv
  5. 3
      spec/fixtures/files/missing_lettings_logs_addresses_and_town_or_city.csv
  6. 4
      spec/fixtures/files/missing_lettings_logs_town_or_city.csv
  7. 2
      spec/fixtures/files/missing_lettings_logs_wrong_uprn.csv
  8. 4
      spec/fixtures/files/missing_sales_logs_addresses.csv
  9. 4
      spec/fixtures/files/missing_sales_logs_addresses_all_issues.csv
  10. 3
      spec/fixtures/files/missing_sales_logs_addresses_and_town_or_city.csv
  11. 4
      spec/fixtures/files/missing_sales_logs_town_or_city.csv
  12. 2
      spec/fixtures/files/missing_sales_logs_wrong_uprn.csv
  13. 89
      spec/lib/tasks/send_missing_addresses_csv_spec.rb
  14. 138
      spec/services/csv/missing_addresses_csv_service_spec.rb

89
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,

19
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

4
spec/fixtures/files/missing_lettings_logs_addresses.csv vendored

@ -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,,,,,,,

1 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
2 Full address required {id} 2023-04-05 tenancycode propcode testy@example.com Address test Address org Address test Address org

4
spec/fixtures/files/missing_lettings_logs_addresses_all_issues.csv vendored

@ -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,
1 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
2 Full address required {id} 2023-04-05 tenancycode propcode testy@example.com Address org Address org
3 Missing town or city {id} 2023-04-05 tenancycode propcode testy@example.com Address org Address org existing address
4 UPRN issues {id} 2023-04-05 tenancycode propcode testy@example.com Address org Address org 123 Some Place Bristol BS1 1AD

3
spec/fixtures/files/missing_lettings_logs_addresses_and_town_or_city.csv vendored

@ -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,,,,,
1 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
2 Full address required {id} 2023-04-05 tenancycode propcode testy@example.com Address test Address test
3 Missing town or city {id} 2023-04-05 tenancycode propcode testy@example.com Address test Address test existing address

4
spec/fixtures/files/missing_lettings_logs_town_or_city.csv vendored

@ -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,,,,,

1 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
2 Missing town or city {id} 2023-04-05 tenancycode propcode testy@example.com Address test Address org Address test Address org existing address

2
spec/fixtures/files/missing_lettings_logs_wrong_uprn.csv vendored

@ -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,
1 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
2 UPRN issues {id} 2023-04-05 tenancycode 12 testy@example.com Address org Address org 12 Some Place Newcastle EC1N 2TD

4
spec/fixtures/files/missing_sales_logs_addresses.csv vendored

@ -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,,,,,,,

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
2 Full address required {id} 2023-04-05 purchaser code testy@example.com Address test Address org

4
spec/fixtures/files/missing_sales_logs_addresses_all_issues.csv vendored

@ -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,
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
2 Full address required {id} 2023-04-05 purchaser code testy@example.com Address org
3 Missing town or city {id} 2023-04-05 purchaser code testy@example.com Address org existing address line 1
4 UPRN issues {id} 2023-04-05 purchaser code testy@example.com Address org 123 Some Place Bristol BS1 1AD

3
spec/fixtures/files/missing_sales_logs_addresses_and_town_or_city.csv vendored

@ -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,,,,,
1 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
2 Full address required {id} 2023-04-05 purchaser code testy@example.com Address test
3 Missing town or city {id} 2023-04-05 purchaser code testy@example.com Address test existing address line 1

4
spec/fixtures/files/missing_sales_logs_town_or_city.csv vendored

@ -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,,,,,

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
2 Missing town or city {id} 2023-04-05 purchaser code testy@example.com Address test Address org existing address line 1

2
spec/fixtures/files/missing_sales_logs_wrong_uprn.csv vendored

@ -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,
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
2 UPRN issues {id} 2023-04-05 12 testy@example.com Address org 12 Some Place Newcastle EC1N 2TD

89
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

138
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

Loading…
Cancel
Save