Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions chatbot/utils/elevate/profile_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def fetch_elevate_user(access_token):
'school_name': school_name,
'district': district.get('label'),
'state': state.get('label'),
'has_accepted_tnc': bool(user_data.get('has_accepted_terms_and_conditions', False)),
}

except requests.exceptions.HTTPError as e:
Expand Down Expand Up @@ -107,7 +108,7 @@ def upsert_elevate_profile(user_data):

return {
"profileid": profile.id,
"has_accepted_tnc": (profile.other_params or {}).get('is_tnc_accepted', False),
"has_accepted_tnc": user_data.get('has_accepted_tnc', False),
"route": language,
"reroute_url": os.getenv('SSO_REROUTE_URL'),
"ums_profile": {
Expand All @@ -125,7 +126,8 @@ def handle_elevate_profile(access_token):
return upsert_elevate_profile(user_data)


def update_elevate_profile(access_token, name=None, role=None, school_name=None, district=None, state=None):
def update_elevate_profile(access_token, name=None, role=None, school_name=None, district=None, state=None,
has_accepted_terms_and_conditions=None):
try:
url = f"{elevate_base_url}/user/v1/user/update"
headers = {'X-auth-token': access_token}
Expand All @@ -140,6 +142,8 @@ def update_elevate_profile(access_token, name=None, role=None, school_name=None,
body['userDistrict'] = district
if state:
body['profileState'] = state
if has_accepted_terms_and_conditions is not None:
body['has_accepted_terms_and_conditions'] = has_accepted_terms_and_conditions

logger.info(f'[update_elevate_profile] sending body={body}')
response = requests.patch(url, headers=headers, json=body, timeout=30)
Expand Down
39 changes: 32 additions & 7 deletions chatbot/views/api_views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import os
import traceback
from django.contrib.auth.hashers import check_password
from rest_framework.response import Response
Expand All @@ -9,13 +10,22 @@
from chatbot.models.company_models import Company, CompanyBot
from chatbot.models.profile_models import Profile
from chatbot.serializer.profile_serializer import ProfileSerializer
from chatbot.utils.elevate.profile_utils import fetch_elevate_user, update_elevate_profile
from django.http import JsonResponse
from django.contrib.sessions.backends.db import SessionStore
from rest_framework_simplejwt.tokens import RefreshToken
from chatbot.translate.ai4Bharat.transliterate import call_ai4bharat_transliterate_api
from chatbot.models.company_models import Flow

logger = logging.getLogger('django')
ACCESS_TOKEN_COOKIE_KEY = os.getenv('ACCESS_TOKEN_COOKIE_KEY')


def _get_access_token(request):
access_token = request.COOKIES.get(ACCESS_TOKEN_COOKIE_KEY) if ACCESS_TOKEN_COOKIE_KEY else None
if not access_token:
access_token = request.headers.get('X-auth-token')
return access_token


def generate_session_id(request):
Expand Down Expand Up @@ -212,9 +222,19 @@ def get_profile_view(request):
}, status=404)
profile = Profile.objects.get(email=email, company=company)

is_tnc_accepted = bool(
profile.other_params and profile.other_params.get('is_tnc_accepted', False)
)
access_token = _get_access_token(request)
is_tnc_accepted = False
if access_token:
elevate_user_data = fetch_elevate_user(access_token)
if elevate_user_data.get('error'):
logger.error(
'[get_profile_view] failed to fetch tnc status from Elevate: %s',
elevate_user_data.get('error')
)
else:
is_tnc_accepted = elevate_user_data.get('has_accepted_tnc', False)
else:
logger.error('[get_profile_view] no access_token available — defaulting is_tnc_accepted to False')

is_onboarding_completed = bool(
profile.other_params and profile.other_params.get('is_onboarding_completed', False)
Expand Down Expand Up @@ -272,10 +292,15 @@ def accept_tnc_view(request):
'message': 'No company found'
}, status=404)
profile = Profile.objects.get(email=email, company=company)
other_params = profile.other_params or {}
other_params['is_tnc_accepted'] = True
profile.other_params = other_params
profile.save(update_fields=['other_params', 'updated_at'])

access_token = _get_access_token(request)
if not access_token:
return Response({
'status': 'error',
'message': 'access_token is required to accept terms and conditions'
}, status=400)

update_elevate_profile(access_token, has_accepted_terms_and_conditions=True)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

return Response({
'status': 'ok',
Expand Down
2 changes: 1 addition & 1 deletion shikshalokam/views/profile_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def read_elevate_profile(request):
'profile_details': {
'profileid': profile_details.get('profileid'),
'company': company_slug,
'has_accepted_tnc': "ONGOING",
'has_accepted_tnc': profile_details.get('has_accepted_tnc', False),
'route': profile_details.get('route'),
'reroute_url': profile_details.get('reroute_url'),
}
Expand Down