-
Notifications
You must be signed in to change notification settings - Fork 0
feat/#33 초대 알림 보내기/초대 알림 수락or 거절하기 #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -505,4 +505,153 @@ class CustomTokenRefreshView(TokenRefreshView): | |
| }, | ||
| ) | ||
| def post(self, request, *args, **kwargs): | ||
| return super().post(request, *args, **kwargs) | ||
| return super().post(request, *args, **kwargs) | ||
|
|
||
|
|
||
| class GroupMembershipInviteView(APIView): | ||
| @swagger_auto_schema( | ||
| tags=['그룹'], | ||
| operation_summary="유저에게 그룹 초대 알림 보내기", | ||
| operation_description="그룹장이 특정 유저에게 초대 요청을 보냅니다.", | ||
| request_body=openapi.Schema( | ||
| type=openapi.TYPE_OBJECT, | ||
| properties={ | ||
| 'user_id': openapi.Schema(type=openapi.TYPE_INTEGER, description='User ID to invite') | ||
| }, | ||
| required=['user_id'] | ||
| ), | ||
| responses={201: "Invitation sent", 400: "Error message"} | ||
| ) | ||
| def post(self, request, group_id, *args, **kwargs): | ||
| # 그룹 가져오기 | ||
| group = get_object_or_404(UserGroup, pk=group_id) | ||
| user_id = request.data.get('user_id') | ||
| sender_id = request.data.get('sender_id') # 지금 토큰 발급을 안 해서 이렇게 하고 개발하고 끝나면 request.user로 받을 예정 | ||
|
|
||
|
|
||
|
|
||
| if not user_id: | ||
| return Response({"error": "User ID가 필요합니다."}, status=status.HTTP_400_BAD_REQUEST) | ||
|
|
||
| # 초대 대상 사용자 가져오기 | ||
| user = get_object_or_404(CustomUser, email=user_id) | ||
| sender = get_object_or_404(CustomUser, email=sender_id) # 지금 토큰 발급을 안 해서 이렇게 하고 개발하고 끝나면 request.user로 받을 예정 | ||
|
|
||
| # 이미 그룹에 속해 있는지 확인 | ||
| if GroupMembership.objects.filter(user=user, group=group).exists(): | ||
| return Response({"error": "이미 유저가 그룹에 속해 있습니다."}, status=status.HTTP_400_BAD_REQUEST) | ||
|
|
||
| # 기존 초대 요청이 있는지 확인 | ||
| existing_invite = Notification.objects.filter( | ||
| receiver=user, group=group, notification_type='invite', status='pending' | ||
| ) | ||
| if existing_invite.exists(): | ||
| return Response({"error": "이미 해당 유저를 그룹에 초대했습니다."}, status=status.HTTP_400_BAD_REQUEST) | ||
|
|
||
| # 그룹의 일정 가져오기 (없으면 기본값) | ||
| schedule = getattr(group, 'schedule', None) # group.schedule이 없으면 None 반환 | ||
| course_name = schedule.course_name if schedule and schedule.course_name else "일정 없음" | ||
|
|
||
| # 초대 알림 생성 | ||
| Notification.objects.create( | ||
| sender=sender, # 현재 요청을 보낸 유저 | ||
| #sender = request.user | ||
| receiver=user, | ||
| notification_type='invite', | ||
| group=group, | ||
| content=f"{sender.name}님이 '{course_name}' 일정에 초대했습니다." | ||
| ) | ||
|
|
||
| return Response({ | ||
| "message": "Invitation sent successfully.", | ||
| "group": { | ||
| "id": group.id, | ||
| "code": group.code | ||
| }, | ||
| "invited_user": { | ||
| "id": user.id, | ||
| "name": user.name | ||
| }, | ||
| "course_name": course_name # 초대된 일정 이름 추가 | ||
| }, status=status.HTTP_201_CREATED) | ||
|
|
||
|
|
||
|
|
||
| class GroupMembershipInviteResponseView(APIView): | ||
| @swagger_auto_schema( | ||
| tags=['그룹'], | ||
| operation_summary="초대 요청 수락/거절", | ||
| operation_description="초대받은 유저가 그룹 초대 요청을 수락하거나 거절합니다.", | ||
| request_body=openapi.Schema( | ||
| type=openapi.TYPE_OBJECT, | ||
| properties={ | ||
| 'status': openapi.Schema( | ||
| type=openapi.TYPE_STRING, | ||
| enum=['accepted', 'rejected'], | ||
| description='Invitation response (accepted/rejected)' | ||
| ) | ||
| }, | ||
| required=['status'] | ||
| ), | ||
| responses={200: "Response recorded", 400: "Error message"} | ||
| ) | ||
| def post(self, request, group_id, *args, **kwargs): | ||
| # 그룹 가져오기 | ||
| group = get_object_or_404(UserGroup, pk=group_id) | ||
|
|
||
| # 현재 로그인된 사용자 가져오기 (초대받은 사람) | ||
| # user = request.user 로 바꿀 예정정 | ||
| user_id = request.data.get("user_id") | ||
| user = get_object_or_404(CustomUser,email=user_id) | ||
|
|
||
| # 초대 알림 찾기 (대기 상태인 초대만) | ||
| invite_notification = Notification.objects.filter( | ||
| receiver=user, | ||
| group=group, | ||
| notification_type='invite', | ||
| status='pending' | ||
| ).first() | ||
|
|
||
| if not invite_notification: | ||
| return Response({"error": "보낸 알림이 없습니다."}, status=status.HTTP_400_BAD_REQUEST) | ||
|
|
||
| # 요청에서 응답 상태 가져오기 | ||
| status_response = request.data.get('status') | ||
|
|
||
| if status_response == 'accepted': | ||
| # 그룹에 사용자 추가 | ||
| GroupMembership.objects.create(user=user, group=group, role='member') | ||
| invite_notification.status = 'accepted' | ||
| invite_notification.save() | ||
| return Response({"message": "그룹에 가입했습니다."}, status=status.HTTP_200_OK) | ||
|
|
||
| elif status_response == 'rejected': | ||
| invite_notification.status = 'rejected' | ||
| invite_notification.save() | ||
| return Response({"message": "그룹에 가입을 거절했습니다."}, status=status.HTTP_200_OK) | ||
|
|
||
| return Response({"error": "Invalid status. Use 'accepted' or 'rejected'."}, status=status.HTTP_400_BAD_REQUEST) | ||
|
|
||
| class NotificationListView(APIView): | ||
| # permission_classes = [IsAuthenticated] # 로그인한 사용자만 접근 가능 | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기에 나중에 알림 삭제 기능 같은거 넣으면 될 듯! |
||
| def get(self, request, *args, **kwargs): | ||
| # user = request.user # 현재 로그인한 유저 | ||
|
|
||
| user_id = request.data.get("user_id") | ||
| user = get_object_or_404(CustomUser,email = user_id) | ||
|
|
||
|
|
||
| is_unread = request.query_params.get("unread", None) # 읽지 않은 알림 필터 | ||
|
|
||
| # 기본적으로 해당 유저의 모든 알림 조회 (최신순) | ||
| notifications = Notification.objects.filter(receiver=user).order_by("-created_at") | ||
|
|
||
| # 만약 "unread=true"가 요청에 포함되면 읽지 않은 알림만 반환 | ||
| if is_unread and is_unread.lower() == "true": | ||
| notifications = notifications.filter(is_read=False) | ||
|
|
||
| # 시리얼라이저를 이용해 데이터 변환 | ||
| serializer = NotificationSerializer(notifications, many=True) | ||
|
|
||
| return Response(serializer.data, status=status.HTTP_200_OK) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
지금은 급한대로 알림을 account 앱에 넣었지만,, 나중에 앱을 분리하는게 좋을 듯 합니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
좋습니다 나중에 notice 앱 따로 만들면 될거같아요!