diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-06-10 18:09:22 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-06-10 18:09:22 +0300 |
commit | 37140013714814d8ffe662a372697c56eea2fde0 (patch) | |
tree | b25c0bfc62da359f97b8b3742007c07723242f93 /app/finders | |
parent | 948023c9c900344aa1e2f334bcaae5a194873b0d (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/finders')
-rw-r--r-- | app/finders/crm/contacts_finder.rb | 27 | ||||
-rw-r--r-- | app/finders/crm/organizations_finder.rb | 64 |
2 files changed, 90 insertions, 1 deletions
diff --git a/app/finders/crm/contacts_finder.rb b/app/finders/crm/contacts_finder.rb index c2d44bec27b..f7c2625f788 100644 --- a/app/finders/crm/contacts_finder.rb +++ b/app/finders/crm/contacts_finder.rb @@ -6,6 +6,8 @@ # 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 module Crm class ContactsFinder include Gitlab::Allowable @@ -21,7 +23,10 @@ module Crm def execute return CustomerRelations::Contact.none unless root_group - root_group.contacts + contacts = root_group.contacts + contacts = by_state(contacts) + contacts = by_search(contacts) + contacts.sort_by_name end private @@ -35,5 +40,25 @@ module Crm 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 search? + params[:search].present? + end + + def state? + params[:state].present? + end end end diff --git a/app/finders/crm/organizations_finder.rb b/app/finders/crm/organizations_finder.rb new file mode 100644 index 00000000000..1a3df05aa11 --- /dev/null +++ b/app/finders/crm/organizations_finder.rb @@ -0,0 +1,64 @@ +# frozen_string_literal: true + +# Finder for retrieving organizations 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::OrganizationStateEnum, optional +module Crm + class OrganizationsFinder + 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::Organization.none unless root_group + + organizations = root_group.organizations + organizations = by_search(organizations) + organizations = by_state(organizations) + organizations.sort_by_name + end + + private + + def root_group + strong_memoize(:root_group) do + group = params[:group]&.root_ancestor + + next unless can?(@current_user, :read_crm_organization, group) + + group + end + end + + def by_search(organizations) + return organizations unless search? + + organizations.search(params[:search]) + end + + def by_state(organizations) + return organizations unless state? + + organizations.search_by_state(params[:state]) + end + + def search? + params[:search].present? + end + + def state? + params[:state].present? + end + end +end |