diff --git a/apps/workspaces/serializers.py b/apps/workspaces/serializers.py index 8248382..60b0cf6 100644 --- a/apps/workspaces/serializers.py +++ b/apps/workspaces/serializers.py @@ -52,6 +52,8 @@ def create(self, validated_data): if workspace: # Adding user relation to workspace workspace.user.add(User.objects.get(user_id=user)) + workspace.org_id = org_id + 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 581dbc8..0a7e6b5 100644 --- a/apps/workspaces/tasks.py +++ b/apps/workspaces/tasks.py @@ -1,5 +1,7 @@ import logging +from fyle_rest_auth.helpers import get_fyle_admin + from apps.fyle.queue import queue_import_credit_card_expenses, queue_import_reimbursable_expenses from apps.qbd.queue import ( queue_create_bills_iif_file, @@ -9,7 +11,7 @@ from apps.tasks.models import AccountingExport from apps.fyle.models import Expense -from .models import ExportSettings +from .models import ExportSettings, Workspace logger = logging.getLogger(__name__) @@ -68,3 +70,17 @@ def run_import_export(workspace_id: int): elif export_settings.credit_card_expense_export_type == 'JOURNAL_ENTRY': queue_create_journals_iif_file('CCC', workspace_id) + + +def async_update_workspace_name(workspace: Workspace, access_token: str): + """ + Update Workspace Name + + :param workspace: Workspace object + :param access_token: Fyle access token + """ + 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 6429f0f..1f17221 100644 --- a/apps/workspaces/views.py +++ b/apps/workspaces/views.py @@ -2,6 +2,8 @@ from rest_framework.views import Response, status from rest_framework.permissions import IsAuthenticated +from django_q.tasks import async_task + from quickbooks_desktop_api.utils import assert_valid from apps.fyle.models import Expense @@ -44,6 +46,12 @@ def get_object(self): 'Workspace not found or the user does not have access to workspaces' ) + async_task( + 'apps.workspaces.tasks.async_update_workspace_name', + workspace, + self.request.headers.get('Authorization') + ) + return workspace diff --git a/tests/test_workspaces/test_tasks.py b/tests/test_workspaces/test_tasks.py index 1adcb54..c2f76a8 100644 --- a/tests/test_workspaces/test_tasks.py +++ b/tests/test_workspaces/test_tasks.py @@ -1,6 +1,7 @@ import pytest from apps.fyle.models import Expense -from apps.workspaces.tasks import run_import_export +from apps.workspaces.models import Workspace +from apps.workspaces.tasks import async_update_workspace_name, run_import_export from tests.test_fyle.fixtures import fixtures as fyle_fixtures @@ -55,3 +56,16 @@ def test_run_import_export_journal_journal( tasks = OrmQ.objects.all() assert tasks.count() == 2 + + +@pytest.mark.django_db(databases=['default']) +def test_async_update_workspace_name(mocker, create_temp_workspace): + 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'