feat: Bulk add team members in Team APIs (#1695)
This commit is contained in:
@@ -7,17 +7,28 @@ class Api::V1::Accounts::TeamMembersController < Api::V1::Accounts::BaseControll
|
||||
end
|
||||
|
||||
def create
|
||||
record = @team.team_members.find_or_create_by(user_id: params[:user_id])
|
||||
@team_member = record.user
|
||||
ActiveRecord::Base.transaction do
|
||||
@team_members = params[:user_ids].map { |user_id| create_team_member(user_id) }
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@team.team_members.find_by(user_id: params[:user_id])&.destroy
|
||||
ActiveRecord::Base.transaction do
|
||||
params[:user_ids].map { |user_id| remove_team_member(user_id) }
|
||||
end
|
||||
head :ok
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def create_team_member(user_id)
|
||||
@team.team_members.find_or_create_by(user_id: user_id)&.user
|
||||
end
|
||||
|
||||
def remove_team_member(user_id)
|
||||
@team.team_members.find_by(user_id: user_id)&.destroy
|
||||
end
|
||||
|
||||
def fetch_team
|
||||
@team = Current.account.teams.find(params[:team_id])
|
||||
end
|
||||
|
||||
@@ -12,14 +12,26 @@
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_teams_on_account_id (account_id)
|
||||
# index_teams_on_account_id (account_id)
|
||||
# index_teams_on_name_and_account_id (name,account_id) UNIQUE
|
||||
#
|
||||
# Foreign Keys
|
||||
#
|
||||
# fk_rails_... (account_id => accounts.id)
|
||||
#
|
||||
class Team < ApplicationRecord
|
||||
include RegexHelper
|
||||
|
||||
belongs_to :account
|
||||
has_many :team_members, dependent: :destroy
|
||||
has_many :conversations, dependent: :nullify
|
||||
|
||||
validates :name,
|
||||
presence: { message: 'must not be blank' },
|
||||
format: { with: UNICODE_CHARACTER_NUMBER_HYPHEN_UNDERSCORE },
|
||||
uniqueness: { scope: :account_id }
|
||||
|
||||
before_validation do
|
||||
self.name = name.downcase if attribute_present?('name')
|
||||
end
|
||||
end
|
||||
|
||||
@@ -10,8 +10,9 @@
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_team_members_on_team_id (team_id)
|
||||
# index_team_members_on_user_id (user_id)
|
||||
# index_team_members_on_team_id (team_id)
|
||||
# index_team_members_on_team_id_and_user_id (team_id,user_id) UNIQUE
|
||||
# index_team_members_on_user_id (user_id)
|
||||
#
|
||||
# Foreign Keys
|
||||
#
|
||||
|
||||
@@ -1 +1,7 @@
|
||||
json.partial! 'api/v1/models/agent.json.jbuilder', resource: @team_member
|
||||
if @team_member
|
||||
json.partial! 'api/v1/models/agent.json.jbuilder', resource: @team_member
|
||||
elsif @team_members.present?
|
||||
json.array! @team_members do |team_member|
|
||||
json.partial! 'api/v1/models/agent.json.jbuilder', resource: team_member
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user