module Public class ClubsController < WebBaseController include BrandingAttachments before_action :require_login! before_action :set_club, only: %i[show edit update billing checkout portal] def new redirect_to public_club_path(current_user.primary_club) if current_user.primary_club end def create if current_user.owned_clubs.exists? redirect_to public_club_path(current_user.primary_club), alert: "Hai già registrato una società" return end club = Club.new(club_params) attach_branding_logo(club) club.save! ClubMembership.create!(user: current_user, club: club, role: "owner") first_team_name = params.dig(:first_team, :name).presence || "Prima squadra" club.teams.create!( name: first_team_name, sport: club.sport ) plan = params[:plan].presence_in(%w[free premium_light premium_full]) || "free" Billing::AssignPlan.call(club: club, plan_slug: plan) if plan.in?(%w[premium_light premium_full]) && MatchLiveTv.stripe_enabled? redirect_to public_club_checkout_path(club, plan: plan) else redirect_to public_club_path(club), notice: "Società e prima squadra create." end rescue ActiveRecord::RecordInvalid => e flash.now[:alert] = e.record.errors.full_messages.join(", ") render :new, status: :unprocessable_entity end def show require_club_owner!(@club) @entitlements_team = @club.teams.first @entitlements = @entitlements_team&.entitlements @teams = @club.teams.order(:name) end def edit require_club_owner!(@club) end def update require_club_owner!(@club) @club.assign_attributes(club_params) attach_branding_logo(@club) @club.save! redirect_to public_club_path(@club), notice: "Società aggiornata." rescue ActiveRecord::RecordInvalid => e flash.now[:alert] = e.record.errors.full_messages.join(", ") render :edit, status: :unprocessable_entity end def billing require_club_owner!(@club) @team = @club.teams.first! @entitlements = @team.entitlements @plans = Plan.ordered end def checkout require_club_owner!(@club) unless MatchLiveTv.stripe_enabled? redirect_to public_club_billing_path(@club), alert: "Pagamenti non ancora configurati sul server" return end plan_slug = params[:plan].presence_in(%w[premium_light premium_full]) || "premium_light" url = Billing::Stripe::CheckoutSession.new(club: @club, user: current_user, plan_slug: plan_slug).url redirect_to url, allow_other_host: true end def portal require_club_owner!(@club) unless MatchLiveTv.stripe_enabled? redirect_to public_club_billing_path(@club), alert: "Portale pagamenti non configurato" return end url = Billing::Stripe::CheckoutSession.new(club: @club, user: current_user).portal_url redirect_to url, allow_other_host: true end private def set_club @club = current_user.owned_clubs.find(params[:id]) end def club_params p = params.require(:club).permit(:name, :sport, :logo_url, :primary_color, :secondary_color) p[:primary_color] = normalize_hex_color(p[:primary_color], "#e53935") p[:secondary_color] = normalize_hex_color(p[:secondary_color], "#ffffff") p end end end