Skip to content

Commit

Permalink
fix: 그룹초대 서비스로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
SangWoon123 committed May 16, 2024
1 parent e9c4286 commit db9a013
Showing 1 changed file with 37 additions and 85 deletions.
Original file line number Diff line number Diff line change
@@ -1,135 +1,87 @@
package com.tukorea.planding.domain.group.service;

import com.tukorea.planding.domain.group.entity.GroupInvite;
import com.tukorea.planding.domain.group.dto.request.GroupInviteRequest;
import com.tukorea.planding.domain.group.entity.GroupRoom;
import com.tukorea.planding.domain.group.entity.UserGroup;
import com.tukorea.planding.domain.group.dto.request.GroupInviteRequest;
import com.tukorea.planding.domain.group.dto.response.GroupInviteResponse;
import com.tukorea.planding.domain.group.entity.InviteStatus;
import com.tukorea.planding.domain.group.repository.GroupInviteRepository;
import com.tukorea.planding.domain.notify.dto.NotificationScheduleRequest;
import com.tukorea.planding.domain.notify.entity.NotificationType;
import com.tukorea.planding.domain.group.service.query.GroupQueryService;
import com.tukorea.planding.domain.group.dto.response.GroupInviteMessageResponse;
import com.tukorea.planding.domain.notify.service.NotificationService;
import com.tukorea.planding.domain.user.dto.UserInfo;
import com.tukorea.planding.domain.user.entity.User;
import com.tukorea.planding.domain.user.service.UserQueryService;
import com.tukorea.planding.global.error.BusinessException;
import com.tukorea.planding.global.error.ErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
import java.util.UUID;

