Commit 6895e1ad authored by Lee Tickett's avatar Lee Tickett Committed by Kerri Miller

Enforce unique contact email for group hierarchy

Changelog: added
parent 93ccfa72
......@@ -24,6 +24,7 @@ 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)
raise ArgumentError, "Cannot lookup more than #{MAX_PLUCK} emails" if emails.length > MAX_PLUCK
......@@ -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
......@@ -41910,6 +41910,9 @@ msgstr ""
msgid "compliance violation has already been recorded"
msgstr ""
msgid "contact with same email already exists in group hierarchy"
msgstr ""
msgid "container_name can contain only lowercase letters, digits, '-', and '.' and must start and end with an alphanumeric character"
msgstr ""
......
......@@ -26,6 +26,38 @@ RSpec.describe CustomerRelations::Contact, type: :model do
it_behaves_like 'an object with RFC3696 compliant email-formatted attributes', :email
end
describe '#unique_email_for_group_hierarchy' do
let_it_be(:parent) { create(:group) }
let_it_be(:group) { create(:group, parent: parent) }
let_it_be(:subgroup) { create(:group, parent: group) }
let_it_be(:existing_contact) { create(:contact, group: group) }
context 'with unique email for group hierarchy' do
subject { build(:contact, group: group) }
it { is_expected.to be_valid }
end
context 'with duplicate email in group' do
subject { build(:contact, email: existing_contact.email, group: group) }
it { is_expected.to be_invalid }
end
context 'with duplicate email in parent group' do
subject { build(:contact, email: existing_contact.email, group: subgroup) }
it { is_expected.to be_invalid }
end
context 'with duplicate email in subgroup' do
subject { build(:contact, email: existing_contact.email, group: parent) }
it { is_expected.to be_invalid }
end
end
describe '#before_validation' do
it 'strips leading and trailing whitespace' do
contact = described_class.new(first_name: ' First ', last_name: ' Last ', phone: ' 123456 ')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment