Billing Stripe, link regia mobile e staff solo trasmissione.

Aggiunge fatturazione club, pagina regia condivisibile senza account, roster squadre e rimuove la modalità controller dall'app (v1.1.0).

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-05-29 07:23:13 +02:00
parent 4083bc5dee
commit f4b7be0f80
156 changed files with 5033 additions and 2033 deletions

View File

@@ -0,0 +1,38 @@
require "rails_helper"
RSpec.describe Public::BillingHelper, type: :helper do
before { load Rails.root.join("db/seeds/plans.rb") }
let(:premium_light) { Plan["premium_light"] }
let(:premium_full) { Plan["premium_full"] }
let(:free_plan) { Plan["free"] }
it "segna il piano corrente" do
action = helper.plan_billing_action(
current_slug: "premium_light",
target_plan: premium_light,
stripe_subscription_active: true
)
expect(action[:kind]).to eq(:current)
end
it "propone attivazione da free" do
action = helper.plan_billing_action(
current_slug: "free",
target_plan: premium_light,
stripe_subscription_active: false
)
expect(action[:kind]).to eq(:checkout)
expect(action[:label]).to include("Attiva")
end
it "propone upgrade con abbonamento attivo" do
action = helper.plan_billing_action(
current_slug: "premium_light",
target_plan: premium_full,
stripe_subscription_active: true
)
expect(action[:kind]).to eq(:change)
expect(action[:label]).to include("Passa a")
end
end

View File

@@ -0,0 +1,27 @@
require "rails_helper"
RSpec.describe Billing::Invoice do
let(:club) { Club.create!(name: "Invoice Club", sport: "volleyball", primary_color: "#e53935", secondary_color: "#ffffff") }
it "richiede PDF per fatture emesse" do
invoice = club.billing_invoices.build(
number: "2026/001",
issued_on: Date.current,
amount_cents: 4000,
status: "issued"
)
expect(invoice).not_to be_valid
expect(invoice.errors[:pdf]).to be_present
end
it "accetta bozza senza PDF" do
invoice = club.billing_invoices.create!(
number: "2026/002",
issued_on: Date.current,
amount_cents: 4000,
status: "draft"
)
expect(invoice).to be_persisted
expect(invoice.downloadable?).to be false
end
end

View File

@@ -0,0 +1,29 @@
require "rails_helper"
RSpec.describe ClubBillingProfile do
let(:club) do
Club.create!(
name: "Test Club",
sport: "volleyball",
primary_color: "#e53935",
secondary_color: "#ffffff",
billing_legal_name: "ASD Test",
billing_email: "billing@test.it",
billing_address_line: "Via Roma 1",
billing_city: "Milano",
billing_postal_code: "20100",
billing_vat_number: "12345678901",
billing_recipient_code: "ABCDEFG"
)
end
it "is complete with required fields" do
expect(club.billing_profile_complete?).to be true
end
it "requires SDI or PEC" do
club.billing_recipient_code = nil
club.billing_pec = nil
expect(club.billing_profile_complete?).to be false
end
end

View File

@@ -1,4 +1,4 @@
ENV["RAILS_ENV"] ||= "test"
ENV["RAILS_ENV"] = "test"
require_relative "../config/environment"
require "rspec/rails"
@@ -6,4 +6,8 @@ RSpec.configure do |config|
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
config.before(:each, type: :request) do
host! "www.example.com"
end
end

View File

