debian-mirror-gitlab/app/finders/crm/contacts_finder.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

77 lines
1.6 KiB
Ruby
Raw Normal View History

2022-04-04 11:22:00 +05:30
# 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-07-23 23:45:48 +05:30
# search: String, optional
# state: CustomerRelations::ContactStateEnum, optional
# ids: int[], optional
2022-04-04 11:22:00 +05:30
module Crm
class ContactsFinder
include Gitlab::Allowable
include Gitlab::Utils::StrongMemoize
attr_reader :params, :current_user
def initialize(current_user, params = {})
@current_user = current_user
@params = params
end
def execute
return CustomerRelations::Contact.none unless root_group
2022-07-23 23:45:48 +05:30
contacts = root_group.contacts
contacts = by_ids(contacts)
contacts = by_state(contacts)
contacts = by_search(contacts)
contacts.sort_by_name
2022-04-04 11:22:00 +05:30
end
private
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-07-23 23:45:48 +05:30
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
def by_ids(contacts)
return contacts unless ids?
contacts.id_in(params[:ids])
end
def search?
params[:search].present?
end
def state?
params[:state].present?
end
def ids?
params[:ids].present?
end
2022-04-04 11:22:00 +05:30
end
end