Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Workspace name when org name is updated #66

Merged
merged 2 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/workspaces/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
18 changes: 17 additions & 1 deletion apps/workspaces/tasks.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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__)
Expand Down Expand Up @@ -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()
8 changes: 8 additions & 0 deletions apps/workspaces/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down
16 changes: 15 additions & 1 deletion tests/test_workspaces/test_tasks.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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'
Loading