Skip to content

Commit

Permalink
Update workspace name when org name is update (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shwetabhk authored Oct 31, 2023
1 parent 8f3dad0 commit cbdb3c6
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 @@ -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(
Expand Down
9 changes: 9 additions & 0 deletions apps/workspaces/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
13 changes: 11 additions & 2 deletions apps/workspaces/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)

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

0 comments on commit cbdb3c6

Please sign in to comment.