diff --git a/apps/workspaces/actions.py b/apps/workspaces/actions.py index d42a60af..a3d976dd 100644 --- a/apps/workspaces/actions.py +++ b/apps/workspaces/actions.py @@ -44,6 +44,8 @@ def update_or_create_workspace(user, access_token): if workspace: workspace.user.add(User.objects.get(user_id=user)) + workspace.name = org_name + workspace.save() cache.delete(str(workspace.id)) else: workspace = Workspace.objects.create(name=org_name, fyle_org_id=org_id, fyle_currency=org_currency, app_version='v2') diff --git a/apps/workspaces/tasks.py b/apps/workspaces/tasks.py index 40f33cac..7457581d 100644 --- a/apps/workspaces/tasks.py +++ b/apps/workspaces/tasks.py @@ -11,6 +11,8 @@ from fyle_accounting_mappings.models import ExpenseAttribute from fyle_integrations_platform_connector import PlatformConnector +from fyle_rest_auth.helpers import get_fyle_admin + from apps.fyle.tasks import async_create_expense_groups from apps.tasks.models import Error, TaskLog from apps.workspaces.models import ( @@ -210,3 +212,11 @@ def async_create_admin_subcriptions(workspace_id: int) -> None: 'webhook_url': '{}/workspaces/{}/fyle/exports/'.format(settings.API_URL, workspace_id) } platform.subscriptions.post(payload) + + +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 1ec90f52..a56f9e1b 100644 --- a/apps/workspaces/views.py +++ b/apps/workspaces/views.py @@ -9,6 +9,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_qbo_oauth, @@ -58,9 +60,16 @@ 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, status=status.HTTP_200_OK) + return Response(data=WorkspaceSerializer(workspaces, many=True).data, status=status.HTTP_200_OK) def patch(self, request, **kwargs): """ diff --git a/tests/test_workspaces/test_tasks.py b/tests/test_workspaces/test_tasks.py index 0949d843..7b339122 100644 --- a/tests/test_workspaces/test_tasks.py +++ b/tests/test_workspaces/test_tasks.py @@ -2,8 +2,9 @@ from apps.fyle.models import ExpenseGroupSettings from apps.tasks.models import TaskLog -from apps.workspaces.models import WorkspaceGeneralSettings, WorkspaceSchedule +from apps.workspaces.models import Workspace, WorkspaceGeneralSettings, WorkspaceSchedule from apps.workspaces.tasks import ( + async_update_workspace_name, run_email_notification, run_sync_schedule, schedule_sync, @@ -115,3 +116,15 @@ def test_async_create_admin_subcriptions(db, mocker): return_value={} ) async_create_admin_subcriptions(3) + + +def test_async_update_workspace_name(db, 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'