Submit social housing lettings and sales data (CORE)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
6.6 KiB

CLDC-2810 Send missing addresses template (#1952) * 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 var
1 year ago
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