debian-mirror-gitlab/app/graphql/mutations/issues/set_crm_contacts.rb

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

56 lines
1.9 KiB
Ruby
Raw Normal View History

2021-12-11 22:18:48 +05:30
# frozen_string_literal: true
module Mutations
module Issues
class SetCrmContacts < Base
graphql_name 'IssueSetCrmContacts'
2022-01-26 12:08:38 +05:30
argument :contact_ids,
2021-12-11 22:18:48 +05:30
[::Types::GlobalIDType[::CustomerRelations::Contact]],
required: true,
description: 'Customer relations contact IDs to set. Replaces existing contacts by default.'
argument :operation_mode,
Types::MutationOperationModeEnum,
required: false,
description: 'Changes the operation mode. Defaults to REPLACE.'
2022-01-26 12:08:38 +05:30
def resolve(project_path:, iid:, contact_ids:, operation_mode: Types::MutationOperationModeEnum.enum[:replace])
2021-12-11 22:18:48 +05:30
issue = authorized_find!(project_path: project_path, iid: iid)
project = issue.project
2022-03-02 08:16:31 +05:30
raise Gitlab::Graphql::Errors::ResourceNotAvailable, 'Feature disabled' unless feature_enabled?(project)
2021-12-11 22:18:48 +05:30
2022-01-26 12:08:38 +05:30
contact_ids = contact_ids.compact.map do |contact_id|
raise Gitlab::Graphql::Errors::ArgumentError, "Contact #{contact_id} is invalid." unless contact_id.respond_to?(:model_id)
2021-12-11 22:18:48 +05:30
2022-01-26 12:08:38 +05:30
contact_id.model_id.to_i
2021-12-11 22:18:48 +05:30
end
attribute_name = case operation_mode
when Types::MutationOperationModeEnum.enum[:append]
2022-01-26 12:08:38 +05:30
:add_ids
2021-12-11 22:18:48 +05:30
when Types::MutationOperationModeEnum.enum[:remove]
2022-01-26 12:08:38 +05:30
:remove_ids
2021-12-11 22:18:48 +05:30
else
2022-01-26 12:08:38 +05:30
:replace_ids
2021-12-11 22:18:48 +05:30
end
2022-01-26 12:08:38 +05:30
response = ::Issues::SetCrmContactsService.new(project: project, current_user: current_user, params: { attribute_name => contact_ids })
2021-12-11 22:18:48 +05:30
.execute(issue)
{
issue: issue,
errors: response.errors
}
end
2022-03-02 08:16:31 +05:30
private
def feature_enabled?(project)
2022-07-16 23:28:13 +05:30
Feature.enabled?(:customer_relations, project.group) && project.group&.crm_enabled?
2022-03-02 08:16:31 +05:30
end
2021-12-11 22:18:48 +05:30
end
end
end