From 52c8195181dbbfa5665029f53f7841f3d60e63cd Mon Sep 17 00:00:00 2001 From: pablohashescobar Date: Thu, 1 Feb 2024 21:04:34 +0530 Subject: [PATCH] dev: return identifier and also add the validation for state --- apiserver/plane/api/views/cycle.py | 12 ++++++-- apiserver/plane/api/views/issue.py | 12 ++++++-- apiserver/plane/api/views/module.py | 12 ++++++-- apiserver/plane/api/views/state.py | 46 +++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 6 deletions(-) diff --git a/apiserver/plane/api/views/cycle.py b/apiserver/plane/api/views/cycle.py index d70693648..41280c19e 100644 --- a/apiserver/plane/api/views/cycle.py +++ b/apiserver/plane/api/views/cycle.py @@ -253,9 +253,16 @@ class CycleAPIEndpoint(WebhookMixin, BaseAPIView): external_id=request.data.get("external_id"), ).exists() ): + cycle = Cycle.objects.filter( + workspace__slug=slug, + project_id=project_id, + external_source=request.data.get("external_source"), + external_id=request.data.get("external_id"), + ).first() return Response( { - "error": "Cycle with the same external id and external source already exists" + "error": "Cycle with the same external id and external source already exists", + "cycle": str(cycle.id), }, status=status.HTTP_410_GONE, ) @@ -322,7 +329,8 @@ class CycleAPIEndpoint(WebhookMixin, BaseAPIView): ): return Response( { - "error": "Cycle with the same external id and external source already exists" + "error": "Cycle with the same external id and external source already exists", + "cycle_id": str(cycle.id), }, status=status.HTTP_410_GONE, ) diff --git a/apiserver/plane/api/views/issue.py b/apiserver/plane/api/views/issue.py index a76a5357e..b475e0caf 100644 --- a/apiserver/plane/api/views/issue.py +++ b/apiserver/plane/api/views/issue.py @@ -230,9 +230,16 @@ class IssueAPIEndpoint(WebhookMixin, BaseAPIView): external_id=request.data.get("external_id"), ).exists() ): + issue = Issue.objects.filter( + workspace__slug=slug, + project_id=project_id, + external_id=request.data.get("external_id"), + external_source=request.data.get("external_source"), + ).first() return Response( { - "error": "Issue with the same external id and external source already exists" + "error": "Issue with the same external id and external source already exists", + "issue_id": str(issue.id), }, status=status.HTTP_410_GONE, ) @@ -290,7 +297,8 @@ class IssueAPIEndpoint(WebhookMixin, BaseAPIView): ): return Response( { - "error": "Issue with the same external id and external source already exists" + "error": "Issue with the same external id and external source already exists", + "issue_id": str(issue.id) }, status=status.HTTP_410_GONE, ) diff --git a/apiserver/plane/api/views/module.py b/apiserver/plane/api/views/module.py index c1cac5b53..83e888d3c 100644 --- a/apiserver/plane/api/views/module.py +++ b/apiserver/plane/api/views/module.py @@ -142,9 +142,16 @@ class ModuleAPIEndpoint(WebhookMixin, BaseAPIView): external_id=request.data.get("external_id"), ).exists() ): + module = Module.objects.filter( + project_id=project_id, + workspace__slug=slug, + external_source=request.data.get("external_source"), + external_id=request.data.get("external_id"), + ).first() return Response( { - "error": "Module with the same external id and external source already exists" + "error": "Module with the same external id and external source already exists", + "module_id": str(module.id), }, status=status.HTTP_410_GONE, ) @@ -182,7 +189,8 @@ class ModuleAPIEndpoint(WebhookMixin, BaseAPIView): ): return Response( { - "error": "Module with the same external id and external source already exists" + "error": "Module with the same external id and external source already exists", + "module_id": str(module.id), }, status=status.HTTP_410_GONE, ) diff --git a/apiserver/plane/api/views/state.py b/apiserver/plane/api/views/state.py index f931c2ed2..4ac91903f 100644 --- a/apiserver/plane/api/views/state.py +++ b/apiserver/plane/api/views/state.py @@ -38,6 +38,30 @@ class StateAPIEndpoint(BaseAPIView): data=request.data, context={"project_id": project_id} ) if serializer.is_valid(): + if ( + request.data.get("external_id") + and request.data.get("external_source") + and State.objects.filter( + project_id=project_id, + workspace__slug=slug, + external_source=request.data.get("external_source"), + external_id=request.data.get("external_id"), + ).exists() + ): + state = State.objects.filter( + workspace__slug=slug, + project_id=project_id, + external_id=request.data.get("external_id"), + external_source=request.data.get("external_source"), + ).first() + return Response( + { + "error": "State with the same external id and external source already exists", + "state_id": str(state.id), + }, + status=status.HTTP_410_GONE, + ) + serializer.save(project_id=project_id) return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) @@ -91,6 +115,28 @@ class StateAPIEndpoint(BaseAPIView): ) serializer = StateSerializer(state, data=request.data, partial=True) if serializer.is_valid(): + if ( + str(request.data.get("external_id")) + and (state.external_id != str(request.data.get("external_id"))) + and request.data.get("external_source") + and ( + state.external_source + != request.data.get("external_source") + ) + and Issue.objects.filter( + project_id=project_id, + workspace__slug=slug, + external_source=request.data.get("external_source"), + external_id=request.data.get("external_id"), + ).exists() + ): + return Response( + { + "error": "Issue with the same external id and external source already exists", + "state_id": str(state.id), + }, + status=status.HTTP_410_GONE, + ) serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)