Skip to content

Commit d90a64d

Browse files
authored
Merge pull request #115 from Team-Shaka/feature/114
✨ Feat: 초대 관련 부가기능 구현
2 parents 13266af + b3bf893 commit d90a64d

File tree

6 files changed

+32
-4
lines changed

6 files changed

+32
-4
lines changed

src/main/java/treehouse/server/api/invitation/business/InvitationService.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,30 @@ public InvitationResponseDTO.myInvitationInfo getMyInvitationInfo(User user){
7171

7272
@Transactional
7373
public InvitationResponseDTO.createInvitation createInvitation(User user, InvitationRequestDTO.createInvitation request){
74+
// 남아있는 초대장이 있는지 확인
75+
if (user.getInvitationCount() <= 0) {
76+
throw new InvitationException(GlobalErrorCode.INVITATION_COUNT_ZERO);
77+
}
78+
7479
// 트리 찾기
7580
TreeHouse treehouse = treehouseQueryAdapter.getTreehouseById(request.getTreehouseId());
7681
// 초대 멤버 찾기
7782
Member sender = memberQueryAdapter.findByUserAndTreehouse(user, treehouse);
7883
// 초대 대상이 가입된 사람인지 찾기
79-
8084
User receiverUser = userQueryAdapter.findByPhoneNumberOptional(request.getPhoneNumber()).orElse(null);
8185

86+
//동일한 초대장이 이미 존재하는지 확인하기
87+
if (invitationQueryAdapter.existByPhoneAndTreehouse(request.getPhoneNumber(), treehouse)) {
88+
throw new InvitationException(GlobalErrorCode.INVITATION_ALREADY_EXIST);
89+
}
90+
8291
// 초대장 만들어서 저장하기
8392

8493
Invitation invitation = invitationCommandAdapter.saveInvitation(InvitationMapper.toInvitation(request.getPhoneNumber(), sender, receiverUser, treehouse));
8594

95+
// 초대자의 잔여 초대장 개수 차감
96+
user.reduceInvitationCount();
97+
8698
//알림 생성
8799
if (receiverUser != null) {
88100
NotificationRequestDTO.createNotification notificationRequest = new NotificationRequestDTO.createNotification();

src/main/java/treehouse/server/api/invitation/implement/InvitationQueryAdapter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import treehouse.server.global.annotations.Adapter;
88
import treehouse.server.global.entity.Invitation.Invitation;
99
import treehouse.server.global.entity.User.User;
10+
import treehouse.server.global.entity.treeHouse.TreeHouse;
1011
import treehouse.server.global.exception.GlobalErrorCode;
1112
import treehouse.server.global.exception.ThrowClass.InvitationException;
1213
import treehouse.server.global.exception.ThrowClass.UserException;
@@ -32,4 +33,8 @@ public Invitation findById(Long invitationId) {
3233
return invitationRepository.findById(invitationId)
3334
.orElseThrow(() -> new InvitationException(GlobalErrorCode.INVITATION_NOT_FOUND));
3435
}
36+
37+
public Boolean existByPhoneAndTreehouse(String phoneNumber, TreeHouse treehouse) {
38+
return invitationRepository.existsByPhoneAndTreeHouse(phoneNumber, treehouse);
39+
}
3540
}

src/main/java/treehouse/server/api/invitation/persistence/InvitationRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ public interface InvitationRepository extends JpaRepository<Invitation, Long> {
1515
Invitation findByPhoneAndTreeHouse(String phone, TreeHouse treeHouse);
1616

1717
Boolean existsByPhone(String phoneNumber);
18+
19+
Boolean existsByPhoneAndTreeHouse(String phoneNumber, TreeHouse treehouse);
1820
}

src/main/java/treehouse/server/api/notification/business/NotificationService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public void createNotification(User user, Long treehouseId, NotificationRequestD
4949
User receiver = userQueryAdapter.findByIdOptional(request.getReceiverId()).orElse(null);
5050
Notification notification = NotificationMapper.toNotification(sender, receiver, request, reactionName);
5151

52-
if(receiver.getUser().isPushAgree()) {
53-
fcmService.sendFcmMessage(receiver.getUser(), notification.getTitle(), notification.getBody());
52+
if(receiver.isPushAgree()) {
53+
fcmService.sendFcmMessage(receiver, notification.getTitle(), notification.getBody());
5454

5555
}
5656
notificationCommandAdapter.createNotification(notification);

src/main/java/treehouse/server/global/entity/User/User.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,7 @@ public boolean updatePushAgree(boolean pushAgree){
7272
return this.pushAgree;
7373
}
7474

75+
public void reduceInvitationCount() {
76+
this.invitationCount--;
77+
}
7578
}

src/main/java/treehouse/server/global/exception/GlobalErrorCode.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,15 @@ public enum GlobalErrorCode implements BaseErrorCode{
5353
// TREEHOUSE + 404 Not Found - 찾을 수 없음
5454
TREEHOUSE_NOT_FOUND(NOT_FOUND, "TREEHOUSE404_1", "존재하지 않는 트리입니다."),
5555

56+
// INVITATION + 400 Bad Request - 잘못된 요청
57+
INVITATION_COUNT_ZERO(BAD_REQUEST, "INVITATION400_1", "사용 가능한 초대장 개수가 0입니다."),
58+
5659
// INVITATION + 404 Not Found - 찾을 수 없음
5760
INVITATION_NOT_FOUND(NOT_FOUND, "INVITATION404_1", "존재하지 않는 초대장입니다."),
5861

62+
// INVITATION + 409 CONFLICT : Resource 를 찾을 수 없음
63+
INVITATION_ALREADY_EXIST(CONFLICT, "INVITATION409_1", "이미 존재하는 초대장입니다."),
64+
5965
// POST + 401 Unauthorized - 권한 없음
6066
POST_UNAUTHORIZED(UNAUTHORIZED, "POST401_1", "게시글 수정 및 삭제 권한이 없습니다."),
6167

@@ -101,7 +107,7 @@ public enum GlobalErrorCode implements BaseErrorCode{
101107

102108
FCM_ALREADY_EXISTS_TOKEN(BAD_REQUEST, "FCM400_1", "이미 저장되어 있는 FCM 토큰입니다."),
103109
FCM_ACCESS_TOKEN_REQUEST_ERROR(INTERNAL_SERVER_ERROR, "FCM500_2", "서버 에러, FCM 서버에 AccessToken 요청할 때 에러 발생."),
104-
FCM_SEND_MESSAGE_ERROR(INTERNAL_SERVER_ERROR , "FCM500_3", "서버 에러, FCM 서버에 메시지를 전송할 때 에러 발생. FcmToken이 유효한지 확인해주세요.");
110+
FCM_SEND_MESSAGE_ERROR(INTERNAL_SERVER_ERROR , "FCM500_3", "서버 에러, FCM 서버에 메시지를 전송할 때 에러 발생. FcmToken이 유효한지 확인해주세요."), ;
105111

106112
;
107113

0 commit comments

Comments
 (0)