fix(registration): seed gamification profile with eventify_id on account creation

Added _seed_gamification_profile() helper that inserts a row into
user_gamification_profiles immediately after user.save(), so every new
account has their eventify_id in the Node.js gamification DB from day one.
Non-fatal: failures are logged as warnings without blocking registration.
Called in both RegisterView (mobile) and WebRegisterView (web).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-09 06:47:58 +05:30
parent 60d98f1ae8
commit 086bbbf546

View File

@@ -10,12 +10,32 @@ from rest_framework.authtoken.models import Token
from mobile_api.forms import RegisterForm, LoginForm, WebRegisterForm
from rest_framework.authentication import TokenAuthentication
from django.contrib.auth import logout
from django.db import connection
from mobile_api.utils import validate_token_and_get_user
from utils.errors_json_convertor import simplify_form_errors
from accounts.models import User
from eventify_logger.services import log
def _seed_gamification_profile(user):
"""Insert a gamification profile row for a newly registered user.
Non-fatal: if the insert fails for any reason, registration still succeeds."""
try:
with connection.cursor() as cursor:
cursor.execute("""
INSERT INTO user_gamification_profiles (user_id, eventify_id)
VALUES (%s, %s)
ON CONFLICT (user_id) DO UPDATE
SET eventify_id = COALESCE(
user_gamification_profiles.eventify_id,
EXCLUDED.eventify_id
)
""", [user.email, user.eventify_id])
except Exception as e:
log("warning", "Failed to seed gamification profile on registration",
logger_data={"user": user.email, "error": str(e)})
@method_decorator(csrf_exempt, name='dispatch')
class RegisterView(View):
def post(self, request):
@@ -24,6 +44,7 @@ class RegisterView(View):
form = RegisterForm(data)
if form.is_valid():
user = form.save()
_seed_gamification_profile(user)
token, _ = Token.objects.get_or_create(user=user)
log("info", "API user registration", request=request, user=user)
return JsonResponse({'message': 'User registered successfully', 'token': token.key}, status=201)
@@ -51,6 +72,7 @@ class WebRegisterView(View):
if form.is_valid():
print('2')
user = form.save()
_seed_gamification_profile(user)
token, _ = Token.objects.get_or_create(user=user)
print('3')
log("info", "Web user registration", request=request, user=user)