Skip to content

Commit

Permalink
add support for code prepending in CUSTOM attribute (#211)
Browse files Browse the repository at this point in the history
* add support for code prepending in CUSTOM attribute

* improve test case
  • Loading branch information
Hrishabh17 authored Jul 23, 2024
1 parent 17a6371 commit 9f4271b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
32 changes: 31 additions & 1 deletion apps/mappings/imports/modules/expense_custom_fields.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
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
from fyle_integrations_platform_connector import PlatformConnector
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):
"""
Expand Down Expand Up @@ -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()
1 change: 1 addition & 0 deletions apps/sage300/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}


Expand Down
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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

0 comments on commit 9f4271b

Please sign in to comment.