Add subcription feature 3

This commit is contained in:
Urtzi Alfaro
2026-01-15 20:45:49 +01:00
parent a4c3b7da3f
commit b674708a4c
83 changed files with 9451 additions and 6828 deletions

View File

@@ -11,7 +11,7 @@ import structlog
import uuid
from .base import TenantBaseRepository
from app.models.tenants import Tenant
from app.models.tenants import Tenant, Subscription
from shared.database.exceptions import DatabaseError, ValidationError, DuplicateRecordError
logger = structlog.get_logger()
@@ -570,3 +570,39 @@ class TenantRepository(TenantBaseRepository):
session_id=session_id,
error=str(e))
raise DatabaseError(f"Failed to get enterprise demo tenants: {str(e)}")
async def get_by_customer_id(self, customer_id: str) -> Optional[Tenant]:
"""
Get tenant by Stripe customer ID
Args:
customer_id: Stripe customer ID
Returns:
Tenant object if found, None otherwise
"""
try:
# Find tenant by joining with subscriptions table
# Tenant doesn't have customer_id directly, so we need to find via subscription
query = select(Tenant).join(
Subscription, Subscription.tenant_id == Tenant.id
).where(Subscription.customer_id == customer_id)
result = await self.session.execute(query)
tenant = result.scalar_one_or_none()
if tenant:
logger.debug("Found tenant by customer_id",
customer_id=customer_id,
tenant_id=str(tenant.id))
return tenant
else:
logger.debug("No tenant found for customer_id",
customer_id=customer_id)
return None
except Exception as e:
logger.error("Error getting tenant by customer_id",
customer_id=customer_id,
error=str(e))
raise DatabaseError(f"Failed to get tenant by customer_id: {str(e)}")