Skip to content

Commit

Permalink
refactor: 메세지 전송시 알림 서비스 추가
Browse files Browse the repository at this point in the history
메세지 전송시 알림 서비스 추가
  • Loading branch information
Programmer-may committed Jan 20, 2024
1 parent da37ecb commit 8e8ac80
Showing 1 changed file with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import kr.co.fastcampus.yanabada.domain.chat.repository.ChatRoomRepository;
import kr.co.fastcampus.yanabada.domain.member.entity.Member;
import kr.co.fastcampus.yanabada.domain.member.repository.MemberRepository;
import kr.co.fastcampus.yanabada.domain.notification.dto.ChatNotificationDto;
import kr.co.fastcampus.yanabada.domain.notification.service.NotificationService;
import kr.co.fastcampus.yanabada.domain.product.entity.Product;
import kr.co.fastcampus.yanabada.domain.product.entity.enums.ProductStatus;
import kr.co.fastcampus.yanabada.domain.product.repository.ProductRepository;
Expand All @@ -43,6 +45,8 @@ public class ChatService {

private final ProductRepository productRepository;

private final NotificationService notificationService;

@Transactional
public ChatRoomInfoResponse getOrSaveChatRoom(ChatRoomSaveRequest request) {
Product product = productRepository.getProduct(request.productId());
Expand Down Expand Up @@ -240,6 +244,11 @@ public SendChatMessage saveChatMessage(ReceivedChatMessage message) {
LocalDateTime sendTime = LocalDateTime.now();
checkChatRoomMember(chatRoom, sender);
updateMemberPresenceStatus(chatRoom, sender);
if (chatRoom.getMessages().size() == 0) {
sendFcmAndHistoryNotification(chatRoom, sender, message.content());
} else {
sendFcmNotification(chatRoom, sender, message.content());
}
addMessageToChatRoom(chatRoom, message, sender, sendTime);
return createSendChatMessage(chatRoom, sender, message, sendTime);
}
Expand All @@ -263,4 +272,32 @@ private SendChatMessage createSendChatMessage(
) {
return SendChatMessage.from(chatRoom, sender, message.content(), sendTime);
}

private ChatNotificationDto createChatNotificationDto(
Member sender, Member receiver, ChatRoom chatRoom
) {
return ChatNotificationDto.from(sender, receiver, chatRoom);
}

private void sendFcmAndHistoryNotification(ChatRoom chatRoom, Member sender, String content) {
if (isSeller(sender, chatRoom)) {
notificationService.sendChatCreated(
createChatNotificationDto(sender, chatRoom.getBuyer(), chatRoom),
content
);
} else {
notificationService.sendChatCreated(
createChatNotificationDto(sender, chatRoom.getSeller(), chatRoom),
content
);
}
}

private void sendFcmNotification(ChatRoom chatRoom, Member sender, String content) {
if (isSeller(sender, chatRoom)) {
notificationService.sendChatMessage(sender, chatRoom.getBuyer(), content);
} else {
notificationService.sendChatMessage(sender, chatRoom.getSeller(), content);
}
}
}

0 comments on commit 8e8ac80

Please sign in to comment.