Skip to content

Commit

Permalink
Merge pull request #303 from kookmin-sw/#301-fix
Browse files Browse the repository at this point in the history
[fix] 매칭 요청 거절/취소 예외처리 수정 #301
  • Loading branch information
tmdtmdqorekf authored May 22, 2024
2 parents 59796e8 + 1a7766c commit 3b1fb6a
Showing 1 changed file with 63 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.coffee.backend.domain.match.service;

import com.coffee.backend.domain.chatroom.dto.ChatroomCreationDto;
import com.coffee.backend.domain.chatroom.service.ChatroomService;
import com.coffee.backend.domain.fcm.service.FcmService;
import com.coffee.backend.domain.match.dto.IsMatchingDto;
Expand All @@ -21,14 +22,12 @@
import com.coffee.backend.exception.CustomException;
import com.coffee.backend.exception.ErrorCode;
import com.coffee.backend.utils.CustomMapper;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TimeZone;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -126,10 +125,6 @@ public MatchInfoResponseDto getMatchRequestInfo(Long senderId) {
return response;
}

private boolean hasNotExpired(String expirationTime) {
return System.currentTimeMillis() < Long.parseLong(expirationTime);
}

// 받은 요청 정보
public List<MatchReceivedInfoDto> getMatchReceivedInfo(Long receiverId) {
log.trace("getReceivedMatchRequests() for receiverId: {}", receiverId);
Expand Down Expand Up @@ -179,8 +174,8 @@ public MatchAcceptResponse acceptMatchRequest(MatchIdDto dto) {
Long senderId = getLongId(redisTemplate.opsForHash().get(key, "senderId"));
Long receiverId = getLongId(redisTemplate.opsForHash().get(key, "receiverId"));

// ChatroomCreationDto chatroomCreationDto = new ChatroomCreationDto(senderId, receiverId);
// Long chatroomId = chatroomService.createChatroom(chatroomCreationDto);
ChatroomCreationDto chatroomCreationDto = new ChatroomCreationDto(senderId, receiverId);
Long chatroomId = chatroomService.createChatroom(chatroomCreationDto);

redisTemplate.opsForHash().put(key, "status", "accepted");
redisTemplate.opsForHash().put("receiverId:" + receiverId + "-senderId:" + senderId, "status", "accepted");
Expand All @@ -205,15 +200,22 @@ public MatchAcceptResponse acceptMatchRequest(MatchIdDto dto) {
redisTemplate.delete(LOCK_KEY_PREFIX + senderId); // sender 락 해제

// 매칭 중인지 조회 위해 저장
redisTemplate.opsForHash().put("userId:" + senderId, "isMatching", "yes");
Map<String, String> isMatchingInfo = Map.of(
"matchId", dto.getMatchId(),
"senderId", senderId.toString(),
"receiverId", receiverId.toString(),
"isMatching", "yes"
);

redisTemplate.opsForHash().putAll("userId:" + senderId, isMatchingInfo);
redisTemplate.opsForHash().put("userId:" + receiverId, "isMatching", "yes");

MatchAcceptResponse match = new MatchAcceptResponse();
match.setMatchId(dto.getMatchId());
match.setSenderId(senderId);
match.setReceiverId(receiverId);
match.setStatus("accepted");
// match.setChatroomId(chatroomId);
match.setChatroomId(chatroomId);

return match;
}
Expand All @@ -235,6 +237,11 @@ public MatchDto declineMatchRequest(MatchIdDto dto) {
validateRequest(dto.getMatchId());

String key = "matchId:" + dto.getMatchId();
String expirationTime = (String) redisTemplate.opsForHash().get(key, "expirationTime");
if (!hasNotExpired(expirationTime)) {
throw new CustomException(ErrorCode.REQUEST_EXPIRED);
}

Long senderId = getLongId(redisTemplate.opsForHash().get(key, "senderId"));
Long receiverId = getLongId(redisTemplate.opsForHash().get(key, "receiverId"));

Expand All @@ -256,17 +263,22 @@ public MatchDto declineMatchRequest(MatchIdDto dto) {
return match;
}

// 매칭 요청 수동 취소
// 매칭 요청 취소
public MatchDto cancelMatchRequest(MatchIdDto dto) {
log.trace("cancelMatchRequest()");

validateRequest(dto.getMatchId());

String key = "matchId:" + dto.getMatchId();
String expirationTime = (String) redisTemplate.opsForHash().get(key, "expirationTime");
if (!hasNotExpired(expirationTime)) {
throw new CustomException(ErrorCode.REQUEST_EXPIRED);
}

Long senderId = getLongId(redisTemplate.opsForHash().get(key, "senderId"));
Long receiverId = getLongId(redisTemplate.opsForHash().get(key, "receiverId"));

redisTemplate.opsForHash().put(key, "status", "declined");
redisTemplate.opsForHash().put(key, "status", "canceled");
redisTemplate.opsForHash().put("receiverId:" + receiverId + "-senderId:" + senderId, "status", "canceled");
redisTemplate.delete(LOCK_KEY_PREFIX + senderId); // 락 해제

Expand Down Expand Up @@ -303,6 +315,23 @@ private void validateUser(MatchRequestDto dto) {
}
}

private boolean hasNotExpired(String expirationTime) {
return System.currentTimeMillis() < Long.parseLong(expirationTime);
}

// 매칭 요청 검증
private void validateRequest(String matchId) {
log.trace("validateRequest()");

String status = (String) redisTemplate.opsForHash().get("matchId:" + matchId, "status");
switch (Objects.requireNonNull(status)) {
case "accepted" -> throw new CustomException(ErrorCode.REQUEST_ALREADY_ACCEPTED);
case "declined" -> throw new CustomException(ErrorCode.REQUEST_DECLINED);
case "canceled" -> throw new CustomException(ErrorCode.REQUEST_CANCELED);
case "finished" -> throw new CustomException(ErrorCode.REQUEST_FINISHED);
}
}

// 매칭 종료
public MatchStatusDto finishMatch(MatchFinishRequestDto dto) {
log.trace("finishMatch()");
Expand Down Expand Up @@ -339,21 +368,6 @@ private void validateEnder(Long enderId, Long senderId, Long receiverId) {
}
}

// 매칭 요청 검증
private void validateRequest(String matchId) {
log.trace("validateRequest()");

String status = (String) redisTemplate.opsForHash().get("matchId:" + matchId, "status");
switch (Objects.requireNonNull(status)) {
case "pending" -> throw new CustomException(ErrorCode.REQUEST_NOT_ACCEPTED);
case "accepted" -> throw new CustomException(ErrorCode.REQUEST_ALREADY_ACCEPTED);
case "declined" -> throw new CustomException(ErrorCode.REQUEST_DECLINED);
case "canceled" -> throw new CustomException(ErrorCode.REQUEST_CANCELED);
case "finished" -> throw new CustomException(ErrorCode.REQUEST_FINISHED);
default -> throw new CustomException(ErrorCode.REQUEST_NOT_FOUND);
}
}

private void sendMatchFinishNotification(Long fromUserId, Long toUserId) {
log.trace("sendMatchFinishNotification()");

Expand All @@ -372,6 +386,28 @@ public IsMatchingDto isMatching(Long userId) {
throw new CustomException(ErrorCode.REQUEST_NOT_ACCEPTED);
}

Set<String> keys = redisTemplate.keys("receiverId:*-senderId:" + senderId);

MatchInfoResponseDto response = new MatchInfoResponseDto();
for (String key : keys) {
Map<Object, Object> matchInfo = redisTemplate.opsForHash().entries(key);
String expirationTime = (String) matchInfo.get("expirationTime");
if (matchInfo.get("status").equals("pending") && hasNotExpired(expirationTime)) {
Long receiverId = getLongId(matchInfo.get("receiverId"));
User receiver = userRepository.findById(receiverId)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
ReceiverInfoDto receiverInfo = mapper.map(receiver, ReceiverInfoDto.class);
receiverInfo.setCompany(customMapper.toCompanyDto(receiver.getCompany()));

response = mapper.map(matchInfo, MatchInfoResponseDto.class);
response.setReceiverInfo(receiverInfo);
} else {
throw new CustomException(ErrorCode.REQUEST_NOT_FOUND);
}
}

if (userId == )

IsMatchingDto response = new IsMatchingDto();
response.setIsMatching(isMatching);
return response;
Expand Down

0 comments on commit 3b1fb6a

Please sign in to comment.