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

49 lines
1.8 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
raise Gitlab::Graphql::Errors::ResourceNotAvailable, 'Feature disabled' unless Feature.enabled?(:customer_relations, project.group, default_enabled: :yaml)
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
end
end
end