Skip to content

Commit

Permalink
Max retry based on repetition count for all exports (#635)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashutosh619-sudo authored Aug 29, 2024
1 parent f919293 commit 2a58395
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 20 deletions.
58 changes: 53 additions & 5 deletions apps/netsuite/queue.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import logging
from typing import List
from datetime import datetime, timedelta, timezone

from django.db.models import Q
from django_q.tasks import Chain

from apps.fyle.models import ExpenseGroup, Expense
from apps.tasks.models import TaskLog
from apps.tasks.models import TaskLog, Error
from apps.workspaces.models import FyleCredential

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -36,7 +37,18 @@ def __create_chain_and_run(fyle_credentials: FyleCredential, in_progress_expense
chain.run()


def schedule_bills_creation(workspace_id: int, expense_group_ids: List[str], is_auto_export: bool, fund_source: str):
def validate_failing_export(is_auto_export: bool, interval_hours: int, error: Error):
"""
Validate failing export
:param is_auto_export: Is auto export
:param interval_hours: Interval hours
:param error: Error
"""
# If auto export is enabled and interval hours is set and error repetition count is greater than 100, export only once a day
return is_auto_export and interval_hours and error and error.repetition_count > 100 and datetime.now().replace(tzinfo=timezone.utc) - error.updated_at <= timedelta(hours=24)


def schedule_bills_creation(workspace_id: int, expense_group_ids: List[str], is_auto_export: bool, fund_source: str, interval_hours: int):
"""
Schedule bills creation
:param expense_group_ids: List of expense group ids
Expand All @@ -50,10 +62,19 @@ def schedule_bills_creation(workspace_id: int, expense_group_ids: List[str], is_
workspace_id=workspace_id, id__in=expense_group_ids, bill__id__isnull=True, exported_at__isnull=True
).all()

errors = Error.objects.filter(workspace_id=workspace_id, is_resolved=False, expense_group_id__in=expense_group_ids).all()

chain_tasks = []
in_progress_expenses = []

for index, expense_group in enumerate(expense_groups):

error = errors.filter(workspace_id=workspace_id, expense_group=expense_group, is_resolved=False).first()
skip_export = validate_failing_export(is_auto_export, interval_hours, error)
if skip_export:
logger.info('Skipping expense group %s as it has %s errors', expense_group.id, error.repetition_count)
continue

task_log, _ = TaskLog.objects.get_or_create(
workspace_id=expense_group.workspace_id,
expense_group=expense_group,
Expand Down Expand Up @@ -86,7 +107,7 @@ def schedule_bills_creation(workspace_id: int, expense_group_ids: List[str], is_
__create_chain_and_run(fyle_credentials, in_progress_expenses, workspace_id, chain_tasks, fund_source)


def schedule_credit_card_charge_creation(workspace_id: int, expense_group_ids: List[str], is_auto_export: bool, fund_source: str):
def schedule_credit_card_charge_creation(workspace_id: int, expense_group_ids: List[str], is_auto_export: bool, fund_source: str, interval_hours: int):
"""
Schedule Credit Card Charge creation
:param expense_group_ids: List of expense group ids
Expand All @@ -101,10 +122,19 @@ def schedule_credit_card_charge_creation(workspace_id: int, expense_group_ids: L
creditcardcharge__id__isnull=True, exported_at__isnull=True
).all()

errors = Error.objects.filter(workspace_id=workspace_id, is_resolved=False, expense_group_id__in=expense_group_ids).all()

chain_tasks = []
in_progress_expenses = []

for index, expense_group in enumerate(expense_groups):

error = errors.filter(workspace_id=workspace_id, expense_group=expense_group, is_resolved=False).first()
skip_export = validate_failing_export(is_auto_export, interval_hours, error)
if skip_export:
logger.info('Skipping expense group %s as it has %s errors', expense_group.id, error.repetition_count)
continue

expense_amount = expense_group.expenses.first().amount
export_type = 'CREATING_CREDIT_CARD_CHARGE'
if expense_amount < 0:
Expand Down Expand Up @@ -143,7 +173,7 @@ def schedule_credit_card_charge_creation(workspace_id: int, expense_group_ids: L
__create_chain_and_run(fyle_credentials, in_progress_expenses, workspace_id, chain_tasks, fund_source)


def schedule_expense_reports_creation(workspace_id: int, expense_group_ids: List[str], is_auto_export: bool, fund_source: str):
def schedule_expense_reports_creation(workspace_id: int, expense_group_ids: List[str], is_auto_export: bool, fund_source: str, interval_hours: int):
"""
Schedule expense reports creation
:param expense_group_ids: List of expense group ids
Expand All @@ -158,10 +188,19 @@ def schedule_expense_reports_creation(workspace_id: int, expense_group_ids: List
expensereport__id__isnull=True, exported_at__isnull=True
).all()

errors = Error.objects.filter(workspace_id=workspace_id, is_resolved=False, expense_group_id__in=expense_group_ids).all()

chain_tasks = []
in_progress_expenses = []

for index, expense_group in enumerate(expense_groups):

error = errors.filter(workspace_id=workspace_id, expense_group=expense_group, is_resolved=False).first()
skip_export = validate_failing_export(is_auto_export, interval_hours, error)
if skip_export:
logger.info('Skipping expense group %s as it has %s errors', expense_group.id, error.repetition_count)
continue

task_log, _ = TaskLog.objects.get_or_create(
workspace_id=expense_group.workspace_id,
expense_group=expense_group,
Expand Down Expand Up @@ -195,7 +234,7 @@ def schedule_expense_reports_creation(workspace_id: int, expense_group_ids: List
__create_chain_and_run(fyle_credentials, in_progress_expenses, workspace_id, chain_tasks, fund_source)


def schedule_journal_entry_creation(workspace_id: int, expense_group_ids: List[str], is_auto_export: bool, fund_source: str):
def schedule_journal_entry_creation(workspace_id: int, expense_group_ids: List[str], is_auto_export: bool, fund_source: str, interval_hours: int):
"""
Schedule journal entries creation
:param expense_group_ids: List of expense group ids
Expand All @@ -209,10 +248,19 @@ def schedule_journal_entry_creation(workspace_id: int, expense_group_ids: List[s
workspace_id=workspace_id, id__in=expense_group_ids, journalentry__id__isnull=True, exported_at__isnull=True
).all()

errors = Error.objects.filter(workspace_id=workspace_id, is_resolved=False, expense_group_id__in=expense_group_ids).all()

chain_tasks = []
in_progress_expenses = []

for index, expense_group in enumerate(expense_groups):

error = errors.filter(workspace_id=workspace_id, expense_group=expense_group, is_resolved=False).first()
skip_export = validate_failing_export(is_auto_export, interval_hours, error)
if skip_export:
logger.info('Skipping expense group %s as it has %s errors', expense_group.id, error.repetition_count)
continue

task_log, _ = TaskLog.objects.get_or_create(
workspace_id=expense_group.workspace_id,
expense_group=expense_group,
Expand Down
21 changes: 14 additions & 7 deletions apps/workspaces/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,26 @@ def export_to_netsuite(workspace_id, export_mode=None, expense_group_ids=[]):
workspace_id=workspace_id,
is_auto_export=export_mode == 'AUTO',
expense_group_ids=expense_group_ids,
fund_source='PERSONAL'
fund_source='PERSONAL',
interval_hours=workspace_schedule.interval_hours if workspace_schedule else 0
)

elif configuration.reimbursable_expenses_object == 'BILL':
schedule_bills_creation(
workspace_id=workspace_id,
is_auto_export=export_mode == 'AUTO',
expense_group_ids=expense_group_ids,
fund_source='PERSONAL'
fund_source='PERSONAL',
interval_hours=workspace_schedule.interval_hours if workspace_schedule else 0
)

elif configuration.reimbursable_expenses_object == 'JOURNAL ENTRY':
schedule_journal_entry_creation(
workspace_id=workspace_id,
is_auto_export=export_mode == 'AUTO',
expense_group_ids=expense_group_ids,
fund_source='PERSONAL'
fund_source='PERSONAL',
interval_hours=workspace_schedule.interval_hours if workspace_schedule else 0
)

if configuration.corporate_credit_card_expenses_object:
Expand All @@ -68,31 +71,35 @@ def export_to_netsuite(workspace_id, export_mode=None, expense_group_ids=[]):
workspace_id=workspace_id,
is_auto_export=export_mode == 'AUTO',
expense_group_ids=expense_group_ids,
fund_source='CCC'
fund_source='CCC',
interval_hours=workspace_schedule.interval_hours if workspace_schedule else 0
)

elif configuration.corporate_credit_card_expenses_object == 'BILL':
schedule_bills_creation(
workspace_id=workspace_id,
is_auto_export=export_mode == 'AUTO',
expense_group_ids=expense_group_ids,
fund_source='CCC'
fund_source='CCC',
interval_hours=workspace_schedule.interval_hours if workspace_schedule else 0
)

elif configuration.corporate_credit_card_expenses_object == 'EXPENSE REPORT':
schedule_expense_reports_creation(
workspace_id=workspace_id,
is_auto_export=export_mode == 'AUTO',
expense_group_ids=expense_group_ids,
fund_source='CCC'
fund_source='CCC',
interval_hours=workspace_schedule.interval_hours if workspace_schedule else 0
)

elif configuration.corporate_credit_card_expenses_object == 'JOURNAL ENTRY':
schedule_journal_entry_creation(
workspace_id=workspace_id,
is_auto_export=export_mode == 'AUTO',
expense_group_ids=expense_group_ids,
fund_source='CCC'
fund_source='CCC',
interval_hours=workspace_schedule.interval_hours if workspace_schedule else 0
)

if is_expenses_exported:
Expand Down
Loading

0 comments on commit 2a58395

Please sign in to comment.