Rails API, app Flutter, infrastruttura Docker/MediaMTX, sito marketing e documentazione di deploy. Co-authored-by: Cursor <cursoragent@cursor.com>
123 lines
4.1 KiB
Ruby
123 lines
4.1 KiB
Ruby
module Api
|
|
module V1
|
|
class TeamsController < BaseController
|
|
def index
|
|
teams = current_user.teams.includes(:matches)
|
|
render json: teams.map { |t| team_json(t) }
|
|
end
|
|
|
|
def show
|
|
team = current_user.teams.find(params[:id])
|
|
render json: team_json(team, detail: true)
|
|
end
|
|
|
|
def create
|
|
if current_user.user_teams.where(role: "owner").count >= 1
|
|
return render json: {
|
|
error: "Puoi gestire una sola squadra. Usa il sito per inviti o contattaci."
|
|
}, status: :unprocessable_entity
|
|
end
|
|
|
|
team = Team.create!(team_params)
|
|
UserTeam.create!(user: current_user, team: team, role: "owner")
|
|
Billing::AssignPlan.call(team: team, plan_slug: "free")
|
|
render json: team_json(team), status: :created
|
|
end
|
|
|
|
def recordings
|
|
team = current_user.teams.find(params[:id])
|
|
unless team.entitlements.can_access_recordings?
|
|
return render json: {
|
|
error: "Archivio gare disponibile con Premium Light o Full",
|
|
error_code: "premium_required",
|
|
billing_url: team.entitlements.billing_url
|
|
}, status: :forbidden
|
|
end
|
|
|
|
recs = team.recordings.ready.includes(stream_session: :match).order(created_at: :desc).limit(50)
|
|
render json: recs.map { |r| recording_json(r) }
|
|
end
|
|
|
|
def update
|
|
team = current_user.teams.find(params[:id])
|
|
team.update!(team_params)
|
|
render json: team_json(team)
|
|
end
|
|
|
|
def add_member
|
|
team = current_user.teams.find(params[:id])
|
|
team.entitlements.assert_can_invite!
|
|
member = User.find(params[:user_id])
|
|
UserTeam.find_or_create_by!(user: member, team: team) { |ut| ut.role = "member" }
|
|
head :no_content
|
|
end
|
|
|
|
def remove_member
|
|
team = current_user.teams.find(params[:id])
|
|
ut = team.user_teams.find_by!(user_id: params[:user_id], role: "member")
|
|
ut.destroy!
|
|
head :no_content
|
|
end
|
|
|
|
private
|
|
|
|
def team_params
|
|
params.require(:team).permit(:name, :sport, :logo_url)
|
|
end
|
|
|
|
def team_json(team, detail: false)
|
|
ent = team.entitlements
|
|
data = {
|
|
id: team.id,
|
|
name: team.name,
|
|
sport: team.sport,
|
|
logo_url: team.logo_url,
|
|
youtube_connected: team.youtube_credential.present?,
|
|
plan_slug: ent.plan.slug,
|
|
plan_name: ent.plan.name,
|
|
premium_active: ent.premium_active?,
|
|
premium_full: ent.premium_full?,
|
|
subscription_status: ent.subscription.status,
|
|
features: ent.plan.features,
|
|
billing_url: ent.billing_url,
|
|
staff_manage_url: ent.staff_manage_url,
|
|
max_staff: ent.max_staff,
|
|
max_staff_transmission: ent.max_staff_transmission,
|
|
max_staff_regia: ent.max_staff_regia,
|
|
staff_used: ent.staff_count,
|
|
staff_transmission_used: ent.staff_count_for("transmission"),
|
|
staff_regia_used: ent.staff_count_for("regia"),
|
|
concurrent_streams_used: ent.concurrent_streams_used,
|
|
concurrent_streams_limit: ent.concurrent_streams_limit,
|
|
recordings_enabled: ent.can_access_recordings?,
|
|
phone_download_enabled: ent.phone_download_enabled?,
|
|
youtube_enabled: ent.youtube_enabled?,
|
|
youtube_mode: ent.plan.youtube_mode
|
|
}
|
|
if detail
|
|
data[:members] = team.user_teams.includes(:user).where.not(role: "owner").map do |ut|
|
|
{ id: ut.user.id, name: ut.user.name, email: ut.user.email, role: ut.role }
|
|
end
|
|
end
|
|
data
|
|
end
|
|
|
|
def recording_json(recording)
|
|
session = recording.stream_session
|
|
match = session.match
|
|
{
|
|
id: recording.id,
|
|
session_id: session.id,
|
|
match_id: match.id,
|
|
opponent_name: match.opponent_name,
|
|
team_name: match.team.name,
|
|
ended_at: session.ended_at,
|
|
replay_url: recording.replay_url,
|
|
download_url: recording.replay_url,
|
|
expires_at: recording.expires_at
|
|
}
|
|
end
|
|
end
|
|
end
|
|
end
|