2019-08-14 09:48:44 +00:00
|
|
|
class User < ApplicationRecord
|
|
|
|
# Include default devise modules.
|
|
|
|
include DeviseTokenAuth::Concerns::User
|
|
|
|
include Events::Types
|
2019-10-16 22:18:48 +00:00
|
|
|
include Pubsubable
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-20 08:47:26 +00:00
|
|
|
devise :database_authenticatable,
|
|
|
|
:registerable,
|
|
|
|
:recoverable,
|
|
|
|
:rememberable,
|
|
|
|
:trackable,
|
|
|
|
:validatable,
|
|
|
|
:confirmable
|
|
|
|
|
|
|
|
# Used by the pusher/PubSub Service we use for real time communications
|
2019-10-16 21:48:07 +00:00
|
|
|
has_secure_token :pubsub_token
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
validates_uniqueness_of :email, scope: :account_id
|
|
|
|
validates :email, presence: true
|
|
|
|
validates :name, presence: true
|
|
|
|
validates :account_id, presence: true
|
|
|
|
|
2019-10-20 08:47:26 +00:00
|
|
|
enum role: [:agent, :administrator]
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
belongs_to :account
|
2019-10-14 08:54:58 +00:00
|
|
|
belongs_to :inviter, class_name: 'User', required: false
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-10-20 08:47:26 +00:00
|
|
|
has_many :assigned_conversations, foreign_key: 'assignee_id', class_name: 'Conversation', dependent: :nullify
|
2019-08-14 09:48:44 +00:00
|
|
|
has_many :inbox_members, dependent: :destroy
|
|
|
|
has_many :assigned_inboxes, through: :inbox_members, source: :inbox
|
|
|
|
has_many :messages
|
|
|
|
|
|
|
|
before_validation :set_password_and_uid, on: :create
|
|
|
|
|
|
|
|
accepts_nested_attributes_for :account
|
|
|
|
|
2019-08-19 08:19:57 +00:00
|
|
|
after_create :notify_creation
|
|
|
|
after_destroy :notify_deletion
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
def set_password_and_uid
|
2019-10-20 08:47:26 +00:00
|
|
|
self.uid = email
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def serializable_hash(options = nil)
|
2019-10-20 08:47:26 +00:00
|
|
|
super(options).merge(confirmed: confirmed?, subscription: account.try(:subscription).try(:summary))
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def notify_creation
|
2019-10-20 08:47:26 +00:00
|
|
|
Rails.configuration.dispatcher.dispatch(AGENT_ADDED, Time.zone.now, account: account)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def notify_deletion
|
2019-10-20 08:47:26 +00:00
|
|
|
Rails.configuration.dispatcher.dispatch(AGENT_REMOVED, Time.zone.now, account: account)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def push_event_data
|
|
|
|
{
|
|
|
|
name: name
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|