From 5c91e0bef32b92600cbbcf5f1ad9860e63115f8c Mon Sep 17 00:00:00 2001 From: pablohashescobar Date: Tue, 13 Dec 2022 23:45:41 +0530 Subject: [PATCH] feat: last visited workspace and project details in a single endpoint --- apiserver/plane/api/urls.py | 6 ++++ apiserver/plane/api/views/__init__.py | 1 + apiserver/plane/api/views/project.py | 1 - apiserver/plane/api/views/workspace.py | 48 ++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/apiserver/plane/api/urls.py b/apiserver/plane/api/urls.py index 68ce32bd0..3a7e02f62 100644 --- a/apiserver/plane/api/urls.py +++ b/apiserver/plane/api/urls.py @@ -56,6 +56,7 @@ from plane.api.views import ( BulkAssignIssuesToCycleEndpoint, ProjectUserViewsEndpoint, UserWorkSpaceIssues, + UserLastProjectWithWorkspaceEndpoint, ) from plane.api.views.project import AddTeamToProjectEndpoint @@ -233,6 +234,11 @@ urlpatterns = [ ), name="workspace", ), + path( + "users/last-visited-workspace/", + UserLastProjectWithWorkspaceEndpoint.as_view(), + name="workspace-project-details", + ), ## End Workspaces ## # Projects path( diff --git a/apiserver/plane/api/views/__init__.py b/apiserver/plane/api/views/__init__.py index a6bb70af0..c2efaa707 100644 --- a/apiserver/plane/api/views/__init__.py +++ b/apiserver/plane/api/views/__init__.py @@ -32,6 +32,7 @@ from .workspace import ( WorkspaceInvitationsViewset, UserWorkspaceInvitationsEndpoint, UserWorkspaceInvitationEndpoint, + UserLastProjectWithWorkspaceEndpoint, ) from .state import StateViewSet from .shortcut import ShortCutViewSet diff --git a/apiserver/plane/api/views/project.py b/apiserver/plane/api/views/project.py index 0685cebe4..dafc62743 100644 --- a/apiserver/plane/api/views/project.py +++ b/apiserver/plane/api/views/project.py @@ -22,7 +22,6 @@ from plane.api.serializers import ( ProjectMemberSerializer, ProjectDetailSerializer, ProjectMemberInviteSerializer, - ProjectIdentifierSerializer, ) from plane.api.permissions import ProjectBasePermission diff --git a/apiserver/plane/api/views/workspace.py b/apiserver/plane/api/views/workspace.py index 8e10a72b9..2a412ec76 100644 --- a/apiserver/plane/api/views/workspace.py +++ b/apiserver/plane/api/views/workspace.py @@ -26,6 +26,7 @@ from plane.api.serializers import ( TeamSerializer, WorkSpaceMemberInviteSerializer, UserLiteSerializer, + ProjectMemberSerializer, ) from plane.api.views.base import BaseAPIView from . import BaseViewSet @@ -35,6 +36,7 @@ from plane.db.models import ( WorkspaceMember, WorkspaceMemberInvite, Team, + ProjectMember, ) from plane.api.permissions import WorkSpaceBasePermission, WorkSpaceAdminPermission from plane.bgtasks.workspace_invitation_task import workspace_invitation @@ -460,3 +462,49 @@ class UserWorkspaceInvitationEndpoint(BaseViewSet): .filter(pk=self.kwargs.get("pk")) .select_related("workspace") ) + + +class UserLastProjectWithWorkspaceEndpoint(BaseAPIView): + def get(self, request): + try: + + user = User.objects.get(pk=request.user.id) + + last_workspace_id = user.last_workspace_id + + if last_workspace_id is None: + return Response( + { + "project_details": [], + "workspace_details": {}, + }, + status=status.HTTP_200_OK, + ) + + workspace = Workspace.objects.get(pk=last_workspace_id) + workspace_serializer = WorkSpaceSerializer(workspace) + + project_member = ProjectMember.objects.filter( + workspace_id=last_workspace_id, member=request.user + ).select_related("workspace", "project", "member") + + project_member_serializer = ProjectMemberSerializer( + project_member, many=True + ) + + return Response( + { + "workspace_details": workspace_serializer.data, + "project_details": project_member_serializer.data, + }, + status=status.HTTP_200_OK, + ) + + except User.DoesNotExist: + return Response({"error": "Forbidden"}, status=status.HTTP_403_FORBIDDEN) + except Exception as e: + capture_exception(e) + return Response( + {"error": "Something went wrong please try again later"}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR, + ) \ No newline at end of file