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.
 
 
 
 

39 lines
1.8 KiB

namespace :correct_addresses do
# rubocop:disable Lint/ConstantDefinitionInBlock
MISSING_ADDRESSES_THRESHOLD = 50
# rubocop:enable Lint/ConstantDefinitionInBlock
desc "Send missing addresses csv"
task :send_missing_addresses_csv, %i[] => :environment do |_task, _args|
Organisation.all.each do |organisation|
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).count
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).count
logs_impacted_by_uprn_issue = organisation.managed_lettings_logs
.imported
.filter_by_year(2023)
.where(needstype: 1)
.where.not(uprn: nil)
.where("uprn = propcode OR uprn = tenancycode or town_or_city = 'Bristol'")
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?
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")
Rails.logger.info("Sending missing 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
end