Feature: Ability to switch between multiple accounts (#881)
* Feature: Ability to switch between multiple accounts * Fix rubocop * Fix assigned inboxes * fix auth json * Add account switcher in UI * fix ordering on administrate * Add switch accounts to sidebar * add account id * Fix schema.rb timestamp * Revert "add account id" This reverts commit 27570f50ef584cb9a5f69454f43f630b318c8807. * Add a check for account Co-authored-by: Pranav Raj Sreepuram <pranavrajs@gmail.com>
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
# 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
|
||||
@@ -23,6 +24,8 @@
|
||||
#
|
||||
|
||||
class AccountUser < ApplicationRecord
|
||||
include Events::Types
|
||||
|
||||
belongs_to :account
|
||||
belongs_to :user
|
||||
belongs_to :inviter, class_name: 'User', optional: true
|
||||
@@ -30,8 +33,8 @@ class AccountUser < ApplicationRecord
|
||||
enum role: { agent: 0, administrator: 1 }
|
||||
accepts_nested_attributes_for :account
|
||||
|
||||
after_create :create_notification_setting
|
||||
after_destroy :destroy_notification_setting
|
||||
after_create :notify_creation, :create_notification_setting
|
||||
after_destroy :notify_deletion, :destroy_notification_setting
|
||||
|
||||
validates :user_id, uniqueness: { scope: :account_id }
|
||||
|
||||
@@ -46,4 +49,14 @@ class AccountUser < ApplicationRecord
|
||||
setting = user.notification_settings.find_by(account_id: account.id)
|
||||
setting.destroy!
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
@@ -64,7 +64,7 @@ class User < ApplicationRecord
|
||||
|
||||
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 :inboxes, through: :inbox_members, source: :inbox
|
||||
has_many :messages
|
||||
has_many :invitees, through: :account_users, class_name: 'User', foreign_key: 'inviter_id', dependent: :nullify
|
||||
|
||||
@@ -74,9 +74,7 @@ class User < ApplicationRecord
|
||||
|
||||
before_validation :set_password_and_uid, on: :create
|
||||
|
||||
after_create :notify_creation, :create_access_token
|
||||
|
||||
after_destroy :notify_deletion
|
||||
after_create :create_access_token
|
||||
|
||||
def send_devise_notification(notification, *args)
|
||||
devise_mailer.send(notification, self, *args).deliver_later
|
||||
@@ -86,30 +84,36 @@ class User < ApplicationRecord
|
||||
self.uid = email
|
||||
end
|
||||
|
||||
def account_user
|
||||
# FIXME : temporary hack to transition over to multiple accounts per user
|
||||
# We should be fetching the current account user relationship here.
|
||||
account_users&.first
|
||||
def active_account_user
|
||||
account_users.order(active_at: :desc)&.first
|
||||
end
|
||||
|
||||
def current_account_user
|
||||
account_users.find_by(account_id: Current.account.id) if Current.account
|
||||
end
|
||||
|
||||
def account
|
||||
account_user&.account
|
||||
current_account_user&.account
|
||||
end
|
||||
|
||||
def assigned_inboxes
|
||||
inboxes.where(account_id: Current.account.id)
|
||||
end
|
||||
|
||||
def administrator?
|
||||
account_user&.administrator?
|
||||
current_account_user&.administrator?
|
||||
end
|
||||
|
||||
def agent?
|
||||
account_user&.agent?
|
||||
current_account_user&.agent?
|
||||
end
|
||||
|
||||
def role
|
||||
account_user&.role
|
||||
current_account_user&.role
|
||||
end
|
||||
|
||||
def inviter
|
||||
account_user&.inviter
|
||||
current_account_user&.inviter
|
||||
end
|
||||
|
||||
def serializable_hash(options = nil)
|
||||
@@ -118,14 +122,6 @@ class User < ApplicationRecord
|
||||
serialized_user
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
def push_event_data
|
||||
{
|
||||
id: id,
|
||||
|
||||
Reference in New Issue
Block a user