From 4dda4ec6101f2b9fdac16c9c82fde7b1e92ddedd Mon Sep 17 00:00:00 2001 From: pablohashescobar <118773738+pablohashescobar@users.noreply.github.com> Date: Thu, 20 Apr 2023 18:14:38 +0530 Subject: [PATCH] chore: single endpoint to create estimate and estimate points (#897) --- apiserver/plane/api/urls.py | 5 ++++ apiserver/plane/api/views/estimate.py | 37 +++++++++++++++++++++------ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/apiserver/plane/api/urls.py b/apiserver/plane/api/urls.py index f064d370a..2c202a1c0 100644 --- a/apiserver/plane/api/urls.py +++ b/apiserver/plane/api/urls.py @@ -566,6 +566,11 @@ urlpatterns = [ ProjectEstimatePointEndpoint.as_view(), name="project-estimate-points", ), + path( + "workspaces//projects//estimates/bulk-estimate-points/", + BulkEstimatePointEndpoint.as_view(), + name="bulk-create-estimate-points", + ), path( "workspaces//projects//estimates//bulk-estimate-points/", BulkEstimatePointEndpoint.as_view(), diff --git a/apiserver/plane/api/views/estimate.py b/apiserver/plane/api/views/estimate.py index 96d0ed1a4..99374282d 100644 --- a/apiserver/plane/api/views/estimate.py +++ b/apiserver/plane/api/views/estimate.py @@ -146,11 +146,13 @@ class BulkEstimatePointEndpoint(BaseAPIView): ProjectEntityPermission, ] - def post(self, request, slug, project_id, estimate_id): + def post(self, request, slug, project_id): try: - estimate = Estimate.objects.get( - pk=estimate_id, workspace__slug=slug, project=project_id - ) + if not request.data.get("estimate", False): + return Response( + {"error": "Estimate is required"}, + status=status.HTTP_400_BAD_REQUEST, + ) estimate_points = request.data.get("estimate_points", []) @@ -160,6 +162,18 @@ class BulkEstimatePointEndpoint(BaseAPIView): status=status.HTTP_400_BAD_REQUEST, ) + estimate_serializer = EstimateSerializer(data=request.data.get("estimate")) + if not estimate_serializer.is_valid(): + return Response( + estimate_serializer.errors, status=status.HTTP_400_BAD_REQUEST + ) + try: + estimate = estimate_serializer.save(project_id=project_id) + except IntegrityError: + return Response( + {"errror": "Estimate with the name already exists"}, + status=status.HTTP_400_BAD_REQUEST, + ) estimate_points = EstimatePoint.objects.bulk_create( [ EstimatePoint( @@ -178,9 +192,17 @@ class BulkEstimatePointEndpoint(BaseAPIView): ignore_conflicts=True, ) - serializer = EstimatePointSerializer(estimate_points, many=True) + estimate_point_serializer = EstimatePointSerializer( + estimate_points, many=True + ) - return Response(serializer.data, status=status.HTTP_200_OK) + return Response( + { + "estimate": estimate_serializer.data, + "estimate_points": estimate_point_serializer.data, + }, + status=status.HTTP_200_OK, + ) except Estimate.DoesNotExist: return Response( {"error": "Estimate does not exist"}, @@ -212,7 +234,6 @@ class BulkEstimatePointEndpoint(BaseAPIView): estimate_id=estimate_id, ) - print(estimate_points) updated_estimate_points = [] for estimate_point in estimate_points: # Find the data for that estimate point @@ -238,7 +259,7 @@ class BulkEstimatePointEndpoint(BaseAPIView): {"error": "Estimate does not exist"}, status=status.HTTP_400_BAD_REQUEST ) except Exception as e: - print(e) + capture_exception(e) return Response( {"error": "Something went wrong please try again later"}, status=status.HTTP_400_BAD_REQUEST,