diff --git a/apps/accounting_exports/migrations/0003_error_repetition_count.py b/apps/accounting_exports/migrations/0003_error_repetition_count.py new file mode 100644 index 00000000..a8cd0fd5 --- /dev/null +++ b/apps/accounting_exports/migrations/0003_error_repetition_count.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.2 on 2024-06-14 06:26 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('accounting_exports', '0002_initial'), + ] + + operations = [ + migrations.AddField( + model_name='error', + name='repetition_count', + field=models.IntegerField(default=0, help_text='repetition count for the error'), + ), + ] diff --git a/apps/accounting_exports/models.py b/apps/accounting_exports/models.py index e6843db9..cba881da 100644 --- a/apps/accounting_exports/models.py +++ b/apps/accounting_exports/models.py @@ -1,25 +1,24 @@ -from typing import List -from django.db import models from datetime import datetime -from django.contrib.postgres.fields import ArrayField +from typing import List + from django.contrib.postgres.aggregates import ArrayAgg +from django.contrib.postgres.fields import ArrayField +from django.db import models from django.db.models import Count - from fyle_accounting_mappings.models import ExpenseAttribute +from apps.fyle.models import Expense +from apps.workspaces.models import BaseForeignWorkspaceModel, BaseModel, ExportSetting from sage_desktop_api.models.fields import ( + BooleanFalseField, + CustomDateTimeField, + CustomJsonField, + IntegerNullField, StringNotNullField, StringNullField, - CustomJsonField, - CustomDateTimeField, - BooleanFalseField, - TextNotNullField, StringOptionsField, - IntegerNullField + TextNotNullField, ) -from apps.workspaces.models import BaseForeignWorkspaceModel, BaseModel, ExportSetting -from apps.fyle.models import Expense - TYPE_CHOICES = ( ('INVOICES', 'INVOICES'), @@ -166,10 +165,18 @@ class Error(BaseForeignWorkspaceModel): ExpenseAttribute, on_delete=models.PROTECT, null=True, help_text='Reference to Expense Attribute' ) + repetition_count = models.IntegerField(help_text='repetition count for the error', default=0) is_resolved = BooleanFalseField(help_text='Is resolved') error_title = StringNotNullField(help_text='Error title') error_detail = TextNotNullField(help_text='Error detail') + def increase_repetition_count_by_one(self): + """ + Increase the repetition count by 1. + """ + self.repetition_count += 1 + self.save() + class Meta: db_table = 'errors' diff --git a/apps/sage300/exceptions.py b/apps/sage300/exceptions.py index dca84948..b318fd8d 100644 --- a/apps/sage300/exceptions.py +++ b/apps/sage300/exceptions.py @@ -2,13 +2,11 @@ import logging import traceback -from sage_desktop_api.exceptions import BulkError +from apps.accounting_exports.models import AccountingExport, Error +from apps.sage300.actions import update_accounting_export_summary from apps.workspaces.models import FyleCredential, Sage300Credential +from sage_desktop_api.exceptions import BulkError from sage_desktop_sdk.exceptions.hh2_exceptions import WrongParamsError -from apps.accounting_exports.models import AccountingExport -from apps.accounting_exports.models import Error -from apps.sage300.actions import update_accounting_export_summary - logger = logging.getLogger(__name__) logger.level = logging.INFO @@ -20,7 +18,9 @@ def handle_sage300_error(exception, accounting_export: AccountingExport, export_ sage300_error = exception.response error_msg = 'Failed to create {0}'.format(export_type) - Error.objects.update_or_create(workspace_id=accounting_export.workspace_id, accounting_export=accounting_export, defaults={'error_title': error_msg, 'type': 'SAGE300_ERROR', 'error_detail': sage300_error, 'is_resolved': False}) + error, _ = Error.objects.update_or_create(workspace_id=accounting_export.workspace_id, accounting_export=accounting_export, defaults={'error_title': error_msg, 'type': 'SAGE300_ERROR', 'error_detail': sage300_error, 'is_resolved': False}) + + error.increase_repetition_count_by_one() accounting_export.status = 'FAILED' accounting_export.detail = None diff --git a/apps/sage300/exports/direct_cost/queues.py b/apps/sage300/exports/direct_cost/queues.py index 8c2a341c..55e9309e 100644 --- a/apps/sage300/exports/direct_cost/queues.py +++ b/apps/sage300/exports/direct_cost/queues.py @@ -1,15 +1,15 @@ -from typing import List from datetime import datetime +from typing import List + from django.db.models import Q -from django_q.tasks import Chain from django_q.models import Schedule +from django_q.tasks import Chain from apps.accounting_exports.models import AccountingExport, Error -from apps.workspaces.models import FyleCredential -from apps.workspaces.models import Sage300Credential -from apps.sage300.utils import SageDesktopConnector -from apps.sage300.exports.helpers import resolve_errors_for_exported_accounting_export from apps.sage300.actions import update_accounting_export_summary +from apps.sage300.exports.helpers import resolve_errors_for_exported_accounting_export +from apps.sage300.utils import SageDesktopConnector +from apps.workspaces.models import FyleCredential, Sage300Credential def check_accounting_export_and_start_import(workspace_id: int, accounting_export_ids: List[str]): @@ -120,7 +120,7 @@ def poll_operation_status(workspace_id: int, last_export: bool): # Save the updated accounting export accounting_export.save() - Error.objects.update_or_create( + error, _ = Error.objects.update_or_create( workspace_id=accounting_export.workspace_id, accounting_export=accounting_export, defaults={ @@ -131,6 +131,8 @@ def poll_operation_status(workspace_id: int, last_export: bool): } ) + error.increase_repetition_count_by_one() + # Continue to the next iteration continue diff --git a/apps/sage300/exports/helpers.py b/apps/sage300/exports/helpers.py index 34482246..d5860f55 100644 --- a/apps/sage300/exports/helpers.py +++ b/apps/sage300/exports/helpers.py @@ -1,8 +1,8 @@ import itertools -from fyle_accounting_mappings.models import CategoryMapping, ExpenseAttribute, Mapping, EmployeeMapping -from apps.accounting_exports.models import AccountingExport, Error +from fyle_accounting_mappings.models import CategoryMapping, EmployeeMapping, ExpenseAttribute, Mapping +from apps.accounting_exports.models import AccountingExport, Error from sage_desktop_api.exceptions import BulkError @@ -66,7 +66,7 @@ def __validate_category_mapping(accounting_export: AccountingExport): }) if category_attribute: - Error.objects.update_or_create( + error, _ = Error.objects.update_or_create( workspace_id=accounting_export.workspace_id, expense_attribute=category_attribute, defaults={ @@ -77,6 +77,8 @@ def __validate_category_mapping(accounting_export: AccountingExport): } ) + error.increase_repetition_count_by_one() + row = row + 1 return bulk_errors diff --git a/apps/sage300/exports/purchase_invoice/queues.py b/apps/sage300/exports/purchase_invoice/queues.py index b1a01f11..42b30f62 100644 --- a/apps/sage300/exports/purchase_invoice/queues.py +++ b/apps/sage300/exports/purchase_invoice/queues.py @@ -1,20 +1,18 @@ -from typing import List import logging from datetime import datetime +from typing import List + from django.db.models import Q -from django_q.tasks import Chain from django_q.models import Schedule - +from django_q.tasks import Chain from fyle_integrations_platform_connector import PlatformConnector from apps.accounting_exports.models import AccountingExport, Error -from apps.workspaces.models import FyleCredential -from apps.workspaces.models import Sage300Credential -from apps.sage300.utils import SageDesktopConnector from apps.sage300.actions import update_accounting_export_summary from apps.sage300.exports.helpers import resolve_errors_for_exported_accounting_export from apps.sage300.exports.purchase_invoice.models import PurchaseInvoice, PurchaseInvoiceLineitems - +from apps.sage300.utils import SageDesktopConnector +from apps.workspaces.models import FyleCredential, Sage300Credential logger = logging.getLogger(__name__) logger.level = logging.INFO @@ -136,7 +134,7 @@ def poll_operation_status(workspace_id: int, last_export: bool): # Save the updated accounting export accounting_export.save() - Error.objects.update_or_create( + error, _ = Error.objects.update_or_create( workspace_id=accounting_export.workspace_id, accounting_export=accounting_export, defaults={ @@ -147,6 +145,8 @@ def poll_operation_status(workspace_id: int, last_export: bool): } ) + error.increase_repetition_count_by_one() + # Save the updated accounting export accounting_export.save()