Browse Source
* Add missing addresses csv job * Update missing addresses csv service methods * Add rake task * Update the job to send missing town or city templates * Update service to create mising town or city templates * Add send missing town or city csv rake task * Add IDs to the CSVs * Put all log in the same csv * Add issue type column * Write wrong uprn logs to csv * Add mailer methods * Skip uprn issue for specified orgs * Add sales csv rake task * set SKIP_UPRN_ISSUE_ORG_IDS on review apps * test * Update notify template IDs for testing * Initialize service with organisation instead of a hash * Add expiration time to url * Add optional tags and remove LA from csv * Extract log to csv methods * Update casing * Update old IDs in factories * Move constant * Extract some repeating scopes * Pass in organisations to skip instead of using an env var * update template id for sales * Update link expiry time and headers * Lower the threshold for testing * Add issue explanation to the email * Add how to fix * update emails * CLDC-2810 Create all addresses CSV (#1953) * Add rake tasks for creating all addresses CSV * Write headers if logs don't exist, update header names * Rename method * CLDC-2810 Correct addresses from csv (#1957) * Updating importing lettings addresses form csv * Add import_sales_addresses_from_csv rake * Allow correcting addresses from both templates * escape . * Reinfer LA if the postcode hasn't changed * Update labels and email content * Update missing addresses threshold * Remove unused env varCLDC-2896-pipes-in-bu
kosiakkatrina
1 year ago
committed by
GitHub
27 changed files with 2221 additions and 120 deletions
@ -0,0 +1,22 @@
|
||||
class CreateAddressesCsvJob < ApplicationJob |
||||
queue_as :default |
||||
|
||||
BYTE_ORDER_MARK = "\uFEFF".freeze # Required to ensure Excel always reads CSV as UTF-8 |
||||
|
||||
def perform(organisation, log_type) |
||||
csv_service = Csv::MissingAddressesCsvService.new(organisation, []) |
||||
case log_type |
||||
when "lettings" |
||||
csv_string = csv_service.create_lettings_addresses_csv |
||||
filename = "#{['lettings-logs-addresses', organisation.name, Time.zone.now].compact.join('-')}.csv" |
||||
when "sales" |
||||
csv_string = csv_service.create_sales_addresses_csv |
||||
filename = "#{['sales-logs-addresses', organisation.name, Time.zone.now].compact.join('-')}.csv" |
||||
end |
||||
|
||||
storage_service = Storage::S3Service.new(Configuration::EnvConfigurationService.new, ENV["CSV_DOWNLOAD_PAAS_INSTANCE"]) |
||||
storage_service.write_file(filename, BYTE_ORDER_MARK + csv_string) |
||||
|
||||
Rails.logger.info("Created addresses file: #{filename}") |
||||
end |
||||
end |
@ -0,0 +1,33 @@
|
||||
class EmailMissingAddressesCsvJob < ApplicationJob |
||||
queue_as :default |
||||
|
||||
BYTE_ORDER_MARK = "\uFEFF".freeze # Required to ensure Excel always reads CSV as UTF-8 |
||||
EXPIRATION_TIME = 1.week.to_i |
||||
MISSING_ADDRESSES_THRESHOLD = 50 |
||||
|
||||
def perform(user_ids, organisation, log_type, issue_types, skip_uprn_issue_organisations) |
||||
csv_service = Csv::MissingAddressesCsvService.new(organisation, skip_uprn_issue_organisations) |
||||
case log_type |
||||
when "lettings" |
||||
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 = 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 |
||||
|
||||
storage_service = Storage::S3Service.new(Configuration::EnvConfigurationService.new, ENV["CSV_DOWNLOAD_PAAS_INSTANCE"]) |
||||
storage_service.write_file(filename, BYTE_ORDER_MARK + csv_string) |
||||
|
||||
url = storage_service.get_presigned_url(filename, EXPIRATION_TIME) |
||||
|
||||
user_ids.each do |id| |
||||
user = User.find(id) |
||||
next if user.blank? |
||||
|
||||
CsvDownloadMailer.new.send(email_method, user, url, EXPIRATION_TIME, issue_types) |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,140 @@
|
||||
module Csv |
||||
class MissingAddressesCsvService |
||||
def initialize(organisation, skip_uprn_issue_organisations) |
||||
@organisation = organisation |
||||
@skip_uprn_issue_organisations = skip_uprn_issue_organisations |
||||
end |
||||
|
||||
def create_missing_lettings_addresses_csv |
||||
logs_with_missing_addresses = @organisation.managed_lettings_logs |
||||
.imported_2023_with_old_form_id |
||||
.where(needstype: 1, address_line1: nil, town_or_city: nil, uprn_known: [0, nil]) |
||||
|
||||
logs_with_missing_town_or_city = @organisation.managed_lettings_logs |
||||
.imported_2023_with_old_form_id |
||||
.where(needstype: 1, town_or_city: nil, uprn_known: [0, nil]) |
||||
.where.not(address_line1: nil) |
||||
|
||||
logs_with_wrong_uprn = if @skip_uprn_issue_organisations.include?(@organisation.id) |
||||
[] |
||||
else |
||||
@organisation.managed_lettings_logs |
||||
.imported_2023 |
||||
.where(needstype: 1) |
||||
.where.not(uprn: nil) |
||||
.where("uprn = propcode OR uprn = tenancycode OR town_or_city = 'Bristol'") |
||||
end |
||||
|
||||
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", "Log ID", "Tenancy start date", "Tenant code", "Property reference", "Log owner", "Owning organisation", "Managing organisation", "UPRN", "Address Line 1", "Address Line 2 (optional)", "Town or City", "County (optional)", "Property's postcode"] |
||||
|
||||
logs_with_missing_addresses.each do |log| |
||||
csv << ["Full address required"] + lettings_log_to_csv_row(log) |
||||
end |
||||
|
||||
logs_with_missing_town_or_city.each do |log| |
||||
csv << ["Missing town or city"] + lettings_log_to_csv_row(log) |
||||
end |
||||
|
||||
logs_with_wrong_uprn.each do |log| |
||||
csv << ["Incorrect UPRN"] + lettings_log_to_csv_row(log) |
||||
end |
||||
end |
||||
end |
||||
|
||||
def create_missing_sales_addresses_csv |
||||
logs_with_missing_addresses = @organisation.sales_logs |
||||
.imported_2023_with_old_form_id |
||||
.where(address_line1: nil, town_or_city: nil, uprn_known: [0, nil]) |
||||
|
||||
logs_with_missing_town_or_city = @organisation.sales_logs |
||||
.imported_2023_with_old_form_id |
||||
.where(town_or_city: nil, uprn_known: [0, nil]) |
||||
.where.not(address_line1: nil) |
||||
|
||||
logs_with_wrong_uprn = if @skip_uprn_issue_organisations.include?(@organisation.id) |
||||
[] |
||||
else |
||||
@organisation.sales_logs |
||||
.imported_2023 |
||||
.where.not(uprn: nil) |
||||
.where("uprn = purchid OR town_or_city = 'Bristol'") |
||||
end |
||||
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", "Log ID", "Sale completion date", "Purchaser code", "Log owner", "Owning organisation", "UPRN", "Address Line 1", "Address Line 2 (optional)", "Town or City", "County (optional)", "Property's postcode"] |
||||
|
||||
logs_with_missing_addresses.each do |log| |
||||
csv << ["Full address required"] + sales_log_to_csv_row(log) |
||||
end |
||||
|
||||
logs_with_missing_town_or_city.each do |log| |
||||
csv << ["Missing town or city"] + sales_log_to_csv_row(log) |
||||
end |
||||
|
||||
logs_with_wrong_uprn.each do |log| |
||||
csv << ["Incorrect UPRN"] + sales_log_to_csv_row(log) |
||||
end |
||||
end |
||||
end |
||||
|
||||
def create_lettings_addresses_csv |
||||
logs = @organisation.managed_lettings_logs.filter_by_year(2023) |
||||
|
||||
CSV.generate(headers: true) do |csv| |
||||
csv << ["Log ID", "Tenancy start date", "Tenant code", "Property reference", "Log owner", "Owning organisation", "Managing organisation", "UPRN", "Address Line 1", "Address Line 2 (optional)", "Town or City", "County (optional)", "Property's postcode"] |
||||
|
||||
logs.each do |log| |
||||
csv << lettings_log_to_csv_row(log) |
||||
end |
||||
end |
||||
end |
||||
|
||||
def create_sales_addresses_csv |
||||
logs = @organisation.sales_logs.filter_by_year(2023) |
||||
|
||||
CSV.generate(headers: true) do |csv| |
||||
csv << ["Log ID", "Sale completion date", "Purchaser code", "Log owner", "Owning organisation", "UPRN", "Address Line 1", "Address Line 2 (optional)", "Town or City", "County (optional)", "Property's postcode"] |
||||
|
||||
logs.each do |log| |
||||
csv << sales_log_to_csv_row(log) |
||||
end |
||||
end |
||||
end |
||||
|
||||
private |
||||
|
||||
def sales_log_to_csv_row(log) |
||||
[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, |
||||
log.county, |
||||
log.postcode_full] |
||||
end |
||||
|
||||
def lettings_log_to_csv_row(log) |
||||
[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, |
||||
log.county, |
||||
log.postcode_full] |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,118 @@
|
||||
namespace :correct_addresses do |
||||
desc "Send missing lettings addresses csv" |
||||
task :send_missing_addresses_lettings_csv, %i[skip_uprn_issue_organisations] => :environment do |_task, args| |
||||
skip_uprn_issue_organisations = args[:skip_uprn_issue_organisations]&.split(" ")&.map(&:to_i) || [] |
||||
|
||||
Organisation.all.each do |organisation| |
||||
logs_impacted_by_missing_address = organisation.managed_lettings_logs |
||||
.imported_2023_with_old_form_id |
||||
.where(needstype: 1, address_line1: nil, town_or_city: nil, uprn_known: [0, nil]).count |
||||
|
||||
logs_impacted_by_missing_town_or_city = organisation.managed_lettings_logs |
||||
.imported_2023_with_old_form_id |
||||
.where(needstype: 1, town_or_city: nil, uprn_known: [0, nil]) |
||||
.where.not(address_line1: nil).count |
||||
|
||||
logs_impacted_by_uprn_issue = if skip_uprn_issue_organisations.include?(organisation.id) |
||||
[] |
||||
else |
||||
organisation.managed_lettings_logs |
||||
.imported_2023 |
||||
.where(needstype: 1) |
||||
.where.not(uprn: nil) |
||||
.where("uprn = propcode OR uprn = tenancycode") |
||||
end |
||||
|
||||
logs_impacted_by_bristol_uprn_issue = if skip_uprn_issue_organisations.include?(organisation.id) |
||||
[] |
||||
else |
||||
organisation.managed_lettings_logs |
||||
.imported_2023 |
||||
.where(needstype: 1) |
||||
.where.not(uprn: nil) |
||||
.where("town_or_city = 'Bristol'") |
||||
end |
||||
|
||||
missing_addresses_threshold = EmailMissingAddressesCsvJob::MISSING_ADDRESSES_THRESHOLD |
||||
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? || logs_impacted_by_bristol_uprn_issue.any? |
||||
issue_types = [] |
||||
issue_types << "missing_address" if logs_impacted_by_missing_address.positive? |
||||
issue_types << "missing_town" if logs_impacted_by_missing_town_or_city.positive? |
||||
issue_types << "wrong_uprn" if logs_impacted_by_uprn_issue.any? |
||||
issue_types << "bristol_uprn" if logs_impacted_by_bristol_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", issue_types, skip_uprn_issue_organisations) |
||||
Rails.logger.info("Sending missing lettings 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 |
||||
|
||||
desc "Send missing sales addresses csv" |
||||
task :send_missing_addresses_sales_csv, %i[skip_uprn_issue_organisations] => :environment do |_task, args| |
||||
skip_uprn_issue_organisations = args[:skip_uprn_issue_organisations]&.split(" ")&.map(&:to_i) || [] |
||||
|
||||
Organisation.all.each do |organisation| |
||||
logs_impacted_by_missing_address = organisation.sales_logs |
||||
.imported_2023_with_old_form_id |
||||
.where(address_line1: nil, town_or_city: nil, uprn_known: [0, nil]).count |
||||
|
||||
logs_impacted_by_missing_town_or_city = organisation.sales_logs |
||||
.imported_2023_with_old_form_id |
||||
.where(town_or_city: nil, uprn_known: [0, nil]) |
||||
.where.not(address_line1: nil).count |
||||
|
||||
logs_impacted_by_uprn_issue = if skip_uprn_issue_organisations.include?(organisation.id) |
||||
[] |
||||
else |
||||
organisation.sales_logs |
||||
.imported_2023 |
||||
.where.not(uprn: nil) |
||||
.where("uprn = purchid OR town_or_city = 'Bristol'") |
||||
end |
||||
missing_addresses_threshold = EmailMissingAddressesCsvJob::MISSING_ADDRESSES_THRESHOLD |
||||
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? |
||||
issue_types = [] |
||||
issue_types << "missing_address" if logs_impacted_by_missing_address.positive? |
||||
issue_types << "missing_town" if logs_impacted_by_missing_town_or_city.positive? |
||||
issue_types << "wrong_uprn" if 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, "sales", issue_types, skip_uprn_issue_organisations) |
||||
Rails.logger.info("Sending missing sales 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 |
||||
|
||||
desc "Send all 2023 lettings addresses csv" |
||||
task :create_lettings_addresses_csv, %i[organisation_id] => :environment do |_task, args| |
||||
organisation_id = args[:organisation_id] |
||||
raise "Usage: rake correct_addresses:create_lettings_addresses_csv['organisation_id']" if organisation_id.blank? |
||||
|
||||
organisation = Organisation.find_by(id: organisation_id) |
||||
if organisation.present? |
||||
CreateAddressesCsvJob.perform_later(organisation, "lettings") |
||||
Rails.logger.info("Creating lettings addresses CSV for #{organisation.name}") |
||||
else |
||||
Rails.logger.error("Organisation with ID #{organisation_id} not found") |
||||
end |
||||
end |
||||
|
||||
desc "Send all 2023 sales addresses csv" |
||||
task :create_sales_addresses_csv, %i[organisation_id] => :environment do |_task, args| |
||||
organisation_id = args[:organisation_id] |
||||
raise "Usage: rake correct_addresses:create_sales_addresses_csv['organisation_id']" if organisation_id.blank? |
||||
|
||||
organisation = Organisation.find_by(id: organisation_id) |
||||
if organisation.present? |
||||
CreateAddressesCsvJob.perform_later(organisation, "sales") |
||||
Rails.logger.info("Creating sales addresses CSV for #{organisation.name}") |
||||
else |
||||
Rails.logger.error("Organisation with ID #{organisation_id} not found") |
||||
end |
||||
end |
||||
end |
|
|
|
|
|
|
|
|
|
|
|
|
@ -0,0 +1,49 @@
|
||||
require "rails_helper" |
||||
|
||||
describe CreateAddressesCsvJob do |
||||
include Helpers |
||||
|
||||
let(:job) { described_class.new } |
||||
let(:storage_service) { instance_double(Storage::S3Service) } |
||||
let(:mailer) { instance_double(CsvDownloadMailer) } |
||||
let(:missing_addresses_csv_service) { instance_double(Csv::MissingAddressesCsvService) } |
||||
let(:organisation) { build(:organisation) } |
||||
let(:users) { create_list(:user, 2) } |
||||
|
||||
before do |
||||
allow(Storage::S3Service).to receive(:new).and_return(storage_service) |
||||
allow(storage_service).to receive(:write_file) |
||||
|
||||
allow(Csv::MissingAddressesCsvService).to receive(:new).and_return(missing_addresses_csv_service) |
||||
allow(missing_addresses_csv_service).to receive(:create_lettings_addresses_csv).and_return("") |
||||
allow(missing_addresses_csv_service).to receive(:create_sales_addresses_csv).and_return("") |
||||
end |
||||
|
||||
context "when sending all lettings logs csv" do |
||||
it "uses an appropriate filename in S3" do |
||||
expect(storage_service).to receive(:write_file).with(/lettings-logs-addresses-#{organisation.name}-.*\.csv/, anything) |
||||
expect(Rails.logger).to receive(:info).with(/Created addresses file: lettings-logs-addresses-#{organisation.name}-.*\.csv/) |
||||
job.perform(organisation, "lettings") |
||||
end |
||||
|
||||
it "creates a MissingAddressesCsvService with the correct organisation and calls create all lettings logs adresses csv" do |
||||
expect(Csv::MissingAddressesCsvService).to receive(:new).with(organisation, []) |
||||
expect(missing_addresses_csv_service).to receive(:create_lettings_addresses_csv) |
||||
job.perform(organisation, "lettings") |
||||
end |
||||
end |
||||
|
||||
context "when sending all sales logs csv" do |
||||
it "uses an appropriate filename in S3" do |
||||
expect(storage_service).to receive(:write_file).with(/sales-logs-addresses-#{organisation.name}-.*\.csv/, anything) |
||||
expect(Rails.logger).to receive(:info).with(/Created addresses file: sales-logs-addresses-#{organisation.name}-.*\.csv/) |
||||
job.perform(organisation, "sales") |
||||
end |
||||
|
||||
it "creates a MissingAddressesCsvService with the correct organisation and calls create all sales logs adresses csv" do |
||||
expect(Csv::MissingAddressesCsvService).to receive(:new).with(organisation, []) |
||||
expect(missing_addresses_csv_service).to receive(:create_sales_addresses_csv) |
||||
job.perform(organisation, "sales") |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,66 @@
|
||||
require "rails_helper" |
||||
|
||||
describe EmailMissingAddressesCsvJob do |
||||
include Helpers |
||||
|
||||
test_url = :test_url |
||||
|
||||
let(:job) { described_class.new } |
||||
let(:storage_service) { instance_double(Storage::S3Service) } |
||||
let(:mailer) { instance_double(CsvDownloadMailer) } |
||||
let(:missing_addresses_csv_service) { instance_double(Csv::MissingAddressesCsvService) } |
||||
let(:organisation) { build(:organisation) } |
||||
let(:users) { create_list(:user, 2) } |
||||
|
||||
before do |
||||
allow(Storage::S3Service).to receive(:new).and_return(storage_service) |
||||
allow(storage_service).to receive(:write_file) |
||||
allow(storage_service).to receive(:get_presigned_url).and_return(test_url) |
||||
|
||||
allow(Csv::MissingAddressesCsvService).to receive(:new).and_return(missing_addresses_csv_service) |
||||
allow(missing_addresses_csv_service).to receive(:create_missing_lettings_addresses_csv).and_return("") |
||||
allow(missing_addresses_csv_service).to receive(:create_missing_sales_addresses_csv).and_return("") |
||||
|
||||
allow(CsvDownloadMailer).to receive(:new).and_return(mailer) |
||||
allow(mailer).to receive(:send_missing_lettings_addresses_csv_download_mail) |
||||
allow(mailer).to receive(:send_missing_sales_addresses_csv_download_mail) |
||||
end |
||||
|
||||
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", %w[missing_address wrong_uprn], [1, 2]) |
||||
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, [1, 2]) |
||||
expect(missing_addresses_csv_service).to receive(:create_missing_lettings_addresses_csv) |
||||
job.perform(users.map(&:id), organisation, "lettings", %w[missing_address wrong_uprn], [1, 2]) |
||||
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, instance_of(Integer), %w[missing_address wrong_uprn]) |
||||
expect(mailer).to receive(:send_missing_lettings_addresses_csv_download_mail).with(users[1], test_url, instance_of(Integer), %w[missing_address wrong_uprn]) |
||||
job.perform(users.map(&:id), organisation, "lettings", %w[missing_address wrong_uprn], [1, 2]) |
||||
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", %w[missing_town], [2, 3]) |
||||
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, [2, 3]) |
||||
expect(missing_addresses_csv_service).to receive(:create_missing_sales_addresses_csv) |
||||
job.perform(users.map(&:id), organisation, "sales", %w[missing_town], [2, 3]) |
||||
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, instance_of(Integer), %w[missing_town]) |
||||
expect(mailer).to receive(:send_missing_sales_addresses_csv_download_mail).with(users[1], test_url, instance_of(Integer), %w[missing_town]) |
||||
job.perform(users.map(&:id), organisation, "sales", %w[missing_town], [2, 3]) |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,560 @@
|
||||
require "rails_helper" |
||||
require "rake" |
||||
|
||||
RSpec.describe "correct_addresses" do |
||||
describe ":send_missing_addresses_lettings_csv", type: :task do |
||||
subject(:task) { Rake::Task["correct_addresses:send_missing_addresses_lettings_csv"] } |
||||
|
||||
before do |
||||
organisation.users.destroy_all |
||||
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 |
||||
let(:organisation) { create(:organisation, name: "test organisation") } |
||||
|
||||
before do |
||||
stub_const("EmailMissingAddressesCsvJob::MISSING_ADDRESSES_THRESHOLD", 5) |
||||
end |
||||
|
||||
context "when org has more than 5 missing addresses 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), address_line1: nil, town_or_city: nil, needstype: 1, old_form_id: "form_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", %w[missing_address], []) |
||||
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 lettings addresses CSV for test organisation to data_coordinator1@example.com, data_coordinator2@example.com") |
||||
task.invoke |
||||
end |
||||
end |
||||
|
||||
context "when org has 5 missing addresses 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), address_line1: nil, town_or_city: nil, needstype: 1, old_form_id: "form_2", 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", %w[missing_address], []) |
||||
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 lettings 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 missing addresses" do |
||||
before do |
||||
create_list(:lettings_log, 3, :imported, startdate: Time.zone.local(2023, 9, 9), address_line1: nil, town_or_city: nil, needstype: 1, old_form_id: "form_2", owning_organisation: organisation, managing_organisation: organisation, created_by: organisation.users.first) |
||||
create_list(:lettings_log, 2, :imported, startdate: Time.zone.local(2023, 9, 9), address_line1: nil, needstype: 1, owning_organisation: organisation, managing_organisation: organisation, created_by: organisation.users.first) |
||||
end |
||||
|
||||
it "does not enqueue the job with organisations that is missing less addresses than threshold amount" do |
||||
expect { task.invoke }.not_to enqueue_job(EmailMissingAddressesCsvJob) |
||||
end |
||||
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") } |
||||
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), address_line1: "exists", town_or_city: nil, needstype: 1, old_form_id: "form_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", %w[missing_town], []) |
||||
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 lettings addresses CSV for test organisation to data_coordinator1@example.com, data_coordinator2@example.com") |
||||
task.invoke |
||||
end |
||||
end |
||||
|
||||
context "when org has 5 missing town or city 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), address_line1: "exists", town_or_city: nil, needstype: 1, old_form_id: "form_2", 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", %w[missing_town], []) |
||||
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 lettings 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 missing town or city" do |
||||
before do |
||||
create_list(:lettings_log, 3, :imported, startdate: Time.zone.local(2023, 9, 9), address_line1: "address", town_or_city: nil, needstype: 1, old_form_id: "form_2", owning_organisation: organisation, managing_organisation: organisation, created_by: organisation.users.first) |
||||
create_list(:lettings_log, 2, :imported, startdate: Time.zone.local(2023, 9, 9), address_line1: "address", needstype: 1, owning_organisation: organisation, managing_organisation: organisation, created_by: organisation.users.first) |
||||
end |
||||
|
||||
it "does not enqueue the job with organisations that is missing less town or city data than threshold amount" 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", %w[bristol_uprn], []) |
||||
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 lettings 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", %w[wrong_uprn], []) |
||||
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 lettings 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", %w[wrong_uprn bristol_uprn], []) |
||||
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 lettings addresses CSV for test organisation to data_provider3@example.com, data_provider4@example.com") |
||||
task.invoke |
||||
end |
||||
end |
||||
|
||||
context "when org is included in skip_uprn_issue_organisations list" do |
||||
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 "does not enqueue the job" do |
||||
expect { task.invoke(organisation.id.to_s) }.not_to enqueue_job(EmailMissingAddressesCsvJob) |
||||
end |
||||
end |
||||
|
||||
context "when org is included in skip_uprn_issue_organisations list but faces a different issue" 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), address_line1: nil, town_or_city: nil, needstype: 1, old_form_id: "form_2", owning_organisation: organisation, managing_organisation: organisation, created_by: organisation.users.first) |
||||
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 "does not enqueue the job" do |
||||
expect { task.invoke(organisation.id.to_s) }.to enqueue_job(EmailMissingAddressesCsvJob).with(include(data_provider.id, data_provider2.id), organisation, "lettings", %w[missing_address], [organisation.id]) |
||||
end |
||||
end |
||||
|
||||
context "when skip_uprn_issue_organisations list is provided" 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 "does enqueues the job with correct skip_uprn_issue_organisations" do |
||||
expect { task.invoke("100 400") }.to enqueue_job(EmailMissingAddressesCsvJob).with(include(data_provider.id, data_provider2.id), organisation, "lettings", %w[wrong_uprn], [100, 400]) |
||||
end |
||||
end |
||||
end |
||||
end |
||||
|
||||
describe ":send_missing_addresses_sales_csv", type: :task do |
||||
subject(:task) { Rake::Task["correct_addresses:send_missing_addresses_sales_csv"] } |
||||
|
||||
before do |
||||
organisation.users.destroy_all |
||||
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 |
||||
let(:organisation) { create(:organisation, name: "test organisation") } |
||||
|
||||
before do |
||||
stub_const("EmailMissingAddressesCsvJob::MISSING_ADDRESSES_THRESHOLD", 5) |
||||
end |
||||
|
||||
context "when org has more than 5 missing addresses 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(:sales_log, 7, :completed, :imported, saledate: Time.zone.local(2023, 9, 9), address_line1: nil, town_or_city: nil, old_form_id: "form_1", owning_organisation: organisation, created_by: organisation.users.first) |
||||
end |
||||
|
||||
it "enqueues the job with correct organisations" do |
||||
expect { task.invoke("70 90") }.to enqueue_job(EmailMissingAddressesCsvJob).with(include(data_coordinator.id, data_coordinator2.id), organisation, "sales", %w[missing_address], [70, 90]) |
||||
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 sales addresses CSV for test organisation to data_coordinator1@example.com, data_coordinator2@example.com") |
||||
task.invoke |
||||
end |
||||
end |
||||
|
||||
context "when org has 5 missing addresses 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(:sales_log, 5, :completed, :imported, saledate: Time.zone.local(2023, 9, 9), address_line1: nil, town_or_city: nil, old_form_id: "form_2", owning_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, "sales", %w[missing_address], []) |
||||
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 sales 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 missing addresses" do |
||||
before do |
||||
create_list(:sales_log, 3, :completed, :imported, saledate: Time.zone.local(2023, 9, 9), address_line1: nil, town_or_city: nil, old_form_id: "form_2", owning_organisation: organisation, created_by: organisation.users.first) |
||||
create_list(:sales_log, 2, :completed, :imported, saledate: Time.zone.local(2023, 9, 9), address_line1: nil, owning_organisation: organisation, created_by: organisation.users.first) |
||||
end |
||||
|
||||
it "does not enqueue the job with organisations that is missing less addresses than threshold amount" do |
||||
expect { task.invoke }.not_to enqueue_job(EmailMissingAddressesCsvJob) |
||||
end |
||||
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") } |
||||
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(:sales_log, 7, :completed, :imported, saledate: Time.zone.local(2023, 9, 9), address_line1: "exists", town_or_city: nil, old_form_id: "form_1", owning_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, "sales", %w[missing_town], []) |
||||
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 sales addresses CSV for test organisation to data_coordinator1@example.com, data_coordinator2@example.com") |
||||
task.invoke |
||||
end |
||||
end |
||||
|
||||
context "when org has 5 missing town or city 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(:sales_log, 5, :completed, :imported, saledate: Time.zone.local(2023, 9, 9), address_line1: "exists", town_or_city: nil, old_form_id: "form_2", owning_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, "sales", %w[missing_town], []) |
||||
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 sales 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 missing town or city" do |
||||
before do |
||||
create_list(:sales_log, 3, :completed, :imported, saledate: Time.zone.local(2023, 9, 9), address_line1: "address", town_or_city: nil, old_form_id: "form_2", owning_organisation: organisation, created_by: organisation.users.first) |
||||
create_list(:sales_log, 2, :completed, :imported, saledate: Time.zone.local(2023, 9, 9), address_line1: "address", owning_organisation: organisation, created_by: organisation.users.first) |
||||
end |
||||
|
||||
it "does not enqueue the job with organisations that is missing less town or city data than threshold amount" 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(:sales_log, 7, :completed, :imported, saledate: Time.zone.local(2023, 9, 9), uprn_known: 1, uprn: "123", town_or_city: "Bristol", owning_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, "sales", %w[wrong_uprn], []) |
||||
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 sales 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(:sales_log, 5, :completed, :imported, saledate: Time.zone.local(2023, 9, 9), uprn_known: 1, uprn: "12", purchid: "12", owning_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, "sales", %w[wrong_uprn], []) |
||||
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 sales 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(:sales_log, 3, :completed, :imported, saledate: Time.zone.local(2023, 9, 9), uprn_known: 1, uprn: "123", town_or_city: "Bristol", owning_organisation: organisation, created_by: organisation.users.first) |
||||
create_list(:sales_log, 2, :completed, :imported, saledate: Time.zone.local(2023, 9, 9), uprn_known: 1, uprn: "12", purchid: "12", owning_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, "sales", %w[wrong_uprn], []) |
||||
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 sales addresses CSV for test organisation to data_provider3@example.com, data_provider4@example.com") |
||||
task.invoke |
||||
end |
||||
end |
||||
|
||||
context "when org is included in skip_uprn_issue_organisations list" do |
||||
before do |
||||
create_list(:sales_log, 5, :completed, :imported, saledate: Time.zone.local(2023, 9, 9), uprn_known: 1, uprn: "12", purchid: "12", owning_organisation: organisation, created_by: organisation.users.first) |
||||
end |
||||
|
||||
it "does not enqueue the job" do |
||||
expect { task.invoke("#{organisation.id} 4") }.not_to enqueue_job(EmailMissingAddressesCsvJob) |
||||
end |
||||
end |
||||
|
||||
context "when skip_uprn_issue_organisations list is provided" 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(:sales_log, 5, :completed, :imported, saledate: Time.zone.local(2023, 9, 9), uprn_known: 1, uprn: "12", purchid: "12", owning_organisation: organisation, created_by: organisation.users.first) |
||||
end |
||||
|
||||
it "does enqueues the job with correct skip_uprn_issue_organisations" do |
||||
expect { task.invoke("100 400") }.to enqueue_job(EmailMissingAddressesCsvJob).with(include(data_provider.id, data_provider2.id), organisation, "sales", %w[wrong_uprn], [100, 400]) |
||||
end |
||||
end |
||||
end |
||||
end |
||||
|
||||
describe ":create_lettings_addresses_csv", type: :task do |
||||
subject(:task) { Rake::Task["correct_addresses:create_lettings_addresses_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") } |
||||
|
||||
context "and organisation ID is provided" do |
||||
it "enqueues the job with correct organisation" do |
||||
expect { task.invoke(organisation.id) }.to enqueue_job(CreateAddressesCsvJob).with(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("Creating lettings addresses CSV for test organisation") |
||||
task.invoke(organisation.id) |
||||
end |
||||
end |
||||
|
||||
context "when organisation with given ID cannot be found" do |
||||
it "prints out error" do |
||||
expect(Rails.logger).to receive(:error).with("Organisation with ID fake not found") |
||||
task.invoke("fake") |
||||
end |
||||
end |
||||
|
||||
context "when organisation ID is not given" do |
||||
it "raises an error" do |
||||
expect { task.invoke }.to raise_error(RuntimeError, "Usage: rake correct_addresses:create_lettings_addresses_csv['organisation_id']") |
||||
end |
||||
end |
||||
end |
||||
end |
||||
|
||||
describe ":create_sales_addresses_csv", type: :task do |
||||
subject(:task) { Rake::Task["correct_addresses:create_sales_addresses_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") } |
||||
|
||||
context "and organisation ID is provided" do |
||||
it "enqueues the job with correct organisation" do |
||||
expect { task.invoke(organisation.id) }.to enqueue_job(CreateAddressesCsvJob).with(organisation, "sales") |
||||
end |
||||
|
||||
it "prints out the jobs enqueued" do |
||||
expect(Rails.logger).to receive(:info).with(nil) |
||||
expect(Rails.logger).to receive(:info).with("Creating sales addresses CSV for test organisation") |
||||
task.invoke(organisation.id) |
||||
end |
||||
end |
||||
|
||||
context "when organisation with given ID cannot be found" do |
||||
it "prints out error" do |
||||
expect(Rails.logger).to receive(:error).with("Organisation with ID fake not found") |
||||
task.invoke("fake") |
||||
end |
||||
end |
||||
|
||||
context "when organisation ID is not given" do |
||||
it "raises an error" do |
||||
expect { task.invoke }.to raise_error(RuntimeError, "Usage: rake correct_addresses:create_sales_addresses_csv['organisation_id']") |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,512 @@
|
||||
require "rails_helper" |
||||
|
||||
RSpec.describe Csv::MissingAddressesCsvService do |
||||
let(:organisation) { create(:organisation, name: "Address org") } |
||||
let(:user) { create(:user, organisation:, email: "testy@example.com") } |
||||
let(:service) { described_class.new(organisation, skip_uprn_issue_organisations) } |
||||
let(:skip_uprn_issue_organisations) { [100, 200] } |
||||
|
||||
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 |
||||
|
||||
describe "#create_missing_lettings_addresses_csv" do |
||||
let!(:lettings_log) 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: nil, |
||||
town_or_city: nil, |
||||
old_id: "old_id", |
||||
old_form_id: "old_form_id", |
||||
needstype: 1, |
||||
uprn_known: 0) |
||||
end |
||||
|
||||
let!(:lettings_log_missing_town) 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 |
||||
|
||||
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_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 |
||||
end |
||||
|
||||
context "when the organisation has logs with missing addresses only" do |
||||
before do |
||||
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 |
||||
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 |
||||
expect(csv).to eq(expected_content) |
||||
end |
||||
end |
||||
|
||||
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_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 |
||||
|
||||
context "and the organisation is marked as an organisation to skip" do |
||||
let(:skip_uprn_issue_organisations) { [organisation.id] } |
||||
|
||||
it "returns nil" do |
||||
expect(service.create_missing_lettings_addresses_csv).to be_nil |
||||
end |
||||
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_missing_town.update!(needstype: 2) |
||||
lettings_log_wrong_uprn.update!(needstype: 2) |
||||
end |
||||
|
||||
it "returns nil" do |
||||
expect(service.create_missing_lettings_addresses_csv).to be_nil |
||||
end |
||||
end |
||||
|
||||
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_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 |
||||
expect(service.create_missing_lettings_addresses_csv).to be_nil |
||||
end |
||||
end |
||||
|
||||
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_missing_town.update!(address_line1: "address_line1", town_or_city: "towncity") |
||||
lettings_log_wrong_uprn.update!(uprn: "12") |
||||
end |
||||
|
||||
it "returns nil" do |
||||
expect(service.create_missing_lettings_addresses_csv).to be_nil |
||||
end |
||||
end |
||||
end |
||||
|
||||
describe "#create_missing_sales_addresses_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: nil, |
||||
town_or_city: nil, |
||||
old_id: "old_id", |
||||
old_form_id: "old_form_id", |
||||
uprn_known: 0) |
||||
end |
||||
|
||||
let!(:sales_log_missing_town) 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: "older_id", |
||||
old_form_id: "old_form_id", |
||||
uprn_known: 0) |
||||
end |
||||
|
||||
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_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 |
||||
end |
||||
|
||||
context "when the organisation has logs with missing addresses" do |
||||
before do |
||||
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 |
||||
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 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_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 |
||||
|
||||
context "and the organisation is marked as an organisation to skip" do |
||||
let(:skip_uprn_issue_organisations) { [organisation.id] } |
||||
|
||||
it "returns nil" do |
||||
expect(service.create_missing_sales_addresses_csv).to be_nil |
||||
end |
||||
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)) |
||||
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 |
||||
expect(service.create_missing_sales_addresses_csv).to be_nil |
||||
end |
||||
end |
||||
|
||||
context "when the organisation has address fields filled in" do |
||||
before do |
||||
sales_log.update!(town_or_city: "town", address_line1: "line1") |
||||
sales_log_missing_town.update!(town_or_city: "town") |
||||
sales_log_wrong_uprn.update!(uprn: "12") |
||||
end |
||||
|
||||
it "returns nil" do |
||||
expect(service.create_missing_sales_addresses_csv).to be_nil |
||||
end |
||||
end |
||||
end |
||||
|
||||
describe "#create_lettings_addresses_csv" do |
||||
context "when the organisation has lettings logs" do |
||||
let!(:lettings_log) do |
||||
create(:lettings_log, |
||||
tenancycode: "tenancycode1", |
||||
propcode: "propcode1", |
||||
startdate: Time.zone.local(2023, 4, 5), |
||||
created_by: user, |
||||
owning_organisation: organisation, |
||||
managing_organisation: organisation, |
||||
address_line1: "address", |
||||
town_or_city: "town", |
||||
old_id: "old_id_1", |
||||
old_form_id: "old_form_id_1", |
||||
needstype: 1, |
||||
uprn_known: 0) |
||||
end |
||||
|
||||
let!(:lettings_log_missing_address) 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: nil, |
||||
town_or_city: nil, |
||||
old_id: "old_id", |
||||
old_form_id: "old_form_id", |
||||
needstype: 1, |
||||
uprn_known: 0) |
||||
end |
||||
|
||||
let!(:lettings_log_missing_town) 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 |
||||
|
||||
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 |
||||
|
||||
let!(:lettings_log_not_imported) 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, |
||||
needstype: 1) |
||||
end |
||||
|
||||
before do |
||||
create(:lettings_log, managing_organisation: organisation, old_id: "exists", startdate: Time.zone.local(2022, 4, 5)) |
||||
end |
||||
|
||||
it "returns a csv with relevant logs" do |
||||
csv = CSV.parse(service.create_lettings_addresses_csv) |
||||
expect(csv.count).to eq(6) |
||||
expect(csv).to include([lettings_log.id.to_s, "2023-04-05", "tenancycode1", "propcode1", "testy@example.com", "Address org", "Address org", nil, "address", nil, "town", nil, nil]) |
||||
expect(csv).to include([lettings_log_missing_address.id.to_s, "2023-04-05", "tenancycode", "propcode", "testy@example.com", "Address org", "Address org", nil, nil, nil, nil, nil, nil]) |
||||
expect(csv).to include([lettings_log_missing_town.id.to_s, "2023-04-05", "tenancycode", "propcode", "testy@example.com", "Address org", "Address org", nil, "existing address", nil, nil, nil, nil]) |
||||
expect(csv).to include([lettings_log_wrong_uprn.id.to_s, "2023-04-05", "tenancycode", "propcode", "testy@example.com", "Address org", "Address org", "123", "Some Place", nil, "Bristol", nil, "BS1 1AD"]) |
||||
expect(csv).to include([lettings_log_not_imported.id.to_s, "2023-04-05", "tenancycode", "propcode", "testy@example.com", "Address org", "Address org", "123", "Some Place", nil, "Bristol", nil, "BS1 1AD"]) |
||||
end |
||||
end |
||||
|
||||
context "when the organisation does not have relevant lettings logs" do |
||||
before do |
||||
create(:lettings_log, managing_organisation: organisation, startdate: Time.zone.local(2022, 4, 5)) |
||||
end |
||||
|
||||
it "returns only headers" do |
||||
csv = service.create_lettings_addresses_csv |
||||
expect(csv).to eq "Log ID,Tenancy start date,Tenant code,Property reference,Log owner,Owning organisation,Managing organisation,UPRN,Address Line 1,Address Line 2 (optional),Town or City,County (optional),Property's postcode\n" |
||||
end |
||||
end |
||||
end |
||||
|
||||
describe "#create_sales_addresses_csv" do |
||||
context "when the organisation has sales" 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: "address", |
||||
town_or_city: "city", |
||||
old_id: "old_id_1", |
||||
old_form_id: "old_form_id_1", |
||||
uprn_known: 0) |
||||
end |
||||
|
||||
let!(:sales_log_missing_address) do |
||||
create(:sales_log, |
||||
purchid: "purchaser code", |
||||
saledate: Time.zone.local(2023, 4, 5), |
||||
created_by: user, |
||||
owning_organisation: organisation, |
||||
address_line1: nil, |
||||
town_or_city: nil, |
||||
old_id: "old_id", |
||||
old_form_id: "old_form_id", |
||||
uprn_known: 0) |
||||
end |
||||
|
||||
let!(:sales_log_missing_town) 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: "older_id", |
||||
old_form_id: "old_form_id", |
||||
uprn_known: 0) |
||||
end |
||||
|
||||
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 |
||||
|
||||
let!(:sales_log_not_imported) 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", |
||||
uprn_known: 1, |
||||
uprn_confirmed: 1, |
||||
la: nil) |
||||
end |
||||
|
||||
before do |
||||
create(:sales_log, :completed, saledate: Time.zone.local(2022, 4, 5)) |
||||
end |
||||
|
||||
it "returns a csv with relevant logs" do |
||||
csv = CSV.parse(service.create_sales_addresses_csv) |
||||
expect(csv.count).to eq(6) |
||||
expect(csv).to include([sales_log.id.to_s, "2023-04-05", "purchaser code", "testy@example.com", "Address org", nil, "address", nil, "city", nil, nil]) |
||||
expect(csv).to include([sales_log_missing_address.id.to_s, "2023-04-05", "purchaser code", "testy@example.com", "Address org", nil, nil, nil, nil, nil, nil]) |
||||
expect(csv).to include([sales_log_missing_town.id.to_s, "2023-04-05", "purchaser code", "testy@example.com", "Address org", nil, "existing address line 1", nil, nil, nil, nil]) |
||||
expect(csv).to include([sales_log_wrong_uprn.id.to_s, "2023-04-05", "purchaser code", "testy@example.com", "Address org", "123", "Some Place", nil, "Bristol", nil, "BS1 1AD"]) |
||||
expect(csv).to include([sales_log_not_imported.id.to_s, "2023-04-05", "purchaser code", "testy@example.com", "Address org", "123", "Some Place", nil, "Bristol", nil, "BS1 1AD"]) |
||||
end |
||||
end |
||||
|
||||
context "when the organisation does not have relevant sales logs" do |
||||
before do |
||||
create(:sales_log, :completed, saledate: Time.zone.local(2022, 4, 5)) |
||||
end |
||||
|
||||
it "returns only headers" do |
||||
csv = service.create_sales_addresses_csv |
||||
expect(csv).to eq("Log ID,Sale completion date,Purchaser code,Log owner,Owning organisation,UPRN,Address Line 1,Address Line 2 (optional),Town or City,County (optional),Property's postcode\n") |
||||
end |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue