Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle delete expense case for accounting export summary #505

Merged
merged 3 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading