Browse Source

update path for duplicate logs in routes, update factories, refactor duplicates scope to eliminate raw sql

pull/1776/head
Arthur Campbell 3 years ago committed by Kat
parent
commit
bdcc1e4b51
  1. 30
      app/models/lettings_log.rb
  2. 4
      app/models/log.rb
  3. 4
      app/models/sales_log.rb
  4. 2
      config/routes.rb
  5. 5
      spec/factories/lettings_log.rb
  6. 4
      spec/helpers/duplicate_logs_helper_spec.rb

30
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 :filter_by_location_postcode, ->(postcode_full) { left_joins(:location).where("REPLACE(locations.postcode, ' ', '') ILIKE ?", "%#{postcode_full.delete(' ')}%") }
scope :search_by, lambda { |param| scope :search_by, lambda { |param|
filter_by_location_postcode(param) filter_by_location_postcode(param)
.or(filter_by_tenant_code(param)) .or(filter_by_tenant_code(param))
.or(filter_by_propcode(param)) .or(filter_by_propcode(param))
.or(filter_by_postcode(param)) .or(filter_by_postcode(param))
.or(filter_by_id(param)) .or(filter_by_id(param))
} }
scope :after_date, ->(date) { where("lettings_logs.startdate >= ?", date) } scope :after_date, ->(date) { where("lettings_logs.startdate >= ?", date) }
scope :unresolved, -> { where(unresolved: true) } scope :unresolved, -> { where(unresolved: true) }
scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org).or(where(managing_organisation: org)) } 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 :age1_answered, -> { where.not(age1: nil).or(where(age1_known: 1)) }
scope :filter_by_managing_organisation, ->(managing_organisation, _user = nil) { where(managing_organisation:) } 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| scope :duplicate_logs, lambda { |log|
visible visible
.where(log.slice(*DUPLICATE_LOG_ATTRIBUTES)) .where(log.slice(*DUPLICATE_LOG_ATTRIBUTES))
.where.not(id: log.id) .where.not(id: log.id)
.where.not(startdate: nil) .where.not(startdate: nil)
.where.not(sex1: nil) .where.not(sex1: nil)
.where.not(ecstat1: nil) .where.not(ecstat1: nil)
.where.not(needstype: nil) .where.not(needstype: nil)
.where("age1 IS NOT NULL OR age1_known = 1") .age1_answered
.where("tcharge IS NOT NULL OR household_charge = 1 OR is_carehome = 1") .tcharge_answered
.where("chcharge IS NOT NULL OR is_carehome IS NULL OR is_carehome = 0") .chcharge_answered
.where("location_id = ? OR needstype = 1", log.location_id) .location_answered(log)
.where("postcode_full = ? OR needstype = 2", log.postcode_full) .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 AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at].freeze
OPTIONAL_FIELDS = %w[tenancycode propcode chcharge].freeze OPTIONAL_FIELDS = %w[tenancycode propcode chcharge].freeze

4
app/models/log.rb

@ -82,6 +82,10 @@ class Log < ApplicationRecord
collection_start_year collection_start_year
end end
def setup_completed?
form.setup_sections.all? { |sections| sections.subsections.all? { |subsection| subsection.status(self) == :completed } }
end
def lettings? def lettings?
false false
end end

4
app/models/sales_log.rb

@ -120,10 +120,6 @@ class SalesLog < Log
collection_start_year < 2023 collection_start_year < 2023
end end
def setup_completed?
form.setup_sections.all? { |sections| sections.subsections.all? { |subsection| subsection.status(self) == :completed } }
end
def unresolved def unresolved
false false
end end

2
config/routes.rb

@ -105,7 +105,7 @@ Rails.application.routes.draw do
end end
end end
resources :duplicate_logs, only: [:index] resources :duplicate_logs, only: [:index], path: "/duplicate-logs"
resources :users do resources :users do
get "edit-dpo", to: "users#dpo" get "edit-dpo", to: "users#dpo"

5
spec/factories/lettings_log.rb

@ -35,12 +35,13 @@ FactoryBot.define do
hhmemb { 1 } hhmemb { 1 }
end end
trait :duplicate do trait :duplicate do
setup_completed
status { 1 } status { 1 }
needstype { 1 }
tenancycode { "same tenancy code" } tenancycode { "same tenancy code" }
postcode_full { "A1 1AA" } postcode_full { "A1 1AA" }
uprn_known { 0 } uprn_known { 0 }
declaration { 1 } declaration { 1 }
age1_known { 0 }
age1 { 18 } age1 { 18 }
sex1 { "M" } sex1 { "M" }
ecstat1 { 0 } ecstat1 { 0 }
@ -51,8 +52,6 @@ FactoryBot.define do
supcharg { 35 } supcharg { 35 }
tcharge { 325 } tcharge { 325 }
propcode { "same property code" } propcode { "same property code" }
renewal { 1 }
rent_type { 1 }
startdate { Time.zone.today } startdate { Time.zone.today }
end end
trait :completed do trait :completed do

4
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 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) } 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).to be_a Hash
expect(result[:lettings].values).to match_array [[lettings_log.id, duplicate_lettings_log.id]] expect(result[:lettings].values).to match_array [[lettings_log.id, duplicate_lettings_log.id]]
end end
@ -41,7 +41,7 @@ RSpec.describe DuplicateLogsHelper do
context "when another user in the same org has created a duplicate sales log" 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) } 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).to be_a Hash
expect(result[:sales].values).to match_array [[sales_log.id, duplicate_sales_log.id]] expect(result[:sales].values).to match_array [[sales_log.id, duplicate_sales_log.id]]
end end

Loading…
Cancel
Save