Skip to content

Instantly share code, notes, and snippets.

@humphriesjm
Created May 13, 2019 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save humphriesjm/9ec3a706f67062520d34d88a05844453 to your computer and use it in GitHub Desktop.
Save humphriesjm/9ec3a706f67062520d34d88a05844453 to your computer and use it in GitHub Desktop.
Complete Contacts by Agent
SUPPORTED_ASSOCIATIONS = [:email_addresses, :names, :phone_numbers, :properties]
def ids_of_complete_contacts_for(user, without: [])
without = Array(without)
with = SUPPORTED_ASSOCIATIONS - without
# all contacts
q = user.contacts
# for associations we want to verify, add a WHERE EXISTS query for it
with.each do |assoc|
q = add_subquery(q, assoc, user, exists: true, user_sourced: false)
end
# add WHERE NOT for associations we don't care about
without.each do |assoc|
q = add_subquery(q, assoc, user, exists: false, user_sourced: false)
end
q.distinct.pluck(:id)
end
def class_of(assoc)
case assoc
when :email_addresses then EmailAddress
when :names then PersonName
when :phone_numbers then PhoneNumber
else raise "Don't know a class for association #{assoc.inspect}"
end
end
def add_subquery(q, assoc, user, exists:, user_sourced:)
op = if exists
"EXISTS"
else
"NOT EXISTS"
end
subq = if assoc == :properties
PropertyRelationship
.select("1")
.where("property_relationships.person_id = people.id")
else
class_of(assoc)
.select("1")
.where("#{class_of(assoc).table_name}.person_id = people.id")
end
if user_sourced
subq = if assoc == :properties
subq.joins(property: :sources)
else
subq.joins(:sources)
end
subq = subq.where(sources: {user_id: user.id})
end
q.where("#{op} (?)", subq)
end
def number_of_complete_contacts_supplied_by(user, complete_contact_ids, without: [])
without = Array(without)
with = SUPPORTED_ASSOCIATIONS - without
q = Person.where(id: complete_contact_ids)
with.each do |assoc|
q = add_subquery(q, assoc, user, exists: true, user_sourced: true)
end
without.each do |assoc|
q = add_subquery(q, assoc, user, exists: false, user_sourced: true)
end
q.count
end
def show_complete_contacts(u)
complete_contact_ids = Set.new
complete_contact_ids |= ids_of_complete_contacts_for(u) | ids_of_complete_contacts_for(u, without: :phone_numbers) | ids_of_complete_contacts_for(u, without: :email_addresses)
complete_contact_ids = complete_contact_ids.to_a
num_complete_contacts = complete_contact_ids.count
user_completed_contacts = number_of_complete_contacts_supplied_by(u, complete_contact_ids) + number_of_complete_contacts_supplied_by(u, complete_contact_ids, without: :phone_numbers) + number_of_complete_contacts_supplied_by(u, complete_contact_ids, without: :email_addresses)
num_complete_contacts_by_agent = user_completed_contacts
num_complete_contacts_by_first = num_complete_contacts - user_completed_contacts
puts "#{u.full_name},#{num_complete_contacts_by_agent},#{num_complete_contacts_by_first},#{num_complete_contacts}"
end
def show_brokerage_complete_contacts(brokerage_name)
users = User
.live_customers
.not_admin
.where(brokerage: brokerage_name)
puts "agent_name,num_complete_contacts_by_agent,num_complete_contacts_by_first,num_complete_contacts"
users.each do |user|
show_complete_contacts(user)
end
return nil
end
show_brokerage_complete_contacts('@properties')
show_brokerage_complete_contacts('Ansley')
show_brokerage_complete_contacts('Beverly-Hanks')
show_brokerage_complete_contacts('C21 Advantage Gold')
show_brokerage_complete_contacts('C21 Results')
show_brokerage_complete_contacts('Climb')
show_brokerage_complete_contacts('Dwellings')
show_brokerage_complete_contacts('Hometown Advisor')
show_brokerage_complete_contacts('KY Select')
show_brokerage_complete_contacts('Moving The Mitten')
show_brokerage_complete_contacts('One South')
show_brokerage_complete_contacts('Savvy + Co.')
show_brokerage_complete_contacts('Warburg')
show_brokerage_complete_contacts('West + Main')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment