Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fyle Fields API #49

Merged
merged 3 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions apps/fyle/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -91,3 +95,28 @@ 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()

def format_fyle_fields(self, workspace_id):
ruuushhh marked this conversation as resolved.
Show resolved Hide resolved
"""
Get Fyle Fields
"""

default_attributes = ['EMPLOYEE', 'CATEGORY', 'PROJECT', 'COST_CENTER', 'TAX_GROUP', 'CORPORATE_CARD', 'MERCHANT']

attributes = ExpenseAttribute.objects.filter(~Q(attribute_type__in=default_attributes), workspace_id=workspace_id).values('attribute_type', 'display_name').distinct()

fyle_fields = [{'attribute_type': 'COST_CENTER', 'display_name': 'Cost Center'}, {'attribute_type': 'PROJECT', 'display_name': 'Project'}]

for attribute in attributes:
fyle_fields.append(attribute)

return fyle_fields
3 changes: 2 additions & 1 deletion apps/fyle/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
"""

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 = [
path('import_attributes/', ImportFyleAttributesView.as_view(), name='import-fyle-attributes'),
path('expense_filters/<int:pk>/', 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'),
]
13 changes: 12 additions & 1 deletion apps/fyle/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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"])
10 changes: 10 additions & 0 deletions tests/test_fyle/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,14 @@
]
}
],
"fyle_fields_response": [
{
'attribute_type': 'COST_CENTER',
'display_name': 'Cost Center'
},
{
'attribute_type': 'PROJECT',
'display_name': 'Project'
}
]
}
15 changes: 15 additions & 0 deletions tests/test_fyle/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Loading