From cbdb3c61e613199ed6467eea63c0b70b4e079df6 Mon Sep 17 00:00:00 2001 From: Shwetabh Kumar Date: Tue, 31 Oct 2023 16:57:29 +0530 Subject: [PATCH] Update workspace name when org name is update (#269) --- apps/workspaces/actions.py | 2 ++ apps/workspaces/tasks.py | 9 +++++++++ apps/workspaces/views.py | 13 +++++++++++-- tests/test_workspaces/test_tasks.py | 16 +++++++++++++++- 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/apps/workspaces/actions.py b/apps/workspaces/actions.py index f142804b..0b078152 100644 --- a/apps/workspaces/actions.py +++ b/apps/workspaces/actions.py @@ -30,6 +30,8 @@ def post_workspace(access_token, request): if workspace: workspace.user.add(User.objects.get(user_id=request.user)) + workspace.name = org_name + workspace.save() cache.delete(str(workspace.id)) else: workspace = Workspace.objects.create( diff --git a/apps/workspaces/tasks.py b/apps/workspaces/tasks.py index 00a254b8..1933f391 100644 --- a/apps/workspaces/tasks.py +++ b/apps/workspaces/tasks.py @@ -2,6 +2,7 @@ from datetime import datetime from fyle_integrations_platform_connector import PlatformConnector +from fyle_rest_auth.helpers import get_fyle_admin from apps.fyle.models import ExpenseGroup from apps.fyle.tasks import async_create_expense_groups @@ -131,3 +132,11 @@ def async_add_admins_to_workspace(workspace_id: int, current_user_id: str): for user in created_users: workspace.user.add(user) + + +def async_update_workspace_name(workspace: Workspace, access_token: str): + fyle_user = get_fyle_admin(access_token.split(' ')[1], None) + org_name = fyle_user['data']['org']['name'] + + workspace.name = org_name + workspace.save() diff --git a/apps/workspaces/views.py b/apps/workspaces/views.py index c4b7c3e5..1053eb02 100644 --- a/apps/workspaces/views.py +++ b/apps/workspaces/views.py @@ -7,6 +7,8 @@ from rest_framework.response import Response from rest_framework.views import status +from django_q.tasks import async_task + from apps.exceptions import handle_view_exceptions from apps.workspaces.actions import connect_xero, get_workspace_admin, post_workspace, revoke_connections from apps.workspaces.models import LastExportDetail, Workspace, WorkspaceGeneralSettings, XeroCredentials @@ -68,10 +70,17 @@ def get(self, request): """ user = User.objects.get(user_id=request.user) org_id = request.query_params.get("org_id") - workspace = Workspace.objects.filter(user__in=[user], fyle_org_id=org_id).all() + workspaces = Workspace.objects.filter(user__in=[user], fyle_org_id=org_id).all() + + if workspaces: + async_task( + "apps.workspaces.tasks.async_update_workspace_name", + workspaces[0], + request.META.get("HTTP_AUTHORIZATION"), + ) return Response( - data=WorkspaceSerializer(workspace, many=True).data, + data=WorkspaceSerializer(workspaces, many=True).data, status=status.HTTP_200_OK, ) diff --git a/tests/test_workspaces/test_tasks.py b/tests/test_workspaces/test_tasks.py index 413db231..6f5ed2ca 100644 --- a/tests/test_workspaces/test_tasks.py +++ b/tests/test_workspaces/test_tasks.py @@ -5,11 +5,12 @@ from apps.tasks.models import TaskLog from apps.users.models import User -from apps.workspaces.models import FyleCredential, LastExportDetail, WorkspaceGeneralSettings, WorkspaceSchedule +from apps.workspaces.models import FyleCredential, LastExportDetail, Workspace, WorkspaceGeneralSettings, WorkspaceSchedule from apps.workspaces.queue import schedule_sync from apps.workspaces.tasks import ( async_add_admins_to_workspace, async_update_fyle_credentials, + async_update_workspace_name, run_email_notification, run_sync_schedule, ) @@ -155,3 +156,16 @@ def test_async_add_admins_to_workspace(db, mocker): new_users_count = User.objects.count() assert new_users_count > old_users_count + + +@pytest.mark.django_db() +def test_async_update_workspace_name(mocker): + mocker.patch( + 'apps.workspaces.tasks.get_fyle_admin', + return_value={'data': {'org': {'name': 'Test Org'}}} + ) + workspace = Workspace.objects.get(id=1) + async_update_workspace_name(workspace, 'Bearer access_token') + + workspace = Workspace.objects.get(id=1) + assert workspace.name == 'Test Org'