From 14551327667eb78b8db6f5857917db7bbf43a994 Mon Sep 17 00:00:00 2001 From: ruuushhh <66899387+ruuushhh@users.noreply.github.com> Date: Thu, 16 May 2024 15:10:50 +0530 Subject: [PATCH] Add direct export loggers (#363) * Add direct export loggers * Fix failed * fix failed --- apps/fyle/tasks.py | 15 ++++++++------- apps/xero/queue.py | 2 +- apps/xero/tasks.py | 11 +++++++++++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/apps/fyle/tasks.py b/apps/fyle/tasks.py index 3dfee331..fa5c6a87 100644 --- a/apps/fyle/tasks.py +++ b/apps/fyle/tasks.py @@ -4,13 +4,11 @@ from typing import Dict, List from django.db import transaction -from fyle.platform.exceptions import ( - RetryException, - InternalServerError, - InvalidTokenError as FyleInvalidTokenError -) -from fyle_integrations_platform_connector import PlatformConnector +from fyle.platform.exceptions import InternalServerError +from fyle.platform.exceptions import InvalidTokenError as FyleInvalidTokenError +from fyle.platform.exceptions import RetryException from fyle_accounting_mappings.models import ExpenseAttribute +from fyle_integrations_platform_connector import PlatformConnector from apps.fyle.actions import create_generator_and_post_in_batches from apps.fyle.enums import ExpenseStateEnum, FundSourceEnum, PlatformExpensesEnum @@ -265,7 +263,7 @@ def import_and_export_expenses(report_id: str, org_id: str) -> None: handle_import_exception(task_log) -def post_accounting_export_summary(org_id: str, workspace_id: int, fund_source: str = None) -> None: +def post_accounting_export_summary(org_id: str, workspace_id: int, fund_source: str = None, is_failed: bool = False) -> None: """ Post accounting export summary to Fyle :param org_id: org id @@ -284,6 +282,9 @@ def post_accounting_export_summary(org_id: str, workspace_id: int, fund_source: if fund_source: filters['fund_source'] = fund_source + if is_failed: + filters['accounting_export_summary__state'] = 'ERROR' + expenses_count = Expense.objects.filter(**filters).count() accounting_export_summary_batches = [] diff --git a/apps/xero/queue.py b/apps/xero/queue.py index 64e51182..b5493e4d 100644 --- a/apps/xero/queue.py +++ b/apps/xero/queue.py @@ -102,7 +102,7 @@ def __create_chain_and_run(fyle_credentials: FyleCredential, xero_connection, in for task in chain_tasks: chain.append(task['target'], task['expense_group_id'], task['task_log_id'], xero_connection, task['last_export']) - chain.append('apps.fyle.tasks.post_accounting_export_summary', fyle_credentials.workspace.fyle_org_id, workspace_id, fund_source) + chain.append('apps.fyle.tasks.post_accounting_export_summary', fyle_credentials.workspace.fyle_org_id, workspace_id, fund_source, True) chain.run() diff --git a/apps/xero/tasks.py b/apps/xero/tasks.py index d4258a15..99990cff 100644 --- a/apps/xero/tasks.py +++ b/apps/xero/tasks.py @@ -204,6 +204,8 @@ def create_bill( sleep(2) expense_group = ExpenseGroup.objects.get(id=expense_group_id) task_log = TaskLog.objects.get(id=task_log_id) + logger.info('Creating Bill for Expense Group %s, current state is %s', expense_group.id, task_log.status) + if task_log.status not in [TaskLogStatusEnum.IN_PROGRESS, TaskLogStatusEnum.COMPLETE]: task_log.status = TaskLogStatusEnum.IN_PROGRESS task_log.save() @@ -224,6 +226,7 @@ def create_bill( ) __validate_expense_group(expense_group) + logger.info('Validated Expense Group %s successfully', expense_group.id) with transaction.atomic(): bill_object = Bill.create_bill(expense_group) @@ -233,6 +236,7 @@ def create_bill( created_bill = xero_connection.post_bill( bill_object, bill_lineitems_objects, general_settings ) + logger.info('Created Bill with Expense Group %s successfully', expense_group.id) task_log.detail = created_bill task_log.bill = bill_object @@ -246,6 +250,7 @@ def create_bill( expense_group.save() resolve_errors_for_exported_expense_group(expense_group) generate_export_url_and_update_expense(expense_group, 'BILL') + logger.info('Updated Expense Group %s successfully', expense_group.id) # Assign billable expenses to customers if general_settings.import_customers: @@ -358,6 +363,8 @@ def create_bank_transaction( sleep(2) expense_group = ExpenseGroup.objects.get(id=expense_group_id) task_log = TaskLog.objects.get(id=task_log_id) + logger.info('Creating Bank Transaction for Expense Group %s, current state is %s', expense_group.id, task_log.status) + if task_log.status not in [TaskLogStatusEnum.IN_PROGRESS, TaskLogStatusEnum.COMPLETE]: task_log.status = TaskLogStatusEnum.IN_PROGRESS task_log.save() @@ -386,6 +393,7 @@ def create_bank_transaction( ) __validate_expense_group(expense_group) + logger.info('Validated Expense Group %s successfully', expense_group.id) with transaction.atomic(): bank_transaction_object = BankTransaction.create_bank_transaction( @@ -401,6 +409,7 @@ def create_bank_transaction( bank_transaction_lineitems_objects, general_settings, ) + logger.info('Created Bank Transaction with Expense Group %s successfully', expense_group.id) task_log.detail = created_bank_transaction task_log.bank_transaction = bank_transaction_object @@ -414,6 +423,7 @@ def create_bank_transaction( expense_group.save() resolve_errors_for_exported_expense_group(expense_group) generate_export_url_and_update_expense(expense_group, 'BANK TRANSACTION') + logger.info('Updated Expense Group %s successfully', expense_group.id) # Assign billable expenses to customers if general_settings.import_customers: @@ -855,3 +865,4 @@ def generate_export_url_and_update_expense(expense_group: ExpenseGroup, export_t logger.error('Error while generating export url %s', error) update_complete_expenses(expense_group.expenses.all(), url) + post_accounting_export_summary(workspace.fyle_org_id, workspace.id, expense_group.fund_source)