2017-08-25 14:54:32 +08:00
|
|
|
module ClientApi
|
|
|
|
module Users
|
|
|
|
class UserTeamsController < ApplicationController
|
2017-08-28 23:05:09 +08:00
|
|
|
include ClientApi::Users::UserTeamsHelper
|
|
|
|
|
2017-08-25 14:54:32 +08:00
|
|
|
def leave_team
|
2017-09-12 23:30:13 +08:00
|
|
|
ut_service = ClientApi::UserTeamService.new(
|
|
|
|
user: current_user,
|
|
|
|
team_id: params[:team],
|
|
|
|
user_team_id: params[:user_team]
|
|
|
|
)
|
2017-09-01 23:10:43 +08:00
|
|
|
ut_service.destroy_user_team_and_assign_new_team_owner!
|
2017-09-12 23:30:13 +08:00
|
|
|
success_response('/client_api/teams/index', ut_service.teams_data)
|
2017-09-01 23:10:43 +08:00
|
|
|
rescue ClientApi::CustomUserTeamError
|
2017-09-12 23:30:13 +08:00
|
|
|
unsuccess_response(t('client_api.user_teams.leave_team_error'))
|
2017-08-28 23:05:09 +08:00
|
|
|
end
|
|
|
|
|
2017-08-31 21:56:55 +08:00
|
|
|
def update_role
|
2017-09-12 23:30:13 +08:00
|
|
|
ut_service = ClientApi::UserTeamService.new(
|
|
|
|
user: current_user,
|
|
|
|
team_id: params[:team],
|
|
|
|
user_team_id: params[:user_team],
|
|
|
|
role: params[:role]
|
|
|
|
)
|
|
|
|
ut_service.update_role!
|
|
|
|
success_response('/client_api/teams/team_users',
|
|
|
|
ut_service.team_users_data)
|
|
|
|
rescue ClientApi::CustomUserTeamError => error
|
|
|
|
unsuccess_response(error.to_s)
|
2017-08-28 23:05:09 +08:00
|
|
|
end
|
|
|
|
|
2017-09-13 23:11:51 +08:00
|
|
|
def remove_user
|
|
|
|
ut_service = ClientApi::UserTeamService.new(
|
|
|
|
user: current_user,
|
|
|
|
team_id: params[:team],
|
|
|
|
user_team_id: params[:user_team]
|
|
|
|
)
|
|
|
|
ut_service.destroy_user_team_and_assign_new_team_owner!
|
|
|
|
success_response('/client_api/teams/team_users',
|
|
|
|
ut_service.team_users_data)
|
|
|
|
rescue ClientApi::CustomUserTeamError => error
|
|
|
|
unsuccess_response(error.to_s)
|
|
|
|
end
|
|
|
|
|
2017-08-31 21:56:55 +08:00
|
|
|
private
|
2017-08-28 23:05:09 +08:00
|
|
|
|
2017-09-12 23:30:13 +08:00
|
|
|
def success_response(template, locals)
|
2017-08-25 14:54:32 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
2017-09-12 23:30:13 +08:00
|
|
|
render template: template,
|
2017-08-25 14:54:32 +08:00
|
|
|
status: :ok,
|
2017-08-31 21:56:55 +08:00
|
|
|
locals: locals
|
2017-08-28 23:05:09 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-12 23:30:13 +08:00
|
|
|
def unsuccess_response(message)
|
2017-08-28 23:05:09 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
2017-09-12 23:30:13 +08:00
|
|
|
render json: { message: message },
|
2017-08-28 23:05:09 +08:00
|
|
|
status: :unprocessable_entity
|
2017-08-25 14:54:32 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|