From c9a953cc45d8db947a3e15221bb9f56aa4d51f45 Mon Sep 17 00:00:00 2001 From: Ashutosh619-sudo Date: Thu, 9 Nov 2023 15:12:25 +0530 Subject: [PATCH] category mapping error settings and resolution --- apps/mappings/signals.py | 10 +++++++++- apps/mappings/tasks.py | 6 ++++++ apps/netsuite/tasks.py | 19 +++++++++++++++++++ tests/test_mappings/test_signals.py | 29 ++++++++++++++++++++++++++++- tests/test_mappings/test_tasks.py | 20 ++++++++++++++++++++ 5 files changed, 82 insertions(+), 2 deletions(-) diff --git a/apps/mappings/signals.py b/apps/mappings/signals.py index 9a222587..ce85df17 100644 --- a/apps/mappings/signals.py +++ b/apps/mappings/signals.py @@ -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 @@ -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): diff --git a/apps/mappings/tasks.py b/apps/mappings/tasks.py index 9f19486e..c6c0bff5 100644 --- a/apps/mappings/tasks.py +++ b/apps/mappings/tasks.py @@ -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): diff --git a/apps/netsuite/tasks.py b/apps/netsuite/tasks.py index ce53a7b8..31ba2197 100644 --- a/apps/netsuite/tasks.py +++ b/apps/netsuite/tasks.py @@ -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': @@ -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 diff --git a/tests/test_mappings/test_signals.py b/tests/test_mappings/test_signals.py index a54a5e38..a71c8292 100644 --- a/tests/test_mappings/test_signals.py +++ b/tests/test_mappings/test_signals.py @@ -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): diff --git a/tests/test_mappings/test_tasks.py b/tests/test_mappings/test_tasks.py index 77a743b2..cbe0757a 100644 --- a/tests/test_mappings/test_tasks.py +++ b/tests/test_mappings/test_tasks.py @@ -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