Skip to content

Commit

Permalink
Add repetation count (#194)
Browse files Browse the repository at this point in the history
* Add repetation count

* Fix tests
  • Loading branch information
ruuushhh authored and GitHub Actions committed Jul 23, 2024
1 parent 4b61d23 commit 3820f16
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 36 deletions.
18 changes: 18 additions & 0 deletions apps/accounting_exports/migrations/0003_error_repetition_count.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
31 changes: 19 additions & 12 deletions apps/accounting_exports/models.py
Original file line number Diff line number Diff line change
@@ -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 = (
('PURCHASE_INVOICE', 'PURCHASE_INVOICE'),
Expand Down Expand Up @@ -167,10 +166,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'

Expand Down
12 changes: 6 additions & 6 deletions apps/sage300/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
16 changes: 9 additions & 7 deletions apps/sage300/exports/direct_cost/queues.py
Original file line number Diff line number Diff line change
@@ -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]):
Expand Down Expand Up @@ -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={
Expand All @@ -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

Expand Down
8 changes: 5 additions & 3 deletions apps/sage300/exports/helpers.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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={
Expand All @@ -77,6 +77,8 @@ def __validate_category_mapping(accounting_export: AccountingExport):
}
)

error.increase_repetition_count_by_one()

row = row + 1

return bulk_errors
Expand Down
16 changes: 8 additions & 8 deletions apps/sage300/exports/purchase_invoice/queues.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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={
Expand All @@ -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()

Expand Down
3 changes: 3 additions & 0 deletions tests/test_fyle/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@
"is_resolved": "false",
"error_title":"Employee Mapping Error",
"error_detail":"Employee Mapping Error",
"repetition_count":0,
"workspace":1,
"accounting_export":"None",
"expense_attribute":"None"
Expand All @@ -377,6 +378,7 @@
"is_resolved": "false",
"error_title":"Category Mapping Error",
"error_detail":"Category Mapping Error",
"repetition_count":0,
"workspace":1,
"accounting_export":"None",
"expense_attribute":"None"
Expand All @@ -389,6 +391,7 @@
"is_resolved": "false",
"error_title":"Sage Error",
"error_detail":"Sage Error",
"repetition_count":0,
"workspace":1,
"accounting_export":"None",
"expense_attribute":"None"
Expand Down

0 comments on commit 3820f16

Please sign in to comment.