2017-08-30 15:29:45 +08:00
|
|
|
module ClientApi
|
|
|
|
module Teams
|
|
|
|
class TeamsController < ApplicationController
|
|
|
|
include ClientApi::Users::UserTeamsHelper
|
|
|
|
|
2017-12-04 18:12:35 +08:00
|
|
|
before_action :check_update_team_permission, only: :update
|
2018-01-09 22:28:27 +08:00
|
|
|
before_action :check_create_team_permission, only: :create
|
2017-12-04 18:12:35 +08:00
|
|
|
|
2017-08-30 15:29:45 +08:00
|
|
|
def index
|
2017-10-26 22:59:00 +08:00
|
|
|
teams = current_user.datatables_teams
|
2017-09-19 16:37:21 +08:00
|
|
|
success_response(template: '/client_api/teams/index',
|
2017-10-26 22:59:00 +08:00
|
|
|
locals: { teams: teams })
|
2017-08-30 15:29:45 +08:00
|
|
|
end
|
|
|
|
|
2017-09-18 15:13:37 +08:00
|
|
|
def create
|
2017-09-19 16:37:21 +08:00
|
|
|
service = ClientApi::Teams::CreateService.new(
|
|
|
|
current_user: current_user,
|
|
|
|
params: team_params
|
|
|
|
)
|
|
|
|
result = service.execute
|
|
|
|
|
|
|
|
if result[:status] == :success
|
|
|
|
success_response(details: { id: service.team.id })
|
|
|
|
else
|
|
|
|
error_response(
|
|
|
|
message: result[:message],
|
|
|
|
details: service.team.errors
|
|
|
|
)
|
|
|
|
end
|
2017-09-18 15:13:37 +08:00
|
|
|
end
|
|
|
|
|
2017-08-30 15:29:45 +08:00
|
|
|
def details
|
2017-09-18 15:13:37 +08:00
|
|
|
teams_service = ClientApi::TeamsService.new(team_id: params[:team_id],
|
2017-08-30 15:29:45 +08:00
|
|
|
current_user: current_user)
|
2017-09-19 16:37:21 +08:00
|
|
|
success_response(template: '/client_api/teams/details',
|
|
|
|
locals: teams_service.team_page_details_data)
|
2017-08-30 22:18:21 +08:00
|
|
|
rescue ClientApi::CustomTeamError
|
2017-08-30 15:29:45 +08:00
|
|
|
error_response
|
|
|
|
end
|
|
|
|
|
|
|
|
def change_team
|
2017-09-18 15:13:37 +08:00
|
|
|
teams_service = ClientApi::TeamsService.new(team_id: params[:team_id],
|
2017-08-30 15:29:45 +08:00
|
|
|
current_user: current_user)
|
2017-09-18 15:13:37 +08:00
|
|
|
teams_service.change_current_team!
|
2017-09-19 16:37:21 +08:00
|
|
|
success_response(template: '/client_api/teams/index',
|
|
|
|
locals: teams_service.teams_data)
|
2017-08-30 22:18:21 +08:00
|
|
|
rescue ClientApi::CustomTeamError
|
2017-08-30 15:29:45 +08:00
|
|
|
error_response
|
|
|
|
end
|
|
|
|
|
2017-09-12 23:30:13 +08:00
|
|
|
def update
|
2017-09-18 15:13:37 +08:00
|
|
|
teams_service = ClientApi::TeamsService.new(team_id: params[:team_id],
|
2017-09-12 23:30:13 +08:00
|
|
|
current_user: current_user,
|
|
|
|
params: team_params)
|
2017-09-18 15:13:37 +08:00
|
|
|
teams_service.update_team!
|
2017-09-19 16:37:21 +08:00
|
|
|
success_response(template: '/client_api/teams/update_details',
|
|
|
|
locals: teams_service.single_team_details_data)
|
2017-09-12 23:30:13 +08:00
|
|
|
rescue ClientApi::CustomTeamError => error
|
2017-09-19 16:37:21 +08:00
|
|
|
error_response(message: error.to_s)
|
2017-09-12 23:30:13 +08:00
|
|
|
end
|
|
|
|
|
2017-11-06 22:14:17 +08:00
|
|
|
def current_team
|
|
|
|
success_response(template: '/client_api/teams/current_team',
|
|
|
|
locals: { team: current_user.current_team })
|
|
|
|
end
|
|
|
|
|
2017-08-30 15:29:45 +08:00
|
|
|
private
|
|
|
|
|
2017-09-12 23:30:13 +08:00
|
|
|
def team_params
|
2017-09-18 15:13:37 +08:00
|
|
|
params.require(:team).permit(:name, :description)
|
2017-09-12 23:30:13 +08:00
|
|
|
end
|
|
|
|
|
2018-01-09 22:28:27 +08:00
|
|
|
def check_create_team_permission
|
|
|
|
unless can_create_teams?
|
|
|
|
respond_422(t('client_api.teams.create_permission_error'))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-04 18:12:35 +08:00
|
|
|
def check_update_team_permission
|
|
|
|
@team = Team.find_by_id(params[:team_id])
|
|
|
|
unless can_update_team?(@team)
|
2018-01-09 22:28:27 +08:00
|
|
|
respond_422(t('client_api.teams.update_permission_error'))
|
2017-12-04 18:12:35 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-19 16:37:21 +08:00
|
|
|
def success_response(args = {})
|
|
|
|
template = args.fetch(:template) { nil }
|
|
|
|
locals = args.fetch(:locals) { {} }
|
|
|
|
details = args.fetch(:details) { {} }
|
|
|
|
|
2017-08-30 15:29:45 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
2017-09-19 16:37:21 +08:00
|
|
|
if template
|
|
|
|
render template: template,
|
|
|
|
status: :ok,
|
|
|
|
locals: locals
|
|
|
|
else
|
|
|
|
render json: { details: details }, status: :ok
|
|
|
|
end
|
2017-08-30 15:29:45 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-20 16:53:38 +08:00
|
|
|
def error_response(args = {})
|
2017-09-19 16:37:21 +08:00
|
|
|
message = args.fetch(:message) { t('client_api.generic_error_message') }
|
|
|
|
details = args.fetch(:details) { {} }
|
|
|
|
status = args.fetch(:status) { :unprocessable_entity }
|
|
|
|
|
2017-08-30 15:29:45 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
2017-09-19 16:37:21 +08:00
|
|
|
render json: {
|
|
|
|
message: message,
|
|
|
|
details: details
|
|
|
|
},
|
|
|
|
status: status
|
2017-08-30 15:29:45 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|