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 #153] 채팅 요청 자동 거절 알림 생성 #160

Merged
merged 7 commits into from
Nov 29, 2024
Merged
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 RejectChatInquiryDto(
Copy link
Member

Choose a reason for hiding this comment

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

거절된거니까 Rejected가 더 적절할 것 같아요

Long chatInquiryId,
Member inquirer,
Member answer
) {
@QueryProjection
public RejectChatInquiryDto(
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.RejectChatInquiryDto;
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<RejectChatInquiryDto> getAutoRejectedChatInquiry();
Copy link
Member

Choose a reason for hiding this comment

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

리스트 조회 메서드니까 복수형으로 Inquires가 되어야 할 것 같습니다~

}
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.QRejectChatInquiryDto;
import com.dnd.gongmuin.chat_inquiry.dto.RejectChatInquiryDto;
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<RejectChatInquiryDto> getAutoRejectedChatInquiry() {
return queryFactory
.select(new QRejectChatInquiryDto(
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 @@ -16,6 +16,7 @@
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.RejectChatInquiryDto;
import com.dnd.gongmuin.chat_inquiry.dto.RejectChatResponse;
import com.dnd.gongmuin.chat_inquiry.exception.ChatInquiryErrorCode;
import com.dnd.gongmuin.chat_inquiry.repository.ChatInquiryRepository;
Expand Down Expand Up @@ -129,11 +130,33 @@ public RejectChatResponse rejectChat(Long chatInquiryId, Member answerer) {
@Transactional
public void rejectChatAuto() {
List<Long> rejectedInquirerIds = chatInquiryRepository.getAutoRejectedInquirerIds();
List<RejectChatInquiryDto> rejectChatInquiryDtos = chatInquiryRepository.getAutoRejectedChatInquiry();
Copy link
Member

Choose a reason for hiding this comment

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

List<Long> rejectedInquirerIds = rejectChatInquiryDtos.stream()
			.map(dto -> dto.inquirer().getId())
			.toList()

위 코드를 추가하면 쿼리를 두 번 날릴 필요없이, 하나의 쿼리만 사용할 수 있을 것 같아요!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

안그래도 쿼리를 줄일 수 없나 고민했었는데 좋은 방법이네요!

chatInquiryRepository.updateChatInquiryStatusRejected();
memberRepository.refundInMemberIds(rejectedInquirerIds, CHAT_REWARD);
creditHistoryService.saveCreditHistoryInMemberIds(
rejectedInquirerIds, CreditType.CHAT_REFUND, CHAT_REWARD
);

autoRejectedChatInquiryNotification(rejectChatInquiryDtos);
}

private void autoRejectedChatInquiryNotification(List<RejectChatInquiryDto> rejectChatInquiryDtos) {
for (RejectChatInquiryDto rejectChatInquiry : rejectChatInquiryDtos) {
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