From 4992b80808d78f3e05714066445e6749b628bd99 Mon Sep 17 00:00:00 2001 From: tmdtmdqorekf Date: Wed, 22 May 2024 17:40:14 +0900 Subject: [PATCH 1/3] =?UTF-8?q?[fix]=20=EB=A7=A4=EC=B9=AD=20=EC=9A=94?= =?UTF-8?q?=EC=B2=AD=20=EA=B1=B0=EC=A0=88/=EC=B7=A8=EC=86=8C=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=EC=B2=98=EB=A6=AC=20=EC=88=98=EC=A0=95=20#301?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coffee/backend/domain/match/service/MatchService.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/backend/src/main/java/com/coffee/backend/domain/match/service/MatchService.java b/backend/src/main/java/com/coffee/backend/domain/match/service/MatchService.java index 10731afd15..d40e0d4502 100644 --- a/backend/src/main/java/com/coffee/backend/domain/match/service/MatchService.java +++ b/backend/src/main/java/com/coffee/backend/domain/match/service/MatchService.java @@ -21,14 +21,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; @@ -256,7 +254,7 @@ public MatchDto declineMatchRequest(MatchIdDto dto) { return match; } - // 매칭 요청 수동 취소 + // 매칭 요청 취소 public MatchDto cancelMatchRequest(MatchIdDto dto) { log.trace("cancelMatchRequest()"); @@ -266,7 +264,7 @@ public MatchDto cancelMatchRequest(MatchIdDto dto) { 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); // 락 해제 @@ -345,12 +343,10 @@ private void validateRequest(String matchId) { 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); } } From 07c19675041bc7696a2ebe6caf2fa796ec22b38b Mon Sep 17 00:00:00 2001 From: tmdtmdqorekf Date: Wed, 22 May 2024 17:50:05 +0900 Subject: [PATCH 2/3] =?UTF-8?q?[fix]=20=EB=A7=A4=EC=B9=AD=20=EB=A7=8C?= =?UTF-8?q?=EB=A3=8C=EC=8B=9C=EA=B0=84=20=EC=98=88=EC=99=B8=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EC=88=98=EC=A0=95=20#301?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/match/service/MatchService.java | 55 +++++++++++-------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/backend/src/main/java/com/coffee/backend/domain/match/service/MatchService.java b/backend/src/main/java/com/coffee/backend/domain/match/service/MatchService.java index d40e0d4502..7efab567ab 100644 --- a/backend/src/main/java/com/coffee/backend/domain/match/service/MatchService.java +++ b/backend/src/main/java/com/coffee/backend/domain/match/service/MatchService.java @@ -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; @@ -59,7 +60,7 @@ public MatchDto sendMatchRequest(MatchRequestDto dto) { validateLock(lockKey); String matchId = UUID.randomUUID().toString(); - long expirationTime = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(10); + long expirationTime = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(1); Map matchInfo = Map.of( "matchId", matchId, "senderId", dto.getSenderId().toString(), @@ -79,7 +80,7 @@ public MatchDto sendMatchRequest(MatchRequestDto dto) { // 10분동안 락 설정 Map lockInfo = Map.of("matchId", matchId); redisTemplate.opsForHash().putAll(lockKey, lockInfo); - redisTemplate.expire(lockKey, 600, TimeUnit.SECONDS); + redisTemplate.expire(lockKey, 60, TimeUnit.SECONDS); // 알림 User fromUser = userRepository.findByUserId(dto.getSenderId()).orElseThrow(); @@ -124,10 +125,6 @@ public MatchInfoResponseDto getMatchRequestInfo(Long senderId) { return response; } - private boolean hasNotExpired(String expirationTime) { - return System.currentTimeMillis() < Long.parseLong(expirationTime); - } - // 받은 요청 정보 public List getMatchReceivedInfo(Long receiverId) { log.trace("getReceivedMatchRequests() for receiverId: {}", receiverId); @@ -177,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"); @@ -211,7 +208,7 @@ public MatchAcceptResponse acceptMatchRequest(MatchIdDto dto) { match.setSenderId(senderId); match.setReceiverId(receiverId); match.setStatus("accepted"); -// match.setChatroomId(chatroomId); + match.setChatroomId(chatroomId); return match; } @@ -233,6 +230,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")); @@ -261,6 +263,11 @@ public MatchDto cancelMatchRequest(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")); @@ -301,6 +308,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()"); @@ -337,19 +361,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 "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); - } - } - private void sendMatchFinishNotification(Long fromUserId, Long toUserId) { log.trace("sendMatchFinishNotification()"); From 1a7766c02936aea00093e1ca916161e605f5ac96 Mon Sep 17 00:00:00 2001 From: tmdtmdqorekf Date: Wed, 22 May 2024 17:58:55 +0900 Subject: [PATCH 3/3] =?UTF-8?q?[chore]=20=EB=A7=8C=EB=A3=8C=20=EC=8B=9C?= =?UTF-8?q?=EA=B0=84=20=EC=9E=AC=EC=84=A4=EC=A0=95=20#301?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/match/service/MatchService.java | 35 +++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/backend/src/main/java/com/coffee/backend/domain/match/service/MatchService.java b/backend/src/main/java/com/coffee/backend/domain/match/service/MatchService.java index 7efab567ab..4b279bded7 100644 --- a/backend/src/main/java/com/coffee/backend/domain/match/service/MatchService.java +++ b/backend/src/main/java/com/coffee/backend/domain/match/service/MatchService.java @@ -60,7 +60,7 @@ public MatchDto sendMatchRequest(MatchRequestDto dto) { validateLock(lockKey); String matchId = UUID.randomUUID().toString(); - long expirationTime = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(1); + long expirationTime = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(10); Map matchInfo = Map.of( "matchId", matchId, "senderId", dto.getSenderId().toString(), @@ -80,7 +80,7 @@ public MatchDto sendMatchRequest(MatchRequestDto dto) { // 10분동안 락 설정 Map lockInfo = Map.of("matchId", matchId); redisTemplate.opsForHash().putAll(lockKey, lockInfo); - redisTemplate.expire(lockKey, 60, TimeUnit.SECONDS); + redisTemplate.expire(lockKey, 600, TimeUnit.SECONDS); // 알림 User fromUser = userRepository.findByUserId(dto.getSenderId()).orElseThrow(); @@ -200,7 +200,14 @@ public MatchAcceptResponse acceptMatchRequest(MatchIdDto dto) { redisTemplate.delete(LOCK_KEY_PREFIX + senderId); // sender 락 해제 // 매칭 중인지 조회 위해 저장 - redisTemplate.opsForHash().put("userId:" + senderId, "isMatching", "yes"); + Map 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(); @@ -379,6 +386,28 @@ public IsMatchingDto isMatching(Long userId) { throw new CustomException(ErrorCode.REQUEST_NOT_ACCEPTED); } + Set keys = redisTemplate.keys("receiverId:*-senderId:" + senderId); + + MatchInfoResponseDto response = new MatchInfoResponseDto(); + for (String key : keys) { + Map 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;