The marketing surface (PricingPage, Stripe products) was wired for "Starter / Pro / Enterprise" while the backend was on "free / pro / team", leaving plan_billing unseeded and BillingPlan accepting a literal that violated the FK to plan_limits. This change: - Migration 4ce3e594cb87: defensive UPDATE of any subscriptions on plan='team' to 'enterprise' (dev has zero), renames the plan_limits row team -> enterprise, inserts a starter row with caps interpolated between free and pro (max_trees=10, sessions=75, ai=15/mo). - Renames the plan tier across schemas (invite_code, billing, admin, subscription comment), is_paid/has_pro_entitlement checks in the Subscription model, admin/admin_dashboard plan validators, and the frontend useSubscription isPaidPlan check. Resource visibility uses the same string 'team' in a separate domain (Tree/StepLibrary visibility) and is intentionally untouched. - New backend/scripts/sync_stripe_plan_ids.py: idempotent upsert of plan_billing rows from Stripe products by exact name match. Picks the active monthly recurring price for tiers that have one; leaves annual fields NULL by design. Works against test or live keys. - Test fixture updates: conftest seeds the new taxonomy, the public plans helper is a true upsert so tests can override max_users, and team -> enterprise across test_admin_plan_limits and test_invite_plan. Verified: 86/86 passing across the subscription/billing/plan/invite/ admin sweep; sync script run against test mode populates plan_billing correctly for all three tiers. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
52 lines
2.7 KiB
Python
52 lines
2.7 KiB
Python
import uuid
|
|
from datetime import datetime, timezone
|
|
from typing import Optional, TYPE_CHECKING
|
|
from sqlalchemy import String, DateTime, ForeignKey, Boolean, Integer
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
from sqlalchemy.dialects.postgresql import UUID
|
|
from app.core.database import Base
|
|
|
|
if TYPE_CHECKING:
|
|
from app.models.account import Account
|
|
|
|
|
|
class Subscription(Base):
|
|
__tablename__ = "subscriptions"
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
|
account_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("accounts.id", ondelete="CASCADE"), unique=True, nullable=False)
|
|
stripe_subscription_id: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
|
stripe_price_id: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
|
plan: Mapped[str] = mapped_column(String(50), nullable=False, default="free")
|
|
billing_interval: Mapped[Optional[str]] = mapped_column(String(20), nullable=True)
|
|
status: Mapped[str] = mapped_column(String(50), nullable=False, default="active")
|
|
seat_limit: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
|
|
current_period_start: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
current_period_end: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True), nullable=True)
|
|
cancel_at_period_end: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
|
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc))
|
|
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc))
|
|
|
|
# Relationships
|
|
account: Mapped["Account"] = relationship("Account", back_populates="subscription")
|
|
|
|
@property
|
|
def is_active(self) -> bool:
|
|
return self.status in ("active", "trialing", "complimentary")
|
|
|
|
@property
|
|
def is_paid(self) -> bool:
|
|
# Excludes complimentary and trialing so MRR/paid-customer metrics aren't inflated.
|
|
return self.plan in ("pro", "starter", "enterprise") and self.status not in ("complimentary", "trialing")
|
|
|
|
@property
|
|
def has_pro_entitlement(self) -> bool:
|
|
"""True if the account can access Pro features right now."""
|
|
if self.plan in ("pro", "starter", "enterprise"):
|
|
if self.status in ("active", "complimentary"):
|
|
return True
|
|
if self.status == "trialing" and self.current_period_end is not None:
|
|
from datetime import datetime, timezone
|
|
return self.current_period_end > datetime.now(timezone.utc)
|
|
return False
|