2019-11-30 13:39:55 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: contacts
|
|
|
|
#
|
2020-02-09 10:17:48 +00:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# additional_attributes :jsonb
|
2020-08-21 14:00:27 +00:00
|
|
|
# custom_attributes :jsonb
|
2020-02-09 10:17:48 +00:00
|
|
|
# email :string
|
2020-04-03 07:34:58 +00:00
|
|
|
# identifier :string
|
2021-05-13 08:02:19 +00:00
|
|
|
# last_activity_at :datetime
|
2020-02-09 10:17:48 +00:00
|
|
|
# name :string
|
|
|
|
# phone_number :string
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# account_id :integer not null
|
2019-11-30 13:39:55 +00:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2021-07-23 13:09:24 +00:00
|
|
|
# index_contacts_on_account_id (account_id)
|
|
|
|
# index_contacts_on_phone_number_and_account_id (phone_number,account_id)
|
|
|
|
# uniq_email_per_account_contact (email,account_id) UNIQUE
|
|
|
|
# uniq_identifier_per_account_contact (identifier,account_id) UNIQUE
|
2019-11-30 13:39:55 +00:00
|
|
|
#
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
class Contact < ApplicationRecord
|
2020-01-07 17:29:17 +00:00
|
|
|
include Avatarable
|
2020-02-02 17:04:16 +00:00
|
|
|
include AvailabilityStatusable
|
2021-01-03 14:37:57 +00:00
|
|
|
include Labelable
|
2022-12-05 16:08:12 +00:00
|
|
|
include PgSearch::Model
|
|
|
|
include MultiSearchableHelpers
|
|
|
|
|
2022-12-09 04:06:30 +00:00
|
|
|
multisearchable(
|
|
|
|
against: [:id, :email, :name, :phone_number],
|
|
|
|
additional_attributes: ->(contact) { { conversation_id: nil, account_id: contact.account_id } }
|
|
|
|
)
|
2020-04-18 08:17:51 +00:00
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
validates :account_id, presence: true
|
2022-07-08 08:28:09 +00:00
|
|
|
validates :email, allow_blank: true, uniqueness: { scope: [:account_id], case_sensitive: false },
|
2022-07-15 02:51:59 +00:00
|
|
|
format: { with: Devise.email_regexp, message: I18n.t('errors.contacts.email.invalid') }
|
2020-04-03 07:34:58 +00:00
|
|
|
validates :identifier, allow_blank: true, uniqueness: { scope: [:account_id] }
|
2021-04-15 09:43:01 +00:00
|
|
|
validates :phone_number,
|
2022-07-08 08:28:09 +00:00
|
|
|
allow_blank: true, uniqueness: { scope: [:account_id] },
|
2022-07-15 02:51:59 +00:00
|
|
|
format: { with: /\+[1-9]\d{1,14}\z/, message: I18n.t('errors.contacts.phone_number.invalid') }
|
2022-02-02 22:21:17 +00:00
|
|
|
validates :name, length: { maximum: 255 }
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
belongs_to :account
|
2021-11-18 05:02:29 +00:00
|
|
|
has_many :conversations, dependent: :destroy_async
|
|
|
|
has_many :contact_inboxes, dependent: :destroy_async
|
|
|
|
has_many :csat_survey_responses, dependent: :destroy_async
|
2019-10-27 07:44:36 +00:00
|
|
|
has_many :inboxes, through: :contact_inboxes
|
2021-11-18 05:02:29 +00:00
|
|
|
has_many :messages, as: :sender, dependent: :destroy_async
|
|
|
|
has_many :notes, dependent: :destroy_async
|
2022-01-11 23:14:55 +00:00
|
|
|
before_validation :prepare_contact_attributes
|
2020-12-13 16:53:56 +00:00
|
|
|
after_create_commit :dispatch_create_event, :ip_lookup
|
2020-06-16 14:09:57 +00:00
|
|
|
after_update_commit :dispatch_update_event
|
2021-09-23 07:22:49 +00:00
|
|
|
after_destroy_commit :dispatch_destroy_event
|
2020-04-03 07:34:58 +00:00
|
|
|
|
2021-11-10 08:41:00 +00:00
|
|
|
scope :order_on_last_activity_at, lambda { |direction|
|
|
|
|
order(
|
|
|
|
Arel::Nodes::SqlLiteral.new(
|
|
|
|
sanitize_sql_for_order("\"contacts\".\"last_activity_at\" #{direction}
|
|
|
|
NULLS LAST")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
scope :order_on_company_name, lambda { |direction|
|
|
|
|
order(
|
|
|
|
Arel::Nodes::SqlLiteral.new(
|
|
|
|
sanitize_sql_for_order(
|
|
|
|
"\"contacts\".\"additional_attributes\"->>'company_name' #{direction}
|
|
|
|
NULLS LAST"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
scope :order_on_city, lambda { |direction|
|
|
|
|
order(
|
|
|
|
Arel::Nodes::SqlLiteral.new(
|
|
|
|
sanitize_sql_for_order(
|
|
|
|
"\"contacts\".\"additional_attributes\"->>'city' #{direction}
|
|
|
|
NULLS LAST"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
scope :order_on_country_name, lambda { |direction|
|
|
|
|
order(
|
|
|
|
Arel::Nodes::SqlLiteral.new(
|
|
|
|
sanitize_sql_for_order(
|
|
|
|
"\"contacts\".\"additional_attributes\"->>'country' #{direction}
|
|
|
|
NULLS LAST"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
scope :order_on_name, lambda { |direction|
|
|
|
|
order(
|
|
|
|
Arel::Nodes::SqlLiteral.new(
|
|
|
|
sanitize_sql_for_order(
|
|
|
|
"CASE
|
|
|
|
WHEN \"contacts\".\"name\" ~~* '^+\d*' THEN 'z'
|
|
|
|
WHEN \"contacts\".\"name\" ~~* '^\b*' THEN 'z'
|
|
|
|
ELSE LOWER(\"contacts\".\"name\")
|
|
|
|
END #{direction}"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-10-27 07:44:36 +00:00
|
|
|
def get_source_id(inbox_id)
|
|
|
|
contact_inboxes.find_by!(inbox_id: inbox_id).source_id
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def push_event_data
|
|
|
|
{
|
2020-06-02 17:29:02 +00:00
|
|
|
additional_attributes: additional_attributes,
|
2021-07-05 09:05:53 +00:00
|
|
|
custom_attributes: custom_attributes,
|
2020-06-02 17:29:02 +00:00
|
|
|
email: email,
|
2019-08-14 09:48:44 +00:00
|
|
|
id: id,
|
2020-06-02 17:29:02 +00:00
|
|
|
identifier: identifier,
|
2019-08-14 09:48:44 +00:00
|
|
|
name: name,
|
2020-06-02 17:29:02 +00:00
|
|
|
phone_number: phone_number,
|
2020-01-07 17:29:17 +00:00
|
|
|
thumbnail: avatar_url,
|
2020-06-02 17:29:02 +00:00
|
|
|
type: 'contact'
|
2019-08-14 09:48:44 +00:00
|
|
|
}
|
|
|
|
end
|
2020-02-26 04:14:24 +00:00
|
|
|
|
|
|
|
def webhook_data
|
|
|
|
{
|
|
|
|
id: id,
|
2020-04-18 08:17:51 +00:00
|
|
|
name: name,
|
2020-05-03 06:47:27 +00:00
|
|
|
avatar: avatar_url,
|
2021-09-23 07:22:49 +00:00
|
|
|
type: 'contact',
|
|
|
|
account: account.webhook_data
|
2020-02-26 04:14:24 +00:00
|
|
|
}
|
|
|
|
end
|
2020-04-03 07:34:58 +00:00
|
|
|
|
2021-11-10 08:41:00 +00:00
|
|
|
def self.resolved_contacts
|
|
|
|
where.not(email: [nil, '']).or(
|
|
|
|
Current.account.contacts.where.not(phone_number: [nil, ''])
|
|
|
|
).or(Current.account.contacts.where.not(identifier: [nil, '']))
|
|
|
|
end
|
|
|
|
|
2022-07-08 08:28:09 +00:00
|
|
|
def discard_invalid_attrs
|
|
|
|
phone_number_format
|
|
|
|
email_format
|
|
|
|
end
|
|
|
|
|
2022-12-09 04:06:30 +00:00
|
|
|
def self.rebuild_pg_search_documents
|
|
|
|
return super unless name == 'Contact'
|
|
|
|
|
|
|
|
connection.execute <<~SQL.squish
|
|
|
|
INSERT INTO pg_search_documents (searchable_type, searchable_id, content, account_id, conversation_id, created_at, updated_at)
|
|
|
|
SELECT 'Contact' AS searchable_type,
|
|
|
|
contacts.id AS searchable_id,
|
2022-12-13 04:42:57 +00:00
|
|
|
CONCAT_WS(' ', contacts.id, contacts.email, contacts.name, contacts.phone_number, contacts.account_id) AS content,
|
2022-12-09 04:06:30 +00:00
|
|
|
contacts.account_id::int AS account_id,
|
|
|
|
conversations.id AS conversation_id,
|
|
|
|
now() AS created_at,
|
|
|
|
now() AS updated_at
|
|
|
|
FROM contacts
|
|
|
|
LEFT OUTER JOIN conversations
|
|
|
|
ON conversations.contact_id = contacts.id
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2021-08-31 10:00:18 +00:00
|
|
|
private
|
|
|
|
|
2020-10-27 20:44:36 +00:00
|
|
|
def ip_lookup
|
|
|
|
return unless account.feature_enabled?('ip_lookup')
|
|
|
|
|
|
|
|
ContactIpLookupJob.perform_later(self)
|
|
|
|
end
|
|
|
|
|
2022-04-25 06:55:38 +00:00
|
|
|
def phone_number_format
|
|
|
|
return if phone_number.blank?
|
|
|
|
|
2022-04-26 09:43:28 +00:00
|
|
|
self.phone_number = phone_number_was unless phone_number.match?(/\+[1-9]\d{1,14}\z/)
|
2022-04-25 06:55:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def email_format
|
|
|
|
return if email.blank?
|
|
|
|
|
2022-04-26 09:43:28 +00:00
|
|
|
self.email = email_was unless email.match(Devise.email_regexp)
|
2022-04-25 06:55:38 +00:00
|
|
|
end
|
|
|
|
|
2022-01-11 23:14:55 +00:00
|
|
|
def prepare_contact_attributes
|
|
|
|
prepare_email_attribute
|
|
|
|
prepare_jsonb_attributes
|
|
|
|
end
|
|
|
|
|
2020-08-22 18:35:07 +00:00
|
|
|
def prepare_email_attribute
|
|
|
|
# So that the db unique constraint won't throw error when email is ''
|
2022-01-11 23:14:55 +00:00
|
|
|
self.email = email.present? ? email.downcase : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def prepare_jsonb_attributes
|
|
|
|
self.additional_attributes = {} if additional_attributes.blank?
|
|
|
|
self.custom_attributes = {} if custom_attributes.blank?
|
2020-04-03 07:34:58 +00:00
|
|
|
end
|
2020-04-18 08:17:51 +00:00
|
|
|
|
|
|
|
def dispatch_create_event
|
|
|
|
Rails.configuration.dispatcher.dispatch(CONTACT_CREATED, Time.zone.now, contact: self)
|
|
|
|
end
|
|
|
|
|
|
|
|
def dispatch_update_event
|
|
|
|
Rails.configuration.dispatcher.dispatch(CONTACT_UPDATED, Time.zone.now, contact: self)
|
|
|
|
end
|
2021-09-23 07:22:49 +00:00
|
|
|
|
|
|
|
def dispatch_destroy_event
|
|
|
|
Rails.configuration.dispatcher.dispatch(CONTACT_DELETED, Time.zone.now, contact: self)
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|