@@ -0,0 +1,62 @@
require "rails_helper"
RSpec.describe "Public club billing", type: :request do
let!(:coach) do
User.create!(email: "billing-coach@test.it", name: "Coach", password: "password123", role: "coach")
end
let!(:club) do
c = Club.create!(name: "Billing HTTP Club", sport: "volleyball", primary_color: "#e53935", secondary_color: "#ffffff",
billing_legal_name: "ASD Billing", billing_email: "bill@test.it", billing_address_line: "Via 1",
billing_city: "Milano", billing_postal_code: "20100", billing_vat_number: "12345678901",
billing_recipient_code: "ABCDEFG")
ClubMembership.create!(user: coach, club: c, role: "owner")
c.teams.create!(name: "U15", sport: "volleyball")
load Rails.root.join("db/seeds/plans.rb")
Billing::AssignPlan.call(club: c, plan_slug: "free")
c
end
def login!
post public_login_path, params: { email: coach.email, password: "password123" }
end
it "mostra pagamenti e fatture in billing" do
club.billing_payments.create!(
amount_cents: 4000, currency: "eur", status: "paid", paid_at: Time.current,
description: "Premium Light", stripe_invoice_id: "in_spec_1"
)
club.billing_invoices.create!(
number: "2026/100", issued_on: Date.current, amount_cents: 4000, status: "draft"
)
login!
get public_club_billing_path(club)
expect(response).to have_http_status(:ok)
expect(response.body).to include("Pagamenti effettuati")
expect(response.body).to include("Fatture")
expect(response.body).not_to include("Aruba")
expect(response.body).to include("Premium Light")
expect(response.body).to include("2026/100")
end
it "salva profilo fatturazione" do
club.update!(billing_recipient_code: nil, billing_pec: nil)
login!
patch public_club_billing_profile_path(club), params: {
club: { billing_recipient_code: "XYZABC1", billing_pec: "" }
}
expect(response).to redirect_to(public_club_billing_path(club))
expect(club.reload.billing_recipient_code).to eq("XYZABC1")
end
it "blocca checkout premium senza profilo completo" do
club.update!(billing_vat_number: nil, billing_fiscal_code: nil)
allow(MatchLiveTv).to receive(:stripe_enabled?).and_return(true)
login!
get public_club_checkout_path(club, plan: "premium_light")
expect(response).to redirect_to(public_club_billing_profile_path(club))
end
end

View File

@@ -0,0 +1,25 @@
require "rails_helper"
RSpec.describe "Public regia", type: :request do
let!(:user) { User.create!(email: "regia@test.it", name: "U", password: "password123", role: "coach") }
let!(:club) { Club.create!(name: "C", sport: "volleyball", primary_color: "#e53935", secondary_color: "#ffffff") }
let!(:team) { club.teams.create!(name: "T", sport: "volleyball") }
let!(:match) { team.matches.create!(opponent_name: "Opp", sport: "volleyball") }
let!(:session) { StreamSession.create!(match: match, user: user, platform: "matchlivetv", status: "live") }
it "mostra la pagina regia con token valido" do
token = Sessions::RegiaAccess.new(session).issue_token!
get public_regia_path(token)
expect(response).to have_http_status(:ok)
expect(response.body).to include("Condividi link regia")
expect(response.body).not_to include("Aruba")
end
it "aggiorna il punteggio via API" do
token = Sessions::RegiaAccess.new(session).issue_token!
session.create_score_state!(home_sets: 0, away_sets: 0, home_points: 0, away_points: 0, current_set: 1)
patch public_regia_score_path(token), params: { cmd: "home_point" }
expect(response).to have_http_status(:ok)
expect(session.score_state.reload.home_points).to eq(1)
end
end

View File

@@ -0,0 +1,57 @@
require "rails_helper"
RSpec.describe Billing::Stripe::ChangePlan do
let(:club) { Club.create!(name: "Billing Club", sport: "volleyball", primary_color: "#e53935", secondary_color: "#ffffff") }
before do
load Rails.root.join("db/seeds/plans.rb")
Billing::AssignPlan.call(
club: club,
plan_slug: "premium_light",
status: "active",
stripe_attrs: {
stripe_customer_id: "cus_test",
stripe_subscription_id: "sub_test_123"
}
)
allow(MatchLiveTv).to receive(:stripe_enabled?).and_return(true)
allow(MatchLiveTv).to receive(:stripe_premium_light_price_id).and_return("price_light")
allow(MatchLiveTv).to receive(:stripe_premium_full_price_id).and_return("price_full")
Plan.find_by!(slug: "premium_light").update!(stripe_price_id: "price_light")
Plan.find_by!(slug: "premium_full").update!(stripe_price_id: "price_full")
end
it "aggiorna Stripe e il piano locale verso premium_full" do
stripe_item = double(id: "si_test")
stripe_sub = double(
id: "sub_test_123",
customer: "cus_test",
status: "active",
current_period_start: Time.current.to_i,
current_period_end: 1.year.from_now.to_i,
cancel_at_period_end: false,
items: double(data: [stripe_item])
)
updated_sub = stripe_sub
allow(Stripe::Subscription).to receive(:retrieve).with("sub_test_123").and_return(stripe_sub)
allow(Stripe::Subscription).to receive(:update).and_return(updated_sub)
described_class.call(club: club, plan_slug: "premium_full")
expect(Stripe::Subscription).to have_received(:update).with(
"sub_test_123",
hash_including(
items: [{ id: "si_test", price: "price_full" }],
metadata: { club_id: club.id, plan_slug: "premium_full" }
)
)
expect(club.reload.subscription.plan.slug).to eq("premium_full")
end
it "rifiuta il cambio sullo stesso piano" do
expect {
described_class.call(club: club, plan_slug: "premium_light")
}.to raise_error(/già su questo piano/)
end
end

