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

[refactor #193] 채팅 요청 상태 업데이트 함수 수정 #194

Merged
merged 7 commits into from
Jan 14, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import com.dnd.gongmuin.member.domain.Member;
import com.querydsl.core.annotations.QueryProjection;

public record RejectedChatInquiryDto(
public record ExpiredChatInquiryDto(
Copy link
Collaborator

Choose a reason for hiding this comment

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

더 정확한 네이밍이네요!

Long chatInquiryId,
Member inquirer,
Member answer
) {
@QueryProjection
public RejectedChatInquiryDto(
public ExpiredChatInquiryDto(
ChatInquiry chatInquiry
) {
this(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
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.chat_inquiry.dto.ExpiredChatInquiryDto;
import com.dnd.gongmuin.member.domain.Member;

public interface ChatInquiryQueryRepository {
Slice<ChatInquiryResponse> getChatInquiresByMember(Member member, Pageable pageable);

List<Long> getAutoRejectedInquirerIds();
void updateChatInquiryStatusRejected(List<Long> expiredChatInquiryIds, LocalDateTime now);

void updateChatInquiryStatusRejected(LocalDateTime now);

List<RejectedChatInquiryDto> getAutoRejectedChatInquiries();
List<ExpiredChatInquiryDto> getExpiredChatInquires();
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

import com.dnd.gongmuin.chat_inquiry.domain.InquiryStatus;
import com.dnd.gongmuin.chat_inquiry.dto.ChatInquiryResponse;
import com.dnd.gongmuin.chat_inquiry.dto.ExpiredChatInquiryDto;
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.chat_inquiry.dto.QExpiredChatInquiryDto;
import com.dnd.gongmuin.member.domain.Member;
import com.querydsl.core.types.dsl.CaseBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
Expand Down Expand Up @@ -44,21 +44,9 @@ public Slice<ChatInquiryResponse> getChatInquiresByMember(Member member, Pageabl
boolean hasNext = hasNext(pageable.getPageSize(), content);
return new SliceImpl<>(content, pageable, hasNext);
}

public List<Long> getAutoRejectedInquirerIds() {
return queryFactory
.select(chatInquiry.inquirer.id)
.from(chatInquiry)
.where(
chatInquiry.createdAt.loe(LocalDateTime.now().minusWeeks(1)),
chatInquiry.status.eq(InquiryStatus.PENDING)
)
.fetch();
}

public List<RejectedChatInquiryDto> getAutoRejectedChatInquiries() {
public List<ExpiredChatInquiryDto> getExpiredChatInquires() {
return queryFactory
.select(new QRejectedChatInquiryDto(
.select(new QExpiredChatInquiryDto(
chatInquiry
))
.from(chatInquiry)
Expand All @@ -69,13 +57,12 @@ public List<RejectedChatInquiryDto> getAutoRejectedChatInquiries() {
.fetch();
}

public void updateChatInquiryStatusRejected(LocalDateTime now) {
public void updateChatInquiryStatusRejected(List<Long> expiredChatInquiryIds, LocalDateTime now) {
queryFactory.update(chatInquiry)
.set(chatInquiry.status, InquiryStatus.REJECTED)
.set(chatInquiry.updatedAt, now)
.where(
chatInquiry.createdAt.loe(LocalDateTime.now().minusWeeks(1)),
chatInquiry.status.eq(InquiryStatus.PENDING)
chatInquiry.id.in(expiredChatInquiryIds)
Copy link
Collaborator

Choose a reason for hiding this comment

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

조건절을 개선해서 속도가 더욱 빨라지겠네요 👍

)
.execute();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public class ChatInquiryScheduler {
@Transactional
@Scheduled(cron = "0 0 0 * * *", zone = "Asia/Seoul")
public void rejectChatInquiry() {
chatInquiryService.rejectChatAuto(LocalDateTime.now());
chatInquiryService.autoRejectChatInquiry(LocalDateTime.now());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,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.dto.ExpiredChatInquiryDto;
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 @@ -133,16 +133,13 @@ public RejectChatResponse rejectChat(Long chatInquiryId, Member answerer) {
}

@Transactional
public void rejectChatAuto(LocalDateTime now) {
List<RejectedChatInquiryDto> rejectedChatInquiryDtos = chatInquiryRepository.getAutoRejectedChatInquiries();
List<Long> rejectedInquirerIds = getRejectedInquirerIds(rejectedChatInquiryDtos);
chatInquiryRepository.updateChatInquiryStatusRejected(now);
memberRepository.refundInMemberIds(rejectedInquirerIds, CHAT_REWARD);
creditHistoryService.saveCreditHistoryInMemberIds(
rejectedInquirerIds, CreditType.CHAT_REFUND, CHAT_REWARD
);
public void autoRejectChatInquiry(LocalDateTime now) {
List<ExpiredChatInquiryDto> expiredChatInquiryDtos = chatInquiryRepository.getExpiredChatInquires();
List<Long> expiredChatInquiryIds = getExpiredChatInquiryIds(expiredChatInquiryDtos);

autoRejectedChatInquiryNotification(rejectedChatInquiryDtos);
chatInquiryRepository.updateChatInquiryStatusRejected(expiredChatInquiryIds, now);
refundAutoRejectedInquiry(expiredChatInquiryDtos);
notifyAutoRejectedInquiry(expiredChatInquiryDtos);
}

private void validateChatAnswerer(Long questionPostId, Member answerer) {
Expand All @@ -156,14 +153,28 @@ private void saveInquirerCreditHistory(Member inquirer) {
creditHistoryService.saveCreditHistory(CreditType.CHAT_REQUEST, CHAT_REWARD, inquirer);
}

private List<Long> getRejectedInquirerIds(List<RejectedChatInquiryDto> rejectedChatInquiryDtos) {
return rejectedChatInquiryDtos.stream()
private List<Long> getExpiredChatInquiryIds(List<ExpiredChatInquiryDto> expiredChatInquiryDtos) {
return expiredChatInquiryDtos.stream()
.map(ExpiredChatInquiryDto::chatInquiryId)
.toList();
}

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

private void autoRejectedChatInquiryNotification(List<RejectedChatInquiryDto> rejectedChatInquiryDtos) {
for (RejectedChatInquiryDto rejectChatInquiry : rejectedChatInquiryDtos) {
private void refundAutoRejectedInquiry(List<ExpiredChatInquiryDto> expiredChatInquiryDtos) {
List<Long> rejectedInquirerIds = getRejectedInquirerIds(expiredChatInquiryDtos);
memberRepository.refundInMemberIds(rejectedInquirerIds, CHAT_REWARD);
creditHistoryService.saveCreditHistoryInMemberIds(
rejectedInquirerIds, CreditType.CHAT_REFUND, CHAT_REWARD
);
}

private void notifyAutoRejectedInquiry(List<ExpiredChatInquiryDto> expiredChatInquiryDtos) {
for (ExpiredChatInquiryDto rejectChatInquiry : expiredChatInquiryDtos) {
eventPublisher.publishEvent( // 채팅 요청자 알림
new NotificationEvent(
NotificationType.AUTO_CHAT_REJECT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void getChatInquiresByMember() {
);
}

@DisplayName("요청중인 채팅방이 일주일이 지나면, 채팅방 상태를 거절함으로 바꾼다.")
@DisplayName("만료된 채팅 요청에 대해, 채팅 요청 상태를 거절함으로 바꾼다.")
@Test
void updateChatInquiryStatusRejected() {
//given
Expand All @@ -85,7 +85,10 @@ void updateChatInquiryStatusRejected() {
ReflectionTestUtils.setField(chatInquiries.get(0), "createdAt", LocalDateTime.now().minusWeeks(1));

//when
chatInquiryRepository.updateChatInquiryStatusRejected(LocalDateTime.now());
chatInquiryRepository.updateChatInquiryStatusRejected(
List.of(chatInquiries.get(0).getId()),
LocalDateTime.now()
);

em.flush();
em.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import com.dnd.gongmuin.chat_inquiry.dto.ChatInquiryResponse;
import com.dnd.gongmuin.chat_inquiry.dto.CreateChatInquiryRequest;
import com.dnd.gongmuin.chat_inquiry.dto.CreateChatInquiryResponse;
import com.dnd.gongmuin.chat_inquiry.dto.ExpiredChatInquiryDto;
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 @@ -326,22 +326,25 @@ void rejectChatAuto() {
// given
final LocalDateTime now = LocalDateTime.now();

List<RejectedChatInquiryDto> rejectedChatInquiryDtos = List.of(
new RejectedChatInquiryDto(1L, MemberFixture.member(1L), MemberFixture.member(2L)),
new RejectedChatInquiryDto(2L, MemberFixture.member(3L), MemberFixture.member(4L))
List<ExpiredChatInquiryDto> expiredChatInquiryDtos = List.of(
new ExpiredChatInquiryDto(1L, MemberFixture.member(1L), MemberFixture.member(2L)),
new ExpiredChatInquiryDto(2L, MemberFixture.member(3L), MemberFixture.member(4L))
);
List<Long> rejectedInquirerIds = rejectedChatInquiryDtos.stream()
List<Long> expiredChatInquiryIds = expiredChatInquiryDtos.stream()
.map(ExpiredChatInquiryDto::chatInquiryId)
.toList();
List<Long> rejectedInquirerIds = expiredChatInquiryDtos.stream()
.map(dto -> dto.inquirer().getId())
.toList();

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

// when
chatInquiryService.rejectChatAuto(now);
chatInquiryService.autoRejectChatInquiry(now);

// then
verify(chatInquiryRepository).getAutoRejectedChatInquiries();
verify(chatInquiryRepository).updateChatInquiryStatusRejected(now);
verify(chatInquiryRepository).getExpiredChatInquires();
verify(chatInquiryRepository).updateChatInquiryStatusRejected(expiredChatInquiryIds,now);
verify(memberRepository).refundInMemberIds(rejectedInquirerIds, CHAT_REWARD);
verify(creditHistoryService).saveCreditHistoryInMemberIds(
rejectedInquirerIds, CreditType.CHAT_REFUND, CHAT_REWARD
Expand Down
Loading