2021-01-17 18:26:56 +00:00
|
|
|
class Api::V1::Accounts::TeamMembersController < Api::V1::Accounts::BaseController
|
|
|
|
before_action :fetch_team
|
|
|
|
before_action :check_authorization
|
|
|
|
|
|
|
|
def index
|
|
|
|
@team_members = @team.team_members.map(&:user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2021-01-29 07:04:52 +00:00
|
|
|
ActiveRecord::Base.transaction do
|
2021-02-09 13:51:31 +00:00
|
|
|
@team_members = params[:user_ids].map { |user_id| @team.add_member(user_id) }
|
2021-01-29 07:04:52 +00:00
|
|
|
end
|
2021-01-17 18:26:56 +00:00
|
|
|
end
|
|
|
|
|
2021-02-09 13:51:31 +00:00
|
|
|
def update
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
members_to_be_added_ids.each { |user_id| @team.add_member(user_id) }
|
|
|
|
members_to_be_removed_ids.each { |user_id| @team.remove_member(user_id) }
|
|
|
|
end
|
|
|
|
@team_members = @team.members
|
|
|
|
render action: 'create'
|
|
|
|
end
|
|
|
|
|
2021-01-17 18:26:56 +00:00
|
|
|
def destroy
|
2021-01-29 07:04:52 +00:00
|
|
|
ActiveRecord::Base.transaction do
|
2021-02-09 13:51:31 +00:00
|
|
|
params[:user_ids].map { |user_id| @team.remove_member(user_id) }
|
2021-01-29 07:04:52 +00:00
|
|
|
end
|
2021-01-17 18:26:56 +00:00
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-02-09 13:51:31 +00:00
|
|
|
def members_to_be_added_ids
|
|
|
|
params[:user_ids] - current_members_ids
|
|
|
|
end
|
|
|
|
|
|
|
|
def members_to_be_removed_ids
|
|
|
|
current_members_ids - params[:user_ids]
|
2021-01-29 07:04:52 +00:00
|
|
|
end
|
|
|
|
|
2021-02-09 13:51:31 +00:00
|
|
|
def current_members_ids
|
|
|
|
@current_members_ids ||= @team.members.pluck(:id)
|
2021-01-29 07:04:52 +00:00
|
|
|
end
|
|
|
|
|
2021-01-17 18:26:56 +00:00
|
|
|
def fetch_team
|
|
|
|
@team = Current.account.teams.find(params[:team_id])
|
|
|
|
end
|
|
|
|
end
|