Skip to content

Commit ef47809

Browse files
feat: add team invite code retrieval endpoint (#220)
* feat: add team invite code retrieval endpoint - Introduced TeamInviteCodeView to allow team creators or POCs to retrieve the invite code for their teams. - Updated urls.py to include a new route for accessing the invite code. - This feature enhances team management by providing authorized users with easy access to invite codes. * refactor: improve code readability in team views and URLs - Reformatted import statements in `urls.py` for better organization and clarity. - Enhanced the response formatting in `TeamInviteCodeView` to improve readability and maintain consistency in the API response structure. These changes contribute to cleaner code and improved maintainability. --------- Co-authored-by: Amit Prakash <[email protected]>
1 parent 722e518 commit ef47809

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

todo/urls.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55
from todo.views.auth import GoogleLoginView, GoogleCallbackView, LogoutView
66
from todo.views.role import RoleListView, RoleDetailView
77
from todo.views.label import LabelListView
8-
from todo.views.team import TeamListView, TeamDetailView, JoinTeamByInviteCodeView, AddTeamMembersView
8+
from todo.views.team import (
9+
TeamListView,
10+
TeamDetailView,
11+
JoinTeamByInviteCodeView,
12+
AddTeamMembersView,
13+
TeamInviteCodeView,
14+
)
915
from todo.views.watchlist import WatchlistListView, WatchlistDetailView, WatchlistCheckView
1016
from todo.views.task_assignment import TaskAssignmentView, TaskAssignmentDetailView
1117
from todo.views.task import AssignTaskToUserView
@@ -15,6 +21,7 @@
1521
path("teams/join-by-invite", JoinTeamByInviteCodeView.as_view(), name="join_team_by_invite"),
1622
path("teams/<str:team_id>", TeamDetailView.as_view(), name="team_detail"),
1723
path("teams/<str:team_id>/members", AddTeamMembersView.as_view(), name="add_team_members"),
24+
path("teams/<str:team_id>/invite-code", TeamInviteCodeView.as_view(), name="team_invite_code"),
1825
path("tasks", TaskListView.as_view(), name="tasks"),
1926
path("tasks/<str:task_id>", TaskDetailView.as_view(), name="task_detail"),
2027
path("tasks/<str:task_id>/update", TaskUpdateView.as_view(), name="update_task_and_assignee"),

todo/views/team.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from drf_spectacular.types import OpenApiTypes
1919
from todo.dto.team_dto import TeamDTO
2020
from todo.services.user_service import UserService
21+
from todo.repositories.team_repository import TeamRepository
2122

2223

2324
class TeamListView(APIView):
@@ -327,3 +328,42 @@ def _handle_validation_errors(self, errors):
327328
errors=[{"detail": str(error)} for error in errors.values()],
328329
)
329330
return Response(data=error_response.model_dump(mode="json"), status=400)
331+
332+
333+
class TeamInviteCodeView(APIView):
334+
@extend_schema(
335+
operation_id="get_team_invite_code",
336+
summary="Get team invite code (creator or POC only)",
337+
description="Return the invite code for a team if the requesting user is the creator or POC of the team.",
338+
tags=["teams"],
339+
parameters=[
340+
OpenApiParameter(
341+
name="team_id",
342+
type=OpenApiTypes.STR,
343+
location=OpenApiParameter.PATH,
344+
description="Unique identifier of the team",
345+
required=True,
346+
),
347+
],
348+
responses={
349+
200: OpenApiResponse(description="Invite code returned successfully"),
350+
403: OpenApiResponse(description="Forbidden - not creator or POC"),
351+
404: OpenApiResponse(description="Team not found"),
352+
},
353+
)
354+
def get(self, request: Request, team_id: str):
355+
"""
356+
Return the invite code for a team if the requesting user is the creator or POC of the team.
357+
"""
358+
user_id = request.user_id
359+
team = TeamRepository.get_by_id(team_id)
360+
if not team:
361+
return Response({"detail": "Team not found."}, status=status.HTTP_404_NOT_FOUND)
362+
is_creator = str(team.created_by) == str(user_id)
363+
is_poc = str(team.poc_id) == str(user_id)
364+
if is_creator or is_poc:
365+
return Response({"invite_code": team.invite_code}, status=status.HTTP_200_OK)
366+
return Response(
367+
{"detail": "You are not authorized to view the invite code for this team."},
368+
status=status.HTTP_403_FORBIDDEN,
369+
)

0 commit comments

Comments
 (0)