From 5c7640cdbb0194e7453eab39d91b1fb302a51c27 Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 2 Oct 2023 11:12:30 +0100 Subject: [PATCH] Put all log in the same csv --- app/jobs/email_missing_addresses_csv_job.rb | 12 +- app/mailers/csv_download_mailer.rb | 4 +- .../csv/missing_addresses_csv_service.rb | 22 +-- lib/tasks/send_missing_addresses_csv.rake | 27 +-- ...ttings_logs_addresses_and_town_or_city.csv | 3 + ..._sales_logs_addresses_and_town_or_city.csv | 3 + .../email_missing_addresses_csv_job_spec.rb | 58 ++---- .../tasks/send_missing_addresses_csv_spec.rb | 31 +--- .../csv/missing_addresses_csv_service_spec.rb | 167 ++++++++---------- 9 files changed, 119 insertions(+), 208 deletions(-) create mode 100644 spec/fixtures/files/missing_lettings_logs_addresses_and_town_or_city.csv create mode 100644 spec/fixtures/files/missing_sales_logs_addresses_and_town_or_city.csv diff --git a/app/jobs/email_missing_addresses_csv_job.rb b/app/jobs/email_missing_addresses_csv_job.rb index 870054b8f..181d7414f 100644 --- a/app/jobs/email_missing_addresses_csv_job.rb +++ b/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 - def perform(user_ids, organisation, log_type, template_type) + def perform(user_ids, organisation, log_type) csv_service = Csv::MissingAddressesCsvService.new(organisation:) case log_type 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 - filename = "#{['missing-lettings-logs', template_type, organisation.name, Time.zone.now].compact.join('-')}.csv" + csv_string = csv_service.create_missing_lettings_addresses_csv + filename = "#{['missing-lettings-logs-addresses', organisation.name, Time.zone.now].compact.join('-')}.csv" email_method = :send_missing_lettings_addresses_csv_download_mail 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 - filename = "#{['missing-sales-logs', template_type, organisation.name, Time.zone.now].compact.join('-')}.csv" + csv_string = csv_service.create_missing_sales_addresses_csv + filename = "#{['missing-sales-logs-addresses', organisation.name, Time.zone.now].compact.join('-')}.csv" email_method = :send_missing_sales_addresses_csv_download_mail end @@ -25,7 +25,7 @@ class EmailMissingAddressesCsvJob < ApplicationJob user = User.find(id) next if user.blank? - CsvDownloadMailer.new.send(email_method, user, url, template_type) + CsvDownloadMailer.new.send(email_method, user, url) end end end diff --git a/app/mailers/csv_download_mailer.rb b/app/mailers/csv_download_mailer.rb index 49aa95dbc..db5957d23 100644 --- a/app/mailers/csv_download_mailer.rb +++ b/app/mailers/csv_download_mailer.rb @@ -9,7 +9,7 @@ class CsvDownloadMailer < NotifyMailer ) 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 diff --git a/app/services/csv/missing_addresses_csv_service.rb b/app/services/csv/missing_addresses_csv_service.rb index 616c9ac85..724f2fae7 100644 --- a/app/services/csv/missing_addresses_csv_service.rb +++ b/app/services/csv/missing_addresses_csv_service.rb @@ -6,30 +6,18 @@ module 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) - 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 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) - 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) - 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 private diff --git a/lib/tasks/send_missing_addresses_csv.rake b/lib/tasks/send_missing_addresses_csv.rake index e32f0e61f..67a8c54e5 100644 --- a/lib/tasks/send_missing_addresses_csv.rake +++ b/lib/tasks/send_missing_addresses_csv.rake @@ -6,36 +6,25 @@ namespace :correct_addresses do desc "Send missing addresses csv" task :send_missing_addresses_csv, %i[] => :environment do |_task, _args| Organisation.all.each do |organisation| - impacted_logs = organisation.managed_lettings_logs + logs_impacted_by_missing_address = 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) - - next unless impacted_logs.count >= MISSING_ADDRESSES_THRESHOLD + .where.not(old_form_id: nil).count - 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", "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 + logs_impacted_by_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) - next unless impacted_logs.count >= MISSING_ADDRESSES_THRESHOLD + .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 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", "town-or-city") - Rails.logger.info("Sending missing town or city CSV for #{organisation.name} to #{users_to_contact.map(&:email).join(', ')}") + 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(', ')}") end end end 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 new file mode 100644 index 000000000..3bc35d30a --- /dev/null +++ b/spec/fixtures/files/missing_lettings_logs_addresses_and_town_or_city.csv @@ -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,,,,, 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 new file mode 100644 index 000000000..2bbed2e77 --- /dev/null +++ b/spec/fixtures/files/missing_sales_logs_addresses_and_town_or_city.csv @@ -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,,,,, diff --git a/spec/jobs/email_missing_addresses_csv_job_spec.rb b/spec/jobs/email_missing_addresses_csv_job_spec.rb index e6f3edb43..120835184 100644 --- a/spec/jobs/email_missing_addresses_csv_job_spec.rb +++ b/spec/jobs/email_missing_addresses_csv_job_spec.rb @@ -31,76 +31,38 @@ describe EmailMissingAddressesCsvJob do context "when sending missing lettings logs csv" 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) - job.perform(users.map(&:id), organisation, "lettings", "addresses") + job.perform(users.map(&:id), organisation, "lettings") 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_addresses_csv) - job.perform(users.map(&:id), organisation, "lettings", "addresses") + job.perform(users.map(&:id), organisation, "lettings") 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, "addresses") - expect(mailer).to receive(:send_missing_lettings_addresses_csv_download_mail).with(users[1], test_url, "addresses") - job.perform(users.map(&:id), organisation, "lettings", "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) + job.perform(users.map(&:id), organisation, "lettings") end end context "when sending missing sales logs csv" 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) - job.perform(users.map(&:id), organisation, "sales", "addresses") + job.perform(users.map(&:id), organisation, "sales") 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_addresses_csv) - job.perform(users.map(&:id), organisation, "sales", "addresses") + job.perform(users.map(&:id), organisation, "sales") 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, "addresses") - expect(mailer).to receive(:send_missing_sales_addresses_csv_download_mail).with(users[1], test_url, "addresses") - job.perform(users.map(&:id), organisation, "sales", "addresses") - 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") + 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) + job.perform(users.map(&:id), organisation, "sales") end end end diff --git a/spec/lib/tasks/send_missing_addresses_csv_spec.rb b/spec/lib/tasks/send_missing_addresses_csv_spec.rb index 6e73e5207..dbba071b5 100644 --- a/spec/lib/tasks/send_missing_addresses_csv_spec.rb +++ b/spec/lib/tasks/send_missing_addresses_csv_spec.rb @@ -29,7 +29,7 @@ RSpec.describe "correct_addresses" do 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", "addresses") + 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 @@ -48,7 +48,7 @@ RSpec.describe "correct_addresses" do 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", "addresses") + 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 @@ -68,25 +68,6 @@ RSpec.describe "correct_addresses" do expect { task.invoke }.not_to enqueue_job(EmailMissingAddressesCsvJob) 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 let!(:data_coordinator) { create(:user, :data_coordinator, organisation:, email: "data_coordinator1@example.com") } @@ -98,12 +79,12 @@ RSpec.describe "correct_addresses" do 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", "town-or-city") + 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 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 end end @@ -117,12 +98,12 @@ RSpec.describe "correct_addresses" do 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", "town-or-city") + 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 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 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 46e08142a..f7c76d933 100644 --- a/spec/services/csv/missing_addresses_csv_service_spec.rb +++ b/spec/services/csv/missing_addresses_csv_service_spec.rb @@ -26,7 +26,36 @@ RSpec.describe Csv::MissingAddressesCsvService do uprn_known: 0) 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 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 @@ -34,9 +63,22 @@ RSpec.describe Csv::MissingAddressesCsvService do 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 lettings_log.update!(needstype: 2) + lettings_log_2.update!(needstype: 2) end it "returns nil" do @@ -44,9 +86,10 @@ RSpec.describe Csv::MissingAddressesCsvService do 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 lettings_log.update!(startdate: Time.zone.local(2022, 4, 5)) + lettings_log_2.update!(startdate: Time.zone.local(2022, 4, 5)) end it "returns nil" do @@ -54,9 +97,10 @@ RSpec.describe Csv::MissingAddressesCsvService do 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 - 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 it "returns nil" do @@ -79,130 +123,71 @@ RSpec.describe Csv::MissingAddressesCsvService do uprn_known: 0) end - context "when the organisation has logs with missing addresses" 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.csv").read) - csv = service.create_missing_sales_addresses_csv - 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), + let!(:sales_log_2) do + create(:sales_log, + purchid: "purchaser code", + saledate: Time.zone.local(2023, 4, 5), created_by: user, owning_organisation: organisation, - managing_organisation: organisation, - address_line1: "existing address", + address_line1: "existing address line 1", town_or_city: nil, - old_id: "old_id", + old_id: "older_id", old_form_id: "old_form_id", - needstype: 1, uprn_known: 0) 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 - expected_content = replace_entity_ids(lettings_log, File.open("spec/fixtures/files/missing_lettings_logs_town_or_city.csv").read) - csv = service.create_missing_lettings_town_or_city_csv + 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) + csv = service.create_missing_sales_addresses_csv expect(csv).to eq(expected_content) end end - context "when the organisation only has supported housing logs with missing town or city only" 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 + context "when the organisation has logs with missing addresses" do before do - lettings_log.update!(startdate: Time.zone.local(2022, 4, 5)) + sales_log_2.update!(town_or_city: "towncity") end - it "returns nil" do - expect(service.create_missing_lettings_town_or_city_csv).to be_nil + it "returns a csv with relevant logs" do + 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 - 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 - lettings_log.update!(address_line1: nil) + sales_log.update!(address_line1: "address", town_or_city: "towncity") 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 - expected_content = replace_entity_ids(sales_log, File.open("spec/fixtures/files/missing_sales_logs_town_or_city.csv").read) - csv = service.create_missing_sales_town_or_city_csv + 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_addresses_csv expect(csv).to eq(expected_content) 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 sales_log.update!(saledate: Time.zone.local(2022, 4, 5)) + sales_log_2.update!(saledate: Time.zone.local(2022, 4, 5)) end 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 - context "when the organisation has any address fields not filled in" do + context "when the organisation has address fields filled in" 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 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