Skip to content

Commit

Permalink
[feat #153] 채팅 요청 자동 거절 알림 생성 (#160)
Browse files Browse the repository at this point in the history
* [feat] : 자동거절 채팅 Dto 추가

* [feat] : 자동거절 채팅 Dto 조회 쿼리 추가

* [feat] : 자동거절 시 알림 생성 추가

* [feat] : 채팅 요청 자동거절 알림타입 추가

* [refactor] : 조회쿼리가 아닌 rejecChatInquiryDtos를 이용하여 ids 가져오도록 변경

* [rename] : 메서드, DTO명 변경

* [test] : 채팅요청 자동거절 단위테스트 수정
  • Loading branch information
dudxo authored Nov 29, 2024
1 parent 1dd4523 commit 8a10d8b
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.dnd.gongmuin.chat_inquiry.dto;

import com.dnd.gongmuin.chat_inquiry.domain.ChatInquiry;
import com.dnd.gongmuin.member.domain.Member;
import com.querydsl.core.annotations.QueryProjection;

public record RejectedChatInquiryDto(
Long chatInquiryId,
Member inquirer,
Member answer
) {
@QueryProjection
public RejectedChatInquiryDto(
ChatInquiry chatInquiry
) {
this(
chatInquiry.getId(),
chatInquiry.getInquirer(),
chatInquiry.getAnswerer()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.data.domain.Slice;

import com.dnd.gongmuin.chat_inquiry.dto.ChatInquiryResponse;
import com.dnd.gongmuin.chat_inquiry.dto.RejectedChatInquiryDto;
import com.dnd.gongmuin.member.domain.Member;

public interface ChatInquiryQueryRepository {
Expand All @@ -14,4 +15,6 @@ public interface ChatInquiryQueryRepository {
List<Long> getAutoRejectedInquirerIds();

void updateChatInquiryStatusRejected();

List<RejectedChatInquiryDto> getAutoRejectedChatInquiries();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.dnd.gongmuin.chat_inquiry.domain.InquiryStatus;
import com.dnd.gongmuin.chat_inquiry.dto.ChatInquiryResponse;
import com.dnd.gongmuin.chat_inquiry.dto.QChatInquiryResponse;
import com.dnd.gongmuin.chat_inquiry.dto.QRejectedChatInquiryDto;
import com.dnd.gongmuin.chat_inquiry.dto.RejectedChatInquiryDto;
import com.dnd.gongmuin.member.domain.Member;
import com.querydsl.core.types.dsl.CaseBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
Expand Down Expand Up @@ -54,6 +56,19 @@ public List<Long> getAutoRejectedInquirerIds() {
.fetch();
}

public List<RejectedChatInquiryDto> getAutoRejectedChatInquiries() {
return queryFactory
.select(new QRejectedChatInquiryDto(
chatInquiry
))
.from(chatInquiry)
.where(
chatInquiry.createdAt.loe(LocalDateTime.now().minusWeeks(1)),
chatInquiry.status.eq(InquiryStatus.PENDING)
)
.fetch();
}

public void updateChatInquiryStatusRejected() {
queryFactory.update(chatInquiry)
.set(chatInquiry.status, InquiryStatus.REJECTED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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.dto.RejectedChatInquiryDto;
import com.dnd.gongmuin.chat_inquiry.exception.ChatInquiryErrorCode;
import com.dnd.gongmuin.chat_inquiry.repository.ChatInquiryRepository;
import com.dnd.gongmuin.chatroom.domain.ChatRoom;
Expand Down Expand Up @@ -128,12 +129,40 @@ public RejectChatResponse rejectChat(Long chatInquiryId, Member answerer) {

@Transactional
public void rejectChatAuto() {
List<Long> rejectedInquirerIds = chatInquiryRepository.getAutoRejectedInquirerIds();
List<RejectedChatInquiryDto> rejectedChatInquiryDtos = chatInquiryRepository.getAutoRejectedChatInquiries();
List<Long> rejectedInquirerIds = getRejectedInquirerIds(rejectedChatInquiryDtos);
chatInquiryRepository.updateChatInquiryStatusRejected();
memberRepository.refundInMemberIds(rejectedInquirerIds, CHAT_REWARD);
creditHistoryService.saveCreditHistoryInMemberIds(
rejectedInquirerIds, CreditType.CHAT_REFUND, CHAT_REWARD
);

autoRejectedChatInquiryNotification(rejectedChatInquiryDtos);
}

private List<Long> getRejectedInquirerIds(List<RejectedChatInquiryDto> rejectedChatInquiryDtos) {
return rejectedChatInquiryDtos.stream()
.map(dto -> dto.inquirer().getId())
.toList();
}

private void autoRejectedChatInquiryNotification(List<RejectedChatInquiryDto> rejectedChatInquiryDtos) {
for (RejectedChatInquiryDto rejectChatInquiry : rejectedChatInquiryDtos) {
eventPublisher.publishEvent( // 채팅 요청자 알림
new NotificationEvent(
NotificationType.AUTO_CHAT_REJECT,
rejectChatInquiry.chatInquiryId(),
rejectChatInquiry.inquirer().getId(),
rejectChatInquiry.inquirer())
);
eventPublisher.publishEvent(
new NotificationEvent( // 채팅 답변자 알림
NotificationType.AUTO_CHAT_REJECT,
rejectChatInquiry.chatInquiryId(),
rejectChatInquiry.answer().getId(),
rejectChatInquiry.answer())
);
}
}

private ChatInquiry getChatInquiryById(Long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public enum NotificationType {
CHOSEN("채택"),
CHAT_REQUEST("채팅신청"),
CHAT_REJECT("채팅거절"),
CHAT_ACCEPT("채팅수락");
CHAT_ACCEPT("채팅수락"),
AUTO_CHAT_REJECT("채팅자동거절");

private final String label;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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.dto.RejectedChatInquiryDto;
import com.dnd.gongmuin.chat_inquiry.repository.ChatInquiryRepository;
import com.dnd.gongmuin.chatroom.domain.ChatRoom;
import com.dnd.gongmuin.chatroom.repository.ChatMessageRepository;
Expand Down Expand Up @@ -286,15 +287,21 @@ void rejectChatWithEventPublish() {
@Test
void rejectChatAuto() {
// given
List<Long> rejectedInquirerIds = List.of(1L, 2L);
given(chatInquiryRepository.getAutoRejectedInquirerIds())
.willReturn(rejectedInquirerIds);
List<RejectedChatInquiryDto> rejectedChatInquiryDtos = List.of(
new RejectedChatInquiryDto(1L, MemberFixture.member(1L), MemberFixture.member(2L)),
new RejectedChatInquiryDto(2L, MemberFixture.member(3L), MemberFixture.member(4L))
);
List<Long> rejectedInquirerIds = rejectedChatInquiryDtos.stream()
.map(dto -> dto.inquirer().getId())
.toList();

given(chatInquiryRepository.getAutoRejectedChatInquiries()).willReturn(rejectedChatInquiryDtos);

// when
chatInquiryService.rejectChatAuto();

// then
verify(chatInquiryRepository).getAutoRejectedInquirerIds();
verify(chatInquiryRepository).getAutoRejectedChatInquiries();
verify(chatInquiryRepository).updateChatInquiryStatusRejected();
verify(memberRepository).refundInMemberIds(rejectedInquirerIds, CHAT_REWARD);
verify(creditHistoryService).saveCreditHistoryInMemberIds(
Expand Down

0 comments on commit 8a10d8b

Please sign in to comment.