diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-01-20 09:16:11 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-01-20 09:16:11 +0000 |
commit | edaa33dee2ff2f7ea3fac488d41558eb5f86d68c (patch) | |
tree | 11f143effbfeba52329fb7afbd05e6e2a3790241 /app/models/customer_relations/contact.rb | |
parent | d8a5691316400a0f7ec4f83832698f1988eb27c1 (diff) | |
download | gitlab-ce-edaa33dee2ff2f7ea3fac488d41558eb5f86d68c.tar.gz |
Add latest changes from gitlab-org/gitlab@14-7-stable-eev14.7.0-rc42
Diffstat (limited to 'app/models/customer_relations/contact.rb')
-rw-r--r-- | app/models/customer_relations/contact.rb | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/app/models/customer_relations/contact.rb b/app/models/customer_relations/contact.rb index d8669f1f4c2..168f1c48a6c 100644 --- a/app/models/customer_relations/contact.rb +++ b/app/models/customer_relations/contact.rb @@ -24,11 +24,12 @@ class CustomerRelations::Contact < ApplicationRecord validates :email, length: { maximum: 255 } validates :description, length: { maximum: 1024 } validate :validate_email_format + validate :unique_email_for_group_hierarchy - def self.find_ids_by_emails(group_id, emails) + def self.find_ids_by_emails(group, emails) raise ArgumentError, "Cannot lookup more than #{MAX_PLUCK} emails" if emails.length > MAX_PLUCK - where(group_id: group_id, email: emails) + where(group_id: group.self_and_ancestor_ids, email: emails) .pluck(:id) end @@ -39,4 +40,14 @@ class CustomerRelations::Contact < ApplicationRecord self.errors.add(:email, I18n.t(:invalid, scope: 'valid_email.validations.email')) unless ValidateEmail.valid?(self.email) end + + def unique_email_for_group_hierarchy + return unless group + return unless email + + duplicate_email_exists = CustomerRelations::Contact + .where(group_id: group.self_and_hierarchy.pluck(:id), email: email) + .where.not(id: id).exists? + self.errors.add(:email, _('contact with same email already exists in group hierarchy')) if duplicate_email_exists + end end |