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-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
|
|
|
|
|
|
|
|
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
|