diff --git a/.github/workflows/production_deployment.yml b/.github/workflows/production_deployment.yml index 31fe6604..10b647f9 100644 --- a/.github/workflows/production_deployment.yml +++ b/.github/workflows/production_deployment.yml @@ -10,8 +10,6 @@ jobs: environment: Production steps: - uses: actions/checkout@v2 - - uses: satackey/action-docker-layer-caching@v0.0.11 - continue-on-error: true - name: push to dockerhub uses: fylein/docker-release-action@master env: diff --git a/.github/workflows/pytest_action.yml b/.github/workflows/pytest_action.yml index f97e3f05..6f4294a0 100644 --- a/.github/workflows/pytest_action.yml +++ b/.github/workflows/pytest_action.yml @@ -10,8 +10,6 @@ jobs: environment: CI Environment steps: - uses: actions/checkout@v2 - - uses: satackey/action-docker-layer-caching@v0.0.11 - continue-on-error: true - name: Bring up Services and Run Tests run: | docker-compose -f docker-compose-pipeline.yml build diff --git a/.github/workflows/staging_deployment.yml b/.github/workflows/staging_deployment.yml index 83bb2fdc..68f3224b 100644 --- a/.github/workflows/staging_deployment.yml +++ b/.github/workflows/staging_deployment.yml @@ -14,8 +14,6 @@ jobs: environment: Staging steps: - uses: actions/checkout@v2 - - uses: satackey/action-docker-layer-caching@v0.0.11 - continue-on-error: true - name: push to dockerhub uses: fylein/docker-release-action@master env: diff --git a/apps/workspaces/tasks.py b/apps/workspaces/tasks.py index 978e244a..a573e7e9 100644 --- a/apps/workspaces/tasks.py +++ b/apps/workspaces/tasks.py @@ -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 @@ -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() - \ No newline at end of file + + +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() diff --git a/apps/workspaces/views.py b/apps/workspaces/views.py index dc67116b..39026d9b 100644 --- a/apps/workspaces/views.py +++ b/apps/workspaces/views.py @@ -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 @@ -32,6 +34,7 @@ ConfigurationSerializer, WorkspaceScheduleSerializer from .permissions import IsAuthenticatedForTest + logger = logging.getLogger(__name__) logger.level = logging.INFO @@ -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) @@ -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 ) diff --git a/tests/test_workspaces/test_tasks.py b/tests/test_workspaces/test_tasks.py index fb82cfb5..52668f78 100644 --- a/tests/test_workspaces/test_tasks.py +++ b/tests/test_workspaces/test_tasks.py @@ -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'