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

Business central auth fixed #61

Merged
merged 2 commits into from
Jan 2, 2024
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
26 changes: 1 addition & 25 deletions apps/workspaces/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
from dynamics.exceptions.dynamics_exceptions import InternalServerError, InvalidTokenError
from future.moves.urllib.parse import urlencode

from apps.business_central.utils import BusinessCentralConnector
from apps.workspaces.models import BusinessCentralCredentials, Workspace
from apps.workspaces.models import BusinessCentralCredentials

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -89,8 +88,6 @@ def connect_business_central(authorization_code, redirect_uri, workspace_id):
# Retrieve or create BusinessCentralCredentials based on workspace_id
business_central_credentials = BusinessCentralCredentials.objects.filter(workspace_id=workspace_id).first()

workspace = Workspace.objects.get(pk=workspace_id)

if not business_central_credentials:
# If BusinessCentralCredentials does not exist, create a new one
business_central_credentials = BusinessCentralCredentials.objects.create(
Expand All @@ -102,25 +99,4 @@ def connect_business_central(authorization_code, redirect_uri, workspace_id):
business_central_credentials.is_expired = False
business_central_credentials.save()

if workspace and not workspace.business_central_company_id:
# If workspace has no associated Business Central company ID, fetch and update it
business_central_connector = BusinessCentralConnector(business_central_credentials, workspace_id=workspace_id)
connections = business_central_connector.connection.connections.get_all()
connection = list(
filter(
lambda connection: connection["id"] == workspace.business_central_company_id,
connections,
)
)

if connection:
# If a matching connection is found, update workspace's Business Central company ID
workspace.business_central_company_id = connection[0]["id"]
workspace.save()

if workspace.onboarding_state == "COMPANY_SELECTION":
# If workspace's onboarding state is "COMPANY_SELECTION", update it to "EXPORT_SETTINGS"
workspace.onboarding_state = "EXPORT_SETTINGS"
workspace.save()

return business_central_credentials
20 changes: 0 additions & 20 deletions apps/workspaces/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

from apps.fyle.helpers import get_cluster_domain
from apps.users.models import User
from apps.workspaces.helpers import connect_business_central
from apps.workspaces.models import (
AdvancedSetting,
BusinessCentralCredentials,
Expand Down Expand Up @@ -85,25 +84,6 @@ class Meta:
model = BusinessCentralCredentials
fields = "__all__"

def create(self, validated_data):
"""
Create Business Central Credentials
"""
try:
workspace_id = self.context['request'].parser_context.get('kwargs').get('workspace_id')
authorization_code = self.context['request'].data.get('code')
redirect_uri = self.context['request'].data.get('redirect_uri')

business_central_credentials = connect_business_central(
authorization_code=authorization_code,
redirect_uri=redirect_uri,
workspace_id=workspace_id,
)

return business_central_credentials
except Exception as exception:
raise serializers.ValidationError(exception)


class ExportSettingsSerializer(serializers.ModelSerializer):
"""
Expand Down
20 changes: 19 additions & 1 deletion apps/workspaces/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from rest_framework import generics
from rest_framework.views import Response, status

from apps.workspaces.helpers import connect_business_central
from apps.workspaces.models import AdvancedSetting, BusinessCentralCredentials, ExportSetting, ImportSetting, Workspace
from apps.workspaces.serializers import (
AdvancedSettingSerializer,
Expand Down Expand Up @@ -77,7 +78,24 @@ class ConnectBusinessCentralView(generics.CreateAPIView, generics.RetrieveAPIVie
serializer_class = BusinessCentralCredentialSerializer
lookup_field = 'workspace_id'

queryset = BusinessCentralCredentials.objects.all()
def post(self, request, **kwargs):
"""
Create Business Central Credentials
"""

business_central_credentials = connect_business_central(
authorization_code=request.data.get("code"),
redirect_uri=request.data.get("redirect_uri"),
workspace_id=kwargs["workspace_id"],
)

return Response(
data=BusinessCentralCredentialSerializer(business_central_credentials).data,
status=status.HTTP_200_OK,
)

def get_queryset(self):
return BusinessCentralCredentials.objects.all()


class ExportSettingView(generics.CreateAPIView, generics.RetrieveAPIView):
Expand Down
Loading