diff options
Diffstat (limited to 'app/services/issues/set_crm_contacts_service.rb')
-rw-r--r-- | app/services/issues/set_crm_contacts_service.rb | 81 |
1 files changed, 61 insertions, 20 deletions
diff --git a/app/services/issues/set_crm_contacts_service.rb b/app/services/issues/set_crm_contacts_service.rb index 13fe30b5ac8..c435ab81b4d 100644 --- a/app/services/issues/set_crm_contacts_service.rb +++ b/app/services/issues/set_crm_contacts_service.rb @@ -2,10 +2,9 @@ module Issues class SetCrmContactsService < ::BaseProjectService - attr_accessor :issue, :errors - MAX_ADDITIONAL_CONTACTS = 6 + # Replacing contacts by email is not currently supported def execute(issue) @issue = issue @errors = [] @@ -13,33 +12,49 @@ module Issues return error_no_permissions unless allowed? return error_invalid_params unless valid_params? - determine_changes if params[:crm_contact_ids] - + @existing_ids = issue.customer_relations_contact_ids + determine_changes if params[:replace_ids].present? return error_too_many if too_many? - add_contacts if params[:add_crm_contact_ids] - remove_contacts if params[:remove_crm_contact_ids] + 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 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) + ServiceResponse.error(payload: issue, message: issue.errors.full_messages.to_sentence) end end private + attr_accessor :issue, :errors, :existing_ids + def determine_changes - existing_contact_ids = issue.issue_customer_relations_contacts.map(&:contact_id) - params[:add_crm_contact_ids] = params[:crm_contact_ids] - existing_contact_ids - params[:remove_crm_contact_ids] = existing_contact_ids - params[:crm_contact_ids] + params[:add_ids] = params[:replace_ids] - existing_ids + params[:remove_ids] = existing_ids - params[:replace_ids] + end + + def add + add_by_id(params[:add_ids]) + end + + def add_by_email + contact_ids = ::CustomerRelations::Contact.find_ids_by_emails(project_group.id, params[:add_emails]) + add_by_id(contact_ids) end - def add_contacts - params[:add_crm_contact_ids].uniq.each do |contact_id| + def add_by_id(contact_ids) + contact_ids -= existing_ids + contact_ids.uniq.each do |contact_id| issue_contact = issue.issue_customer_relations_contacts.create(contact_id: contact_id) unless issue_contact.persisted? @@ -49,9 +64,19 @@ module Issues end end - def remove_contacts + def remove + remove_by_id(params[:remove_ids]) + end + + def remove_by_email + contact_ids = ::CustomerRelations::IssueContact.find_contact_ids_by_emails(issue.id, params[:remove_emails]) + remove_by_id(contact_ids) + end + + def remove_by_id(contact_ids) + contact_ids &= existing_ids issue.issue_customer_relations_contacts - .where(contact_id: params[:remove_crm_contact_ids]) # rubocop: disable CodeReuse/ActiveRecord + .where(contact_id: contact_ids) # rubocop: disable CodeReuse/ActiveRecord .delete_all end @@ -64,27 +89,43 @@ module Issues end def set_present? - params[:crm_contact_ids].present? + params[:replace_ids].present? end def add_or_remove_present? - params[:add_crm_contact_ids].present? || params[:remove_crm_contact_ids].present? + add_present? || remove_present? + end + + def add_present? + params[:add_ids].present? || params[:add_emails].present? + end + + def remove_present? + params[:remove_ids].present? || params[:remove_emails].present? end def too_many? - params[:add_crm_contact_ids] && params[:add_crm_contact_ids].length > MAX_ADDITIONAL_CONTACTS + too_many_ids? || too_many_emails? + end + + def too_many_ids? + params[:add_ids] && params[:add_ids].length > MAX_ADDITIONAL_CONTACTS + end + + def too_many_emails? + params[:add_emails] && params[:add_emails].length > MAX_ADDITIONAL_CONTACTS end def error_no_permissions - ServiceResponse.error(message: ['You have insufficient permissions to set customer relations contacts for this issue']) + ServiceResponse.error(message: _('You have insufficient permissions to set customer relations contacts for this issue')) end def error_invalid_params - ServiceResponse.error(message: ['You cannot combine crm_contact_ids with add_crm_contact_ids or remove_crm_contact_ids']) + ServiceResponse.error(message: _('You cannot combine replace_ids with add_ids or remove_ids')) end def error_too_many - ServiceResponse.error(payload: issue, message: ["You can only add up to #{MAX_ADDITIONAL_CONTACTS} contacts at one time"]) + ServiceResponse.error(payload: issue, message: _("You can only add up to %{max_contacts} contacts at one time" % { max_contacts: MAX_ADDITIONAL_CONTACTS })) end end end |