diff --git a/apps/mappings/imports/modules/expense_custom_fields.py b/apps/mappings/imports/modules/expense_custom_fields.py index 73a5f33..97891b3 100644 --- a/apps/mappings/imports/modules/expense_custom_fields.py +++ b/apps/mappings/imports/modules/expense_custom_fields.py @@ -1,9 +1,11 @@ +import logging from datetime import datetime from typing import List, Dict from apps.mappings.imports.modules.base import Base from fyle_accounting_mappings.models import ( DestinationAttribute, - ExpenseAttribute + ExpenseAttribute, + Mapping ) from apps.mappings.exceptions import handle_import_exceptions from apps.mappings.models import ImportLog @@ -11,6 +13,9 @@ from apps.workspaces.models import FyleCredential from apps.mappings.constants import FYLE_EXPENSE_SYSTEM_FIELDS +logger = logging.getLogger(__name__) +logger.level = logging.INFO + class ExpenseCustomField(Base): """ @@ -176,3 +181,28 @@ def import_destination_attribute_to_fyle(self, import_log: ImportLog): self.sync_expense_attributes(platform) self.create_mappings() + + +def disable_custom_attributes(workspace_id: int, custom_fields_to_disable: Dict, *args, **kwargs): + """ + custom_fields_to_disable object format: + { + 'destination_id': { + 'value': 'old_custom_field_name', + 'updated_value': 'new_custom_field_name', + 'code': 'old_code', + 'update_code': 'new_code' ---- if the code is updated else same as code + } + } + + Currently JOB is only field that can be imported as Custom Field, may need to + update this function if more fields are added in future + """ + custom_field_mappings = Mapping.objects.filter( + workspace_id=workspace_id, + destination_type='JOB', + destination_id__destination_id__in=custom_fields_to_disable.keys() + ) + + logger.info(f"Deleting Custom Field Mappings | WORKSPACE_ID: {workspace_id} | COUNT: {custom_field_mappings.count()}") + custom_field_mappings.delete() diff --git a/apps/sage300/utils.py b/apps/sage300/utils.py index 342eecc..873575b 100644 --- a/apps/sage300/utils.py +++ b/apps/sage300/utils.py @@ -16,6 +16,7 @@ 'CATEGORY': 'apps.mappings.imports.modules.categories.disable_categories', 'MERCHANT': 'apps.mappings.imports.modules.merchants.disable_merchants', 'COST_CENTER': 'apps.mappings.imports.modules.cost_centers.disable_cost_centers', + 'CUSTOM': 'apps.mappings.imports.modules.expense_custom_fields.disable_custom_attributes' } diff --git a/tests/test_mappings/test_imports/test_modules/test_expense_fields.py b/tests/test_mappings/test_imports/test_modules/test_expense_fields.py index 6027e01..9d5f0ef 100644 --- a/tests/test_mappings/test_imports/test_modules/test_expense_fields.py +++ b/tests/test_mappings/test_imports/test_modules/test_expense_fields.py @@ -1,5 +1,5 @@ -from apps.mappings.imports.modules.expense_custom_fields import ExpenseCustomField -from fyle_accounting_mappings.models import DestinationAttribute +from apps.mappings.imports.modules.expense_custom_fields import ExpenseCustomField, disable_custom_attributes +from fyle_accounting_mappings.models import DestinationAttribute, ExpenseAttribute, Mapping from apps.mappings.models import ImportLog @@ -202,3 +202,50 @@ def test_post_to_fyle_and_sync( 'is_enabled': True }] ) + + +def test_disable_custom_attributes(db, create_temp_workspace): + destination_attribute = DestinationAttribute.objects.create( + workspace_id=1, + attribute_type='JOB', + value='old_custom_field_name', + code='old_code', + destination_id='123', + active=True + ) + + expense_attribute = ExpenseAttribute.objects.create( + workspace_id=1, + attribute_type='CUSTOM', + value='old_code old_custom_field_name', + source_id='456', + active=True + ) + + mapping = Mapping.objects.create( + workspace_id=1, + source_id=expense_attribute.id, + source_type='CUSTOM', + destination_id=destination_attribute.id, + destination_type='JOB' + ) + + custom_fields_to_disable = { + '123': { + 'value': 'old_custom_field_name', + 'updated_value': 'new_custom_field_name', + 'code': 'old_code', + 'updated_code': 'new_code' + } + } + + disable_custom_attributes(1, custom_fields_to_disable) + + count = 0 + try: + mapping.refresh_from_db() + except Exception as e: + count += 1 + assert str(e) == 'Mapping matching query does not exist.' + + assert count == 1