Chatwoot/app/models/user.rb
Lauren b89353b76c Feature/update confirmation email information (#145)
* Add `invited_by` foreign key to User

Allows for a User to be tied to the user who invited them

* Include `current_user` in new agent initialization parameters

* Add `shoulda-matchers` for testing associations

* Add Inviter information and associated account to welcome email

* Only show inviter info if applicable

* Update conversation spec for FFaker compatibility
2019-10-14 14:24:58 +05:30

64 lines
1.6 KiB
Ruby

class User < ApplicationRecord
# Include default devise modules.
include DeviseTokenAuth::Concerns::User
include Events::Types
devise :database_authenticatable,
:registerable,
:recoverable,
:rememberable,
:trackable,
:validatable,
:confirmable
validates_uniqueness_of :email, scope: :account_id
validates :email, presence: true
validates :name, presence: true
validates :account_id, presence: true
enum role: [ :agent, :administrator ]
belongs_to :account
belongs_to :inviter, class_name: 'User', required: false
has_many :assigned_conversations, foreign_key: "assignee_id", class_name: "Conversation", dependent: :nullify
has_many :inbox_members, dependent: :destroy
has_many :assigned_inboxes, through: :inbox_members, source: :inbox
has_many :messages
before_create :set_channel
before_validation :set_password_and_uid, on: :create
accepts_nested_attributes_for :account
after_create :notify_creation
after_destroy :notify_deletion
def set_password_and_uid
self.uid = self.email
end
def serializable_hash(options = nil)
super(options).merge(confirmed: confirmed?, subscription: account.try(:subscription).try(:summary) )
end
def set_channel
begin
self.channel = SecureRandom.hex
end while self.class.exists?(channel: channel)
end
def notify_creation
Rails.configuration.dispatcher.dispatch(AGENT_ADDED, Time.zone.now, account: self.account)
end
def notify_deletion
Rails.configuration.dispatcher.dispatch(AGENT_REMOVED, Time.zone.now, account: self.account)
end
def push_event_data
{
name: name
}
end
end