Skip to content
代码片段 群组 项目
提交 cc1698f0 编辑于 作者: Marco Zille's avatar Marco Zille
浏览文件

Added possibility to search CRM contacts and organizations by ids

Changelog: added
上级 6ab6b87a
No related branches found
No related tags found
无相关合并请求
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
# group: Group, required # group: Group, required
# search: String, optional # search: String, optional
# state: CustomerRelations::ContactStateEnum, optional # state: CustomerRelations::ContactStateEnum, optional
# ids: int[], optional
module Crm module Crm
class ContactsFinder class ContactsFinder
include Gitlab::Allowable include Gitlab::Allowable
...@@ -24,6 +25,7 @@ def execute ...@@ -24,6 +25,7 @@ def execute
return CustomerRelations::Contact.none unless root_group return CustomerRelations::Contact.none unless root_group
contacts = root_group.contacts contacts = root_group.contacts
contacts = by_ids(contacts)
contacts = by_state(contacts) contacts = by_state(contacts)
contacts = by_search(contacts) contacts = by_search(contacts)
contacts.sort_by_name contacts.sort_by_name
...@@ -53,6 +55,12 @@ def by_state(contacts) ...@@ -53,6 +55,12 @@ def by_state(contacts)
contacts.search_by_state(params[:state]) contacts.search_by_state(params[:state])
end end
def by_ids(contacts)
return contacts unless ids?
contacts.search_by_ids(params[:ids])
end
def search? def search?
params[:search].present? params[:search].present?
end end
...@@ -60,5 +68,9 @@ def search? ...@@ -60,5 +68,9 @@ def search?
def state? def state?
params[:state].present? params[:state].present?
end end
def ids?
params[:ids].present?
end
end end
end end
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
# group: Group, required # group: Group, required
# search: String, optional # search: String, optional
# state: CustomerRelations::OrganizationStateEnum, optional # state: CustomerRelations::OrganizationStateEnum, optional
# ids: int[], optional
module Crm module Crm
class OrganizationsFinder class OrganizationsFinder
include Gitlab::Allowable include Gitlab::Allowable
...@@ -24,6 +25,7 @@ def execute ...@@ -24,6 +25,7 @@ def execute
return CustomerRelations::Organization.none unless root_group return CustomerRelations::Organization.none unless root_group
organizations = root_group.organizations organizations = root_group.organizations
organizations = by_ids(organizations)
organizations = by_search(organizations) organizations = by_search(organizations)
organizations = by_state(organizations) organizations = by_state(organizations)
organizations.sort_by_name organizations.sort_by_name
...@@ -53,6 +55,12 @@ def by_state(organizations) ...@@ -53,6 +55,12 @@ def by_state(organizations)
organizations.search_by_state(params[:state]) organizations.search_by_state(params[:state])
end end
def by_ids(organizations)
return organizations unless ids?
organizations.search_by_ids(params[:ids])
end
def search? def search?
params[:search].present? params[:search].present?
end end
...@@ -60,5 +68,9 @@ def search? ...@@ -60,5 +68,9 @@ def search?
def state? def state?
params[:state].present? params[:state].present?
end end
def ids?
params[:ids].present?
end
end end
end end
...@@ -17,13 +17,25 @@ class ContactsResolver < BaseResolver ...@@ -17,13 +17,25 @@ class ContactsResolver < BaseResolver
required: false, required: false,
description: 'State of the contacts to search for.' description: 'State of the contacts to search for.'
argument :ids, [GraphQL::Types::ID],
required: false,
description: 'Filter contacts by IDs.'
def resolve(**args) def resolve(**args)
args[:ids] = parse_gids(args.delete(:ids))
::Crm::ContactsFinder.new(current_user, { group: group }.merge(args)).execute ::Crm::ContactsFinder.new(current_user, { group: group }.merge(args)).execute
end end
def group def group
object.respond_to?(:sync) ? object.sync : object object.respond_to?(:sync) ? object.sync : object
end end
private
def parse_gids(gids)
gids&.map { |gid| GitlabSchema.parse_gid(gid, expected_type: CustomerRelations::Contact).model_id }
end
end end
end end
end end
...@@ -17,13 +17,25 @@ class OrganizationsResolver < BaseResolver ...@@ -17,13 +17,25 @@ class OrganizationsResolver < BaseResolver
required: false, required: false,
description: 'State of the organization to search for.' description: 'State of the organization to search for.'
argument :ids, [GraphQL::Types::ID],
required: false,
description: 'Filter organizations by IDs.'
def resolve(**args) def resolve(**args)
args[:ids] = parse_gids(args.delete(:ids))
::Crm::OrganizationsFinder.new(current_user, { group: group }.merge(args)).execute ::Crm::OrganizationsFinder.new(current_user, { group: group }.merge(args)).execute
end end
def group def group
object.respond_to?(:sync) ? object.sync : object object.respond_to?(:sync) ? object.sync : object
end end
private
def parse_gids(gids)
gids&.map { |gid| GitlabSchema.parse_gid(gid, expected_type: CustomerRelations::Organization).model_id }
end
end end
end end
end end
...@@ -56,6 +56,10 @@ def self.search_by_state(state) ...@@ -56,6 +56,10 @@ def self.search_by_state(state)
where(state: state) where(state: state)
end end
def self.search_by_ids(ids)
where(id: ids)
end
def self.sort_by_name def self.sort_by_name
order("last_name ASC, first_name ASC") order("last_name ASC, first_name ASC")
end end
......
...@@ -38,6 +38,10 @@ def self.search_by_state(state) ...@@ -38,6 +38,10 @@ def self.search_by_state(state)
where(state: state) where(state: state)
end end
def self.search_by_ids(ids)
where(id: ids)
end
def self.sort_by_name def self.sort_by_name
order(name: :asc) order(name: :asc)
end end
......
...@@ -11713,6 +11713,7 @@ four standard [pagination arguments](#connection-pagination-arguments): ...@@ -11713,6 +11713,7 @@ four standard [pagination arguments](#connection-pagination-arguments):
   
| Name | Type | Description | | Name | Type | Description |
| ---- | ---- | ----------- | | ---- | ---- | ----------- |
| <a id="groupcontactsids"></a>`ids` | [`[ID!]`](#id) | Filter contacts by IDs. |
| <a id="groupcontactssearch"></a>`search` | [`String`](#string) | Search term to find contacts with. | | <a id="groupcontactssearch"></a>`search` | [`String`](#string) | Search term to find contacts with. |
| <a id="groupcontactsstate"></a>`state` | [`CustomerRelationsContactState`](#customerrelationscontactstate) | State of the contacts to search for. | | <a id="groupcontactsstate"></a>`state` | [`CustomerRelationsContactState`](#customerrelationscontactstate) | State of the contacts to search for. |
   
...@@ -12069,6 +12070,7 @@ four standard [pagination arguments](#connection-pagination-arguments): ...@@ -12069,6 +12070,7 @@ four standard [pagination arguments](#connection-pagination-arguments):
   
| Name | Type | Description | | Name | Type | Description |
| ---- | ---- | ----------- | | ---- | ---- | ----------- |
| <a id="grouporganizationsids"></a>`ids` | [`[ID!]`](#id) | Filter organizations by IDs. |
| <a id="grouporganizationssearch"></a>`search` | [`String`](#string) | Search term used to find organizations with. | | <a id="grouporganizationssearch"></a>`search` | [`String`](#string) | Search term used to find organizations with. |
| <a id="grouporganizationsstate"></a>`state` | [`CustomerRelationsOrganizationState`](#customerrelationsorganizationstate) | State of the organization to search for. | | <a id="grouporganizationsstate"></a>`state` | [`CustomerRelationsOrganizationState`](#customerrelationsorganizationstate) | State of the organization to search for. |
   
...@@ -143,6 +143,13 @@ ...@@ -143,6 +143,13 @@
expect(finder.execute).to match_array([search_test_b]) expect(finder.execute).to match_array([search_test_b])
end end
end end
context 'when searching for contacts ids' do
it 'returns the expected contacts' do
finder = described_class.new(user, group: search_test_group, ids: [search_test_b.id])
expect(finder.execute).to match_array([search_test_b])
end
end
end end
end end
end end
...@@ -129,6 +129,13 @@ ...@@ -129,6 +129,13 @@
expect(finder.execute).to match_array([search_test_b]) expect(finder.execute).to match_array([search_test_b])
end end
end end
context 'when searching for organizations ids' do
it 'returns the expected organizations' do
finder = described_class.new(user, group: search_test_group, ids: [search_test_a.id])
expect(finder.execute).to match_array([search_test_a])
end
end
end end
end end
end end
...@@ -77,6 +77,12 @@ ...@@ -77,6 +77,12 @@
expect(resolve_contacts(group, { state: :inactive })).to match_array([contact_a]) expect(resolve_contacts(group, { state: :inactive })).to match_array([contact_a])
end end
end end
context 'when ids are provided' do
it 'returns the correct contacts' do
expect(resolve_contacts(group, { ids: [contact_a.to_global_id.to_s] })).to match_array([contact_a])
end
end
end end
end end
......
...@@ -71,6 +71,14 @@ ...@@ -71,6 +71,14 @@
expect(resolve_organizations(group, { state: :inactive })).to match_array([organization_a]) expect(resolve_organizations(group, { state: :inactive })).to match_array([organization_a])
end end
end end
context 'when ids are provided' do
it 'returns the correct organizations' do
expect(resolve_organizations(group, {
ids: [organization_b.to_global_id.to_s]
})).to match_array([organization_b])
end
end
end end
end end
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册