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
|
|
|
|
# email :string
|
2020-04-03 07:34:58 +00:00
|
|
|
# identifier :string
|
2020-02-09 10:17:48 +00:00
|
|
|
# name :string
|
|
|
|
# phone_number :string
|
|
|
|
# pubsub_token :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
|
|
|
|
#
|
2020-04-03 07:34:58 +00:00
|
|
|
# index_contacts_on_account_id (account_id)
|
|
|
|
# index_contacts_on_pubsub_token (pubsub_token) UNIQUE
|
|
|
|
# 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
|
2019-10-16 22:18:48 +00:00
|
|
|
include Pubsubable
|
2020-01-07 17:29:17 +00:00
|
|
|
include Avatarable
|
2020-02-02 17:04:16 +00:00
|
|
|
include AvailabilityStatusable
|
2020-04-18 08:17:51 +00:00
|
|
|
include Events::Types
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
validates :account_id, presence: true
|
2020-04-03 07:34:58 +00:00
|
|
|
validates :email, allow_blank: true, uniqueness: { scope: [:account_id], case_sensitive: false }
|
|
|
|
validates :identifier, allow_blank: true, uniqueness: { scope: [:account_id] }
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
belongs_to :account
|
2019-10-20 19:10:18 +00:00
|
|
|
has_many :conversations, dependent: :destroy
|
2019-10-27 07:44:36 +00:00
|
|
|
has_many :contact_inboxes, dependent: :destroy
|
|
|
|
has_many :inboxes, through: :contact_inboxes
|
2020-03-18 20:22:38 +00:00
|
|
|
has_many :messages, dependent: :destroy
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-04-03 07:34:58 +00:00
|
|
|
before_validation :downcase_email
|
2020-04-18 08:17:51 +00:00
|
|
|
after_create :dispatch_create_event
|
|
|
|
after_update :dispatch_update_event
|
2020-04-03 07:34:58 +00:00
|
|
|
|
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
|
|
|
|
{
|
|
|
|
id: id,
|
|
|
|
name: name,
|
2020-01-07 17:29:17 +00:00
|
|
|
thumbnail: avatar_url,
|
2020-05-03 06:47:27 +00:00
|
|
|
type: 'contact',
|
2019-10-16 21:48:07 +00:00
|
|
|
pubsub_token: pubsub_token
|
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,
|
|
|
|
type: 'contact'
|
2020-02-26 04:14:24 +00:00
|
|
|
}
|
|
|
|
end
|
2020-04-03 07:34:58 +00:00
|
|
|
|
|
|
|
def downcase_email
|
|
|
|
email.downcase! if email.present?
|
|
|
|
end
|
2020-04-18 08:17:51 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
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
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|