From bdcc1e4b51b18bc1678ffc16e754b0bf65a4e503 Mon Sep 17 00:00:00 2001 From: Arthur Campbell Date: Fri, 14 Jul 2023 15:23:55 +0100 Subject: [PATCH] update path for duplicate logs in routes, update factories, refactor duplicates scope to eliminate raw sql --- app/models/lettings_log.rb | 30 ++++++++++++---------- app/models/log.rb | 4 +++ app/models/sales_log.rb | 4 --- config/routes.rb | 2 +- spec/factories/lettings_log.rb | 5 ++-- spec/helpers/duplicate_logs_helper_spec.rb | 4 +-- 6 files changed, 25 insertions(+), 24 deletions(-) diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index 6104ee3d5..0ba989472 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -45,32 +45,34 @@ class LettingsLog < Log scope :filter_by_location_postcode, ->(postcode_full) { left_joins(:location).where("REPLACE(locations.postcode, ' ', '') ILIKE ?", "%#{postcode_full.delete(' ')}%") } scope :search_by, lambda { |param| filter_by_location_postcode(param) - .or(filter_by_tenant_code(param)) - .or(filter_by_propcode(param)) - .or(filter_by_postcode(param)) - .or(filter_by_id(param)) + .or(filter_by_tenant_code(param)) + .or(filter_by_propcode(param)) + .or(filter_by_postcode(param)) + .or(filter_by_id(param)) } scope :after_date, ->(date) { where("lettings_logs.startdate >= ?", date) } scope :unresolved, -> { where(unresolved: true) } - scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org).or(where(managing_organisation: org)) } - scope :filter_by_owning_organisation, ->(owning_organisation, _user = nil) { where(owning_organisation:) } - scope :filter_by_managing_organisation, ->(managing_organisation, _user = nil) { where(managing_organisation:) } - + scope :age1_answered, -> { where.not(age1: nil).or(where(age1_known: 1)) } + scope :tcharge_answered, -> { where.not(tcharge: nil).or(where(household_charge: 1)).or(where(is_carehome: 1)) } + scope :chcharge_answered, -> { where.not(chcharge: nil).or(where(is_carehome: [nil, 0])) } + scope :location_answered, ->(log) { where(location_id: log.location_id).or(where(needstype: 1)) } + scope :postcode_answered, ->(log) { where(postcode_full: log.postcode_full).or(where(needstype: 2)) } scope :duplicate_logs, lambda { |log| visible - .where(log.slice(*DUPLICATE_LOG_ATTRIBUTES)) + .where(log.slice(*DUPLICATE_LOG_ATTRIBUTES)) .where.not(id: log.id) .where.not(startdate: nil) .where.not(sex1: nil) .where.not(ecstat1: nil) .where.not(needstype: nil) - .where("age1 IS NOT NULL OR age1_known = 1") - .where("tcharge IS NOT NULL OR household_charge = 1 OR is_carehome = 1") - .where("chcharge IS NOT NULL OR is_carehome IS NULL OR is_carehome = 0") - .where("location_id = ? OR needstype = 1", log.location_id) - .where("postcode_full = ? OR needstype = 2", log.postcode_full) + .age1_answered + .tcharge_answered + .chcharge_answered + .location_answered(log) + .postcode_answered(log) } + scope :all_duplicates, -> { select(:id, :propcode, :tenancycode).group(:tenancycode, :propcode, :age1).having("count(*) > 1").size } AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at].freeze OPTIONAL_FIELDS = %w[tenancycode propcode chcharge].freeze diff --git a/app/models/log.rb b/app/models/log.rb index e597a3bc5..1d7e000bb 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -82,6 +82,10 @@ class Log < ApplicationRecord collection_start_year end + def setup_completed? + form.setup_sections.all? { |sections| sections.subsections.all? { |subsection| subsection.status(self) == :completed } } + end + def lettings? false end diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index 4e829d760..fa5f623bd 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -120,10 +120,6 @@ class SalesLog < Log collection_start_year < 2023 end - def setup_completed? - form.setup_sections.all? { |sections| sections.subsections.all? { |subsection| subsection.status(self) == :completed } } - end - def unresolved false end diff --git a/config/routes.rb b/config/routes.rb index ab3643e6c..43d0c7232 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -105,7 +105,7 @@ Rails.application.routes.draw do end end - resources :duplicate_logs, only: [:index] + resources :duplicate_logs, only: [:index], path: "/duplicate-logs" resources :users do get "edit-dpo", to: "users#dpo" diff --git a/spec/factories/lettings_log.rb b/spec/factories/lettings_log.rb index ae203b79e..59eb48da2 100644 --- a/spec/factories/lettings_log.rb +++ b/spec/factories/lettings_log.rb @@ -35,12 +35,13 @@ FactoryBot.define do hhmemb { 1 } end trait :duplicate do + setup_completed status { 1 } - needstype { 1 } tenancycode { "same tenancy code" } postcode_full { "A1 1AA" } uprn_known { 0 } declaration { 1 } + age1_known { 0 } age1 { 18 } sex1 { "M" } ecstat1 { 0 } @@ -51,8 +52,6 @@ FactoryBot.define do supcharg { 35 } tcharge { 325 } propcode { "same property code" } - renewal { 1 } - rent_type { 1 } startdate { Time.zone.today } end trait :completed do diff --git a/spec/helpers/duplicate_logs_helper_spec.rb b/spec/helpers/duplicate_logs_helper_spec.rb index 8e728f4dc..388a5a76b 100644 --- a/spec/helpers/duplicate_logs_helper_spec.rb +++ b/spec/helpers/duplicate_logs_helper_spec.rb @@ -32,7 +32,7 @@ RSpec.describe DuplicateLogsHelper do context "when another user in the same org has created a duplicate lettings log" do let!(:duplicate_lettings_log) { create(:lettings_log, :duplicate, created_by: user_same_org) } - it "returns the ids of the duplicates in an array under the lettings key in the duplicates hash" do + it "returns the ids of the duplicates in a hash under the lettings key" do expect(result).to be_a Hash expect(result[:lettings].values).to match_array [[lettings_log.id, duplicate_lettings_log.id]] end @@ -41,7 +41,7 @@ RSpec.describe DuplicateLogsHelper do context "when another user in the same org has created a duplicate sales log" do let!(:duplicate_sales_log) { create(:sales_log, :duplicate, created_by: user_same_org) } - it "returns the ids of the duplicates in an array under the sales key in the duplicates hash" do + it "returns the ids of the duplicates in a hash under the sales key" do expect(result).to be_a Hash expect(result[:sales].values).to match_array [[sales_log.id, duplicate_sales_log.id]] end