Skip to content

Commit

Permalink
Update workspace name async (#435)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shwetabhk authored Oct 31, 2023
1 parent 1b33c4d commit 80b4f6a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 11 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/production_deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ jobs:
environment: Production
steps:
- uses: actions/checkout@v2
- uses: satackey/[email protected]
continue-on-error: true
- name: push to dockerhub
uses: fylein/docker-release-action@master
env:
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/pytest_action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ jobs:
environment: CI Environment
steps:
- uses: actions/checkout@v2
- uses: satackey/[email protected]
continue-on-error: true
- name: Bring up Services and Run Tests
run: |
docker-compose -f docker-compose-pipeline.yml build
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/staging_deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ jobs:
environment: Staging
steps:
- uses: actions/checkout@v2
- uses: satackey/[email protected]
continue-on-error: true
- name: push to dockerhub
uses: fylein/docker-release-action@master
env:
Expand Down
12 changes: 9 additions & 3 deletions apps/workspaces/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
from django.conf import settings
from django.db.models import Q
from django.core.mail import EmailMessage
from django.template.loader import get_template
from django.contrib.auth import get_user_model
from django.template.loader import render_to_string
from django_q.models import Schedule
from fyle_accounting_mappings.models import MappingSetting, ExpenseAttribute
from fyle_rest_auth.helpers import get_fyle_admin

from apps.fyle.models import ExpenseGroup
from apps.mappings.models import SubsidiaryMapping
Expand Down Expand Up @@ -226,4 +225,11 @@ def async_update_fyle_credentials(fyle_org_id: str, refresh_token: str):
if fyle_credentials:
fyle_credentials.refresh_token = refresh_token
fyle_credentials.save()



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()
16 changes: 14 additions & 2 deletions apps/workspaces/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from django.core.cache import cache
from django.db import transaction, connection

from django_q.tasks import async_task

from rest_framework.response import Response
from rest_framework.views import status
from rest_framework import generics
Expand Down Expand Up @@ -32,6 +34,7 @@
ConfigurationSerializer, WorkspaceScheduleSerializer
from .permissions import IsAuthenticatedForTest


logger = logging.getLogger(__name__)
logger.level = logging.INFO

Expand Down Expand Up @@ -82,6 +85,9 @@ def post(self, 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(name=org_name, fyle_org_id=org_id)
Expand Down Expand Up @@ -110,10 +116,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,
data=WorkspaceSerializer(workspaces, many=True).data,
status=status.HTTP_200_OK
)

Expand Down
13 changes: 13 additions & 0 deletions tests/test_workspaces/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,16 @@ def test_run_email_notification(db, mocker, create_task_logs):
workspace_id=49
)
assert ws_schedule.enabled == True


@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 80b4f6a

Please sign in to comment.