Class: Issues::SetCrmContactsService

Inherits:
BaseProjectService show all
Defined in:
app/services/issues/set_crm_contacts_service.rb

Constant Summary collapse

MAX_ADDITIONAL_CONTACTS =
6

Instance Attribute Summary

Attributes inherited from BaseProjectService

#project

Attributes inherited from BaseContainerService

#container, #current_user, #group, #params, #project

Instance Method Summary collapse

Methods inherited from BaseProjectService

#initialize

Methods inherited from BaseContainerService

#group_container?, #initialize, #namespace_container?, #project_container?, #project_group

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?

Constructor Details

This class inherits a constructor from BaseProjectService

Instance Method Details

#execute(issue) ⇒ Object

Replacing contacts by email is not currently supported



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/services/issues/set_crm_contacts_service.rb', line 8

def execute(issue)
  @issue = issue
  @errors = []

  return error_no_permissions unless allowed?
  return error_invalid_params unless valid_params?

  @existing_ids = issue.customer_relations_contact_ids
  determine_changes if params[:replace_ids].present?
  return error_too_many if too_many?

  @added_count = 0
  @removed_count = 0

  add if params[:add_ids].present?
  remove if params[:remove_ids].present?

  add_by_email if params[:add_emails].present?
  remove_by_email if params[:remove_emails].present?

  if issue.valid?
    GraphqlTriggers.issue_crm_contacts_updated(issue)
    issue.touch
    create_system_note
    ServiceResponse.success(payload: issue)
  else
    # The default error isn't very helpful: "Issue customer relations contacts is invalid"
    issue.errors.delete(:issue_customer_relations_contacts)
    issue.errors.add(:issue_customer_relations_contacts, errors.to_sentence)
    ServiceResponse.error(payload: issue, message: issue.errors.full_messages.to_sentence)
  end
end