module Admin class ClubsController < BaseController before_action :set_club, only: %i[show edit update grant_comped revoke_comped set_quote revoke_quote] def index @clubs = Club.includes(:teams, :billing_quote, { club_memberships: :user }, subscription: %i[plan admin_comped_by]) .order(:name) end def show load_show_context end def edit load_edit_context end def update Admin::UpdateClubData.call( club: @club, club_attrs: club_update_params, owner_attrs: owner_params, staff_attrs: staff_params, invitation_attrs: invitation_params, team_attrs: team_params ) redirect_to admin_club_path(@club), notice: t("admin.flash.club_updated", club: @club.name) rescue Admin::UpdateClubData::Error, ActiveRecord::RecordInvalid => e flash.now[:alert] = e.message load_edit_context render :edit, status: :unprocessable_entity end def grant_comped Billing::AdminCompedSubscription.grant( club: @club, plan_slug: params.require(:plan_slug), reason: params[:reason], admin: current_admin_account ) redirect_back_or_club notice: t("admin.flash.comped_granted", plan: Plan[params[:plan_slug]].name, club: @club.name) rescue Billing::AdminCompedSubscription::Error, ActiveRecord::RecordInvalid => e redirect_back_or_club alert: e.message end def revoke_comped Billing::AdminCompedSubscription.revoke(club: @club, admin: current_admin_account) redirect_back_or_club notice: t("admin.flash.comped_revoked", club: @club.name) rescue Billing::AdminCompedSubscription::Error => e redirect_back_or_club alert: e.message end def set_quote quote = Billing::SetClubQuote.upsert( club: @club, plan_slug: params.require(:plan_slug), interval: params[:interval], amount_euros: params[:amount_euros], note: params[:note], admin: current_admin_account ) redirect_back_or_club notice: t( "admin.flash.quote_saved", club: @club.name, plan: quote.plan.name, amount: quote.formatted_amount ) rescue Billing::SetClubQuote::Error, ActionController::ParameterMissing => e redirect_back_or_club alert: e.message end def revoke_quote Billing::SetClubQuote.revoke(club: @club, admin: current_admin_account) redirect_back_or_club notice: t("admin.flash.quote_revoked", club: @club.name) rescue Billing::SetClubQuote::Error => e redirect_back_or_club alert: e.message end private def set_club @club = Club.includes(club_memberships: :user).find(params[:id]) end def load_show_context @subscription = @club.subscription || @club.build_subscription(plan: Plan["free"], status: "active") @plans = Plan.ordered.reject { |p| p.slug == "free" } @teams = @club.teams.order(:name) @quote = @club.active_billing_quote @concurrency_violations = StreamConcurrencyViolation.for_club(@club.id).recent.limit(20) end def load_edit_context @teams = @club.teams.includes(:user_teams, :team_invitations).order(:name) @owner = @club.owner @staff_users = User.joins(:user_teams) .where(user_teams: { team_id: @club.teams.select(:id) }) .distinct .order(:email) .to_a @pending_invitations = TeamInvitation.pending .where(team_id: @club.teams.select(:id)) .includes(:team) .order(:email) @sport_options = Sports::Catalog.as_api_list.map { |entry| [entry[:label], entry[:key]] } end def club_update_params params.require(:club).permit( :name, :sport, :logo_url, :primary_color, :secondary_color, :billing_entity_type, :billing_legal_name, :billing_vat_number, :billing_fiscal_code, :billing_email, :billing_phone, :billing_address_line, :billing_city, :billing_province, :billing_postal_code, :billing_country, :billing_recipient_code, :billing_pec ) end def owner_params params.fetch(:owner, {}).permit(:name, :email).to_h.symbolize_keys end def staff_params raw = params[:staff_users] return {} if raw.blank? raw.permit!.to_h.each_with_object({}) do |(user_id, attrs), acc| next unless attrs.is_a?(Hash) acc[user_id] = attrs.slice("name", "email").symbolize_keys end end def invitation_params raw = params[:invitations] return {} if raw.blank? raw.permit!.to_h.each_with_object({}) do |(invitation_id, attrs), acc| next unless attrs.is_a?(Hash) acc[invitation_id] = attrs.slice("email").symbolize_keys end end def team_params raw = params[:teams] return {} if raw.blank? raw.permit!.to_h.each_with_object({}) do |(team_id, attrs), acc| next unless attrs.is_a?(Hash) acc[team_id] = attrs.slice("name", "sport").symbolize_keys end end def redirect_back_or_club(notice: nil, alert: nil) target = params[:return_to].presence || admin_club_path(@club) redirect_to target, notice: notice, alert: alert end end end