Skip to content

Commit

Permalink
category mapping error settings and resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashutosh619-sudo committed Nov 9, 2023
1 parent 469efda commit c9a953c
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
10 changes: 9 additions & 1 deletion apps/mappings/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.dispatch import receiver
from django_q.tasks import async_task

from fyle_accounting_mappings.models import MappingSetting, EmployeeMapping, Mapping
from fyle_accounting_mappings.models import MappingSetting, EmployeeMapping, Mapping, CategoryMapping

from apps.mappings.tasks import upload_attributes_to_fyle, schedule_cost_centers_creation,\
schedule_fyle_attributes_creation
Expand All @@ -18,6 +18,14 @@
from .models import GeneralMapping, SubsidiaryMapping
from .tasks import schedule_auto_map_ccc_employees

@receiver(post_save, sender=CategoryMapping)
def resolve_post_category_mapping_errors(sender, instance: Mapping, **kwargs):
"""
Resolve errors after mapping is created
"""
Error.objects.filter(expense_attribute_id=instance.source_category_id).update(
is_resolved=True
)

@receiver(post_save, sender=EmployeeMapping)
def resolve_post_employees_mapping_errors(sender, instance: Mapping, **kwargs):
Expand Down
6 changes: 6 additions & 0 deletions apps/mappings/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,12 @@ def auto_create_category_mappings(workspace_id):
if reimbursable_expenses_object == 'EXPENSE REPORT' and \
corporate_credit_card_expenses_object in ('BILL', 'JOURNAL ENTRY', 'CREDIT CARD CHARGE'):
bulk_create_ccc_category_mappings(workspace_id)

resolve_expense_attribute_errors(
source_attribute_type="CATEGORY",
destination_attribute_type=reimbursable_destination_type,
workspace_id=workspace_id
)


def auto_import_and_map_fyle_fields(workspace_id):
Expand Down
19 changes: 19 additions & 0 deletions apps/netsuite/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,12 @@ def __validate_category_mapping(expense_group: ExpenseGroup, configuration: Conf
workspace_id=expense_group.workspace_id
).first()

category_attribute = ExpenseAttribute.objects.filter(
value=category,
workspace_id=expense_group.workspace_id,
attribute_type='CATEGORY'
).first()

if category_mapping:
if expense_group.fund_source == 'PERSONAL':
if configuration.reimbursable_expenses_object == 'EXPENSE REPORT':
Expand All @@ -867,6 +873,19 @@ def __validate_category_mapping(expense_group: ExpenseGroup, configuration: Conf
'message': 'Category Mapping Not Found'
})

if category_attribute:
Error.objects.update_or_create(
workspace_id=expense_group.workspace_id,
expense_attribute=category_attribute,
defaults={
'type': 'CATEGORY_MAPPING',
'error_title': category_attribute.value,
'error_detail': 'Category mapping is missing',
'is_resolved': False
}
)


row = row + 1

return bulk_errors
Expand Down
29 changes: 28 additions & 1 deletion tests/test_mappings/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,35 @@
from apps.workspaces.models import Configuration, Workspace
from apps.mappings.models import GeneralMapping
from apps.tasks.models import Error
from fyle_accounting_mappings.models import MappingSetting, ExpenseAttribute, EmployeeMapping
from fyle_accounting_mappings.models import MappingSetting, ExpenseAttribute, EmployeeMapping, CategoryMapping

@pytest.mark.django_db()
def test_resolve_post_category_mapping_errors(access_token):
source_category = ExpenseAttribute.objects.filter(
id=96,
workspace_id=1,
attribute_type='CATEGORY'
).first()

Error.objects.update_or_create(
workspace_id=1,
expense_attribute=source_category,
defaults={
'type': 'CATEGORY_MAPPING',
'error_title': source_category.value,
'error_detail': 'Category mapping is missing',
'is_resolved': False
}
)
category_mapping, _ = CategoryMapping.objects.update_or_create(
source_category_id=96,
destination_account_id=791,
destination_expense_head_id=791,
workspace_id=1
)

error = Error.objects.filter(expense_attribute_id=category_mapping.source_category_id).first()
assert error.is_resolved == True

@pytest.mark.django_db()
def test_resolve_post_employees_mapping_errors(access_token):
Expand Down
20 changes: 20 additions & 0 deletions tests/test_mappings/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ def test_resolve_expense_attribute_errors(db):
resolve_expense_attribute_errors('EMPLOYEE', workspace_id, 'VENDOR')
assert Error.objects.get(id=error.id).is_resolved == True

source_category = ExpenseAttribute.objects.filter(
id=96,
workspace_id=1,
attribute_type='CATEGORY'
).first()

error, _ = Error.objects.update_or_create(
workspace_id=1,
expense_attribute=source_category,
defaults={
'type': 'CATEGORY_MAPPING',
'error_title': source_category.value,
'error_detail': 'Category mapping is missing',
'is_resolved': False
}
)

resolve_expense_attribute_errors('CATEGORY', workspace_id, 'ACCOUNT')
assert Error.objects.get(id=error.id).is_resolved == True


def test_disable_category_for_items_mapping(db ,mocker):
workspace_id = 49
Expand Down

0 comments on commit c9a953c

Please sign in to comment.