Skip to content

Commit

Permalink
[refactor #193] 채팅 요청 상태 업데이트 함수 수정 (#194)
Browse files Browse the repository at this point in the history
* [rename] : DTO 이름 변경

* [refactor] : 만료된 채팅 요청 아이디를 비교해 상태 변경하도록 수정

* [test] : 아이디를 비교해 상태 변경하도록 수정 -> 테스트 반영

* [refactor] : 스케줄러 함수명 변경

* [feat] : 만료된 채팅 요청 아이디로 벌크 연산 호출

* [test] : 만료된 채팅 요청 아이디로 벌크 연산 -> 테스트 반영

* [refactor] : 로직 변경으로 불필요해진 repository 함수 제거
  • Loading branch information
hyun2371 authored and dudxo committed Jan 14, 2025
1 parent 3e56e54 commit 335eab1
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 52 deletions.
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(
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)
)
.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

0 comments on commit 335eab1

Please sign in to comment.