Browse Source

Put all log in the same csv

pull/1953/head
Kat 3 years ago
parent
commit
5c7640cdbb
  1. 12
      app/jobs/email_missing_addresses_csv_job.rb
  2. 4
      app/mailers/csv_download_mailer.rb
  3. 22
      app/services/csv/missing_addresses_csv_service.rb
  4. 27
      lib/tasks/send_missing_addresses_csv.rake
  5. 3
      spec/fixtures/files/missing_lettings_logs_addresses_and_town_or_city.csv
  6. 3
      spec/fixtures/files/missing_sales_logs_addresses_and_town_or_city.csv
  7. 58
      spec/jobs/email_missing_addresses_csv_job_spec.rb
  8. 31
      spec/lib/tasks/send_missing_addresses_csv_spec.rb
  9. 167
      spec/services/csv/missing_addresses_csv_service_spec.rb

12
app/jobs/email_missing_addresses_csv_job.rb

@ -3,16 +3,16 @@ class EmailMissingAddressesCsvJob < ApplicationJob
BYTE_ORDER_MARK = "\uFEFF".freeze # Required to ensure Excel always reads CSV as UTF-8 BYTE_ORDER_MARK = "\uFEFF".freeze # Required to ensure Excel always reads CSV as UTF-8
def perform(user_ids, organisation, log_type, template_type) def perform(user_ids, organisation, log_type)
csv_service = Csv::MissingAddressesCsvService.new(organisation:) csv_service = Csv::MissingAddressesCsvService.new(organisation:)
case log_type case log_type
when "lettings" when "lettings"
csv_string = template_type == "town-or-city" ? csv_service.create_missing_lettings_town_or_city_csv : csv_service.create_missing_lettings_addresses_csv csv_string = csv_service.create_missing_lettings_addresses_csv
filename = "#{['missing-lettings-logs', template_type, organisation.name, Time.zone.now].compact.join('-')}.csv" filename = "#{['missing-lettings-logs-addresses', organisation.name, Time.zone.now].compact.join('-')}.csv"
email_method = :send_missing_lettings_addresses_csv_download_mail email_method = :send_missing_lettings_addresses_csv_download_mail
when "sales" when "sales"
csv_string = template_type == "town-or-city" ? csv_service.create_missing_sales_town_or_city_csv : csv_service.create_missing_sales_addresses_csv csv_string = csv_service.create_missing_sales_addresses_csv
filename = "#{['missing-sales-logs', template_type, organisation.name, Time.zone.now].compact.join('-')}.csv" filename = "#{['missing-sales-logs-addresses', organisation.name, Time.zone.now].compact.join('-')}.csv"
email_method = :send_missing_sales_addresses_csv_download_mail email_method = :send_missing_sales_addresses_csv_download_mail
end end
@ -25,7 +25,7 @@ class EmailMissingAddressesCsvJob < ApplicationJob
user = User.find(id) user = User.find(id)
next if user.blank? next if user.blank?
CsvDownloadMailer.new.send(email_method, user, url, template_type) CsvDownloadMailer.new.send(email_method, user, url)
end end
end end
end end

4
app/mailers/csv_download_mailer.rb

@ -9,7 +9,7 @@ class CsvDownloadMailer < NotifyMailer
) )
end end
def send_missing_lettings_addresses_csv_download_mail(user, link, template_type); end def send_missing_lettings_addresses_csv_download_mail(user, link); end
def send_missing_sales_addresses_csv_download_mail(user, link, template_type); end def send_missing_sales_addresses_csv_download_mail(user, link); end
end end

22
app/services/csv/missing_addresses_csv_service.rb

@ -6,30 +6,18 @@ module Csv
def create_missing_lettings_addresses_csv 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_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)
return if logs_with_missing_addresses.empty? 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?
generate_missing_lettings_addresses_csv(logs_with_missing_addresses) generate_missing_lettings_addresses_csv(logs_with_missing_addresses + logs_with_missing_town_or_city)
end end
def create_missing_sales_addresses_csv 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_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)
return if logs_with_missing_addresses.empty?
generate_missing_sales_addresses_csv(logs_with_missing_addresses)
end
def create_missing_lettings_town_or_city_csv
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_town_or_city.empty?
generate_missing_lettings_addresses_csv(logs_with_missing_town_or_city)
end
def create_missing_sales_town_or_city_csv
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_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_town_or_city.empty? return if logs_with_missing_addresses.empty? && logs_with_missing_town_or_city.empty?
generate_missing_sales_addresses_csv(logs_with_missing_town_or_city) generate_missing_sales_addresses_csv(logs_with_missing_addresses + logs_with_missing_town_or_city)
end end
private private

