Browse Source

Extract some repeating scopes

pull/1953/head
Kat 3 years ago
parent
commit
653900542c
  1. 3
      app/models/log.rb
  2. 22
      app/services/csv/missing_addresses_csv_service.rb
  3. 26
      lib/tasks/send_missing_addresses_csv.rake

3
app/models/log.rb

@ -46,6 +46,9 @@ class Log < ApplicationRecord
scope :created_by, ->(user) { where(created_by: user) }
scope :imported, -> { where.not(old_id: nil) }
scope :not_imported, -> { where(old_id: nil) }
scope :has_old_form_id, -> { where.not(old_form_id: nil) }
scope :imported_2023_with_old_form_id, -> { imported.filter_by_year(2023).has_old_form_id }
scope :imported_2023, -> { imported.filter_by_year(2023) }
attr_accessor :skip_update_status, :skip_update_uprn_confirmed

22
app/services/csv/missing_addresses_csv_service.rb

@ -6,24 +6,19 @@ module Csv
def create_missing_lettings_addresses_csv
logs_with_missing_addresses = @organisation.managed_lettings_logs
.imported
.filter_by_year(2023)
.imported_2023_with_old_form_id
.where(needstype: 1, address_line1: nil, town_or_city: nil, uprn_known: [0, nil])
.where.not(old_form_id: nil)
logs_with_missing_town_or_city = @organisation.managed_lettings_logs
.imported
.filter_by_year(2023)
.imported_2023_with_old_form_id
.where(needstype: 1, town_or_city: nil, uprn_known: [0, nil])
.where.not(old_form_id: nil)
.where.not(address_line1: nil)
logs_with_wrong_uprn = if JSON.parse(ENV["SKIP_UPRN_ISSUE_ORG_IDS"]).include?(@organisation.id)
[]
else
@organisation.managed_lettings_logs
.imported
.filter_by_year(2023)
.imported_2023
.where(needstype: 1)
.where.not(uprn: nil)
.where("uprn = propcode OR uprn = tenancycode OR town_or_city = 'Bristol'")
@ -50,24 +45,19 @@ module Csv
def create_missing_sales_addresses_csv
logs_with_missing_addresses = @organisation.sales_logs
.imported
.filter_by_year(2023)
.imported_2023_with_old_form_id
.where(address_line1: nil, town_or_city: nil, uprn_known: [0, nil])
.where.not(old_form_id: nil)
logs_with_missing_town_or_city = @organisation.sales_logs
.imported
.filter_by_year(2023)
.imported_2023_with_old_form_id
.where(town_or_city: nil, uprn_known: [0, nil])
.where.not(old_form_id: nil)
.where.not(address_line1: nil)
logs_with_wrong_uprn = if JSON.parse(ENV["SKIP_UPRN_ISSUE_ORG_IDS"]).include?(@organisation.id)
[]
else
@organisation.sales_logs
.imported
.filter_by_year(2023)
.imported_2023
.where.not(uprn: nil)
.where("uprn = purchid OR town_or_city = 'Bristol'")
end

26
lib/tasks/send_missing_addresses_csv.rake

@ -3,24 +3,19 @@ namespace :correct_addresses do
task :send_missing_addresses_lettings_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
.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
.filter_by_year(2023)
.imported_2023_with_old_form_id
.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 = if JSON.parse(ENV["SKIP_UPRN_ISSUE_ORG_IDS"]).include?(organisation.id)
[]
else
organisation.managed_lettings_logs
.imported
.filter_by_year(2023)
.imported_2023
.where(needstype: 1)
.where.not(uprn: nil)
.where("uprn = propcode OR uprn = tenancycode OR town_or_city = 'Bristol'")
@ -42,24 +37,19 @@ namespace :correct_addresses do
task :send_missing_addresses_sales_csv, %i[] => :environment do |_task, _args|
Organisation.all.each do |organisation|
logs_impacted_by_missing_address = 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).count
.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
.filter_by_year(2023)
.imported_2023_with_old_form_id
.where(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 = if JSON.parse(ENV["SKIP_UPRN_ISSUE_ORG_IDS"]).include?(organisation.id)
[]
else
organisation.sales_logs
.imported
.filter_by_year(2023)
.imported_2023
.where.not(uprn: nil)
.where("uprn = purchid OR town_or_city = 'Bristol'")
end

Loading…
Cancel
Save