chore: Move agent availability to Account level (#3074)
- Move agent availability to the account level
This commit is contained in:
@@ -2,14 +2,16 @@
|
||||
#
|
||||
# Table name: account_users
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# active_at :datetime
|
||||
# role :integer default("agent")
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# account_id :bigint
|
||||
# inviter_id :bigint
|
||||
# user_id :bigint
|
||||
# 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
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
@@ -24,15 +26,20 @@
|
||||
#
|
||||
|
||||
class AccountUser < ApplicationRecord
|
||||
include AvailabilityStatusable
|
||||
|
||||
belongs_to :account
|
||||
belongs_to :user
|
||||
belongs_to :inviter, class_name: 'User', optional: true
|
||||
|
||||
enum role: { agent: 0, administrator: 1 }
|
||||
enum availability: { online: 0, offline: 1, busy: 2 }
|
||||
|
||||
accepts_nested_attributes_for :account
|
||||
|
||||
after_create_commit :notify_creation, :create_notification_setting
|
||||
after_destroy :notify_deletion, :remove_user_from_account
|
||||
after_save :update_presence_in_redis, if: :saved_change_to_availability?
|
||||
|
||||
validates :user_id, uniqueness: { scope: :account_id }
|
||||
|
||||
@@ -56,4 +63,8 @@ class AccountUser < ApplicationRecord
|
||||
def notify_deletion
|
||||
Rails.configuration.dispatcher.dispatch(AGENT_REMOVED, Time.zone.now, account: account)
|
||||
end
|
||||
|
||||
def update_presence_in_redis
|
||||
OnlineStatusTracker.set_status(account.id, user.id, availability)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,29 +2,29 @@ module AvailabilityStatusable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
def online_presence?
|
||||
return if user_profile_page_context?
|
||||
|
||||
::OnlineStatusTracker.get_presence(availability_account_id, self.class.name, id)
|
||||
obj_id = is_a?(Contact) ? id : user_id
|
||||
::OnlineStatusTracker.get_presence(account_id, self.class.name, obj_id)
|
||||
end
|
||||
|
||||
def availability_status
|
||||
return availability if user_profile_page_context?
|
||||
return 'offline' unless online_presence?
|
||||
return 'online' if is_a? Contact
|
||||
|
||||
::OnlineStatusTracker.get_status(availability_account_id, id) || 'online'
|
||||
if is_a? Contact
|
||||
contact_availability_status
|
||||
else
|
||||
user_availability_status
|
||||
end
|
||||
end
|
||||
|
||||
def user_profile_page_context?
|
||||
# at the moment profile pages aren't account scoped
|
||||
# hence we will return availability attribute instead of true presence
|
||||
# we will revisit this later
|
||||
is_a?(User) && Current.account.blank?
|
||||
private
|
||||
|
||||
def contact_availability_status
|
||||
online_presence? ? 'online' : 'offline'
|
||||
end
|
||||
|
||||
def availability_account_id
|
||||
return account_id if is_a? Contact
|
||||
def user_availability_status
|
||||
# we are not considering presence in this case. Just returns the availability
|
||||
return availability unless auto_offline
|
||||
|
||||
Current.account.id
|
||||
# availability as a fallback in case the status is not present in redis
|
||||
online_presence? ? (::OnlineStatusTracker.get_status(account_id, user_id) || availability) : 'offline'
|
||||
end
|
||||
end
|
||||
|
||||
@@ -39,7 +39,6 @@
|
||||
|
||||
class User < ApplicationRecord
|
||||
include AccessTokenable
|
||||
include AvailabilityStatusable
|
||||
include Avatarable
|
||||
# Include default devise modules.
|
||||
include DeviseTokenAuth::Concerns::User
|
||||
@@ -57,6 +56,8 @@ class User < ApplicationRecord
|
||||
:confirmable,
|
||||
:password_has_required_content
|
||||
|
||||
# TODO: remove in a future version once online status is moved to account users
|
||||
# remove the column availability from users
|
||||
enum availability: { online: 0, offline: 1, busy: 2 }
|
||||
|
||||
# The validation below has been commented out as it does not
|
||||
@@ -89,8 +90,6 @@ class User < ApplicationRecord
|
||||
|
||||
before_validation :set_password_and_uid, on: :create
|
||||
|
||||
after_save :update_presence_in_redis, if: :saved_change_to_availability?
|
||||
|
||||
scope :order_by_full_name, -> { order('lower(name) ASC') }
|
||||
|
||||
def send_devise_notification(notification, *args)
|
||||
@@ -141,6 +140,14 @@ class User < ApplicationRecord
|
||||
current_account_user&.role
|
||||
end
|
||||
|
||||
def availability_status
|
||||
current_account_user&.availability_status
|
||||
end
|
||||
|
||||
def auto_offline
|
||||
current_account_user&.auto_offline
|
||||
end
|
||||
|
||||
def inviter
|
||||
current_account_user&.inviter
|
||||
end
|
||||
@@ -169,12 +176,4 @@ class User < ApplicationRecord
|
||||
type: 'user'
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def update_presence_in_redis
|
||||
accounts.each do |account|
|
||||
OnlineStatusTracker.set_status(account.id, id, availability)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user