27
lib/tasks/send_missing_addresses_csv.rake

@ -6,36 +6,25 @@ namespace :correct_addresses do
desc "Send missing addresses csv" desc "Send missing addresses csv"
task :send_missing_addresses_csv, %i[] => :environment do |_task, _args| task :send_missing_addresses_csv, %i[] => :environment do |_task, _args|
Organisation.all.each do |organisation| Organisation.all.each do |organisation|
impacted_logs = organisation.managed_lettings_logs logs_impacted_by_missing_address = organisation.managed_lettings_logs
.imported .imported
.filter_by_year(2023) .filter_by_year(2023)
.where(needstype: 1, address_line1: nil, town_or_city: nil, uprn_known: [0, nil]) .where(needstype: 1, address_line1: nil, town_or_city: nil, uprn_known: [0, nil])
.where.not(old_form_id: nil) .where.not(old_form_id: nil).count
next unless impacted_logs.count >= MISSING_ADDRESSES_THRESHOLD
data_coordinators = organisation.users.where(role: 2).filter_by_active logs_impacted_by_missing_town_or_city = organisation.managed_lettings_logs
users_to_contact = data_coordinators.any? ? data_coordinators : organisation.users.filter_by_active
EmailMissingAddressesCsvJob.perform_later(users_to_contact.map(&:id), organisation, "lettings", "addresses")
Rails.logger.info("Sending missing addresses CSV for #{organisation.name} to #{users_to_contact.map(&:email).join(', ')}")
end
end
desc "Send missing town or city csv"
task :send_missing_town_or_city_csv, %i[] => :environment do |_task, _args|
Organisation.all.each do |organisation|
impacted_logs = organisation.managed_lettings_logs
.imported .imported
.filter_by_year(2023) .filter_by_year(2023)
.where(needstype: 1, town_or_city: nil, uprn_known: [0, nil]) .where(needstype: 1, town_or_city: nil, uprn_known: [0, nil])
.where.not(old_form_id: nil) .where.not(old_form_id: nil)
.where.not(address_line1: nil) .where.not(address_line1: nil).count
next unless impacted_logs.count >= MISSING_ADDRESSES_THRESHOLD
next unless logs_impacted_by_missing_address >= MISSING_ADDRESSES_THRESHOLD || logs_impacted_by_missing_town_or_city >= MISSING_ADDRESSES_THRESHOLD
data_coordinators = organisation.users.where(role: 2).filter_by_active data_coordinators = organisation.users.where(role: 2).filter_by_active
users_to_contact = data_coordinators.any? ? data_coordinators : organisation.users.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", "town-or-city") EmailMissingAddressesCsvJob.perform_later(users_to_contact.map(&:id), organisation, "lettings")
Rails.logger.info("Sending missing town or city CSV for #{organisation.name} to #{users_to_contact.map(&:email).join(', ')}") Rails.logger.info("Sending missing addresses CSV for #{organisation.name} to #{users_to_contact.map(&:email).join(', ')}")
end end
end end
end end

3
spec/fixtures/files/missing_lettings_logs_addresses_and_town_or_city.csv vendored

@ -0,0 +1,3 @@
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
{id},2023-04-05,tenancycode,propcode,testy@example.com,Address test,Address test,,,,,,
{id},2023-04-05,tenancycode,propcode,testy@example.com,Address test,Address test,existing address,,,,,
1 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 {id} 2023-04-05 tenancycode propcode testy@example.com Address test Address test
3 {id} 2023-04-05 tenancycode propcode testy@example.com Address test Address test existing address

3
spec/fixtures/files/missing_sales_logs_addresses_and_town_or_city.csv vendored

@ -0,0 +1,3 @@
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
{id},2023-04-05,purchaser code,testy@example.com,Address test,,,,,,
{id},2023-04-05,purchaser code,testy@example.com,Address test,existing address line 1,,,,,
1 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 {id} 2023-04-05 purchaser code testy@example.com Address test
3 {id} 2023-04-05 purchaser code testy@example.com Address test existing address line 1

58
spec/jobs/email_missing_addresses_csv_job_spec.rb

