Skip to content

Commit

Permalink
Add direct export loggers (#363)
Browse files Browse the repository at this point in the history
* Add direct export loggers

* Fix failed

* fix failed
  • Loading branch information
ruuushhh authored May 16, 2024
1 parent 05fa3b8 commit 1455132
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
15 changes: 8 additions & 7 deletions apps/fyle/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 = []
Expand Down
2 changes: 1 addition & 1 deletion apps/xero/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
11 changes: 11 additions & 0 deletions apps/xero/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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)

0 comments on commit 1455132

Please sign in to comment.