2022-01-25 16:15:18 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Finder for retrieving contacts scoped to a group
|
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# current_user - user performing the action. Must have the correct permission level for the group.
|
|
|
|
# params:
|
|
|
|
# group: Group, required
|
2022-06-10 11:09:22 -04:00
|
|
|
# search: String, optional
|
|
|
|
# state: CustomerRelations::ContactStateEnum, optional
|
2022-06-16 08:09:26 -04:00
|
|
|
# ids: int[], optional
|
2022-01-25 16:15:18 -05:00
|
|
|
module Crm
|
|
|
|
class ContactsFinder
|
|
|
|
include Gitlab::Allowable
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
attr_reader :params, :current_user
|
|
|
|
|
2022-08-15 14:12:51 -04:00
|
|
|
def self.counts_by_state(current_user, params = {})
|
|
|
|
params = params.merge(sort: nil)
|
|
|
|
new(current_user, params).execute.counts_by_state
|
|
|
|
end
|
|
|
|
|
2022-01-25 16:15:18 -05:00
|
|
|
def initialize(current_user, params = {})
|
|
|
|
@current_user = current_user
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
return CustomerRelations::Contact.none unless root_group
|
|
|
|
|
2022-06-10 11:09:22 -04:00
|
|
|
contacts = root_group.contacts
|
2022-06-16 08:09:26 -04:00
|
|
|
contacts = by_ids(contacts)
|
2022-06-10 11:09:22 -04:00
|
|
|
contacts = by_state(contacts)
|
|
|
|
contacts = by_search(contacts)
|
2022-08-15 14:12:51 -04:00
|
|
|
sort_contacts(contacts)
|
2022-01-25 16:15:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-08-15 14:12:51 -04:00
|
|
|
def sort_contacts(contacts)
|
|
|
|
return contacts.sort_by_name unless @params.key?(:sort)
|
|
|
|
return contacts if @params[:sort].nil?
|
|
|
|
|
|
|
|
field = @params[:sort][:field]
|
|
|
|
direction = @params[:sort][:direction]
|
|
|
|
|
|
|
|
if field == 'organization'
|
|
|
|
contacts.sort_by_organization(direction)
|
|
|
|
else
|
|
|
|
contacts.sort_by_field(field, direction)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-25 16:15:18 -05:00
|
|
|
def root_group
|
|
|
|
strong_memoize(:root_group) do
|
|
|
|
group = params[:group]&.root_ancestor
|
|
|
|
|
|
|
|
next unless can?(@current_user, :read_crm_contact, group)
|
|
|
|
|
|
|
|
group
|
|
|
|
end
|
|
|
|
end
|
2022-06-10 11:09:22 -04:00
|
|
|
|
|
|
|
def by_search(contacts)
|
|
|
|
return contacts unless search?
|
|
|
|
|
|
|
|
contacts.search(params[:search])
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_state(contacts)
|
|
|
|
return contacts unless state?
|
|
|
|
|
|
|
|
contacts.search_by_state(params[:state])
|
|
|
|
end
|
|
|
|
|
2022-06-16 08:09:26 -04:00
|
|
|
def by_ids(contacts)
|
|
|
|
return contacts unless ids?
|
|
|
|
|
|
|
|
contacts.id_in(params[:ids])
|
|
|
|
end
|
|
|
|
|
2022-06-10 11:09:22 -04:00
|
|
|
def search?
|
|
|
|
params[:search].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def state?
|
|
|
|
params[:state].present?
|
|
|
|
end
|
2022-06-16 08:09:26 -04:00
|
|
|
|
|
|
|
def ids?
|
|
|
|
params[:ids].present?
|
|
|
|
end
|
2022-01-25 16:15:18 -05:00
|
|
|
end
|
|
|
|
end
|