Skip to content

Commit 5064ad9

Browse files
feat: add functionality to retrieve users by team ID and update team detail endpoint (#151)
Co-authored-by: Amit Prakash <[email protected]>
1 parent 0bffd57 commit 5064ad9

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

todo/repositories/team_repository.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,15 @@ def get_by_user_id(cls, user_id: str) -> list[UserTeamDetailsModel]:
8989
return [UserTeamDetailsModel(**data) for data in user_teams_data]
9090
except Exception:
9191
return []
92+
93+
@classmethod
94+
def get_users_by_team_id(cls, team_id: str) -> list[str]:
95+
"""
96+
Get all user IDs for a specific team.
97+
"""
98+
collection = cls.get_collection()
99+
try:
100+
user_teams_data = collection.find({"team_id": ObjectId(team_id), "is_active": True})
101+
return [str(data["user_id"]) for data in user_teams_data]
102+
except Exception:
103+
return []

todo/services/user_service.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
)
88
from rest_framework.exceptions import ValidationError as DRFValidationError
99
from typing import List, Tuple
10+
from todo.dto.user_dto import UserDTO
1011

1112

1213
class UserService:
@@ -35,6 +36,21 @@ def search_users(cls, query: str, page: int = 1, limit: int = 10) -> Tuple[List[
3536
cls._validate_search_params(query, page, limit)
3637
return UserRepository.search_users(query, page, limit)
3738

39+
@classmethod
40+
def get_users_by_ids(cls, user_ids: list[str]) -> list[UserDTO]:
41+
users = []
42+
for user_id in user_ids:
43+
user = UserRepository.get_by_id(user_id)
44+
if user:
45+
users.append(UserDTO(
46+
id=str(user.id),
47+
name=user.name,
48+
email_id=user.email_id,
49+
created_at=user.created_at,
50+
updated_at=user.updated_at,
51+
))
52+
return users
53+
3854
@classmethod
3955
def _validate_google_user_data(cls, google_user_data: dict) -> None:
4056
validation_errors = {}

todo/views/team.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from drf_spectacular.utils import extend_schema, OpenApiParameter, OpenApiResponse
1515
from drf_spectacular.types import OpenApiTypes
1616
from todo.dto.team_dto import TeamDTO
17+
from todo.services.user_service import UserService
1718

1819

1920
class TeamListView(APIView):
@@ -99,7 +100,7 @@ class TeamDetailView(APIView):
99100
@extend_schema(
100101
operation_id="get_team_by_id",
101102
summary="Get team by ID",
102-
description="Retrieve a single team by its unique identifier",
103+
description="Retrieve a single team by its unique identifier. Optionally, set ?member=true to get users belonging to this team.",
103104
tags=["teams"],
104105
parameters=[
105106
OpenApiParameter(
@@ -108,20 +109,34 @@ class TeamDetailView(APIView):
108109
location=OpenApiParameter.PATH,
109110
description="Unique identifier of the team",
110111
),
112+
OpenApiParameter(
113+
name="member",
114+
type=OpenApiTypes.BOOL,
115+
location=OpenApiParameter.QUERY,
116+
description="If true, returns users that belong to this team instead of team details.",
117+
required=False,
118+
),
111119
],
112120
responses={
113-
200: OpenApiResponse(description="Team retrieved successfully"),
121+
200: OpenApiResponse(description="Team or team members retrieved successfully"),
114122
404: OpenApiResponse(description="Team not found"),
115123
500: OpenApiResponse(description="Internal server error"),
116124
},
117125
)
118126
def get(self, request: Request, team_id: str):
119127
"""
120-
Retrieve a single team by ID.
128+
Retrieve a single team by ID, or users in the team if ?member=true.
121129
"""
122130
try:
123-
team_dto: TeamDTO = TeamService.get_team_by_id(team_id)
124-
return Response(data=team_dto.model_dump(mode="json"), status=status.HTTP_200_OK)
131+
member = request.query_params.get("member", "false").lower() == "true"
132+
if member:
133+
from todo.repositories.team_repository import UserTeamDetailsRepository
134+
user_ids = UserTeamDetailsRepository.get_users_by_team_id(team_id)
135+
users = UserService.get_users_by_ids(user_ids)
136+
return Response(data=[user.model_dump(mode="json") for user in users], status=status.HTTP_200_OK)
137+
else:
138+
team_dto: TeamDTO = TeamService.get_team_by_id(team_id)
139+
return Response(data=team_dto.model_dump(mode="json"), status=status.HTTP_200_OK)
125140
except ValueError as e:
126141
fallback_response = ApiErrorResponse(
127142
statusCode=404,

0 commit comments

Comments
 (0)