|
18 | 18 | from drf_spectacular.types import OpenApiTypes
|
19 | 19 | from todo.dto.team_dto import TeamDTO
|
20 | 20 | from todo.services.user_service import UserService
|
| 21 | +from todo.repositories.team_repository import TeamRepository |
21 | 22 |
|
22 | 23 |
|
23 | 24 | class TeamListView(APIView):
|
@@ -327,3 +328,42 @@ def _handle_validation_errors(self, errors):
|
327 | 328 | errors=[{"detail": str(error)} for error in errors.values()],
|
328 | 329 | )
|
329 | 330 | 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