2022-06-10 11:09:22 -04:00
|
|
|
# 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
|
2022-06-16 08:09:26 -04:00
|
|
|
# ids: int[], optional
|
2022-06-10 11:09:22 -04:00
|
|
|
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
|
2022-06-16 08:09:26 -04:00
|
|
|
organizations = by_ids(organizations)
|
2022-06-10 11:09:22 -04:00
|
|
|
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
|
|
|
|
|
2022-06-16 08:09:26 -04:00
|
|
|
def by_ids(organizations)
|
|
|
|
return organizations unless ids?
|
|
|
|
|
|
|
|
organizations.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-06-10 11:09:22 -04:00
|
|
|
end
|
|
|
|
end
|