debian-mirror-gitlab/app/models/customer_relations/contact.rb

72 lines
2 KiB
Ruby
Raw Normal View History

2021-11-11 11:23:49 +05:30
# frozen_string_literal: true
class CustomerRelations::Contact < ApplicationRecord
include StripAttribute
self.table_name = "customer_relations_contacts"
2021-11-18 22:05:49 +05:30
belongs_to :group, -> { where(type: Group.sti_name) }, foreign_key: 'group_id'
2021-11-11 11:23:49 +05:30
belongs_to :organization, optional: true
2021-12-11 22:18:48 +05:30
has_many :issue_contacts, inverse_of: :contact
has_many :issues, through: :issue_contacts, inverse_of: :customer_relations_contacts
2021-11-11 11:23:49 +05:30
strip_attributes! :phone, :first_name, :last_name
enum state: {
inactive: 0,
active: 1
}
validates :group, presence: true
validates :phone, length: { maximum: 32 }
validates :first_name, presence: true, length: { maximum: 255 }
validates :last_name, presence: true, length: { maximum: 255 }
validates :email, length: { maximum: 255 }
validates :description, length: { maximum: 1024 }
validate :validate_email_format
2022-03-02 08:16:31 +05:30
validate :unique_email_for_group_hierarchy
2021-11-11 11:23:49 +05:30
2022-04-04 11:22:00 +05:30
def self.reference_prefix
'[contact:'
end
def self.reference_prefix_quoted
'["contact:'
end
def self.reference_postfix
']'
end
2022-03-02 08:16:31 +05:30
def self.find_ids_by_emails(group, emails)
2022-01-26 12:08:38 +05:30
raise ArgumentError, "Cannot lookup more than #{MAX_PLUCK} emails" if emails.length > MAX_PLUCK
2022-03-02 08:16:31 +05:30
where(group_id: group.self_and_ancestor_ids, email: emails)
2022-01-26 12:08:38 +05:30
.pluck(:id)
end
2022-04-04 11:22:00 +05:30
def self.exists_for_group?(group)
return false unless group
exists?(group_id: group.self_and_ancestor_ids)
end
2021-11-11 11:23:49 +05:30
private
def validate_email_format
return unless email
self.errors.add(:email, I18n.t(:invalid, scope: 'valid_email.validations.email')) unless ValidateEmail.valid?(self.email)
end
2022-03-02 08:16:31 +05:30
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
2021-11-11 11:23:49 +05:30
end