Add subcription feature
This commit is contained in:
@@ -147,14 +147,22 @@ class TenantMember(Base):
|
||||
|
||||
# Additional models for subscriptions, plans, etc.
|
||||
class Subscription(Base):
|
||||
"""Subscription model for tenant billing"""
|
||||
"""Subscription model for tenant billing with tenant linking support"""
|
||||
__tablename__ = "subscriptions"
|
||||
|
||||
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
tenant_id = Column(UUID(as_uuid=True), ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False)
|
||||
tenant_id = Column(UUID(as_uuid=True), ForeignKey("tenants.id", ondelete="CASCADE"), nullable=True)
|
||||
|
||||
# User reference for tenant-independent subscriptions
|
||||
user_id = Column(UUID(as_uuid=True), nullable=True, index=True)
|
||||
|
||||
# Tenant linking status
|
||||
is_tenant_linked = Column(Boolean, default=False, nullable=False)
|
||||
tenant_linking_status = Column(String(50), nullable=True) # pending, completed, failed
|
||||
linked_at = Column(DateTime(timezone=True), nullable=True)
|
||||
|
||||
plan = Column(String(50), default="starter") # starter, professional, enterprise
|
||||
status = Column(String(50), default="active") # active, pending_cancellation, inactive, suspended
|
||||
status = Column(String(50), default="active") # active, pending_cancellation, inactive, suspended, pending_tenant_linking
|
||||
|
||||
# Billing
|
||||
monthly_price = Column(Float, default=0.0)
|
||||
@@ -182,4 +190,14 @@ class Subscription(Base):
|
||||
tenant = relationship("Tenant")
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Subscription(tenant_id={self.tenant_id}, plan={self.plan}, status={self.status})>"
|
||||
return f"<Subscription(id={self.id}, tenant_id={self.tenant_id}, user_id={self.user_id}, plan={self.plan}, status={self.status})>"
|
||||
|
||||
def is_pending_tenant_linking(self) -> bool:
|
||||
"""Check if subscription is waiting to be linked to a tenant"""
|
||||
return self.tenant_linking_status == "pending" and not self.is_tenant_linked
|
||||
|
||||
def can_be_linked_to_tenant(self, user_id: str) -> bool:
|
||||
"""Check if subscription can be linked to a tenant by the given user"""
|
||||
return (self.is_pending_tenant_linking() and
|
||||
str(self.user_id) == user_id and
|
||||
self.tenant_id is None)
|
||||
|
||||
Reference in New Issue
Block a user