View File

@@ -0,0 +1,37 @@
require "rails_helper"
RSpec.describe Billing::Stripe::RecordPayment do
let(:club) { Club.create!(name: "Pay Club", sport: "volleyball", primary_color: "#e53935", secondary_color: "#ffffff") }
before do
load Rails.root.join("db/seeds/plans.rb")
Billing::AssignPlan.call(club: club, plan_slug: "premium_light", stripe_attrs: { stripe_subscription_id: "sub_x" })
end
it "crea un pagamento da fattura Stripe" do
invoice = double(
id: "in_test_1",
subscription: "sub_x",
metadata: { "club_id" => club.id },
amount_paid: 4000,
currency: "eur",
status: "paid",
description: "Premium Light",
payment_intent: "pi_x",
charge: "ch_x",
invoice_pdf: "https://stripe.com/invoice.pdf",
hosted_invoice_url: "https://stripe.com/hosted",
status_transitions: double(paid_at: Time.current.to_i),
lines: double(data: [double(description: "Premium Light annuale")]),
subscription_details: nil
)
allow(invoice).to receive(:metadata).and_return({ "club_id" => club.id })
payment = described_class.call(stripe_invoice: invoice)
expect(payment).to be_persisted
expect(payment.club).to eq(club)
expect(payment.amount_cents).to eq(4000)
expect(payment.stripe_invoice_id).to eq("in_test_1")
end
end

View File

@@ -0,0 +1,15 @@
require "rails_helper"
RSpec.describe Scoring::ApplyAction do
let!(:user) { User.create!(email: "score@test.it", name: "U", password: "password123", role: "coach") }
let!(:club) { Club.create!(name: "C", sport: "volleyball", primary_color: "#e53935", secondary_color: "#ffffff") }
let!(:team) { club.teams.create!(name: "T", sport: "volleyball") }
let!(:match) { team.matches.create!(opponent_name: "Opp", sport: "volleyball", sets_to_win: 3) }
let!(:session) { StreamSession.create!(match: match, user: user, platform: "matchlivetv", status: "live") }
it "incrementa i punti casa" do
session.create_score_state!(home_sets: 0, away_sets: 0, home_points: 0, away_points: 0, current_set: 1)
result = described_class.new(session: session, action: "home_point").call
expect(result.score.home_points).to eq(1)
end
end

View File

@@ -1,50 +1,18 @@
require "rails_helper"
RSpec.describe Teams::Entitlements do
let(:club) { Club.create!(name: "Test Club", sport: "volleyball", primary_color: "#e53935", secondary_color: "#ffffff") }
let(:team) { club.teams.create!(name: "Test Team", sport: "volleyball") }
let!(:club) { Club.create!(name: "Club", sport: "volleyball", primary_color: "#e53935", secondary_color: "#ffffff") }
let!(:team) { club.teams.create!(name: "Team", sport: "volleyball") }
before do
load Rails.root.join("db/seeds/plans.rb")
Billing::AssignPlan.call(club: club, plan_slug: "free")
end
subject(:ent) { described_class.new(team) }
it "allows matchlivetv on free" do
expect(ent.can_stream_on?("matchlivetv")).to be true
end
it "denies youtube on free" do
expect { ent.assert_can_stream_on!("youtube") }.to raise_error(Teams::EntitlementError)
end
it "allows youtube on premium full" do
Billing::AssignPlan.call(club: club, plan_slug: "premium_full")
expect(ent.can_stream_on?("youtube")).to be true
end
it "enforces transmission staff limit on free" do
team.team_invitations.create!(
email: "camera@test.com",
token_digest: SecureRandom.hex(32),
role: "member",
staff_kind: "transmission",
expires_at: 7.days.from_now
)
expect { ent.assert_can_invite!(staff_kind: "transmission") }.to raise_error(Teams::EntitlementError) do |e|
expect(e.code).to eq("staff_limit_reached")
end
end
it "allows regia invite when only transmission slot is used on free" do
team.team_invitations.create!(
email: "camera@test.com",
token_digest: SecureRandom.hex(32),
role: "member",
staff_kind: "transmission",
expires_at: 7.days.from_now
)
expect { ent.assert_can_invite!(staff_kind: "regia") }.not_to raise_error
it "limits transmission invites on free plan" do
user = User.create!(email: "t1@test.com", name: "T1", password: "password123", role: "coach")
UserTeam.create!(user: user, team: team, role: "member", staff_kind: "transmission")
ent = described_class.new(team)
expect { ent.assert_can_invite! }.to raise_error(Teams::EntitlementError, /trasmissione/)
end
end

