From 3a0c7416d2d62df15a3812fc1ef06d0116b50cc9 Mon Sep 17 00:00:00 2001 From: Anish Kr Singh <116036738+anishfyle@users.noreply.github.com> Date: Fri, 2 Aug 2024 16:30:06 +0530 Subject: [PATCH] fix: added check for empty item_type get_item_and_account_name (#99) * fix: added check for empty item_type get_item_and_account_name * fix for async_task('apps.fyle.actions.sync_fyle_dimensions', workspace.id) * custom field query * added mock * pr comment * Update serializers.py * fix sync bug --------- Co-authored-by: Hrishabh Tiwari <74908943+Hrishabh17@users.noreply.github.com> Co-authored-by: Hrishabh Tiwari --- apps/mappings/connector.py | 12 ++++++------ apps/qbd/models.py | 6 ++++++ apps/workspaces/serializers.py | 4 +++- tests/test_fyle/test_view.py | 3 +++ tests/test_mapping/test_connector.py | 4 ++-- 5 files changed, 20 insertions(+), 9 deletions(-) diff --git a/apps/mappings/connector.py b/apps/mappings/connector.py index ffe0645..b9c9477 100644 --- a/apps/mappings/connector.py +++ b/apps/mappings/connector.py @@ -68,12 +68,12 @@ def sync_custom_field( """ query = { - 'order': 'updated_at.desc', - 'is_custom': 'eq.true', - 'type': 'eq.SELECT', - 'is_enabled': 'eq.true' + 'order': 'updated_at.desc', + 'is_custom': 'eq.true', + 'type': 'eq.SELECT', + 'is_enabled': 'eq.true' } - custom_field_gen = field_mapping.custom_fields + custom_field_gen = self.platform.v1beta.admin.expense_fields.list_all(query) if source_type: query = QBDMapping.objects.filter(workspace_id=self.workspace_id, attribute_type=source_type) existing_source_attributes = query.values_list('source_value', flat=True) @@ -87,7 +87,7 @@ def sync_custom_field( if source_type and source_type == custom_field['field_name']: source_values.extend(custom_field['options']) - if distinct_custom_fields and sync_expense_custom_field_names: + if distinct_custom_fields: field_mapping.custom_fields = distinct_custom_fields field_mapping.save() diff --git a/apps/qbd/models.py b/apps/qbd/models.py index dd6c4a2..fb2083f 100644 --- a/apps/qbd/models.py +++ b/apps/qbd/models.py @@ -15,6 +15,12 @@ def get_item_and_account_name(field_mapping: FieldMapping, expense: Expense, workspace_id: int): item_type = field_mapping.item_type + + # Check if item_type is not None before proceeding + if item_type is None: + # Handle the case where item_type is None, e.g., return a default value or raise a custom error + return '', expense.category + expense_item = None expense_category = expense.category diff --git a/apps/workspaces/serializers.py b/apps/workspaces/serializers.py index 98ee76c..1f937fa 100644 --- a/apps/workspaces/serializers.py +++ b/apps/workspaces/serializers.py @@ -83,7 +83,7 @@ def create(self, validated_data): FieldMapping.objects.update_or_create( workspace=workspace ) - + return workspace @@ -117,6 +117,8 @@ def create(self, validated_data): if workspace.onboarding_state == 'EXPORT_SETTINGS': workspace.onboarding_state = 'FIELD_MAPPINGS' workspace.save() + + async_task('apps.fyle.actions.sync_fyle_dimensions', workspace.id) return export_settings diff --git a/tests/test_fyle/test_view.py b/tests/test_fyle/test_view.py index f10e62d..452b0b5 100644 --- a/tests/test_fyle/test_view.py +++ b/tests/test_fyle/test_view.py @@ -14,6 +14,9 @@ def test_sync_fyle_dimension_view(api_client, test_connection, mocker): 'fyle.platform.apis.v1beta.admin.corporate_cards.list_all', return_value=fixture['credit_card_sdk'] ) + mocker.patch( + 'fyle.platform.apis.v1beta.admin.expense_fields.list_all' + ) url = reverse( 'workspaces' ) diff --git a/tests/test_mapping/test_connector.py b/tests/test_mapping/test_connector.py index 8eebbf0..aaf4441 100644 --- a/tests/test_mapping/test_connector.py +++ b/tests/test_mapping/test_connector.py @@ -65,7 +65,7 @@ def test_sync_cost_center(create_temp_workspace, add_fyle_credentials, mocker): assert mapping.source_value == cost_center -@pytest.mark.django_db(databases=['default']) +@pytest.mark.django_db(databases=['default'], transaction=True) def test_sync_custom_field(mocker, api_client, test_connection): access_token = test_connection.access_token @@ -97,7 +97,7 @@ def test_sync_custom_field(mocker, api_client, test_connection): # Check if custom fields are updated in FieldMapping field_mapping.refresh_from_db() - assert field_mapping.custom_fields == ['field1'] + assert field_mapping.custom_fields == ['field1', 'field2'] # Check if QBDMapping objects are created qbd_mappings = QBDMapping.objects.filter(workspace_id=1, attribute_type='field1')