diff --git a/apiserver/plane/api/urls.py b/apiserver/plane/api/urls.py index 14961adfa..c927b8719 100644 --- a/apiserver/plane/api/urls.py +++ b/apiserver/plane/api/urls.py @@ -83,7 +83,7 @@ from plane.api.views import ( EstimateViewSet, EstimatePointViewSet, ProjectEstimatePointEndpoint, - BulkCreateEstimatePointEndpoint, + BulkEstimatePointEndpoint, ## End Estimates # Shortcuts ShortCutViewSet, @@ -536,8 +536,8 @@ urlpatterns = [ name="project-estimate-points", ), path( - "workspaces//projects//estimates//bulk-create-estimate-points/", - BulkCreateEstimatePointEndpoint.as_view(), + "workspaces//projects//estimates//bulk-estimate-points/", + BulkEstimatePointEndpoint.as_view(), name="bulk-create-estimate-points", ), # End States ## diff --git a/apiserver/plane/api/views/__init__.py b/apiserver/plane/api/views/__init__.py index b6780a518..781005b89 100644 --- a/apiserver/plane/api/views/__init__.py +++ b/apiserver/plane/api/views/__init__.py @@ -135,5 +135,5 @@ from .estimate import ( EstimateViewSet, EstimatePointViewSet, ProjectEstimatePointEndpoint, - BulkCreateEstimatePointEndpoint, + BulkEstimatePointEndpoint, ) diff --git a/apiserver/plane/api/views/estimate.py b/apiserver/plane/api/views/estimate.py index fced7ccfc..96d0ed1a4 100644 --- a/apiserver/plane/api/views/estimate.py +++ b/apiserver/plane/api/views/estimate.py @@ -117,6 +117,10 @@ class EstimatePointViewSet(BaseViewSet): class ProjectEstimatePointEndpoint(BaseAPIView): + permission_classes = [ + ProjectEntityPermission, + ] + def get(self, request, slug, project_id): try: project = Project.objects.get(workspace__slug=slug, pk=project_id) @@ -137,7 +141,11 @@ class ProjectEstimatePointEndpoint(BaseAPIView): ) -class BulkCreateEstimatePointEndpoint(BaseAPIView): +class BulkEstimatePointEndpoint(BaseAPIView): + permission_classes = [ + ProjectEntityPermission, + ] + def post(self, request, slug, project_id, estimate_id): try: estimate = Estimate.objects.get( @@ -161,6 +169,8 @@ class BulkCreateEstimatePointEndpoint(BaseAPIView): description=estimate_point.get("description", ""), project_id=project_id, workspace_id=estimate.workspace_id, + created_by=request.user, + updated_by=request.user, ) for estimate_point in estimate_points ], @@ -182,3 +192,54 @@ class BulkCreateEstimatePointEndpoint(BaseAPIView): {"error": "Something went wrong please try again later"}, status=status.HTTP_400_BAD_REQUEST, ) + + def patch(self, request, slug, project_id, estimate_id): + try: + if not len(request.data.get("estimate_points", [])): + return Response( + {"error": "Estimate points are required"}, + status=status.HTTP_400_BAD_REQUEST, + ) + + estimate_points_data = request.data.get("estimate_points", []) + + estimate_points = EstimatePoint.objects.filter( + pk__in=[ + estimate_point.get("id") for estimate_point in estimate_points_data + ], + workspace__slug=slug, + project_id=project_id, + estimate_id=estimate_id, + ) + + print(estimate_points) + updated_estimate_points = [] + for estimate_point in estimate_points: + # Find the data for that estimate point + estimate_point_data = [ + point + for point in estimate_points_data + if point.get("id") == str(estimate_point.id) + ] + print(estimate_point_data) + if len(estimate_point_data): + estimate_point.value = estimate_point_data[0].get( + "value", estimate_point.value + ) + updated_estimate_points.append(estimate_point) + + EstimatePoint.objects.bulk_update( + updated_estimate_points, ["value"], batch_size=10 + ) + serializer = EstimatePointSerializer(estimate_points, many=True) + return Response(serializer.data, status=status.HTTP_200_OK) + except Estimate.DoesNotExist: + return Response( + {"error": "Estimate does not exist"}, status=status.HTTP_400_BAD_REQUEST + ) + except Exception as e: + print(e) + return Response( + {"error": "Something went wrong please try again later"}, + status=status.HTTP_400_BAD_REQUEST, + )