diff --git a/apiserver/plane/api/serializers/workspace.py b/apiserver/plane/api/serializers/workspace.py index 078a4bf08..4d83d6262 100644 --- a/apiserver/plane/api/serializers/workspace.py +++ b/apiserver/plane/api/serializers/workspace.py @@ -19,6 +19,7 @@ from plane.db.models import ( class WorkSpaceSerializer(BaseSerializer): owner = UserLiteSerializer(read_only=True) total_members = serializers.IntegerField(read_only=True) + total_issues = serializers.IntegerField(read_only=True) class Meta: model = Workspace diff --git a/apiserver/plane/api/views/workspace.py b/apiserver/plane/api/views/workspace.py index 6047765e6..26c82d54c 100644 --- a/apiserver/plane/api/views/workspace.py +++ b/apiserver/plane/api/views/workspace.py @@ -80,9 +80,22 @@ class WorkSpaceViewSet(BaseViewSet): lookup_field = "slug" def get_queryset(self): + member_count = ( + WorkspaceMember.objects.filter(workspace=OuterRef("id")) + .order_by() + .annotate(count=Func(F("id"), function="Count")) + .values("count") + ) + + issue_count = ( + Issue.objects.filter(workspace=OuterRef("id")) + .order_by() + .annotate(count=Func(F("id"), function="Count")) + .values("count") + ) return self.filter_queryset( super().get_queryset().select_related("owner") - ).order_by("name") + ).order_by("name").filter(workspace_member__member=self.request.user).annotate(total_members=member_count).annotate(total_issues=issue_count) def create(self, request): try: @@ -139,6 +152,13 @@ class UserWorkSpacesEndpoint(BaseAPIView): .values("count") ) + issue_count = ( + Issue.objects.filter(workspace=OuterRef("id")) + .order_by() + .annotate(count=Func(F("id"), function="Count")) + .values("count") + ) + workspace = ( Workspace.objects.prefetch_related( Prefetch("workspace_member", queryset=WorkspaceMember.objects.all()) @@ -147,7 +167,7 @@ class UserWorkSpacesEndpoint(BaseAPIView): workspace_member__member=request.user, ) .select_related("owner") - ).annotate(total_members=member_count) + ).annotate(total_members=member_count).annotate(total_issues=issue_count) serializer = WorkSpaceSerializer(self.filter_queryset(workspace), many=True) return Response(serializer.data, status=status.HTTP_200_OK)