View File

@@ -1,38 +1,28 @@
require "rails_helper"
RSpec.describe Teams::StaffAssignment do
let(:club) { Club.create!(name: "Test Club", sport: "volleyball", primary_color: "#e53935", secondary_color: "#ffffff") }
let(:team) { club.teams.create!(name: "Test", sport: "volleyball") }
let(:owner) { User.create!(name: "Coach", email: "owner@test.com", password: "password123", role: "coach") }
let!(:owner) { User.create!(name: "Owner", email: "owner@test.com", password: "password123", role: "coach") }
let!(:club) { Club.create!(name: "Club", sport: "volleyball", primary_color: "#e53935", secondary_color: "#ffffff") }
let!(:team) { club.teams.create!(name: "Team", sport: "volleyball") }
let!(:other) { User.create!(name: "Other", email: "other@test.com", password: "password123", role: "coach") }
before do
ClubMembership.create!(user: owner, club: club, role: "owner")
load Rails.root.join("db/seeds/plans.rb")
Billing::AssignPlan.call(club: club, plan_slug: "free")
ClubMembership.create!(user: owner, club: club, role: "owner")
end
it "assigns owner as transmission staff" do
it "assigns transmission to owner" do
membership = UserTeam.create!(user: owner, team: team, role: "member")
described_class.call(team: team, user: owner, staff_kind: "transmission", membership: membership)
described_class.call(team: team, user: owner, membership: membership)
expect(membership.reload.staff_kind).to eq("transmission")
expect(team.entitlements.staff_count_for("transmission")).to eq(1)
end
it "blocks same email for transmission and regia" do
membership = UserTeam.create!(user: owner, team: team, role: "member")
described_class.call(team: team, user: owner, staff_kind: "transmission", membership: membership)
it "rejects duplicate email for transmission" do
UserTeam.create!(user: other, team: team, role: "member", staff_kind: "transmission")
expect {
described_class.call(team: team, user: owner, staff_kind: "regia", membership: membership)
}.to raise_error(Teams::StaffAssignmentError, /email diverse/i)
end
it "blocks invite when email already used for other staff kind" do
membership = UserTeam.create!(user: owner, team: team, role: "member")
described_class.call(team: team, user: owner, staff_kind: "transmission", membership: membership)
expect {
Teams::StaffEmailValidator.assert_available!(team: team, email: owner.email, staff_kind: "regia")
}.to raise_error(Teams::StaffAssignmentError, /già assegnata/i)
Teams::StaffEmailValidator.assert_available!(team: team, email: other.email)
}.to raise_error(Teams::StaffAssignmentError, /già assegnata/)
end
end

View File

@@ -2,7 +2,8 @@ require "rails_helper"
RSpec.describe Webhooks::MediamtxHandler do
let(:user) { User.create!(email: "c@test.com", name: "C", password: "password123", role: "coach") }
let(:team) { Team.create!(name: "T") }
let(:club) { Club.create!(name: "MTX Club", sport: "volleyball", primary_color: "#e53935", secondary_color: "#ffffff") }
let(:team) { club.teams.create!(name: "T", sport: "volleyball") }
let!(:match) { team.matches.create!(opponent_name: "Away") }
let!(:session) do
StreamSession.create!(