Skip to content

Commit

Permalink
[fix #190] 답변자가 스스로에게 채팅 요청 보낼 수 없도록 수정 (#192)
Browse files Browse the repository at this point in the history
* [feat] : 채팅 요청 에러 코드 추가

* [feat] : 채팅 요청 에러 검증 로직 추가

* [test] : 채팅 요청 에러 검증 로직 테스트

* [refactor] : 검증 로직 함수 추출
  • Loading branch information
hyun2371 authored and dudxo committed Jan 14, 2025
1 parent 335eab1 commit 5902032
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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() {
Expand Down

0 comments on commit 5902032

Please sign in to comment.