@ -31,76 +31,38 @@ describe EmailMissingAddressesCsvJob do
context "when sending missing lettings logs csv" do context "when sending missing lettings logs csv" do
it "uses an appropriate filename in S3" do it "uses an appropriate filename in S3" do
expect(storage_service).to receive(:write_file).with(/missing-lettings-logs-addresses-#{organisation.name}-.*\.csv/, anything) expect(storage_service).to receive(:write_file).with(/missing-lettings-logs-addresses-#{organisation.name}-.*\.csv/, anything)
job.perform(users.map(&:id), organisation, "lettings", "addresses") job.perform(users.map(&:id), organisation, "lettings")
end end
it "creates a MissingAddressesCsvService with the correct organisation and calls create missing lettings logs adresses csv" do it "creates a MissingAddressesCsvService with the correct organisation and calls create missing lettings logs adresses csv" do
expect(Csv::MissingAddressesCsvService).to receive(:new).with(organisation:) expect(Csv::MissingAddressesCsvService).to receive(:new).with(organisation:)
expect(missing_addresses_csv_service).to receive(:create_missing_lettings_addresses_csv) expect(missing_addresses_csv_service).to receive(:create_missing_lettings_addresses_csv)
job.perform(users.map(&:id), organisation, "lettings", "addresses") job.perform(users.map(&:id), organisation, "lettings")
end end
it "sends emails to all the provided users" do it "sends emails to all the provided users" do
expect(mailer).to receive(:send_missing_lettings_addresses_csv_download_mail).with(users[0], test_url, "addresses") expect(mailer).to receive(:send_missing_lettings_addresses_csv_download_mail).with(users[0], test_url)
expect(mailer).to receive(:send_missing_lettings_addresses_csv_download_mail).with(users[1], test_url, "addresses") expect(mailer).to receive(:send_missing_lettings_addresses_csv_download_mail).with(users[1], test_url)
job.perform(users.map(&:id), organisation, "lettings", "addresses") job.perform(users.map(&:id), organisation, "lettings")
end end
end end
context "when sending missing sales logs csv" do context "when sending missing sales logs csv" do
it "uses an appropriate filename in S3" do it "uses an appropriate filename in S3" do
expect(storage_service).to receive(:write_file).with(/missing-sales-logs-addresses-#{organisation.name}-.*\.csv/, anything) expect(storage_service).to receive(:write_file).with(/missing-sales-logs-addresses-#{organisation.name}-.*\.csv/, anything)
job.perform(users.map(&:id), organisation, "sales", "addresses") job.perform(users.map(&:id), organisation, "sales")
end end
it "creates a MissingAddressesCsvService with the correct organisation and calls create missing sales logs adresses csv" do it "creates a MissingAddressesCsvService with the correct organisation and calls create missing sales logs adresses csv" do
expect(Csv::MissingAddressesCsvService).to receive(:new).with(organisation:) expect(Csv::MissingAddressesCsvService).to receive(:new).with(organisation:)
expect(missing_addresses_csv_service).to receive(:create_missing_sales_addresses_csv) expect(missing_addresses_csv_service).to receive(:create_missing_sales_addresses_csv)
job.perform(users.map(&:id), organisation, "sales", "addresses") job.perform(users.map(&:id), organisation, "sales")
end end
it "sends emails to all the provided users" do it "sends emails to all the provided users" do
expect(mailer).to receive(:send_missing_sales_addresses_csv_download_mail).with(users[0], test_url, "addresses") expect(mailer).to receive(:send_missing_sales_addresses_csv_download_mail).with(users[0], test_url)
expect(mailer).to receive(:send_missing_sales_addresses_csv_download_mail).with(users[1], test_url, "addresses") expect(mailer).to receive(:send_missing_sales_addresses_csv_download_mail).with(users[1], test_url)
job.perform(users.map(&:id), organisation, "sales", "addresses") job.perform(users.map(&:id), organisation, "sales")
end
end
context "when sending missing lettings town or city logs csv" do
it "uses an appropriate filename in S3" do
expect(storage_service).to receive(:write_file).with(/missing-lettings-logs-town-or-city-#{organisation.name}-.*\.csv/, anything)
job.perform(users.map(&:id), organisation, "lettings", "town-or-city")
end
it "creates a MissingAddressesCsvService with the correct organisation and calls create missing lettings logs adresses csv" do
expect(Csv::MissingAddressesCsvService).to receive(:new).with(organisation:)
expect(missing_addresses_csv_service).to receive(:create_missing_lettings_town_or_city_csv)
job.perform(users.map(&:id), organisation, "lettings", "town-or-city")
end
it "sends emails to all the provided users" do
expect(mailer).to receive(:send_missing_lettings_addresses_csv_download_mail).with(users[0], test_url, "town-or-city")
expect(mailer).to receive(:send_missing_lettings_addresses_csv_download_mail).with(users[1], test_url, "town-or-city")
job.perform(users.map(&:id), organisation, "lettings", "town-or-city")
end
end
context "when sending missing sales town or city logs csv" do
it "uses an appropriate filename in S3" do
expect(storage_service).to receive(:write_file).with(/missing-sales-logs-town-or-city-#{organisation.name}-.*\.csv/, anything)
job.perform(users.map(&:id), organisation, "sales", "town-or-city")
end
it "creates a MissingAddressesCsvService with the correct organisation and calls create missing sales logs adresses csv" do
expect(Csv::MissingAddressesCsvService).to receive(:new).with(organisation:)
expect(missing_addresses_csv_service).to receive(:create_missing_sales_town_or_city_csv)
job.perform(users.map(&:id), organisation, "sales", "town-or-city")
end
it "sends emails to all the provided users" do
expect(mailer).to receive(:send_missing_sales_addresses_csv_download_mail).with(users[0], test_url, "town-or-city")
expect(mailer).to receive(:send_missing_sales_addresses_csv_download_mail).with(users[1], test_url, "town-or-city")
job.perform(users.map(&:id), organisation, "sales", "town-or-city")
end end
end end
end end

31
spec/lib/tasks/send_missing_addresses_csv_spec.rb

@ -29,7 +29,7 @@ RSpec.describe "correct_addresses" do
end end
it "enqueues the job with correct organisations" do 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", "addresses") expect { task.invoke }.to enqueue_job(EmailMissingAddressesCsvJob).with(include(data_coordinator.id, data_coordinator2.id), organisation, "lettings")
end end
it "prints out the jobs enqueued" do it "prints out the jobs enqueued" do
@ -48,7 +48,7 @@ RSpec.describe "correct_addresses" do
end end
it "enqueues the job with correct organisations" do 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", "addresses") expect { task.invoke }.to enqueue_job(EmailMissingAddressesCsvJob).with(include(data_provider.id, data_provider2.id), organisation, "lettings")
end end
it "prints out the jobs enqueued" do it "prints out the jobs enqueued" do
@ -68,25 +68,6 @@ RSpec.describe "correct_addresses" do
expect { task.invoke }.not_to enqueue_job(EmailMissingAddressesCsvJob) expect { task.invoke }.not_to enqueue_job(EmailMissingAddressesCsvJob)
end end
end end
end
end
describe ":send_missing_town_or_city_csv", type: :task do
subject(:task) { Rake::Task["correct_addresses:send_missing_town_or_city_csv"] }
before do
organisation.users.destroy_all
Rake.application.rake_require("tasks/send_missing_addresses_csv")
Rake::Task.define_task(:environment)
task.reenable
end
context "when the rake task is run" do
let(:organisation) { create(:organisation, name: "test organisation") }
before do
stub_const("MISSING_ADDRESSES_THRESHOLD", 5)
end
context "when org has more than 5 missing town_or_city and data coordinators" do context "when org has more than 5 missing town_or_city and data coordinators" do
let!(:data_coordinator) { create(:user, :data_coordinator, organisation:, email: "data_coordinator1@example.com") } let!(:data_coordinator) { create(:user, :data_coordinator, organisation:, email: "data_coordinator1@example.com") }
@ -98,12 +79,12 @@ RSpec.describe "correct_addresses" do
end end
it "enqueues the job with correct organisations" do 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", "town-or-city") expect { task.invoke }.to enqueue_job(EmailMissingAddressesCsvJob).with(include(data_coordinator.id, data_coordinator2.id), organisation, "lettings")
end end
it "prints out the jobs enqueued" do it "prints out the jobs enqueued" do
expect(Rails.logger).to receive(:info).with(nil) expect(Rails.logger).to receive(:info).with(nil)
expect(Rails.logger).to receive(:info).with("Sending missing town or city CSV for test organisation to data_coordinator1@example.com, data_coordinator2@example.com") 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 task.invoke
end end
end end
@ -117,12 +98,12 @@ RSpec.describe "correct_addresses" do
end end
it "enqueues the job with correct organisations" do 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", "town-or-city") expect { task.invoke }.to enqueue_job(EmailMissingAddressesCsvJob).with(include(data_provider.id, data_provider2.id), organisation, "lettings")
end end
it "prints out the jobs enqueued" do it "prints out the jobs enqueued" do
expect(Rails.logger).to receive(:info).with(nil) expect(Rails.logger).to receive(:info).with(nil)
expect(Rails.logger).to receive(:info).with("Sending missing town or city CSV for test organisation to data_provider3@example.com, data_provider4@example.com") 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 task.invoke
end end
end end

167
spec/services/csv/missing_addresses_csv_service_spec.rb

@ -26,7 +26,36 @@ RSpec.describe Csv::MissingAddressesCsvService do
uprn_known: 0) uprn_known: 0)
end end
context "when the organisation has logs with missing addresses" do let!(:lettings_log_2) do
create(:lettings_log,
tenancycode: "tenancycode",
propcode: "propcode",
startdate: Time.zone.local(2023, 4, 5),
created_by: user,
owning_organisation: organisation,
managing_organisation: organisation,
address_line1: "existing address",
town_or_city: nil,
old_id: "older_id",
old_form_id: "old_form_id",
needstype: 1,
uprn_known: 0)
end
context "when the organisation has logs with missing addresses and logs with missing town or city" 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)
csv = service.create_missing_lettings_addresses_csv
expect(csv).to eq(expected_content)
end
end
context "when the organisation has logs with missing addresses only" do
before do
lettings_log_2.update!(town_or_city: "towncity")
end
it "returns a csv with relevant logs" 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.csv").read) expected_content = replace_entity_ids(lettings_log, File.open("spec/fixtures/files/missing_lettings_logs_addresses.csv").read)
csv = service.create_missing_lettings_addresses_csv csv = service.create_missing_lettings_addresses_csv
@ -34,9 +63,22 @@ RSpec.describe Csv::MissingAddressesCsvService do
end end
end end
context "when the organisation only has supported housing logs with missing addresses" 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")
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)
csv = service.create_missing_lettings_addresses_csv
expect(csv).to eq(expected_content)
end
end
context "when the organisation only has supported housing logs with missing addresses or town or city" do
before do before do
lettings_log.update!(needstype: 2) lettings_log.update!(needstype: 2)
lettings_log_2.update!(needstype: 2)
end end
it "returns nil" do it "returns nil" do
@ -44,9 +86,10 @@ RSpec.describe Csv::MissingAddressesCsvService do
end end
end end
context "when the organisation only has logs with missing addresses from 2022" do context "when the organisation only has logs with missing addresses or town or city from 2022" do
before do before do
lettings_log.update!(startdate: Time.zone.local(2022, 4, 5)) lettings_log.update!(startdate: Time.zone.local(2022, 4, 5))
lettings_log_2.update!(startdate: Time.zone.local(2022, 4, 5))
end end
it "returns nil" do it "returns nil" do
@ -54,9 +97,10 @@ RSpec.describe Csv::MissingAddressesCsvService do
end end
end end
context "when the organisation has any address fields filled in" do context "when the organisation has any address and town or city fields filled in" do
before do before do
lettings_log.update!(address_line1: "address_line1") lettings_log.update!(address_line1: "address_line1", town_or_city: "towncity")
lettings_log_2.update!(address_line1: "address_line1", town_or_city: "towncity")
end end
it "returns nil" do it "returns nil" do
@ -79,130 +123,71 @@ RSpec.describe Csv::MissingAddressesCsvService do
uprn_known: 0) uprn_known: 0)
end end
context "when the organisation has logs with missing addresses" do let!(:sales_log_2) do
it "returns a csv with relevant logs" do create(:sales_log,
expected_content = replace_entity_ids(sales_log, File.open("spec/fixtures/files/missing_sales_logs_addresses.csv").read) purchid: "purchaser code",
csv = service.create_missing_sales_addresses_csv saledate: Time.zone.local(2023, 4, 5),
expect(csv).to eq(expected_content)
end
end
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))
end
it "returns nil" do
expect(service.create_missing_sales_addresses_csv).to be_nil
end
end
context "when the organisation has any address fields filled in" do
before do
sales_log.update!(town_or_city: "town")
end
it "returns nil" do
expect(service.create_missing_sales_addresses_csv).to be_nil
end
end
end
describe "#create_missing_lettings_town_or_city_csv" do
let!(:lettings_log) do
create(:lettings_log,
tenancycode: "tenancycode",
propcode: "propcode",
startdate: Time.zone.local(2023, 4, 5),
created_by: user, created_by: user,
owning_organisation: organisation, owning_organisation: organisation,
managing_organisation: organisation, address_line1: "existing address line 1",
address_line1: "existing address",
town_or_city: nil, town_or_city: nil,
old_id: "old_id", old_id: "older_id",
old_form_id: "old_form_id", old_form_id: "old_form_id",
needstype: 1,
uprn_known: 0) uprn_known: 0)
end end
context "when the organisation has logs with missing town or city only" do context "when the organisation has logs with missing addresses and town or city" do
it "returns a csv with relevant logs" do it "returns a csv with relevant logs" do
expected_content = replace_entity_ids(lettings_log, File.open("spec/fixtures/files/missing_lettings_logs_town_or_city.csv").read) expected_content = replace_entity_ids(sales_log, File.open("spec/fixtures/files/missing_sales_logs_addresses_and_town_or_city.csv").read)
csv = service.create_missing_lettings_town_or_city_csv expected_content = replace_entity_ids(sales_log_2, expected_content)
csv = service.create_missing_sales_addresses_csv
expect(csv).to eq(expected_content) expect(csv).to eq(expected_content)
end end
end end
context "when the organisation only has supported housing logs with missing town or city only" do context "when the organisation has logs with missing addresses" do
before do
lettings_log.update!(needstype: 2)
end
it "returns nil" do
expect(service.create_missing_lettings_town_or_city_csv).to be_nil
end
end
context "when the organisation only has logs with missing town or city from 2022" do
before do before do
lettings_log.update!(startdate: Time.zone.local(2022, 4, 5)) sales_log_2.update!(town_or_city: "towncity")
end end
it "returns nil" do it "returns a csv with relevant logs" do
expect(service.create_missing_lettings_town_or_city_csv).to be_nil expected_content = replace_entity_ids(sales_log, File.open("spec/fixtures/files/missing_sales_logs_addresses.csv").read)
csv = service.create_missing_sales_addresses_csv
expect(csv).to eq(expected_content)
end end
end end
context "when the organisation has any address field 1 not set" do context "when the organisation has logs with missing town_or_city only" do
before do before do
lettings_log.update!(address_line1: nil) sales_log.update!(address_line1: "address", town_or_city: "towncity")
end end
it "returns nil" do
expect(service.create_missing_lettings_town_or_city_csv).to be_nil
end
end
end
describe "#create_missing_sales_town_or_city_csv" do
let!(:sales_log) do
create(:sales_log,
purchid: "purchaser code",
saledate: Time.zone.local(2023, 4, 5),
created_by: user,
owning_organisation: organisation,
address_line1: "existing address line 1",
town_or_city: nil,
old_id: "old_id",
old_form_id: "old_form_id",
uprn_known: 0)
end
context "when the organisation has logs with missing town_or_city only" do
it "returns a csv with relevant logs" do it "returns a csv with relevant logs" do
expected_content = replace_entity_ids(sales_log, File.open("spec/fixtures/files/missing_sales_logs_town_or_city.csv").read) expected_content = replace_entity_ids(sales_log_2, File.open("spec/fixtures/files/missing_sales_logs_town_or_city.csv").read)
csv = service.create_missing_sales_town_or_city_csv csv = service.create_missing_sales_addresses_csv
expect(csv).to eq(expected_content) expect(csv).to eq(expected_content)
end end
end end
context "when the organisation only has logs with missing town_or_city only from 2022" do context "when the organisation only has logs with missing addresses from 2022" do
before do before do
sales_log.update!(saledate: Time.zone.local(2022, 4, 5)) sales_log.update!(saledate: Time.zone.local(2022, 4, 5))
sales_log_2.update!(saledate: Time.zone.local(2022, 4, 5))
end end
it "returns nil" do it "returns nil" do
expect(service.create_missing_sales_town_or_city_csv).to be_nil expect(service.create_missing_sales_addresses_csv).to be_nil
end end
end end
context "when the organisation has any address fields not filled in" do context "when the organisation has address fields filled in" do
before do before do
sales_log.update!(address_line1: nil) sales_log.update!(town_or_city: "town", address_line1: "line1")
sales_log_2.update!(town_or_city: "town")
end end
it "returns nil" do it "returns nil" do
expect(service.create_missing_sales_town_or_city_csv).to be_nil expect(service.create_missing_sales_addresses_csv).to be_nil
end end
end end
end end

Loading…
Cancel
Save