Skip to content

Commit

Permalink
Fixes related to common issues (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hrishabh17 authored Apr 29, 2024
1 parent 3cefa9a commit 6fcacf7
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
18 changes: 16 additions & 2 deletions apps/fyle/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
from typing import Dict, List

from django.db import transaction
from fyle.platform.exceptions import InvalidTokenError as FyleInvalidTokenError
from fyle.platform.exceptions import RetryException
from fyle.platform.exceptions import (
RetryException,
InternalServerError,
InvalidTokenError as FyleInvalidTokenError
)
from fyle_integrations_platform_connector import PlatformConnector
from fyle_accounting_mappings.models import ExpenseAttribute

Expand Down Expand Up @@ -155,6 +158,14 @@ def async_create_expense_groups(
task_log.status = TaskLogStatusEnum.FATAL
task_log.save()

except InternalServerError:
logger.info('Fyle Internal Server Error occured in workspace_id: %s', workspace_id)
task_log.detail = {
'message': 'Fyle Internal Server Error occured'
}
task_log.status = 'FAILED'
task_log.save()

except Exception:
error = traceback.format_exc()
task_log.detail = {"error": error}
Expand Down Expand Up @@ -247,6 +258,9 @@ def import_and_export_expenses(report_id: str, org_id: str) -> None:
if len(expense_group_ids):
export_to_xero(workspace.id, None, expense_group_ids)

except WorkspaceGeneralSettings.DoesNotExist:
logger.info('Configuration does not exist for workspace_id: %s', workspace.id)

except Exception:
handle_import_exception(task_log)

Expand Down
2 changes: 1 addition & 1 deletion apps/workspaces/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def async_update_fyle_credentials(fyle_org_id: str, refresh_token: str):
fyle_credentials = FyleCredential.objects.filter(
workspace__fyle_org_id=fyle_org_id
).first()
if fyle_credentials:
if fyle_credentials and refresh_token:
fyle_credentials.refresh_token = refresh_token
fyle_credentials.save()

Expand Down
23 changes: 23 additions & 0 deletions sql/scripts/025-fix-last-sync-and-ccc-last-sync-null.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
rollback;
begin;

update workspaces
set last_synced_at = (
select max(case when e.fund_source = 'PERSONAL' then e.created_at end)
from expenses e
where
e.fund_source = 'PERSONAL'
and
e.workspace_id = workspaces.id
group by e.workspace_id
),
ccc_last_synced_at = (
select max(case when e.fund_source = 'CCC' then e.created_at end)
from expenses e
where
e.fund_source = 'CCC'
and
e.workspace_id = workspaces.id
group by e.workspace_id
)
where workspaces.last_synced_at is null and workspaces.ccc_last_synced_at is null;
26 changes: 21 additions & 5 deletions tests/test_fyle/test_task.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
from unittest import mock

from fyle.platform.exceptions import InvalidTokenError as FyleInvalidTokenError

from fyle.platform.exceptions import (
InvalidTokenError as FyleInvalidTokenError,
InternalServerError
)
from apps.fyle.actions import update_expenses_in_progress
from apps.fyle.models import Expense, ExpenseGroup, ExpenseGroupSettings
from apps.fyle.tasks import create_expense_groups, import_and_export_expenses, post_accounting_export_summary
from apps.tasks.models import TaskLog
from apps.workspaces.models import FyleCredential
from apps.workspaces.models import FyleCredential, WorkspaceGeneralSettings
from tests.test_fyle.fixtures import data


def test_create_expense_groups(mocker, db):
workspace_id = 1

mocker.patch(
mock_call = mocker.patch(
"fyle_integrations_platform_connector.apis.Expenses.get",
return_value=data["expenses"],
)
Expand Down Expand Up @@ -62,6 +64,14 @@ def test_create_expense_groups(mocker, db):
task_log = TaskLog.objects.get(id=task_log.id)
assert task_log.status == "FATAL"

mock_call.side_effect = InternalServerError('Error')
create_expense_groups(1, ['PERSONAL', 'CCC'], task_log)

mock_call.side_effect = FyleInvalidTokenError('Invalid Token')
create_expense_groups(1, ['PERSONAL', 'CCC'], task_log)

mock_call.call_count = 2


def test_post_accounting_export_summary(db, mocker):
expense_group = ExpenseGroup.objects.filter(workspace_id=1).first()
Expand All @@ -85,5 +95,11 @@ def test_post_accounting_export_summary(db, mocker):
assert Expense.objects.filter(id=expense.id).first().accounting_export_summary['synced'] == True


def test_import_and_export_expenses(db):
def test_import_and_export_expenses(db, mocker):
import_and_export_expenses('rp1s1L3QtMpF', 'orPJvXuoLqvJ')

mock_call = mocker.patch('apps.fyle.helpers.get_fund_source')
mock_call.side_effect = WorkspaceGeneralSettings.DoesNotExist('Error')
import_and_export_expenses('rp1s1L3QtMpF', 'orPJvXuoLqvJ')

assert mock_call.call_count == 0

0 comments on commit 6fcacf7

Please sign in to comment.