diff --git a/src/main/java/com/example/solidconnection/chat/repository/ChatRoomRepository.java b/src/main/java/com/example/solidconnection/chat/repository/ChatRoomRepository.java index cdb0dbfa6..8c0b81a4b 100644 --- a/src/main/java/com/example/solidconnection/chat/repository/ChatRoomRepository.java +++ b/src/main/java/com/example/solidconnection/chat/repository/ChatRoomRepository.java @@ -34,7 +34,7 @@ SELECT COUNT(cm) FROM ChatMessage cm """) long countUnreadMessages(@Param("chatRoomId") long chatRoomId, @Param("userId") long userId); - boolean existsByMentoringId(long mentoringId); + ChatRoom findByMentoringId(long mentoringId); List findAllByMentoringIdIn(List mentoringIds); } diff --git a/src/main/java/com/example/solidconnection/chat/service/ChatService.java b/src/main/java/com/example/solidconnection/chat/service/ChatService.java index 874d71bd5..ae9be659b 100644 --- a/src/main/java/com/example/solidconnection/chat/service/ChatService.java +++ b/src/main/java/com/example/solidconnection/chat/service/ChatService.java @@ -174,15 +174,20 @@ public void sendChatMessage(ChatMessageSendRequest chatMessageSendRequest, long } @Transactional - public void createMentoringChatRoom(Long mentoringId, Long mentorId, Long menteeId) { - if (chatRoomRepository.existsByMentoringId(mentoringId)) { - return; + public Long createMentoringChatRoom(Long mentoringId, Long mentorId, Long menteeId) { + ChatRoom existingChatRoom = chatRoomRepository.findByMentoringId(mentoringId); + if (existingChatRoom != null) { + return existingChatRoom.getId(); } + // 새 채팅방 생성 ChatRoom chatRoom = new ChatRoom(mentoringId, false); chatRoom = chatRoomRepository.save(chatRoom); + ChatParticipant mentorParticipant = new ChatParticipant(mentorId, chatRoom); ChatParticipant menteeParticipant = new ChatParticipant(menteeId, chatRoom); chatParticipantRepository.saveAll(List.of(mentorParticipant, menteeParticipant)); + + return chatRoom.getId(); } } diff --git a/src/main/java/com/example/solidconnection/mentor/dto/MentoringApprovedEvent.java b/src/main/java/com/example/solidconnection/mentor/dto/MentoringApprovedEvent.java deleted file mode 100644 index 4909a5c85..000000000 --- a/src/main/java/com/example/solidconnection/mentor/dto/MentoringApprovedEvent.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.example.solidconnection.mentor.dto; - -public record MentoringApprovedEvent( - long mentoringId, - long mentorId, - long menteeId -) { - - public static MentoringApprovedEvent of(long mentoringId, long mentorId, long menteeId) { - return new MentoringApprovedEvent(mentoringId, mentorId, menteeId); - } -} diff --git a/src/main/java/com/example/solidconnection/mentor/dto/MentoringConfirmResponse.java b/src/main/java/com/example/solidconnection/mentor/dto/MentoringConfirmResponse.java index b4ed1e40f..1cf8f03ff 100644 --- a/src/main/java/com/example/solidconnection/mentor/dto/MentoringConfirmResponse.java +++ b/src/main/java/com/example/solidconnection/mentor/dto/MentoringConfirmResponse.java @@ -1,12 +1,18 @@ package com.example.solidconnection.mentor.dto; +import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL; + import com.example.solidconnection.mentor.domain.Mentoring; +import com.fasterxml.jackson.annotation.JsonInclude; public record MentoringConfirmResponse( - long mentoringId + long mentoringId, + + @JsonInclude(NON_NULL) + Long chatRoomId ) { - public static MentoringConfirmResponse from(Mentoring mentoring) { - return new MentoringConfirmResponse(mentoring.getId()); + public static MentoringConfirmResponse from(Mentoring mentoring, Long chatRoomId) { + return new MentoringConfirmResponse(mentoring.getId(), chatRoomId); } } diff --git a/src/main/java/com/example/solidconnection/mentor/service/MentoringCommandService.java b/src/main/java/com/example/solidconnection/mentor/service/MentoringCommandService.java index 15a1e2507..254323127 100644 --- a/src/main/java/com/example/solidconnection/mentor/service/MentoringCommandService.java +++ b/src/main/java/com/example/solidconnection/mentor/service/MentoringCommandService.java @@ -5,19 +5,18 @@ import static com.example.solidconnection.common.exception.ErrorCode.MENTOR_NOT_FOUND; import static com.example.solidconnection.common.exception.ErrorCode.UNAUTHORIZED_MENTORING; +import com.example.solidconnection.chat.service.ChatService; import com.example.solidconnection.common.VerifyStatus; import com.example.solidconnection.common.exception.CustomException; import com.example.solidconnection.mentor.domain.Mentor; import com.example.solidconnection.mentor.domain.Mentoring; import com.example.solidconnection.mentor.dto.MentoringApplyRequest; import com.example.solidconnection.mentor.dto.MentoringApplyResponse; -import com.example.solidconnection.mentor.dto.MentoringApprovedEvent; import com.example.solidconnection.mentor.dto.MentoringConfirmRequest; import com.example.solidconnection.mentor.dto.MentoringConfirmResponse; import com.example.solidconnection.mentor.repository.MentorRepository; import com.example.solidconnection.mentor.repository.MentoringRepository; import lombok.RequiredArgsConstructor; -import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -27,7 +26,7 @@ public class MentoringCommandService { private final MentoringRepository mentoringRepository; private final MentorRepository mentorRepository; - private final ApplicationEventPublisher eventPublisher; + private final ChatService chatService; @Transactional public MentoringApplyResponse applyMentoring(long siteUserId, MentoringApplyRequest mentoringApplyRequest) { @@ -49,12 +48,13 @@ public MentoringConfirmResponse confirmMentoring(long siteUserId, long mentoring mentoring.confirm(mentoringConfirmRequest.status()); + Long chatRoomId = null; if (mentoringConfirmRequest.status() == VerifyStatus.APPROVED) { mentor.increaseMenteeCount(); - eventPublisher.publishEvent(MentoringApprovedEvent.of(mentoringId, mentor.getSiteUserId(), mentoring.getMenteeId())); + chatRoomId = chatService.createMentoringChatRoom(mentoringId, mentor.getSiteUserId(), mentoring.getMenteeId()); } - return MentoringConfirmResponse.from(mentoring); + return MentoringConfirmResponse.from(mentoring, chatRoomId); } private void validateMentoringNotConfirmed(Mentoring mentoring) { diff --git a/src/main/java/com/example/solidconnection/mentor/service/MentoringEventHandler.java b/src/main/java/com/example/solidconnection/mentor/service/MentoringEventHandler.java deleted file mode 100644 index 920ff007f..000000000 --- a/src/main/java/com/example/solidconnection/mentor/service/MentoringEventHandler.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.example.solidconnection.mentor.service; - -import com.example.solidconnection.chat.service.ChatService; -import com.example.solidconnection.mentor.dto.MentoringApprovedEvent; -import lombok.RequiredArgsConstructor; -import org.springframework.scheduling.annotation.Async; -import org.springframework.stereotype.Component; -import org.springframework.transaction.annotation.Propagation; -import org.springframework.transaction.annotation.Transactional; -import org.springframework.transaction.event.TransactionalEventListener; - -@Component -@RequiredArgsConstructor -public class MentoringEventHandler { - - private final ChatService chatService; - - @Async - @Transactional(propagation = Propagation.REQUIRES_NEW) - @TransactionalEventListener - public void handleMentoringApproved(MentoringApprovedEvent event) { - chatService.createMentoringChatRoom(event.mentoringId(), event.mentorId(), event.menteeId()); - } -} diff --git a/src/test/java/com/example/solidconnection/mentor/service/MentoringCommandServiceTest.java b/src/test/java/com/example/solidconnection/mentor/service/MentoringCommandServiceTest.java index 058388d51..8c6a78468 100644 --- a/src/test/java/com/example/solidconnection/mentor/service/MentoringCommandServiceTest.java +++ b/src/test/java/com/example/solidconnection/mentor/service/MentoringCommandServiceTest.java @@ -134,23 +134,17 @@ class 멘토링_승인_거절_테스트 { assertThat(beforeChatRoom).isEmpty(); // when - mentoringCommandService.confirmMentoring(mentorUser1.getId(), mentoring.getId(), request); + MentoringConfirmResponse response = mentoringCommandService.confirmMentoring(mentorUser1.getId(), mentoring.getId(), request); // then - ChatRoom afterChatRoom = await() - .atMost(Duration.ofSeconds(5)) - .pollInterval(Duration.ofMillis(100)) - .until(() -> chatRoomRepositoryForTest - .findOneOnOneChatRoomByParticipants(mentorUser1.getId(), menteeUser.getId()), - Optional::isPresent) - .orElseThrow(); - + ChatRoom afterChatRoom = chatRoomRepositoryForTest.findOneOnOneChatRoomByParticipants(mentorUser1.getId(), menteeUser.getId()).orElseThrow(); List participantIds = afterChatRoom.getChatParticipants().stream() .map(ChatParticipant::getSiteUserId) .toList(); assertAll( () -> assertThat(afterChatRoom.isGroup()).isFalse(), - () -> assertThat(participantIds).containsExactly(mentorUser1.getId(), menteeUser.getId()) + () -> assertThat(participantIds).containsExactly(mentorUser1.getId(), menteeUser.getId()), + () -> assertThat(response.chatRoomId()).isEqualTo(afterChatRoom.getId()) ); } @@ -186,19 +180,14 @@ class 멘토링_승인_거절_테스트 { assertThat(beforeChatRoom).isEmpty(); // when - mentoringCommandService.confirmMentoring(mentorUser1.getId(), mentoring.getId(), request); + MentoringConfirmResponse response = mentoringCommandService.confirmMentoring(mentorUser1.getId(), mentoring.getId(), request); // then - await() - .pollInterval(Duration.ofMillis(100)) - .during(Duration.ofSeconds(1)) - .until(() -> chatRoomRepositoryForTest - .findOneOnOneChatRoomByParticipants(mentorUser1.getId(), menteeUser.getId()) - .isEmpty()); - Optional afterChatRoom = chatRoomRepositoryForTest.findOneOnOneChatRoomByParticipants(mentorUser1.getId(), menteeUser.getId()); - assertThat(afterChatRoom).isEmpty(); - + assertAll( + () -> assertThat(response.chatRoomId()).isNull(), + () -> assertThat(afterChatRoom).isEmpty() + ); } @Test