Skip to content

Commit

Permalink
[feat #149] 채팅 요청 수락 시 채팅 요청 메시지를 채팅방 메시지에 저장 (#150)
Browse files Browse the repository at this point in the history
* [refactor] : 멤버 비교에 equals 오버라이딩 메서드 사용

* [feat] : 채팅, 채팅 요청 에러코드 분리

* [feat] : 채팅, 채팅 요청 에러코드 분리->서비스 코드 반영

* [feat] : 채팅, 채팅 요청 에러코드 분리->엔티티 코드 반영

* [feat] : 채팅방 생성 시 기존 요청 메시지 -> 채팅 메시지에 저장

* [test] : 채팅방 생성 시 기존 요청 메시지 -> 채팅 메시지에 저장 (테스트 반영)

* [refactor] : 채팅 요청 조회 메서드명 수정

* [style] : 코드 리포멧팅

* [refactor] : error 메시지 수정

* [fix] : error 메시지 오류 수정
  • Loading branch information
hyun2371 authored Nov 21, 2024
1 parent 2727bc4 commit 503175d
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -77,15 +77,15 @@ 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);
}

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -77,14 +80,17 @@ public PageResponse<ChatInquiryResponse> 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);

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())
Expand All @@ -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();
Expand All @@ -113,17 +119,19 @@ public void rejectChatAuto() {
List<Long> 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);
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/dnd/gongmuin/chatroom/dto/ChatMessageMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 18 additions & 4 deletions src/main/java/com/dnd/gongmuin/member/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,6 +69,9 @@ class ChatInquiryServiceTest {
@Mock
private CreditHistoryService creditHistoryService;

@Mock
private ChatMessageRepository chatMessageRepository;

@InjectMocks
private ChatInquiryService chatInquiryService;

Expand Down

0 comments on commit 503175d

Please sign in to comment.