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

support for sage300 expense fields api #40

Merged
merged 25 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 22 commits
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ COPY . /fyle-sage-desktop-api/
WORKDIR /fyle-sage-desktop-api

# Do linting checks
# RUN flake8 .
RUN flake8 .

# Expose development port
EXPOSE 8000
Expand Down
28 changes: 24 additions & 4 deletions apps/sage300/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from rest_framework.response import Response
from rest_framework.views import status

from fyle_accounting_mappings.models import DestinationAttribute

from apps.workspaces.models import Workspace, Sage300Credential
from apps.sage300.helpers import sync_dimensions, check_interval_and_sync_dimension

Expand All @@ -27,7 +29,9 @@ def create(self, validated_data):

# Retrieve the workspace and Sage 300 credentials
workspace = Workspace.objects.get(pk=workspace_id)
sage_intacct_credentials = Sage300Credential.objects.get(workspace_id=workspace.id)
sage_intacct_credentials = Sage300Credential.objects.get(
workspace_id=workspace.id
)

if refresh_dimension:
# If 'refresh' is true, perform a full sync of dimensions
Expand All @@ -45,10 +49,26 @@ def create(self, validated_data):

except Sage300Credential.DoesNotExist:
# Handle the case when Sage 300 credentials are not found or invalid
raise serializers.ValidationError({'message': 'Sage300 credentials not found / invalid in workspace'})
raise serializers.ValidationError(
{'message': 'Sage300 credentials not found / invalid in workspace'}
)

except Exception as exception:
# Handle unexpected exceptions and log the error
logger.error('Something unexpected happened workspace_id: %s %s', workspace_id, exception)
logger.error(
'Something unexpected happened workspace_id: %s %s',
workspace_id,
exception,
)
# Raise a custom exception or re-raise the original exception
raise Exception()
raise


class Sage300FieldSerializer(serializers.ModelSerializer):
"""
Expense Fields Serializer
"""

class Meta:
model = DestinationAttribute
fields = ['attribute_type', 'display_name']
10 changes: 7 additions & 3 deletions apps/sage300/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@

from django.urls import path

from apps.sage300.views import ImportSage300AttributesView
from apps.sage300.views import ImportSage300AttributesView, Sage300FieldsView


urlpatterns = [
path('import_attributes/', ImportSage300AttributesView.as_view(), name='import-sage300-attributes'),
path(
"import_attributes/",
ImportSage300AttributesView.as_view(),
name="import-sage300-attributes",
),
path("fields/", Sage300FieldsView.as_view(), name="sage300-fields"),
]
35 changes: 35 additions & 0 deletions apps/sage300/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from django.db.models import Q
import logging

from rest_framework import generics

from fyle_accounting_mappings.models import DestinationAttribute

from apps.sage300.serializers import Sage300FieldSerializer
from apps.sage300.serializers import ImportSage300AttributesSerializer


logger = logging.getLogger(__name__)
logger.level = logging.INFO

Expand All @@ -14,3 +19,33 @@ class ImportSage300AttributesView(generics.CreateAPIView):
"""

serializer_class = ImportSage300AttributesSerializer


class Sage300FieldsView(generics.ListAPIView):
pagination_class = None
serializer_class = Sage300FieldSerializer

def get_queryset(self):
NileshPant1999 marked this conversation as resolved.
Show resolved Hide resolved
attribute_types = [
"VENDOR",
"ACCOUNT",
"JOB",
"CATEGORY",
"COST_CODE",
"PAYMENT",
]
attributes = (
DestinationAttribute.objects.filter(
~Q(attribute_type__in=attribute_types),
workspace_id=self.kwargs["workspace_id"],
)
.values("attribute_type", "display_name")
.distinct()
)

serialized_attributes = Sage300FieldSerializer(attributes, many=True).data

# Adding job by default since we can support importing projects from Sage300 even though they don't exist
serialized_attributes.append({"attribute_type": "JOB", "display_name": "Job"})

return serialized_attributes
2 changes: 0 additions & 2 deletions sage_desktop_api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,6 @@
}
}



CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
Expand Down
2 changes: 0 additions & 2 deletions sage_desktop_api/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,9 @@
FYLE_APP_URL = os.environ.get('APP_URL')
FYLE_EXPENSE_URL = os.environ.get('FYLE_APP_URL')


SD_API_KEY = os.environ.get('SD_API_KEY')
SD_API_SECRET = os.environ.get('SD_API_SECRET')


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

Expand Down
25 changes: 16 additions & 9 deletions tests/test_sage300/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,11 @@ def test_sync_dimensions(api_client, test_connection, mocker, create_temp_worksp
workspace_id = 1

access_token = test_connection.access_token
url = reverse(
'import-sage300-attributes', kwargs={
'workspace_id': workspace_id
}
)
url = reverse('import-sage300-attributes', kwargs={'workspace_id': workspace_id})

api_client.credentials(HTTP_AUTHORIZATION='Bearer {}'.format(access_token))

mocker.patch(
'apps.sage300.helpers.sync_dimensions',
return_value=None
)
mocker.patch('apps.sage300.helpers.sync_dimensions', return_value=None)

response = api_client.post(url)
assert response.status_code == 201
Expand All @@ -32,3 +25,17 @@ def test_sync_dimensions(api_client, test_connection, mocker, create_temp_worksp

response = json.loads(response.content)
assert response['message'] == 'Sage300 credentials not found / invalid in workspace'


def test_sage300_fields(api_client, test_connection):
workspace_id = 1

access_token = test_connection.access_token
url = reverse('sage300-fields', kwargs={'workspace_id': workspace_id})

api_client.credentials(HTTP_AUTHORIZATION='Bearer {}'.format(access_token))

response = api_client.get(url)
assert response.status_code == 200

assert response.data == [{'attribute_type': 'JOB', 'display_name': 'Job'}]
Loading