diff --git a/apps/fyle/serializers.py b/apps/fyle/serializers.py index 90713d0e..26f70f87 100644 --- a/apps/fyle/serializers.py +++ b/apps/fyle/serializers.py @@ -2,11 +2,15 @@ Fyle Serializers """ import logging +from django.db.models import Q from rest_framework import serializers from rest_framework.response import Response from rest_framework.exceptions import APIException from rest_framework.views import status from fyle_integrations_platform_connector import PlatformConnector + +from fyle_accounting_mappings.models import ExpenseAttribute + from datetime import datetime, timezone from apps.workspaces.models import Workspace, FyleCredential from apps.fyle.models import ExpenseFilter @@ -91,3 +95,39 @@ def get_expense_fields(self, validated_data): expense_fields = get_expense_fields(workspace_id=workspace_id) return expense_fields + + +class FyleFieldsSerializer(serializers.Serializer): + """ + Fyle Fields Serializer + """ + + attribute_type = serializers.CharField() + display_name = serializers.CharField() + is_dependant = serializers.BooleanField() + + def format_fyle_fields(self, workspace_id): + """ + Get Fyle Fields + """ + + attribute_types = ['EMPLOYEE', 'CATEGORY', 'PROJECT', 'COST_CENTER', 'TAX_GROUP', 'CORPORATE_CARD', 'MERCHANT'] + + attributes = ExpenseAttribute.objects.filter( + ~Q(attribute_type__in=attribute_types), + workspace_id=workspace_id + ).values('attribute_type', 'display_name', 'detail__is_dependent').distinct() + + attributes_list = [ + {'attribute_type': 'COST_CENTER', 'display_name': 'Cost Center', 'is_dependant': False}, + {'attribute_type': 'PROJECT', 'display_name': 'Project', 'is_dependant': False} + ] + + for attr in attributes: + attributes_list.append({ + 'attribute_type': attr['attribute_type'], + 'display_name': attr['display_name'], + 'is_dependant': attr['detail__is_dependent'] + }) + + return attributes_list diff --git a/apps/fyle/urls.py b/apps/fyle/urls.py index 229e280e..c2b2a9c9 100644 --- a/apps/fyle/urls.py +++ b/apps/fyle/urls.py @@ -15,7 +15,7 @@ """ from django.urls import path -from apps.fyle.views import ImportFyleAttributesView, ExpenseFilterView, ExpenseFilterDeleteView, CustomFieldView +from apps.fyle.views import ImportFyleAttributesView, ExpenseFilterView, ExpenseFilterDeleteView, CustomFieldView, FyleFieldsView urlpatterns = [ @@ -23,4 +23,5 @@ path('expense_filters//', ExpenseFilterDeleteView.as_view(), name='expense-filters'), path('expense_filters/', ExpenseFilterView.as_view(), name='expense-filters'), path('expense_fields/', CustomFieldView.as_view(), name='fyle-expense-fields'), + path('fields/', FyleFieldsView.as_view(), name='fyle-fields'), ] diff --git a/apps/fyle/views.py b/apps/fyle/views.py index 1bde5903..47f87f1c 100644 --- a/apps/fyle/views.py +++ b/apps/fyle/views.py @@ -2,7 +2,7 @@ from rest_framework import generics from sage_desktop_api.utils import LookupFieldMixin from apps.workspaces.models import Workspace -from apps.fyle.serializers import ImportFyleAttributesSerializer, ExpenseFilterSerializer, ExpenseFieldSerializer +from apps.fyle.serializers import ImportFyleAttributesSerializer, ExpenseFilterSerializer, ExpenseFieldSerializer, FyleFieldsSerializer from apps.fyle.models import ExpenseFilter logger = logging.getLogger(__name__) @@ -42,3 +42,14 @@ class CustomFieldView(generics.ListAPIView): serializer_class = ExpenseFieldSerializer queryset = Workspace.objects.all() + + +class FyleFieldsView(generics.ListAPIView): + """ + Fyle Fields view + """ + + serializer_class = FyleFieldsSerializer + + def get_queryset(self): + return FyleFieldsSerializer().format_fyle_fields(self.kwargs["workspace_id"]) diff --git a/tests/test_fyle/fixtures.py b/tests/test_fyle/fixtures.py index 2660a02d..4ae31113 100644 --- a/tests/test_fyle/fixtures.py +++ b/tests/test_fyle/fixtures.py @@ -177,4 +177,16 @@ ] } ], + "fyle_fields_response": [ + { + 'attribute_type': 'COST_CENTER', + 'display_name': 'Cost Center', + 'is_dependant': False + }, + { + 'attribute_type': 'PROJECT', + 'display_name': 'Project', + 'is_dependant': False + } + ] } diff --git a/tests/test_fyle/test_views.py b/tests/test_fyle/test_views.py index 967118f3..6203cae0 100644 --- a/tests/test_fyle/test_views.py +++ b/tests/test_fyle/test_views.py @@ -85,3 +85,18 @@ def test_fyle_expense_fields(api_client, test_connection, create_temp_workspace, assert ( dict_compare_keys(response['results'], data['fyle_expense_custom_fields']) == [] ), 'expense group api return diffs in keys' + + +def test_fyle_fields(api_client, test_connection, create_temp_workspace, add_fyle_credentials): + + access_token = test_connection.access_token + + url = reverse('fyle-fields', kwargs={'workspace_id': 1}) + + api_client.credentials(HTTP_AUTHORIZATION='Bearer {}'.format(access_token)) + + response = api_client.get(url) + assert response.status_code == 200 + + response = json.loads(response.content) + assert response['results'] == data['fyle_fields_response']