From 653900542c40dafc545be41b93d2b6db25665861 Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 3 Oct 2023 15:45:03 +0100 Subject: [PATCH] Extract some repeating scopes --- app/models/log.rb | 3 +++ .../csv/missing_addresses_csv_service.rb | 22 +++++----------- lib/tasks/send_missing_addresses_csv.rake | 26 ++++++------------- 3 files changed, 17 insertions(+), 34 deletions(-) diff --git a/app/models/log.rb b/app/models/log.rb index c1778b299..5ba887c7a 100644 --- a/app/models/log.rb +++ b/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 diff --git a/app/services/csv/missing_addresses_csv_service.rb b/app/services/csv/missing_addresses_csv_service.rb index 6bad2e8e9..2d9ca65a9 100644 --- a/app/services/csv/missing_addresses_csv_service.rb +++ b/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 diff --git a/lib/tasks/send_missing_addresses_csv.rake b/lib/tasks/send_missing_addresses_csv.rake index dc79577f6..91d21ee7d 100644 --- a/lib/tasks/send_missing_addresses_csv.rake +++ b/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