Skip to content

Commit

Permalink
Handle delete expense case for accounting export summary (#505)
Browse files Browse the repository at this point in the history
* Handle delete expense case for accounting export summary

* fix lint
  • Loading branch information
ashwin1111 authored Oct 20, 2023
1 parent d3999ef commit ffba2a3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
37 changes: 37 additions & 0 deletions apps/fyle/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,41 @@ def bulk_post_accounting_export_summary(platform: PlatformConnector, payload: Li
platform.expenses.post_bulk_accounting_export_summary(payload)


def __handle_post_accounting_export_summary_exception(exception: Exception, workspace_id: int) -> None:
"""
Handle post accounting export summary exception
:param exception: Exception
:param workspace_id: Workspace id
:return: None
"""
error_response = exception.__dict__
expense_to_be_updated = []
if (
'message' in error_response and error_response['message'] == 'Some of the parameters are wrong'
and 'response' in error_response and 'data' in error_response['response'] and error_response['response']['data']
):
logger.info('Error while syncing workspace %s %s',workspace_id, error_response)
for expense in error_response['response']['data']:
if expense['message'] == 'Permission denied to perform this action.':
expense_instance = Expense.objects.get(expense_id=expense['key'], workspace_id=workspace_id)
expense_to_be_updated.append(
Expense(
id=expense_instance.id,
accounting_export_summary=get_updated_accounting_export_summary(
expense_instance.expense_id,
'DELETED',
None,
'{}/workspaces/main/dashboard'.format(settings.QBO_INTEGRATION_APP_URL),
True
)
)
)
if expense_to_be_updated:
Expense.objects.bulk_update(expense_to_be_updated, ['accounting_export_summary'], batch_size=50)
else:
logger.error('Error while syncing accounting export summary, workspace_id: %s %s', workspace_id, str(error_response))


def create_generator_and_post_in_batches(accounting_export_summary_batches: List[dict], platform: PlatformConnector, workspace_id: int) -> None:
"""
Create generator and post in batches
Expand All @@ -264,3 +299,5 @@ def create_generator_and_post_in_batches(accounting_export_summary_batches: List
'Internal server error while posting accounting export summary to Fyle workspace_id: %s',
workspace_id
)
except Exception as exception:
__handle_post_accounting_export_summary_exception(exception, workspace_id)
35 changes: 34 additions & 1 deletion tests/test_fyle/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.db.models import Q

from fyle_integrations_platform_connector import PlatformConnector
from fyle.platform.exceptions import InternalServerError, RetryException
from fyle.platform.exceptions import InternalServerError, RetryException, WrongParamsError

from apps.fyle.models import Expense, ExpenseGroup
from apps.fyle.actions import (
Expand Down Expand Up @@ -131,3 +131,36 @@ def test_create_generator_and_post_in_batches(db):
except RetryException:
# This should not be reached
assert False


def test_handle_post_accounting_export_summary_exception(db):
fyle_credentails = FyleCredential.objects.get(workspace_id=3)
platform = PlatformConnector(fyle_credentails)
expense = Expense.objects.filter(org_id='or79Cob97KSh').first()
expense.workspace_id = 3
expense.save()

expense_id = expense.expense_id

with mock.patch('fyle.platform.apis.v1beta.admin.Expenses.post_bulk_accounting_export_summary') as mock_call:
mock_call.side_effect = WrongParamsError('Some of the parameters are wrong', {
'data': [
{
'message': 'Permission denied to perform this action.',
'key': expense_id
}
]
})
create_generator_and_post_in_batches([{
'id': expense_id
}], platform, 3)

expense = Expense.objects.get(expense_id=expense_id)

assert expense.accounting_export_summary['synced'] == True
assert expense.accounting_export_summary['state'] == 'DELETED'
assert expense.accounting_export_summary['error_type'] == None
assert expense.accounting_export_summary['url'] == '{}/workspaces/main/dashboard'.format(
settings.QBO_INTEGRATION_APP_URL
)
assert expense.accounting_export_summary['id'] == expense_id

0 comments on commit ffba2a3

Please sign in to comment.