2017-08-30 15:29:45 +08:00
|
|
|
module ClientApi
|
|
|
|
class TeamsService
|
|
|
|
def initialize(arg)
|
2017-08-30 22:18:21 +08:00
|
|
|
team_id = arg.fetch(:team_id) { raise ClientApi::CustomTeamError }
|
2017-09-12 23:30:13 +08:00
|
|
|
@params = arg.fetch(:params) { false }
|
2017-08-30 15:29:45 +08:00
|
|
|
@team = Team.find_by_id(team_id)
|
2017-08-30 22:18:21 +08:00
|
|
|
@user = arg.fetch(:current_user) { raise ClientApi::CustomTeamError }
|
|
|
|
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
|
2017-09-12 23:30:13 +08:00
|
|
|
return if @team.update_attributes(@params)
|
|
|
|
raise ClientApi::CustomTeamError, @team.errors.full_messages
|
|
|
|
end
|
|
|
|
|
2017-08-30 15:29:45 +08:00
|
|
|
def teams_data
|
2017-08-30 22:18:21 +08:00
|
|
|
{ teams: @user.teams_data }
|
2017-08-30 15:29:45 +08:00
|
|
|
end
|
|
|
|
end
|
2017-08-30 22:18:21 +08:00
|
|
|
CustomTeamError = Class.new(StandardError)
|
2017-08-30 15:29:45 +08:00
|
|
|
end
|