Skip to content

Commit

Permalink
Update workspace name when org name is updated (#513)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shwetabhk authored Oct 31, 2023
1 parent 6c09e76 commit d3d06e3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
2 changes: 2 additions & 0 deletions apps/workspaces/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
10 changes: 10 additions & 0 deletions apps/workspaces/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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()
13 changes: 11 additions & 2 deletions apps/workspaces/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
"""
Expand Down
15 changes: 14 additions & 1 deletion tests/test_workspaces/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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'

0 comments on commit d3d06e3

Please sign in to comment.