Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'app/services/issues/set_crm_contacts_service.rb')
-rw-r--r--app/services/issues/set_crm_contacts_service.rb34
1 files changed, 29 insertions, 5 deletions
diff --git a/app/services/issues/set_crm_contacts_service.rb b/app/services/issues/set_crm_contacts_service.rb
index 947d46f0809..2edc944435b 100644
--- a/app/services/issues/set_crm_contacts_service.rb
+++ b/app/services/issues/set_crm_contacts_service.rb
@@ -16,6 +16,9 @@ module Issues
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?
@@ -25,6 +28,7 @@ module Issues
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"
@@ -36,7 +40,7 @@ module Issues
private
- attr_accessor :issue, :errors, :existing_ids
+ attr_accessor :issue, :errors, :existing_ids, :added_count, :removed_count
def determine_changes
params[:add_ids] = params[:replace_ids] - existing_ids
@@ -48,16 +52,24 @@ module Issues
end
def add_by_email
- contact_ids = ::CustomerRelations::Contact.find_ids_by_emails(project_group, params[:add_emails])
+ contact_ids = ::CustomerRelations::Contact.find_ids_by_emails(project_group, emails(:add_emails))
add_by_id(contact_ids)
end
+ def emails(key)
+ params[key].map do |email|
+ extract_email_from_request_param(email)
+ end
+ end
+
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?
+ if issue_contact.persisted?
+ @added_count += 1
+ else
# The validation ensures that the id exists and the user has permission
errors << "#{contact_id}: The resource that you are attempting to access does not exist or you don't have permission to perform this action"
end
@@ -69,17 +81,24 @@ module Issues
end
def remove_by_email
- contact_ids = ::CustomerRelations::IssueContact.find_contact_ids_by_emails(issue.id, params[:remove_emails])
+ contact_ids = ::CustomerRelations::IssueContact.find_contact_ids_by_emails(issue.id, emails(:remove_emails))
remove_by_id(contact_ids)
end
def remove_by_id(contact_ids)
contact_ids &= existing_ids
- issue.issue_customer_relations_contacts
+ @removed_count += issue.issue_customer_relations_contacts
.where(contact_id: contact_ids) # rubocop: disable CodeReuse/ActiveRecord
.delete_all
end
+ def extract_email_from_request_param(email_param)
+ email_param.delete_prefix(::CustomerRelations::Contact.reference_prefix_quoted)
+ .delete_prefix(::CustomerRelations::Contact.reference_prefix)
+ .delete_suffix(::CustomerRelations::Contact.reference_postfix)
+ .tr('"', '')
+ end
+
def allowed?
current_user&.can?(:set_issue_crm_contacts, issue)
end
@@ -116,6 +135,11 @@ module Issues
params[:add_emails] && params[:add_emails].length > MAX_ADDITIONAL_CONTACTS
end
+ def create_system_note
+ SystemNoteService.change_issuable_contacts(
+ issue, issue.project, current_user, added_count, removed_count)
+ end
+
def error_no_permissions
ServiceResponse.error(message: _('You have insufficient permissions to set customer relations contacts for this issue'))
end