2020-03-07 06:48:16 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: account_users
|
|
|
|
#
|
2021-10-07 07:51:46 +00:00
|
|
|
# id :bigint not null, primary key
|
|
|
|
# active_at :datetime
|
|
|
|
# auto_offline :boolean default(TRUE), not null
|
|
|
|
# availability :integer default("online"), not null
|
|
|
|
# role :integer default("agent")
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# account_id :bigint
|
|
|
|
# inviter_id :bigint
|
|
|
|
# user_id :bigint
|
2020-03-07 06:48:16 +00:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_account_users_on_account_id (account_id)
|
|
|
|
# index_account_users_on_user_id (user_id)
|
|
|
|
# uniq_user_id_per_account_id (account_id,user_id) UNIQUE
|
|
|
|
#
|
|
|
|
|
|
|
|
class AccountUser < ApplicationRecord
|
2021-10-07 07:51:46 +00:00
|
|
|
include AvailabilityStatusable
|
|
|
|
|
2020-03-07 06:48:16 +00:00
|
|
|
belongs_to :account
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :inviter, class_name: 'User', optional: true
|
|
|
|
|
|
|
|
enum role: { agent: 0, administrator: 1 }
|
2021-10-07 07:51:46 +00:00
|
|
|
enum availability: { online: 0, offline: 1, busy: 2 }
|
|
|
|
|
2020-03-07 06:48:16 +00:00
|
|
|
accepts_nested_attributes_for :account
|
|
|
|
|
2020-08-25 17:34:02 +00:00
|
|
|
after_create_commit :notify_creation, :create_notification_setting
|
2021-02-15 08:02:52 +00:00
|
|
|
after_destroy :notify_deletion, :remove_user_from_account
|
2021-10-07 07:51:46 +00:00
|
|
|
after_save :update_presence_in_redis, if: :saved_change_to_availability?
|
2020-03-07 06:48:16 +00:00
|
|
|
|
|
|
|
validates :user_id, uniqueness: { scope: :account_id }
|
|
|
|
|
|
|
|
def create_notification_setting
|
|
|
|
setting = user.notification_settings.new(account_id: account.id)
|
2020-05-01 09:23:43 +00:00
|
|
|
setting.selected_email_flags = [:email_conversation_assignment]
|
|
|
|
setting.selected_push_flags = [:push_conversation_assignment]
|
2020-03-07 06:48:16 +00:00
|
|
|
setting.save!
|
|
|
|
end
|
|
|
|
|
2021-02-15 08:02:52 +00:00
|
|
|
def remove_user_from_account
|
|
|
|
::Agents::DestroyService.new(account: account, user: user).perform
|
2020-03-07 06:48:16 +00:00
|
|
|
end
|
2020-05-26 17:08:48 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def notify_creation
|
|
|
|
Rails.configuration.dispatcher.dispatch(AGENT_ADDED, Time.zone.now, account: account)
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_deletion
|
|
|
|
Rails.configuration.dispatcher.dispatch(AGENT_REMOVED, Time.zone.now, account: account)
|
|
|
|
end
|
2021-10-07 07:51:46 +00:00
|
|
|
|
|
|
|
def update_presence_in_redis
|
|
|
|
OnlineStatusTracker.set_status(account.id, user.id, availability)
|
|
|
|
end
|
2020-03-07 06:48:16 +00:00
|
|
|
end
|