Browse Source

create helper module to find all logs for a given user that have duplicates

write associated tests
update methods on user model to retrieve logs related to a given user
create a scope on logs to retrieve logs created by a given user
pull/1776/head
Arthur Campbell 3 years ago committed by Kat
parent
commit
7c1b1f7312
  1. 32
      app/helpers/duplicate_logs_helper.rb
  2. 1
      app/models/log.rb
  3. 12
      app/models/user.rb
  4. 66
      spec/helpers/duplicate_logs_helper_spec.rb

32
app/helpers/duplicate_logs_helper.rb

@ -27,4 +27,36 @@ module DuplicateLogsHelper
first_remaining_duplicate_id = all_duplicates.map(&:id).reject { |id| id == log.id }.first first_remaining_duplicate_id = all_duplicates.map(&:id).reject { |id| id == log.id }.first
send("#{log.model_name.param_key}_#{page_id}_path", log, referrer: "duplicate_logs", first_remaining_duplicate_id:, original_log_id:) send("#{log.model_name.param_key}_#{page_id}_path", log, referrer: "duplicate_logs", first_remaining_duplicate_id:, original_log_id:)
end end
def duplicates_for_user(user)
duplicate_sets = { lettings: [], sales: [] }
duplicate_lettings_ids = Set.new
duplicate_sales_ids = Set.new
user.lettings_logs(created_by: true).each do |log|
next if duplicate_lettings_ids.include? log.id
duplicates = LettingsLog.filter_by_organisation(user.organisation).duplicate_logs(log)
next unless duplicates.any?
duplicate_ids = [log.id, *duplicates.map(&:id)]
duplicate_sets[:lettings] << duplicate_ids
duplicate_lettings_ids << duplicate_ids
end
user.sales_logs(created_by: true).each do |log|
next if duplicate_sales_ids.include? log.id
duplicates = SalesLog.filter_by_organisation(user.organisation).duplicate_logs(log)
next unless duplicates.any?
duplicate_ids = [log.id, *duplicates.map(&:id)]
duplicate_sets[:sales] << duplicate_ids
duplicate_sales_ids << duplicate_ids
end
return if duplicate_lettings_ids.empty? && duplicate_sales_ids.empty?
duplicate_sets
end
end end

1
app/models/log.rb

@ -29,6 +29,7 @@ class Log < ApplicationRecord
scope :visible, -> { where(status: %w[not_started in_progress completed]) } scope :visible, -> { where(status: %w[not_started in_progress completed]) }
scope :exportable, -> { where(status: %w[not_started in_progress completed deleted]) } scope :exportable, -> { where(status: %w[not_started in_progress completed deleted]) }
scope :created_by, ->(user) { where(created_by: user) }
scope :filter_by_status, ->(status, _user = nil) { where status: } scope :filter_by_status, ->(status, _user = nil) { where status: }
scope :filter_by_years, lambda { |years, _user = nil| scope :filter_by_years, lambda { |years, _user = nil|
first_year = years.shift first_year = years.shift

12
app/models/user.rb

@ -75,16 +75,20 @@ class User < ApplicationRecord
scope :deactivated, -> { where(active: false) } scope :deactivated, -> { where(active: false) }
scope :active_status, -> { where(active: true).where.not(last_sign_in_at: nil) } scope :active_status, -> { where(active: true).where.not(last_sign_in_at: nil) }
def lettings_logs def lettings_logs(created_by: false)
if support? if created_by
LettingsLog.created_by(self)
elsif support?
LettingsLog.all LettingsLog.all
else else
LettingsLog.filter_by_organisation(organisation.absorbed_organisations + [organisation]) LettingsLog.filter_by_organisation(organisation.absorbed_organisations + [organisation])
end end
end end
def sales_logs def sales_logs(created_by: false)
if support? if created_by
SalesLog.created_by(self)
elsif support?
SalesLog.all SalesLog.all
else else
SalesLog.filter_by_owning_organisation(organisation.absorbed_organisations + [organisation]) SalesLog.filter_by_owning_organisation(organisation.absorbed_organisations + [organisation])

66
spec/helpers/duplicate_logs_helper_spec.rb

@ -0,0 +1,66 @@
require "rails_helper"
RSpec.describe DuplicateLogsHelper do
let(:org) { create(:organisation) }
let(:other_org) { create(:organisation) }
let(:current_user) { create(:user, organisation: org) }
let(:user_same_org) { create(:user, organisation: org) }
let(:user_other_org) { create(:user, organisation: other_org) }
let(:result) { duplicates_for_user(current_user) }
let!(:lettings_log) { create(:lettings_log, :duplicate, created_by: current_user) }
let!(:sales_log) { create(:sales_log, :duplicate, created_by: current_user) }
context "when there are no duplicates" do
it "returns nil" do
expect(result).to be nil
end
end
context "when there are duplicates in another org" do
before do
create(:lettings_log, :duplicate, created_by: user_other_org)
create(:sales_log, :duplicate, created_by: user_other_org)
end
it "returns nil" do
expect(result).to be nil
end
end
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
expect(result).to be_a Hash
expect(result[:lettings]).to match_array [[lettings_log.id, duplicate_lettings_log.id]]
end
end
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
expect(result).to be_a Hash
expect(result[:sales]).to match_array [[sales_log.id, duplicate_sales_log.id]]
end
end
context "when there are multiple sets of duplicates across lettings and sales" do
let!(:duplicate_lettings_log) { create(:lettings_log, :duplicate, created_by: user_same_org) }
let!(:duplicate_sales_log) { create(:sales_log, :duplicate, created_by: user_same_org) }
let!(:further_sales_log) { create(:sales_log, :duplicate, purchid: "further", created_by: current_user) }
let!(:further_duplicate_sales_logs) { create_list(:sales_log, 2, :duplicate, purchid: "further", created_by: user_same_org) }
it "returns them all with no repeats" do
expected_sales_duplicates_result = [
[sales_log.id, duplicate_sales_log.id],
[further_sales_log.id, *further_duplicate_sales_logs.map(&:id)],
]
expect(result[:lettings]).to match_array [[lettings_log.id, duplicate_lettings_log.id]]
expect(result[:sales]).to match_array expected_sales_duplicates_result
end
end
end
Loading…
Cancel
Save