blob: 58ec4cf8a4776d454e79afde9f8543bd9feac6f6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# 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
# search: String, optional
# state: CustomerRelations::ContactStateEnum, optional
# ids: int[], optional
module Crm
class ContactsFinder
include Gitlab::Allowable
include Gitlab::Utils::StrongMemoize
attr_reader :params, :current_user
def self.counts_by_state(current_user, params = {})
params = params.merge(sort: nil)
new(current_user, params).execute.counts_by_state
end
def initialize(current_user, params = {})
@current_user = current_user
@params = params
end
def execute
return CustomerRelations::Contact.none unless root_group
contacts = root_group.contacts
contacts = by_ids(contacts)
contacts = by_state(contacts)
contacts = by_search(contacts)
sort_contacts(contacts)
end
private
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
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
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
end
end
|