diff --git a/apps/users/helpers.py b/apps/users/helpers.py index 1d91ba3..35ededb 100644 --- a/apps/users/helpers.py +++ b/apps/users/helpers.py @@ -1,5 +1,6 @@ from typing import Tuple +from fyle_integrations_platform_connector import PlatformConnector from fyle_rest_auth.models import AuthToken from apps.fyle.helpers import get_cluster_domain @@ -20,3 +21,21 @@ def get_cluster_domain_and_refresh_token(user) -> Tuple[str, str]: cluster_domain = get_cluster_domain(refresh_token) return cluster_domain, refresh_token + + +def get_user_profile(request): + """ + Get user profile + """ + refresh_token = AuthToken.objects.get(user__user_id=request.user).refresh_token + cluster_domain, _ = get_cluster_domain_and_refresh_token(request.user) + + fyle_credentials = FyleCredential( + cluster_domain=cluster_domain, + refresh_token=refresh_token + ) + + platform = PlatformConnector(fyle_credentials) + employee_profile = platform.connection.v1beta.spender.my_profile.get() + + return employee_profile diff --git a/apps/users/urls.py b/apps/users/urls.py index 6475fc7..d264fce 100644 --- a/apps/users/urls.py +++ b/apps/users/urls.py @@ -15,8 +15,9 @@ """ from django.urls import path -from apps.users.views import FyleOrgsView +from apps.users.views import FyleOrgsView, UserProfileView urlpatterns = [ + path('profile/', UserProfileView.as_view(), name='user-profile'), path('orgs/', FyleOrgsView.as_view(), name='fyle-orgs') ] diff --git a/apps/users/views.py b/apps/users/views.py index 05dfd87..768317a 100644 --- a/apps/users/views.py +++ b/apps/users/views.py @@ -3,7 +3,7 @@ from rest_framework.response import Response from apps.fyle.helpers import get_fyle_orgs -from apps.users.helpers import get_cluster_domain_and_refresh_token +from apps.users.helpers import get_cluster_domain_and_refresh_token, get_user_profile class FyleOrgsView(generics.ListCreateAPIView): @@ -24,3 +24,18 @@ def get(self, request, *args, **kwargs): data=fyle_orgs, status=status.HTTP_200_OK ) + + +class UserProfileView(generics.RetrieveAPIView): + + permission_classes = [IsAuthenticated] + + def get(self, request, *args, **kwargs): + """ + Get User Details + """ + employee_profile = get_user_profile(request) + return Response( + data=employee_profile, + status=status.HTTP_200_OK + ) diff --git a/tests/test_users/test_views.py b/tests/test_users/test_views.py index 19f17b0..5e94935 100644 --- a/tests/test_users/test_views.py +++ b/tests/test_users/test_views.py @@ -10,7 +10,9 @@ def test_setup(): def test_get_all_orgs_view(api_client, test_connection, create_temp_workspace, add_fyle_credentials, mocker): - + """ + Test get all orgs view + """ access_token = test_connection.access_token url = reverse('fyle-orgs') @@ -24,3 +26,16 @@ def test_get_all_orgs_view(api_client, test_connection, create_temp_workspace, a response = api_client.get(url) assert response.status_code == 200 assert response.data == fyle_data['get_all_orgs'] + + +def test_get_user_profile(api_client, test_connection, create_temp_workspace, add_fyle_credentials): + """ + Test get user profile + """ + access_token = test_connection.access_token + url = reverse('user-profile') + + api_client.credentials(HTTP_AUTHORIZATION='Bearer {}'.format(access_token)) + + response = api_client.get(url) + assert response.status_code == 200