Chatwoot/app/models/account_user.rb
Sojan Jose b7a583b2c4
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>
2020-05-26 22:38:48 +05:30

62 lines
1.7 KiB
Ruby

# == Schema Information
#
# 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
#
# 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
#
# Foreign Keys
#
# fk_rails_... (account_id => accounts.id)
# fk_rails_... (user_id => users.id)
#
class AccountUser < ApplicationRecord
include Events::Types
belongs_to :account
belongs_to :user
belongs_to :inviter, class_name: 'User', optional: true
enum role: { agent: 0, administrator: 1 }
accepts_nested_attributes_for :account
after_create :notify_creation, :create_notification_setting
after_destroy :notify_deletion, :destroy_notification_setting
validates :user_id, uniqueness: { scope: :account_id }
def create_notification_setting
setting = user.notification_settings.new(account_id: account.id)
setting.selected_email_flags = [:email_conversation_assignment]
setting.selected_push_flags = [:push_conversation_assignment]
setting.save!
end
def destroy_notification_setting
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