2017-08-30 09:29:45 +02:00
|
|
|
module ClientApi
|
|
|
|
class TeamsService
|
|
|
|
def initialize(arg)
|
2017-09-19 10:37:21 +02:00
|
|
|
@params = arg.fetch(:params) { false }
|
|
|
|
@user = arg.fetch(:current_user) { raise ClientApi::CustomTeamError }
|
|
|
|
team_id = arg.fetch(:team_id) { raise ClientApi::CustomTeamError }
|
|
|
|
@team = Team.find_by_id(team_id)
|
|
|
|
raise ClientApi::CustomTeamError unless @user.teams.include? @team
|
2017-08-30 09:29:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def change_current_team!
|
2017-08-30 16:18:21 +02:00
|
|
|
@user.update_attribute(:current_team_id, @team.id)
|
2017-08-30 09:29:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def team_page_details_data
|
2017-08-30 16:18:21 +02:00
|
|
|
team_users = UserTeam.includes(:user)
|
|
|
|
.references(:user)
|
|
|
|
.where(team: @team)
|
|
|
|
.distinct
|
|
|
|
{ team: @team, team_users: team_users }
|
2017-08-30 09:29:45 +02:00
|
|
|
end
|
|
|
|
|
2017-09-12 17:30:13 +02:00
|
|
|
def single_team_details_data
|
|
|
|
{ team: @team }
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_team!
|
2017-09-14 13:11:19 +02:00
|
|
|
raise ClientApi::CustomTeamError unless @params
|
2017-09-12 17:30:13 +02:00
|
|
|
return if @team.update_attributes(@params)
|
|
|
|
raise ClientApi::CustomTeamError, @team.errors.full_messages
|
|
|
|
end
|
|
|
|
|
2017-08-30 09:29:45 +02:00
|
|
|
def teams_data
|
2017-10-27 09:35:39 +02:00
|
|
|
{ teams: @user.datatables_teams }
|
2017-08-30 09:29:45 +02:00
|
|
|
end
|
|
|
|
end
|
2017-09-18 09:13:37 +02:00
|
|
|
|
2017-08-30 16:18:21 +02:00
|
|
|
CustomTeamError = Class.new(StandardError)
|
2017-08-30 09:29:45 +02:00
|
|
|
end
|