@Service
@Slf4j
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class GroupInviteService {

private final UserQueryService userQueryService;
private final GroupInviteRepository groupInviteRepository;
private final GroupQueryService groupQueryService;
private final GroupInviteQueryService groupInviteQueryService;


private final NotificationService notificationService;
private final UserGroupService userGroupService;
private final NotificationService notificationService;
private final RedisGroupInviteService redisGroupInviteService;


@Transactional
public GroupInviteResponse inviteGroupRoom(UserInfo userInfo, GroupInviteRequest invitedUserInfo) {

// 초대하는 유저가 존재하는지 체크하는 로직
User invitingUser = userQueryService.getByUserInfo(userInfo.getUserCode());

public GroupInviteMessageResponse inviteGroupRoom(UserInfo userInfo, GroupInviteRequest groupInviteRequest) {
// 초대하는 사용자와 초대 대상 사용자가 같은지 확인
if (invitingUser.getUserCode().equals(invitedUserInfo.userCode())) {
if (userInfo.getUserCode().equals(groupInviteRequest.getUserCode())) {
throw new BusinessException(ErrorCode.CANNOT_INVITE_YOURSELF);
}

GroupRoom groupRoom = groupQueryService.getGroupById(invitedUserInfo.groupId());
log.info("[그룹 초대] {} : {}", groupRoom.getName(), invitingUser.getUsername());
// 그룹이 존재하는지
if (!groupQueryService.existById(groupInviteRequest.getGroupId())) {
throw new BusinessException(ErrorCode.GROUP_ROOM_NOT_FOUND);
}

// 초대하는 유저가 방장인지 체크하는 로직
validInvitePermission(groupRoom, invitingUser);
//그룹에 속해있는지
// 사용자가 이미 그룹에 속해 있는지 확인
if (groupQueryService.existGroupInUser(groupInviteRequest.getUserCode(), groupInviteRequest.getGroupId())) {
throw new BusinessException(ErrorCode.USER_ALREADY_IN_GROUP);
}

User invitedUser = userQueryService.getByUserInfo(invitedUserInfo.userCode());
checkUserAlreadyOrInvited(groupRoom, invitedUser);
GroupRoom groupRoom = groupQueryService.getGroupById(groupInviteRequest.getGroupId());

GroupInvite invite = createAndSaveInvitation(groupRoom, invitingUser, invitedUser);

sendGroupInviteNotification(invitedUser, groupRoom);
GroupInviteMessageResponse groupInviteMessageResponse = GroupInviteMessageResponse.create(
"INV" + UUID.randomUUID()
, groupInviteRequest.getGroupId()
, groupInviteRequest.getUserCode()
, userInfo.getId());

return GroupInvite.toInviteResponse(invite);
}

public List<GroupInviteResponse> getInvitations(UserInfo userInfo) {
User user = userQueryService.getByUserInfo(userInfo.getUserCode());
redisGroupInviteService.createInvitation(groupInviteRequest.getUserCode(), groupInviteMessageResponse);

List<GroupInvite> groupInvites = groupInviteQueryService.getPendingInvitationForUser(user);
notificationService.notifyInvitation(groupInviteRequest.getUserCode(), groupRoom.getName());

return groupInvites.stream()
.map(GroupInvite::toInviteResponse)
.collect(Collectors.toList());
return groupInviteMessageResponse;
}

@Transactional
public GroupInviteResponse acceptInvitation(UserInfo userInfo, String code) {
User user = userQueryService.getByUserInfo(userInfo.getUserCode());
public void acceptInvitation(UserInfo userInfo, String code, Long groupId) {
User user = userQueryService.getUserByUserCode(userInfo.getUserCode());
GroupRoom group = groupQueryService.getGroupById(groupId);

GroupInvite groupInvite = groupInviteQueryService.getInvitationByInviteCode(code);
groupInvite.accept();

final UserGroup userGroup = UserGroup.createUserGroup(user, groupInvite.getGroupRoom());
final UserGroup userGroup = UserGroup.createUserGroup(user, group);
userGroupService.save(userGroup);

return GroupInvite.toInviteResponse(groupInvite);
}

private void sendGroupInviteNotification(User invitedUser, GroupRoom groupRoom) {
NotificationScheduleRequest request = NotificationScheduleRequest
.builder()
.type(NotificationType.INVITE)
.groupName(groupRoom.getName())
.message(groupRoom.getName() + "그룹으로 부터 초대되었습니다.")
.receiverCode(invitedUser.getUserCode())
.build();

notificationService.send(request);
redisGroupInviteService.deleteInvitation(userInfo.getUserCode(), code);
}

private GroupInvite createAndSaveInvitation(GroupRoom groupRoom, User invitingUser, User invitedUser) {
GroupInvite invite = GroupInvite.builder()
.groupRoom(groupRoom)
.invitingUser(invitingUser)
.invitedUser(invitedUser)
.inviteStatus(InviteStatus.PENDING)
.createdAt(LocalDateTime.now())
.expiredAt(LocalDateTime.now().plusDays(1))
.build();
return groupInviteRepository.save(invite);
public List<GroupInviteMessageResponse> getInvitations(UserInfo userInfo) {
return redisGroupInviteService.getAllInvitations(userInfo.getUserCode());
}

private void validInvitePermission(GroupRoom groupRoom, User invitingUser) {
if (!groupRoom.getOwner().equals(invitingUser.getUserCode())) {
throw new BusinessException(ErrorCode.UNAUTHORIZED_GROUP_ROOM_INVITATION);
}
}

private void checkUserAlreadyOrInvited(GroupRoom groupRoom, User invitedUser) {
// 초대한 유저가 이미 그룹에 속해 있는지 확인
if (groupRoom.getUserGroups().contains(invitedUser)) {
throw new BusinessException(ErrorCode.USER_ALREADY_INVITED);
}
// 이미 보낸 초대인지 확인
if (groupInviteRepository.existsByGroupRoomIdAndInvitedUserAndInviteStatus(groupRoom.getId(), invitedUser, InviteStatus.PENDING)) {
throw new BusinessException(ErrorCode.INVITATION_ALREADY_SENT);
}
@Transactional
public void declineInvitation(UserInfo userInfo, String code) {
User user = userQueryService.getUserByUserCode(userInfo.getUserCode());
redisGroupInviteService.deleteInvitation(userInfo.getUserCode(), code);
}


}

0 comments on commit db9a013

Please sign in to comment.