This commit is contained in:
IGNY8 VPS (Salman)
2025-12-08 06:15:35 +00:00
parent 156742d679
commit 4e9d8af768
5 changed files with 1733 additions and 107 deletions

View File

@@ -275,19 +275,24 @@ class RegisterSerializer(serializers.Serializer):
def create(self, validated_data):
from django.db import transaction
from igny8_core.business.billing.models import CreditTransaction
with transaction.atomic():
# Get or assign free plan
plan = validated_data.get('plan_id')
if not plan:
# Auto-assign free plan
# ALWAYS assign Free Trial plan for simple signup
# Ignore plan_id parameter - this is for free trial signups only
try:
plan = Plan.objects.get(slug='free-trial', is_active=True)
except Plan.DoesNotExist:
# Fallback to 'free' if free-trial doesn't exist
try:
plan = Plan.objects.get(slug='free', is_active=True)
except Plan.DoesNotExist:
# Fallback: get first active plan ordered by price (cheapest)
# Last fallback: get cheapest active plan
plan = Plan.objects.filter(is_active=True).order_by('price').first()
if not plan:
raise serializers.ValidationError({"plan": "No active plans available"})
raise serializers.ValidationError({
"plan": "Free trial plan not configured. Please contact support."
})
# Generate account name if not provided
account_name = validated_data.get('account_name')
@@ -295,7 +300,8 @@ class RegisterSerializer(serializers.Serializer):
first_name = validated_data.get('first_name', '')
last_name = validated_data.get('last_name', '')
if first_name or last_name:
account_name = f"{first_name} {last_name}".strip() or validated_data['email'].split('@')[0]
account_name = f"{first_name} {last_name}".strip() or \
validated_data['email'].split('@')[0]
else:
account_name = validated_data['email'].split('@')[0]
@@ -321,19 +327,39 @@ class RegisterSerializer(serializers.Serializer):
role='owner'
)
# Now create account with user as owner, ensuring slug uniqueness
# Generate unique slug for account
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
# Get trial credits from plan
trial_credits = plan.get_effective_credits_per_month()
# Create account with trial status and credits seeded
account = Account.objects.create(
name=account_name,
slug=slug,
owner=user,
plan=plan
plan=plan,
credits=trial_credits, # CRITICAL: Seed initial credits
status='trial' # CRITICAL: Set as trial account
)
# Log initial credit transaction for transparency
CreditTransaction.objects.create(
account=account,
transaction_type='subscription',
amount=trial_credits,
balance_after=trial_credits,
description=f'Free trial credits from {plan.name}',
metadata={
'plan_slug': plan.slug,
'registration': True,
'trial': True
}
)
# Update user to reference the new account