Chore: Enable Users to create multiple accounts (#440)

Addresses: #402
- migrations to split roles and other attributes from users table
- make changes in code to accommodate this change

Co-authored-by: Sojan Jose <sojan@pepalo.com>
Co-authored-by: Pranav Raj Sreepuram <pranavrajs@gmail.com>
This commit is contained in:
Sojan Jose
2020-03-07 12:18:16 +05:30
committed by GitHub
co-authored by Pranav Raj Sreepuram
parent b2d5cc7b05
commit 8b4df986bf
25 changed files with 264 additions and 74 deletions
+13 -5
View File
@@ -42,18 +42,26 @@ class AccountBuilder
def create_and_link_user
password = Time.now.to_i
@user = @account.users.new(email: @email,
password: password,
password_confirmation: password,
role: User.roles['administrator'],
name: email_to_name(@email))
@user = User.new(email: @email,
password: password,
password_confirmation: password,
name: email_to_name(@email))
if @user.save!
link_user_to_account(@user, @account)
@user
else
raise UserErrors.new(errors: @user.errors)
end
end
def link_user_to_account(user, account)
AccountUser.create!(
account_id: account.id,
user_id: user.id,
role: AccountUser.roles['administrator']
)
end
def email_to_name(email)
name = email[/[^@]+/]
name.split('.').map(&:capitalize).join(' ')
+25 -8
View File
@@ -1,25 +1,27 @@
class Api::V1::AgentsController < Api::BaseController
before_action :fetch_agent, except: [:create, :index]
before_action :check_authorization
before_action :build_agent, only: [:create]
before_action :find_user, only: [:create]
before_action :create_user, only: [:create]
before_action :save_account_user, only: [:create]
def index
@agents = agents
end
def destroy
@agent.destroy
@agent.account_user.destroy
head :ok
end
def update
@agent.update!(agent_params)
render json: @agent
@agent.update!(agent_params.except(:role))
@agent.account_user.update!(role: agent_params[:role]) if agent_params[:role]
render 'api/v1/models/user.json', locals: { resource: @agent }
end
def create
@agent.save!
render json: @agent
render 'api/v1/models/user.json', locals: { resource: @user }
end
private
@@ -32,8 +34,23 @@ class Api::V1::AgentsController < Api::BaseController
@agent = agents.find(params[:id])
end
def build_agent
@agent = agents.new(new_agent_params)
def find_user
@user = User.find_by(email: new_agent_params[:email])
end
def create_user
return if @user
@user = User.create!(new_agent_params.slice(:email, :name, :password, :password_confirmation))
end
def save_account_user
AccountUser.create!(
account_id: current_account.id,
user_id: @user.id,
role: new_agent_params[:role],
inviter_id: current_user.id
)
end
def agent_params
+2 -1
View File
@@ -13,7 +13,8 @@ class Account < ApplicationRecord
validates :name, presence: true
has_many :users, dependent: :destroy
has_many :account_users, dependent: :destroy
has_many :users, through: :account_users
has_many :inboxes, dependent: :destroy
has_many :conversations, dependent: :destroy
has_many :contacts, dependent: :destroy
+48
View File
@@ -0,0 +1,48 @@
# == Schema Information
#
# Table name: account_users
#
# id :bigint not null, primary key
# 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
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 :create_notification_setting
after_destroy :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 = [:conversation_assignment]
setting.save!
end
def destroy_notification_setting
setting = user.notification_settings.new(account_id: account.id)
setting.destroy!
end
end
+33 -17
View File
@@ -19,28 +19,20 @@
# remember_created_at :datetime
# reset_password_sent_at :datetime
# reset_password_token :string
# role :integer default("agent")
# sign_in_count :integer default(0), not null
# tokens :json
# uid :string default(""), not null
# unconfirmed_email :string
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer not null
# inviter_id :bigint
#
# Indexes
#
# index_users_on_email (email)
# index_users_on_inviter_id (inviter_id)
# index_users_on_pubsub_token (pubsub_token) UNIQUE
# index_users_on_reset_password_token (reset_password_token) UNIQUE
# index_users_on_uid_and_provider (uid,provider) UNIQUE
#
# Foreign Keys
#
# fk_rails_... (inviter_id => users.id) ON DELETE => nullify
#
class User < ApplicationRecord
# Include default devise modules.
@@ -62,25 +54,23 @@ class User < ApplicationRecord
# The validation below has been commented out as it does not
# work because :validatable in devise overrides this.
# validates_uniqueness_of :email, scope: :account_id
validates :email, :name, :account_id, presence: true
validates :email, :name, presence: true
enum role: [:agent, :administrator]
belongs_to :account
belongs_to :inviter, class_name: 'User', required: false
has_many :invitees, class_name: 'User', foreign_key: 'inviter_id', dependent: :nullify
has_many :account_users, dependent: :destroy
has_many :accounts, through: :account_users
accepts_nested_attributes_for :account_users
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
has_many :invitees, through: :account_users, class_name: 'User', foreign_key: 'inviter_id', dependent: :nullify
has_many :notification_settings, dependent: :destroy
before_validation :set_password_and_uid, on: :create
accepts_nested_attributes_for :account
after_create :notify_creation
after_create :notify_creation, :create_notification_setting
after_destroy :notify_deletion
def send_devise_notification(notification, *args)
@@ -91,6 +81,32 @@ 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
end
def account
account_user&.account
end
def administrator?
account_user&.administrator?
end
def agent?
account_user&.agent?
end
def role
account_user&.role
end
def inviter
account_user&.inviter
end
def serializable_hash(options = nil)
serialized_user = super(options).merge(confirmed: confirmed?)
serialized_user.merge(subscription: account.try(:subscription).try(:summary)) if ENV['BILLING_ENABLED']
@@ -102,7 +118,7 @@ class User < ApplicationRecord
end
def create_notification_setting
setting = notification_settings.new(account_id: account_id)
setting = notification_settings.new(account_id: account.id)
setting.selected_email_flags = [:conversation_assignment]
setting.save!
end
+1 -1
View File
@@ -1,5 +1,5 @@
json.array! @agents do |agent|
json.account_id agent.account_id
json.account_id agent.account.id
json.availability_status agent.availability_status
json.confirmed agent.confirmed?
json.email agent.email
@@ -0,0 +1,12 @@
json.id resource.id
json.provider resource.provider
json.uid resource.uid
json.name resource.name
json.nickname resource.nickname
json.email resource.email
json.account_id resource.account.id
json.pubsub_token resource.pubsub_token
json.role resource.role
json.inviter_id resource.account_user.inviter_id
json.confirmed resource.confirmed?
json.avatar_url resource.avatar_url
@@ -4,7 +4,7 @@ json.uid @user.uid
json.name @user.name
json.nickname @user.nickname
json.email @user.email
json.account_id @user.account_id
json.account_id @user.account.id
json.pubsub_token @user.pubsub_token
json.role @user.role
json.confirmed @user.confirmed?
+3 -3
View File
@@ -5,10 +5,10 @@ json.data do
json.name @resource.name
json.nickname @resource.nickname
json.email @resource.email
json.account_id @resource.account_id
json.account_id @resource.account.id
json.pubsub_token @resource.pubsub_token
json.role @resource.role
json.inviter_id @resource.inviter_id
json.role @resource.account_user.role
json.inviter_id @resource.account_user.inviter_id
json.confirmed @resource.confirmed?
json.avatar_url @resource.avatar_url
end
+3 -3
View File
@@ -7,10 +7,10 @@ json.payload do
json.name @resource.name
json.nickname @resource.nickname
json.email @resource.email
json.account_id @resource.account_id
json.account_id @resource.account.id
json.pubsub_token @resource.pubsub_token
json.role @resource.role
json.inviter_id @resource.inviter_id
json.role @resource.account_user.role
json.inviter_id @resource.account_user.inviter_id
json.confirmed @resource.confirmed?
json.avatar_url @resource.avatar_url
end