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
|
2022-09-02 17:39:29 +00:00
|
|
|
before_action :validate_member_id_params, only: [:create, :update, :destroy]
|
2021-01-17 18:26:56 +00:00
|
|
|
|
|
|
|
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-12-21 17:18:01 +00:00
|
|
|
@team_members = members_to_be_added_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
|
2022-09-02 17:39:29 +00:00
|
|
|
|
|
|
|
def validate_member_id_params
|
|
|
|
invalid_ids = params[:user_ids].map(&:to_i) - @team.account.user_ids
|
|
|
|
|
|
|
|
render json: { error: 'Invalid User IDs' }, status: :unauthorized and return if invalid_ids.present?
|
|
|
|
end
|
2021-01-17 18:26:56 +00:00
|
|
|
end
|