Browse Source

Update service to create mising town or city templates

pull/1953/head
Kat 3 years ago
parent
commit
a2e6be4d71
  1. 64
      app/services/csv/missing_addresses_csv_service.rb
  2. 2
      spec/fixtures/files/missing_lettings_logs_town_or_city.csv
  3. 2
      spec/fixtures/files/missing_sales_logs_town_or_city.csv
  4. 99
      spec/services/csv/missing_addresses_csv_service_spec.rb

64
app/services/csv/missing_addresses_csv_service.rb

@ -8,30 +8,70 @@ module Csv
logs_with_missing_addresses = @organisation.managed_lettings_logs.imported.filter_by_year(2023).where(needstype: 1, address_line1: nil, town_or_city: nil, uprn_known: [0, nil]).where.not(old_form_id: nil) logs_with_missing_addresses = @organisation.managed_lettings_logs.imported.filter_by_year(2023).where(needstype: 1, address_line1: nil, town_or_city: nil, uprn_known: [0, nil]).where.not(old_form_id: nil)
return if logs_with_missing_addresses.empty? return if logs_with_missing_addresses.empty?
CSV.generate(headers: true) do |csv| generate_missing_lettings_addresses_csv(logs_with_missing_addresses)
csv << ["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"]
logs_with_missing_addresses.each do |log|
csv << [log.startdate&.to_date, log.tenancycode, log.propcode, log.created_by&.email, log.owning_organisation&.name, log.managing_organisation&.name, log.address_line1, log.address_line2, log.town_or_city, log.county, log.postcode_full, log.la]
end
end
end end
def create_missing_sales_addresses_csv def create_missing_sales_addresses_csv
logs_with_missing_addresses = @organisation.sales_logs.imported.filter_by_year(2023).where(address_line1: nil, town_or_city: nil, uprn_known: [0, nil]).where.not(old_form_id: nil) logs_with_missing_addresses = @organisation.sales_logs.imported.filter_by_year(2023).where(address_line1: nil, town_or_city: nil, uprn_known: [0, nil]).where.not(old_form_id: nil)
return if logs_with_missing_addresses.empty? 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?
generate_missing_sales_addresses_csv(logs_with_missing_town_or_city)
end
private
def generate_missing_lettings_addresses_csv(logs)
CSV.generate(headers: true) do |csv| CSV.generate(headers: true) do |csv|
csv << ["Sale completion date", "Purchaser code", "Log owner", "Owning organisation name", "Address line 1", "Address line 2", "Town or City", "County", "Postcode", "Local authority"] csv << ["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"]
logs_with_missing_addresses.each do |log| logs.each do |log|
csv << [log.saledate&.to_date, log.purchid, log.created_by&.email, log.owning_organisation&.name, log.address_line1, log.address_line2, log.town_or_city, log.county, log.postcode_full, log.la] csv << [log.startdate&.to_date,
log.tenancycode,
log.propcode,
log.created_by&.email,
log.owning_organisation&.name,
log.managing_organisation&.name,
log.address_line1,
log.address_line2,
log.town_or_city,
log.county,
log.postcode_full,
log.la]
end end
end end
end end
def create_missing_lettings_town_or_city_csv; end def generate_missing_sales_addresses_csv(logs)
CSV.generate(headers: true) do |csv|
csv << ["Sale completion date", "Purchaser code", "Log owner", "Owning organisation name", "Address line 1", "Address line 2", "Town or City", "County", "Postcode", "Local authority"]
def create_missing_sales_town_or_city_csv; end logs.each do |log|
csv << [log.saledate&.to_date,
log.purchid,
log.created_by&.email,
log.owning_organisation&.name,
log.address_line1,
log.address_line2,
log.town_or_city,
log.county,
log.postcode_full,
log.la]
end
end
end
end end
end end

2
spec/fixtures/files/missing_lettings_logs_town_or_city.csv vendored

@ -0,0 +1,2 @@
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
2023-04-05,tenancycode,propcode,testy@example.com,Address test,Address test,existing address,,,,,
1 Tenancy start date Tenant code Property code Log owner Owning organisation name Managing organisation name Address line 1 Address line 2 Town or City County Postcode Local authority
2 2023-04-05 tenancycode propcode testy@example.com Address test Address test existing address

2
spec/fixtures/files/missing_sales_logs_town_or_city.csv vendored

@ -0,0 +1,2 @@
Sale completion date,Purchaser code,Log owner,Owning organisation name,Address line 1,Address line 2,Town or City,County,Postcode,Local authority
2023-04-05,purchaser code,testy@example.com,Address test,existing address line 1,,,,,
1 Sale completion date Purchaser code Log owner Owning organisation name Address line 1 Address line 2 Town or City County Postcode Local authority
2 2023-04-05 purchaser code testy@example.com Address test existing address line 1

99
spec/services/csv/missing_addresses_csv_service_spec.rb

@ -103,4 +103,103 @@ RSpec.describe Csv::MissingAddressesCsvService do
end end
end end
end end
describe "#create_missing_lettings_town_or_city_csv" do
let!(:lettings_log) do
create(:lettings_log,
tenancycode: "tenancycode",
propcode: "propcode",
startdate: Time.zone.local(2023, 4, 5),
created_by: user,
owning_organisation: organisation,
managing_organisation: organisation,
address_line1: "existing address",
town_or_city: nil,
old_id: "old_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
it "returns a csv with relevant logs" do
expected_content = CSV.read("spec/fixtures/files/missing_lettings_logs_town_or_city.csv")
csv = CSV.parse(service.create_missing_lettings_town_or_city_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
before do
lettings_log.update!(startdate: Time.zone.local(2022, 4, 5))
end
it "returns nil" do
expect(service.create_missing_lettings_town_or_city_csv).to be_nil
end
end
context "when the organisation has any address field 1 not set" do
before do
lettings_log.update!(address_line1: nil)
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 = CSV.read("spec/fixtures/files/missing_sales_logs_town_or_city.csv")
csv = CSV.parse(service.create_missing_sales_town_or_city_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
before do
sales_log.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
end
end
context "when the organisation has any address fields not filled in" do
before do
sales_log.update!(address_line1: nil)
end
it "returns nil" do
expect(service.create_missing_sales_town_or_city_csv).to be_nil
end
end
end
end end

Loading…
Cancel
Save