diff --git a/src/main/java/com/dnd/gongmuin/chat_inquiry/domain/ChatInquiry.java b/src/main/java/com/dnd/gongmuin/chat_inquiry/domain/ChatInquiry.java index 86efde25..378b4bf6 100644 --- a/src/main/java/com/dnd/gongmuin/chat_inquiry/domain/ChatInquiry.java +++ b/src/main/java/com/dnd/gongmuin/chat_inquiry/domain/ChatInquiry.java @@ -4,7 +4,7 @@ import static jakarta.persistence.EnumType.*; import static jakarta.persistence.FetchType.*; -import com.dnd.gongmuin.chatroom.exception.ChatErrorCode; +import com.dnd.gongmuin.chat_inquiry.exception.ChatInquiryErrorCode; import com.dnd.gongmuin.common.entity.TimeBaseEntity; import com.dnd.gongmuin.common.exception.runtime.ValidationException; import com.dnd.gongmuin.member.domain.Member; @@ -77,7 +77,7 @@ public static ChatInquiry of( public void updateStatusAccepted() { if (status != InquiryStatus.PENDING) { - throw new ValidationException(ChatErrorCode.UNABLE_TO_CHANGE_CHAT_STATUS); + throw new ValidationException(ChatInquiryErrorCode.UNABLE_TO_CHANGE_STATUS); } status = InquiryStatus.ACCEPTED; answerer.increaseCredit(CHAT_REWARD); @@ -85,7 +85,7 @@ public void updateStatusAccepted() { public void updateStatusRejected() { if (status != InquiryStatus.PENDING) { - throw new ValidationException(ChatErrorCode.UNABLE_TO_CHANGE_CHAT_STATUS); + throw new ValidationException(ChatInquiryErrorCode.UNABLE_TO_CHANGE_STATUS); } status = InquiryStatus.REJECTED; inquirer.increaseCredit(CHAT_REWARD); diff --git a/src/main/java/com/dnd/gongmuin/chat_inquiry/domain/InquiryStatus.java b/src/main/java/com/dnd/gongmuin/chat_inquiry/domain/InquiryStatus.java index 4a940ad0..fb4181f2 100644 --- a/src/main/java/com/dnd/gongmuin/chat_inquiry/domain/InquiryStatus.java +++ b/src/main/java/com/dnd/gongmuin/chat_inquiry/domain/InquiryStatus.java @@ -2,7 +2,7 @@ import java.util.Arrays; -import com.dnd.gongmuin.chatroom.exception.ChatErrorCode; +import com.dnd.gongmuin.chat_inquiry.exception.ChatInquiryErrorCode; import com.dnd.gongmuin.common.exception.runtime.ValidationException; import lombok.Getter; @@ -22,7 +22,7 @@ public static InquiryStatus from(String input) { return Arrays.stream(values()) .filter(status -> status.isEqual(input)) .findAny() - .orElseThrow(() -> new ValidationException(ChatErrorCode.NOT_FOUND_CHAT_STATUS)); + .orElseThrow(() -> new ValidationException(ChatInquiryErrorCode.NOT_FOUND_STATUS)); } private boolean isEqual(String input) { diff --git a/src/main/java/com/dnd/gongmuin/chat_inquiry/exception/ChatInquiryErrorCode.java b/src/main/java/com/dnd/gongmuin/chat_inquiry/exception/ChatInquiryErrorCode.java new file mode 100644 index 00000000..d3d239f9 --- /dev/null +++ b/src/main/java/com/dnd/gongmuin/chat_inquiry/exception/ChatInquiryErrorCode.java @@ -0,0 +1,19 @@ +package com.dnd.gongmuin.chat_inquiry.exception; + +import com.dnd.gongmuin.common.exception.ErrorCode; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor +public enum ChatInquiryErrorCode implements ErrorCode { + + NOT_FOUND_INQUIRY("해당 아이디의 채팅 요청이 존재하지 않습니다.", "CI_001"), + UNAUTHORIZED_REQUEST("채팅 요청을 수락을 하거나 거절할 권한이 없습니다.", "CI_002"), + UNABLE_TO_CHANGE_STATUS("이미 수락했거나 거절한 요청입니다.", "CI_003"), + NOT_FOUND_STATUS("채팅방 상태값을 올바르게 입력해주세요.", "CI_004"); + + private final String message; + private final String code; +} diff --git a/src/main/java/com/dnd/gongmuin/chat_inquiry/service/ChatInquiryService.java b/src/main/java/com/dnd/gongmuin/chat_inquiry/service/ChatInquiryService.java index 596b6c90..db7e13c8 100644 --- a/src/main/java/com/dnd/gongmuin/chat_inquiry/service/ChatInquiryService.java +++ b/src/main/java/com/dnd/gongmuin/chat_inquiry/service/ChatInquiryService.java @@ -16,10 +16,12 @@ import com.dnd.gongmuin.chat_inquiry.dto.CreateChatInquiryRequest; import com.dnd.gongmuin.chat_inquiry.dto.CreateChatInquiryResponse; import com.dnd.gongmuin.chat_inquiry.dto.RejectChatResponse; +import com.dnd.gongmuin.chat_inquiry.exception.ChatInquiryErrorCode; import com.dnd.gongmuin.chat_inquiry.repository.ChatInquiryRepository; import com.dnd.gongmuin.chatroom.domain.ChatRoom; +import com.dnd.gongmuin.chatroom.dto.ChatMessageMapper; import com.dnd.gongmuin.chatroom.dto.ChatRoomMapper; -import com.dnd.gongmuin.chatroom.exception.ChatErrorCode; +import com.dnd.gongmuin.chatroom.repository.ChatMessageRepository; import com.dnd.gongmuin.chatroom.repository.ChatRoomRepository; import com.dnd.gongmuin.common.dto.PageMapper; import com.dnd.gongmuin.common.dto.PageResponse; @@ -49,6 +51,7 @@ public class ChatInquiryService { private final QuestionPostRepository questionPostRepository; private final CreditHistoryService creditHistoryService; private final ApplicationEventPublisher eventPublisher; + private final ChatMessageRepository chatMessageRepository; @Transactional public CreateChatInquiryResponse createChatInquiry(CreateChatInquiryRequest request, Member inquirer) { @@ -77,7 +80,7 @@ public PageResponse getChatInquiresByMember(Member member, @Transactional public AcceptChatResponse acceptChat(Long chatInquiryId, Member answerer) { - ChatInquiry chatInquiry = getChatProposalById(chatInquiryId); + ChatInquiry chatInquiry = getChatInquiryById(chatInquiryId); validateIfAnswerer(answerer, chatInquiry); chatInquiry.updateStatusAccepted(); creditHistoryService.saveChatCreditHistory(CreditType.CHAT_ACCEPT, answerer); @@ -85,6 +88,9 @@ public AcceptChatResponse acceptChat(Long chatInquiryId, Member answerer) { ChatRoom chatRoom = chatRoomRepository.save( ChatRoomMapper.toChatRoom(chatInquiry.getQuestionPost(), chatInquiry.getInquirer(), answerer) ); + chatMessageRepository.save( + ChatMessageMapper.toChatMessage(chatInquiry.getMessage(), chatRoom) + ); eventPublisher.publishEvent( new NotificationEvent(NotificationType.CHAT_ACCEPT, chatInquiry.getId(), answerer.getId(), chatInquiry.getInquirer()) @@ -95,7 +101,7 @@ public AcceptChatResponse acceptChat(Long chatInquiryId, Member answerer) { @Transactional public RejectChatResponse rejectChat(Long chatInquiryId, Member answerer) { - ChatInquiry chatInquiry = getChatProposalById(chatInquiryId); + ChatInquiry chatInquiry = getChatInquiryById(chatInquiryId); validateIfAnswerer(answerer, chatInquiry); chatInquiry.updateStatusRejected(); @@ -113,17 +119,19 @@ public void rejectChatAuto() { List rejectedInquirerIds = chatInquiryRepository.getAutoRejectedInquirerIds(); chatInquiryRepository.updateChatInquiryStatusRejected(); memberRepository.refundInMemberIds(rejectedInquirerIds, CHAT_REWARD); - creditHistoryService.saveCreditHistoryInMemberIds(rejectedInquirerIds, CreditType.CHAT_REFUND, CHAT_REWARD); + creditHistoryService.saveCreditHistoryInMemberIds( + rejectedInquirerIds, CreditType.CHAT_REFUND, CHAT_REWARD + ); } - private ChatInquiry getChatProposalById(Long id) { + private ChatInquiry getChatInquiryById(Long id) { return chatInquiryRepository.findById(id) - .orElseThrow(() -> new NotFoundException(ChatErrorCode.NOT_FOUND_CHAT_ROOM)); + .orElseThrow(() -> new NotFoundException(ChatInquiryErrorCode.NOT_FOUND_INQUIRY)); } private static void validateIfAnswerer(Member member, ChatInquiry chatInquiry) { if (!Objects.equals(member.getId(), chatInquiry.getAnswerer().getId())) { - throw new ValidationException(ChatErrorCode.UNAUTHORIZED_REQUEST); + throw new ValidationException(ChatInquiryErrorCode.UNAUTHORIZED_REQUEST); } } diff --git a/src/main/java/com/dnd/gongmuin/chatroom/dto/ChatMessageMapper.java b/src/main/java/com/dnd/gongmuin/chatroom/dto/ChatMessageMapper.java index 98a45f07..7dd0e472 100644 --- a/src/main/java/com/dnd/gongmuin/chatroom/dto/ChatMessageMapper.java +++ b/src/main/java/com/dnd/gongmuin/chatroom/dto/ChatMessageMapper.java @@ -26,6 +26,18 @@ public static ChatMessageResponse toChatMessageResponse( ); } + public static ChatMessage toChatMessage( + String message, + ChatRoom chatRoom + ) { + return ChatMessage.of( + message, + chatRoom.getId(), + chatRoom.getInquirer().getId(), + MessageType.TEXT + ); + } + public static ChatMessage toChatMessage( ChatMessageRequest request, long chatRoomId diff --git a/src/main/java/com/dnd/gongmuin/chatroom/exception/ChatErrorCode.java b/src/main/java/com/dnd/gongmuin/chatroom/exception/ChatErrorCode.java index dca2559f..c3b182b8 100644 --- a/src/main/java/com/dnd/gongmuin/chatroom/exception/ChatErrorCode.java +++ b/src/main/java/com/dnd/gongmuin/chatroom/exception/ChatErrorCode.java @@ -11,10 +11,7 @@ public enum ChatErrorCode implements ErrorCode { INVALID_MESSAGE_TYPE("메시지 타입을 올바르게 입력해주세요.", "CH_001"), NOT_FOUND_CHAT_ROOM("해당 아이디의 채팅방이 존재하지 않습니다.", "CH_002"), - UNAUTHORIZED_REQUEST("채팅 수락을 하거나 거절할 권한이 없습니다.", "CH_003"), - UNABLE_TO_CHANGE_CHAT_STATUS("이미 수락했거나 거절한 요청입니다.", "CH_004"), - UNAUTHORIZED_CHAT_ROOM("권한이 없는 채팅방입니다.", "CH_005"), - NOT_FOUND_CHAT_STATUS("채팅방 상태값을 올바르게 입력해주세요.", "CH_006"); + UNAUTHORIZED_CHAT_ROOM("채팅방 조회 권한이 없습니다.", "CH_003"); private final String message; private final String code; diff --git a/src/main/java/com/dnd/gongmuin/chatroom/service/ChatRoomService.java b/src/main/java/com/dnd/gongmuin/chatroom/service/ChatRoomService.java index e91c7925..69d9d916 100644 --- a/src/main/java/com/dnd/gongmuin/chatroom/service/ChatRoomService.java +++ b/src/main/java/com/dnd/gongmuin/chatroom/service/ChatRoomService.java @@ -103,9 +103,10 @@ private ChatRoom getChatRoomById(Long id) { } private Member getChatPartner(Member member, ChatRoom chatRoom) { - if (member.isEqualMember(chatRoom.getAnswerer().getId())) { + if (member.equals(chatRoom.getAnswerer())) { return chatRoom.getInquirer(); - } else if (member.isEqualMember(chatRoom.getInquirer().getId())) { + } + if (member.equals(chatRoom.getInquirer())) { return chatRoom.getAnswerer(); } throw new ValidationException(ChatErrorCode.UNAUTHORIZED_CHAT_ROOM); diff --git a/src/main/java/com/dnd/gongmuin/member/domain/Member.java b/src/main/java/com/dnd/gongmuin/member/domain/Member.java index aa7cd70c..fa0ac592 100644 --- a/src/main/java/com/dnd/gongmuin/member/domain/Member.java +++ b/src/main/java/com/dnd/gongmuin/member/domain/Member.java @@ -4,6 +4,7 @@ import static jakarta.persistence.GenerationType.*; import static lombok.AccessLevel.*; +import java.util.Objects; import java.util.Random; import com.dnd.gongmuin.common.entity.TimeBaseEntity; @@ -133,12 +134,25 @@ public void updateProfile(String nickname, JobGroup jobGroup, JobCategory jobCat this.jobCategory = jobCategory; } - public boolean isEqualMember(Long id) { - return this.id.equals(id); - } - private int setRandomNumber() { Random random = new Random(); return random.nextInt(1, 10); } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof Member)) { + return false; + } + Member member = (Member)o; + return Objects.equals(id, member.getId()); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } } diff --git a/src/test/java/com/dnd/gongmuin/chat_inquiry/service/ChatInquiryServiceTest.java b/src/test/java/com/dnd/gongmuin/chat_inquiry/service/ChatInquiryServiceTest.java index 5048dec2..632bc320 100644 --- a/src/test/java/com/dnd/gongmuin/chat_inquiry/service/ChatInquiryServiceTest.java +++ b/src/test/java/com/dnd/gongmuin/chat_inquiry/service/ChatInquiryServiceTest.java @@ -27,6 +27,7 @@ import com.dnd.gongmuin.chat_inquiry.dto.RejectChatResponse; import com.dnd.gongmuin.chat_inquiry.repository.ChatInquiryRepository; import com.dnd.gongmuin.chatroom.domain.ChatRoom; +import com.dnd.gongmuin.chatroom.repository.ChatMessageRepository; import com.dnd.gongmuin.chatroom.repository.ChatRoomRepository; import com.dnd.gongmuin.common.exception.runtime.ValidationException; import com.dnd.gongmuin.common.fixture.ChatInquiryFixture; @@ -68,6 +69,9 @@ class ChatInquiryServiceTest { @Mock private CreditHistoryService creditHistoryService; + @Mock + private ChatMessageRepository chatMessageRepository; + @InjectMocks private ChatInquiryService chatInquiryService;