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. 20
      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

20
app/models/lettings_log.rb

@ -52,11 +52,12 @@ class LettingsLog < Log
}
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))
@ -65,12 +66,13 @@ class LettingsLog < Log
.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

4
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

4
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

2
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"

5
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

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
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

Loading…
Cancel
Save