2017-08-30 15:29:45 +08:00
|
|
|
module ClientApi
|
|
|
|
class TeamsService
|
|
|
|
def initialize(arg)
|
2017-09-19 16:37:21 +08: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 15:29:45 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def change_current_team!
|
2017-08-30 22:18:21 +08:00
|
|
|
@user.update_attribute(:current_team_id, @team.id)
|
2017-08-30 15:29:45 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def team_page_details_data
|
2017-08-30 22:18:21 +08:00
|
|
|
team_users = UserTeam.includes(:user)
|
|
|
|
.references(:user)
|
|
|
|
.where(team: @team)
|
|
|
|
.distinct
|
|
|
|
{ team: @team, team_users: team_users }
|
2017-08-30 15:29:45 +08:00
|
|
|
end
|
|
|
|
|
2017-09-12 23:30:13 +08:00
|
|
|
def single_team_details_data
|
|
|
|
{ team: @team }
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_team!
|
2017-09-14 19:11:19 +08:00
|
|
|
raise ClientApi::CustomTeamError unless @params
|
2019-09-12 23:21:48 +08:00
|
|
|
return if @team.update(@params)
|
2017-09-12 23:30:13 +08:00
|
|
|
raise ClientApi::CustomTeamError, @team.errors.full_messages
|
|
|
|
end
|
|
|
|
|
2017-08-30 15:29:45 +08:00
|
|
|
def teams_data
|
2017-10-27 15:35:39 +08:00
|
|
|
{ teams: @user.datatables_teams }
|
2017-08-30 15:29:45 +08:00
|
|
|
end
|
|
|
|
end
|
2017-09-18 15:13:37 +08:00
|
|
|
|
2017-08-30 22:18:21 +08:00
|
|
|
CustomTeamError = Class.new(StandardError)
|
2017-08-30 15:29:45 +08:00
|
|
|
end
|