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 end
def create def create
record = @team.team_members.find_or_create_by(user_id: params[:user_id]) ActiveRecord::Base.transaction do
@team_member = record.user @team_members = params[:user_ids].map { |user_id| create_team_member(user_id) }
end
end end
def destroy 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 head :ok
end end
private 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 def fetch_team
@team = Current.account.teams.find(params[:team_id]) @team = Current.account.teams.find(params[:team_id])
end end

View file

@ -13,13 +13,25 @@
# Indexes # 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 # Foreign Keys
# #
# fk_rails_... (account_id => accounts.id) # fk_rails_... (account_id => accounts.id)
# #
class Team < ApplicationRecord class Team < ApplicationRecord
include RegexHelper
belongs_to :account belongs_to :account
has_many :team_members, dependent: :destroy has_many :team_members, dependent: :destroy
has_many :conversations, dependent: :nullify 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 end

View file

@ -11,6 +11,7 @@
# Indexes # Indexes
# #
# index_team_members_on_team_id (team_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) # index_team_members_on_user_id (user_id)
# #
# Foreign Keys # Foreign Keys

View file

@ -1 +1,7 @@
if @team_member
json.partial! 'api/v1/models/agent.json.jbuilder', resource: @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. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements" 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.bigint "user_id", null: false
t.datetime "created_at", precision: 6, null: false t.datetime "created_at", precision: 6, null: false
t.datetime "updated_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 ["team_id"], name: "index_team_members_on_team_id"
t.index ["user_id"], name: "index_team_members_on_user_id" t.index ["user_id"], name: "index_team_members_on_user_id"
end end
@ -510,6 +511,7 @@ ActiveRecord::Schema.define(version: 2021_01_14_202310) do
t.datetime "created_at", precision: 6, null: false t.datetime "created_at", precision: 6, null: false
t.datetime "updated_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 ["account_id"], name: "index_teams_on_account_id"
t.index ["name", "account_id"], name: "index_teams_on_name_and_account_id", unique: true
end end
create_table "telegram_bots", id: :serial, force: :cascade do |t| 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) expect(response).to have_http_status(:unauthorized)
end end
it 'add a new team member when its administrator' do it 'add a new team members when its administrator' do
params = { user_id: agent.id } 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", post "/api/v1/accounts/#{account.id}/teams/#{team.id}/team_members",
params: params, params: params,
@ -62,7 +63,7 @@ RSpec.describe 'Team Members API', type: :request do
expect(response).to have_http_status(:success) expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body) 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 end
end end
@ -90,9 +91,10 @@ RSpec.describe 'Team Members API', type: :request do
expect(response).to have_http_status(:unauthorized) expect(response).to have_http_status(:unauthorized)
end end
it 'destroys the team member when its administrator' do it 'destroys the team members when its administrator' do
params = { user_id: agent.id } user_ids = (1..5).map { create(:user, account: account, role: :agent).id }
create(:team_member, team: team, user: agent) params = { user_ids: user_ids }
delete "/api/v1/accounts/#{account.id}/teams/#{team.id}", delete "/api/v1/accounts/#{account.id}/teams/#{team.id}",
params: params, params: params,
headers: administrator.create_new_auth_token, headers: administrator.create_new_auth_token,

View file

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