Enhance billing and subscription management: Added payment method checks in ProtectedRoute, improved error handling in billing components, and optimized API calls to reduce throttling. Updated user account handling in various components to ensure accurate plan and subscription data display.

This commit is contained in:
IGNY8 VPS (Salman)
2025-12-07 10:07:28 +00:00
parent 46fc6dcf04
commit 508b6b4220
26 changed files with 518 additions and 69 deletions

View File

@@ -321,10 +321,17 @@ class RegisterSerializer(serializers.Serializer):
role='owner'
)
# Now create account with user as owner
# Now create account with user as owner, ensuring slug uniqueness
base_slug = account_name.lower().replace(' ', '-').replace('_', '-')[:50] or 'account'
slug = base_slug
counter = 1
while Account.objects.filter(slug=slug).exists():
slug = f"{base_slug}-{counter}"
counter += 1
account = Account.objects.create(
name=account_name,
slug=account_name.lower().replace(' ', '-').replace('_', '-')[:50],
slug=slug,
owner=user,
plan=plan
)

View File

@@ -341,7 +341,8 @@ class SubscriptionsViewSet(AccountModelViewSet):
queryset = Subscription.objects.all()
permission_classes = [IsAuthenticatedAndActive, HasTenantAccess, IsOwnerOrAdmin]
pagination_class = CustomPageNumberPagination
throttle_scope = 'auth'
# Use relaxed auth throttle to avoid 429s during onboarding plan fetches
throttle_scope = 'auth_read'
throttle_classes = [DebugScopedRateThrottle]
def get_queryset(self):
@@ -445,8 +446,9 @@ class PlanViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = PlanSerializer
permission_classes = [permissions.AllowAny]
pagination_class = CustomPageNumberPagination
throttle_scope = 'auth'
throttle_classes = [DebugScopedRateThrottle]
# Plans are public and should not throttle aggressively to avoid blocking signup/onboarding
throttle_scope = None
throttle_classes: list = []
def retrieve(self, request, *args, **kwargs):
"""Override retrieve to return unified format"""

View File

@@ -182,6 +182,16 @@ class PaymentService:
if payment.metadata.get('credit_package_id'):
PaymentService._add_credits_for_payment(payment)
# If account is inactive/suspended/trial, activate it on successful payment
try:
account = payment.account
if account and account.status != 'active':
account.status = 'active'
account.save(update_fields=['status', 'updated_at'])
except Exception:
# Do not block payment approval if status update fails
pass
return payment
@staticmethod

View File

@@ -241,6 +241,7 @@ REST_FRAMEWORK = {
# Authentication
'auth': '20/min', # Login, register, password reset
'auth_strict': '5/min', # Sensitive auth operations
'auth_read': '120/min', # Read-only auth-adjacent endpoints (e.g., subscriptions)
# Planner Operations
'planner': '60/min', # Keyword, cluster, idea operations
'planner_ai': '10/min', # AI-powered planner operations