feat: Bulk add team members in Team APIs (#1695)

This commit is contained in:
Sojan Jose 2021-01-29 12:34:52 +05:30 committed by GitHub
parent bf7915c8a3
commit f9c3b7f2f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 58 additions and 18 deletions

View file

@ -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

View file

@ -13,13 +13,25 @@
# Indexes
#
# 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

View file

@ -11,6 +11,7 @@
# Indexes
#
# 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

View file

@ -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

View file

@ -0,0 +1,6 @@
class AdduniqueIndexToTeamMembers < ActiveRecord::Migration[6.0]
def change
add_index :teams, [:name, :account_id], unique: true
add_index :team_members, [:team_id, :user_id], unique: true
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_01_14_202310) do
ActiveRecord::Schema.define(version: 2021_01_26_121313) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
@ -498,6 +498,7 @@ ActiveRecord::Schema.define(version: 2021_01_14_202310) do
t.bigint "user_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["team_id", "user_id"], name: "index_team_members_on_team_id_and_user_id", unique: true
t.index ["team_id"], name: "index_team_members_on_team_id"
t.index ["user_id"], name: "index_team_members_on_user_id"
end
@ -510,6 +511,7 @@ ActiveRecord::Schema.define(version: 2021_01_14_202310) do
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["account_id"], name: "index_teams_on_account_id"
t.index ["name", "account_id"], name: "index_teams_on_name_and_account_id", unique: true
end
create_table "telegram_bots", id: :serial, force: :cascade do |t|

View file

@ -52,8 +52,9 @@ RSpec.describe 'Team Members API', type: :request do
expect(response).to have_http_status(:unauthorized)
end
it 'add a new team member when its administrator' do
params = { user_id: agent.id }
it 'add a new team members when its administrator' do
user_ids = (1..5).map { create(:user, account: account, role: :agent).id }
params = { user_ids: user_ids }
post "/api/v1/accounts/#{account.id}/teams/#{team.id}/team_members",
params: params,
@ -62,7 +63,7 @@ RSpec.describe 'Team Members API', type: :request do
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)
expect(json_response['id']).to eq(agent.id)
expect(json_response.count).to eq(user_ids.count)
end
end
end
@ -90,9 +91,10 @@ RSpec.describe 'Team Members API', type: :request do
expect(response).to have_http_status(:unauthorized)
end
it 'destroys the team member when its administrator' do
params = { user_id: agent.id }
create(:team_member, team: team, user: agent)
it 'destroys the team members when its administrator' do
user_ids = (1..5).map { create(:user, account: account, role: :agent).id }
params = { user_ids: user_ids }
delete "/api/v1/accounts/#{account.id}/teams/#{team.id}",
params: params,
headers: administrator.create_new_auth_token,

View file

@ -75,7 +75,7 @@ RSpec.describe 'Teams API', type: :request do
end
it 'creates a new team when its administrator' do
params = { name: 'Test Team' }
params = { name: 'test-team' }
post "/api/v1/accounts/#{account.id}/teams",
params: params,
@ -102,7 +102,7 @@ RSpec.describe 'Teams API', type: :request do
let(:administrator) { create(:user, account: account, role: :administrator) }
it 'returns unauthorized for agent' do
params = { name: 'New Team' }
params = { name: 'new-team' }
put "/api/v1/accounts/#{account.id}/teams/#{team.id}",
params: params,
@ -113,7 +113,7 @@ RSpec.describe 'Teams API', type: :request do
end
it 'updates an existing team when its an administrator' do
params = { name: 'New Team' }
params = { name: 'new-team' }
put "/api/v1/accounts/#{account.id}/teams/#{team.id}",
params: params,
@ -121,7 +121,7 @@ RSpec.describe 'Teams API', type: :request do
as: :json
expect(response).to have_http_status(:success)
expect(team.reload.name).to eq('New Team')
expect(team.reload.name).to eq('new-team')
end
end
end