diff --git a/backoffice/backoffice/workflows/api/serializers.py b/backoffice/backoffice/workflows/api/serializers.py index f4459d4a..4883d0b2 100644 --- a/backoffice/backoffice/workflows/api/serializers.py +++ b/backoffice/backoffice/workflows/api/serializers.py @@ -11,6 +11,7 @@ class WorkflowTicketSerializer(serializers.ModelSerializer): ticket_url = serializers.SerializerMethodField() + workflow_id = serializers.PrimaryKeyRelatedField(queryset=Workflow.objects.all()) class Meta: model = WorkflowTicket diff --git a/backoffice/backoffice/workflows/api/views.py b/backoffice/backoffice/workflows/api/views.py index 6c3fb6f0..2a67bfdd 100644 --- a/backoffice/backoffice/workflows/api/views.py +++ b/backoffice/backoffice/workflows/api/views.py @@ -59,6 +59,8 @@ def perform_destroy(self, instance): class WorkflowTicketViewSet(viewsets.ViewSet): + serializer_class = WorkflowTicketSerializer + def retrieve(self, request, *args, **kwargs): workflow_id = kwargs.get("pk") ticket_type = request.query_params.get("ticket_type") @@ -73,7 +75,7 @@ def retrieve(self, request, *args, **kwargs): workflow_ticket = WorkflowTicket.objects.get( workflow_id=workflow_id, ticket_type=ticket_type ) - serializer = WorkflowTicketSerializer(workflow_ticket) + serializer = self.serializer_class(workflow_ticket) return Response(serializer.data) except WorkflowTicket.DoesNotExist: return Response( @@ -82,27 +84,11 @@ def retrieve(self, request, *args, **kwargs): ) def create(self, request, *args, **kwargs): - workflow_id = request.data.get("workflow_id") - ticket_type = request.data.get("ticket_type") - ticket_id = request.data.get("ticket_id") - - if not all([workflow_id, ticket_type, ticket_id]): - return Response( - {"error": "Workflow_id, ticket_id and ticket_type are required."}, - status=status.HTTP_400_BAD_REQUEST, - ) + serializer = self.serializer_class(data=request.data) - try: - workflow = Workflow.objects.get(id=workflow_id) - workflow_ticket = WorkflowTicket.objects.create( - workflow_id=workflow, ticket_id=ticket_id, ticket_type=ticket_type - ) - serializer = WorkflowTicketSerializer(workflow_ticket) + if serializer.is_valid(raise_exception=True): + serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) - except Exception as e: - return Response( - {"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR - ) class DecisionViewSet(viewsets.ModelViewSet): diff --git a/backoffice/backoffice/workflows/tests/test_views.py b/backoffice/backoffice/workflows/tests/test_views.py index 2cb09e1b..a0ac4aab 100644 --- a/backoffice/backoffice/workflows/tests/test_views.py +++ b/backoffice/backoffice/workflows/tests/test_views.py @@ -275,8 +275,9 @@ def test_create_missing_params(self): ) assert response.status_code == 400 - assert response.data == { - "error": "Workflow_id, ticket_id and ticket_type are required." + assert response.json() == { + "workflow_id": ["This field is required."], + "ticket_id": ["This field is required."], } def test_create_happy_flow(self):