Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat #149] 채팅 요청 수락 시 채팅 요청 메시지를 채팅방 메시지에 저장 #150

Merged
merged 10 commits into from
Nov 21, 2024
Merged
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
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

음..시간이 지나 자동 거절 되었을 때 알람을 안보내도 될까요?
조금 애매하군요

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

알림이 있는게 더 자연스러울 것 같아요~

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

음..넵 한번 추가해보겠습니다!

}

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())) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

반영 감사합니다~!!

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
Loading