diff --git a/backend/src/zango/api/platform/tenancy/v1/views.py b/backend/src/zango/api/platform/tenancy/v1/views.py index 169e2902..d52c0545 100644 --- a/backend/src/zango/api/platform/tenancy/v1/views.py +++ b/backend/src/zango/api/platform/tenancy/v1/views.py @@ -2,9 +2,7 @@ import traceback from django_celery_results.models import TaskResult -from phonenumbers.phonenumberutil import country_code_for_region -from django.conf import settings from django.db.models import Q from django.utils.decorators import method_decorator @@ -19,7 +17,11 @@ from zango.core.api.utils import ZangoAPIPagination from zango.core.common_utils import set_app_schema_path from zango.core.permissions import IsPlatformUserAllowedApp -from zango.core.utils import get_search_columns, validate_phone +from zango.core.utils import ( + get_country_code_for_tenant, + get_search_columns, + validate_phone, +) from .serializers import ( AppUserModelSerializerModel, @@ -342,6 +344,10 @@ class UserViewAPIV1(ZangoGenericPlatformAPIView, ZangoAPIPagination): pagination_class = ZangoAPIPagination permission_classes = (IsPlatformUserAllowedApp,) + def get_app_tenant(self): + tenant_obj = TenantModel.objects.get(uuid=self.kwargs["app_uuid"]) + return tenant_obj + def get_dropdown_options(self): options = {} options["roles"] = [ @@ -388,11 +394,12 @@ def get(self, request, *args, **kwargs): app_users = self.paginate_queryset(app_users, request, view=self) serializer = AppUserModelSerializerModel(app_users, many=True) app_users_data = self.get_paginated_response_data(serializer.data) + app_tenant = self.get_app_tenant() success = True response = { "users": app_users_data, "message": "Users fetched successfully", - "pn_country_code": f"+{country_code_for_region(settings.PHONENUMBER_DEFAULT_REGION)}", + "pn_country_code": get_country_code_for_tenant(app_tenant), } if include_dropdown_options: response["dropdown_options"] = self.get_dropdown_options() diff --git a/backend/src/zango/core/utils.py b/backend/src/zango/core/utils.py index 2ade2af0..b4e051c7 100644 --- a/backend/src/zango/core/utils.py +++ b/backend/src/zango/core/utils.py @@ -3,6 +3,8 @@ import phonenumbers import pytz +from phonenumbers.phonenumberutil import country_code_for_region + from django.conf import settings from django.db import connection from django.shortcuts import render @@ -154,3 +156,29 @@ def get_region_from_timezone(tzname): for tz in timezones: timezone_country[tz] = countrycode return timezone_country[tzname] + + +def get_country_code_for_tenant(tenant, with_plus_sign=True): + """ + Returns the country code for the given tenant. + + The region is first determined from the tenant's timezone. If no timezone is set, + the default region from `settings.PHONENUMBER_DEFAULT_REGION` is used. + + Args: + tenant: A TenantModel instance. + with_plus_sign (bool): Whether to prepend a "+" to the country code. Default is True. + + Returns: + str: The country code with or without "+" based on the region (e.g., "+1" for "US", "+91" for "IN"). + """ + default_region = settings.PHONENUMBER_DEFAULT_REGION + + if tenant.timezone: + try: + default_region = get_region_from_timezone(tenant.timezone) + except Exception: + pass + + country_code = country_code_for_region(default_region) + return f"+{country_code}" if with_plus_sign else country_code