From 6bd6eda13302d3a3cf1dce0dd29ba06cb0fe7c7e Mon Sep 17 00:00:00 2001 From: ashwin1111 Date: Mon, 16 Oct 2023 21:49:01 +0530 Subject: [PATCH 1/2] Handle delete expense case for accounting export summary --- apps/fyle/actions.py | 37 +++++++++++++++++++++++++++++++++ tests/test_fyle/test_actions.py | 36 +++++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/apps/fyle/actions.py b/apps/fyle/actions.py index 763d5576..69c1d163 100644 --- a/apps/fyle/actions.py +++ b/apps/fyle/actions.py @@ -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 @@ -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) diff --git a/tests/test_fyle/test_actions.py b/tests/test_fyle/test_actions.py index d847b83d..1ce86ecf 100644 --- a/tests/test_fyle/test_actions.py +++ b/tests/test_fyle/test_actions.py @@ -1,10 +1,11 @@ +import json from unittest import mock from django.conf import settings 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 ( @@ -131,3 +132,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 From 8c083b14c0a3e98e8edbf7ae34b14c841bfb4516 Mon Sep 17 00:00:00 2001 From: ashwin1111 Date: Mon, 16 Oct 2023 21:52:00 +0530 Subject: [PATCH 2/2] fix lint --- tests/test_fyle/test_actions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_fyle/test_actions.py b/tests/test_fyle/test_actions.py index 1ce86ecf..87050a40 100644 --- a/tests/test_fyle/test_actions.py +++ b/tests/test_fyle/test_actions.py @@ -1,4 +1,3 @@ -import json from unittest import mock from django.conf import settings