From 5902032448cbbd7eee6ffd9e69d784262ff26235 Mon Sep 17 00:00:00 2001 From: Son Gahyun <77109954+hyun2371@users.noreply.github.com> Date: Tue, 14 Jan 2025 20:54:47 +0900 Subject: [PATCH] =?UTF-8?q?[fix=20#190]=20=EB=8B=B5=EB=B3=80=EC=9E=90?= =?UTF-8?q?=EA=B0=80=20=EC=8A=A4=EC=8A=A4=EB=A1=9C=EC=97=90=EA=B2=8C=20?= =?UTF-8?q?=EC=B1=84=ED=8C=85=20=EC=9A=94=EC=B2=AD=20=EB=B3=B4=EB=82=BC=20?= =?UTF-8?q?=EC=88=98=20=EC=97=86=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95=20?= =?UTF-8?q?(#192)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [feat] : 채팅 요청 에러 코드 추가 * [feat] : 채팅 요청 에러 검증 로직 추가 * [test] : 채팅 요청 에러 검증 로직 테스트 * [refactor] : 검증 로직 함수 추출 --- .../exception/ChatInquiryErrorCode.java | 3 ++- .../service/ChatInquiryService.java | 15 +++++++++-- .../service/ChatInquiryServiceTest.java | 27 ++++++++++++++++++- 3 files changed, 41 insertions(+), 4 deletions(-) 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 index f7a27e1a..b159bdf5 100644 --- a/src/main/java/com/dnd/gongmuin/chat_inquiry/exception/ChatInquiryErrorCode.java +++ b/src/main/java/com/dnd/gongmuin/chat_inquiry/exception/ChatInquiryErrorCode.java @@ -13,7 +13,8 @@ public enum ChatInquiryErrorCode implements ErrorCode { UNAUTHORIZED_REQUEST("채팅 요청을 수락을 하거나 거절할 권한이 없습니다.", "CI_002"), UNABLE_TO_CHANGE_STATUS("이미 수락했거나 거절한 요청입니다.", "CI_003"), NOT_FOUND_STATUS("채팅방 상태값을 올바르게 입력해주세요.", "CI_004"), - NOT_EXISTS_ANSWERER("해당 아이디의 답변자가 해당 게시글에 존재하지 않습니다.", "CI_005"); + NOT_EXISTS_ANSWERER("해당 아이디의 답변자가 해당 게시글에 존재하지 않습니다.", "CI_005"), + SELF_INQUIRY_NOT_ALLOWED("자기 자신에게 채팅 요청을 보낼 수 없습니다.", "CI_006"); 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 23cc3db3..f6a38536 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 @@ -62,7 +62,7 @@ public class ChatInquiryService { public CreateChatInquiryResponse createChatInquiry(CreateChatInquiryRequest request, Member inquirer) { QuestionPost questionPost = getQuestionPostById(request.questionPostId()); Member answerer = getMemberById(request.answererId()); - validateChatAnswerer(request.questionPostId(), answerer); + validateChatAnswerer(request.questionPostId(), inquirer.getId(), answerer); ChatInquiry chatInquiry = chatInquiryRepository.save( ChatInquiryMapper.toChatInquiry(questionPost, inquirer, answerer, request.inquiryMessage()) ); @@ -142,12 +142,23 @@ public void autoRejectChatInquiry(LocalDateTime now) { notifyAutoRejectedInquiry(expiredChatInquiryDtos); } - private void validateChatAnswerer(Long questionPostId, Member answerer) { + private void validateChatAnswerer(Long questionPostId, Long inquirerId, Member answerer) { + validateIfAnswererExists(questionPostId, answerer); + validateIfNotSelfInquiry(inquirerId, answerer); + } + + private void validateIfAnswererExists(Long questionPostId, Member answerer) { if (!answerRepository.existsByQuestionPostIdAndMember(questionPostId, answerer)) { throw new ValidationException(ChatInquiryErrorCode.NOT_EXISTS_ANSWERER); } } + private void validateIfNotSelfInquiry(Long inquirerId, Member answerer) { + if (Objects.equals(answerer.getId(), inquirerId)) { + throw new ValidationException(ChatInquiryErrorCode.SELF_INQUIRY_NOT_ALLOWED); + } + } + private void saveInquirerCreditHistory(Member inquirer) { memberRepository.save(inquirer); creditHistoryService.saveCreditHistory(CreditType.CHAT_REQUEST, CHAT_REWARD, inquirer); 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 27d739bd..dffd4619 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 @@ -152,7 +152,6 @@ void createChatInquiry_fails2() { //given Member inquirer = MemberFixture.member(1L); Member answerer = MemberFixture.member(2L); - ReflectionTestUtils.setField(inquirer, "credit", CHAT_REWARD); QuestionPost questionPost = QuestionPostFixture.questionPost(inquirer); CreateChatInquiryRequest request = new CreateChatInquiryRequest( questionPost.getId(), @@ -173,6 +172,32 @@ void createChatInquiry_fails2() { .hasMessageContaining(ChatInquiryErrorCode.NOT_EXISTS_ANSWERER.getMessage()); } + @DisplayName("[답변자는 스스로에게 채팅 요청을 할 수 없다.]") + @Test + void createChatInquiry_fails3() { + //given + Member questioner = MemberFixture.member(1L); + Member answerer = MemberFixture.member(2L); + QuestionPost questionPost = QuestionPostFixture.questionPost(questioner); + CreateChatInquiryRequest request = new CreateChatInquiryRequest( + questionPost.getId(), + answerer.getId(), + INQUIRY_MESSAGE + ); + + given(questionPostRepository.findById(questionPost.getId())) + .willReturn(Optional.of(questionPost)); + given(memberRepository.findById(answerer.getId())) + .willReturn(Optional.of(answerer)); + given(answerRepository.existsByQuestionPostIdAndMember(questionPost.getId(), answerer)) + .willReturn(true); + + //when & then + assertThatThrownBy(() -> chatInquiryService.createChatInquiry(request, answerer)) + .isInstanceOf(ValidationException.class) + .hasMessageContaining(ChatInquiryErrorCode.SELF_INQUIRY_NOT_ALLOWED.getMessage()); + } + @DisplayName("[채팅 요청 아이디로 채팅 요청 상세를 조회할 수 있다.]") @Test void getChatInquiryById() {