Skip to content

Commit

Permalink
Merge pull request #145 from Re-4aliens/develop
Browse files Browse the repository at this point in the history
2024-07-30 [Release Note]
  • Loading branch information
mjj111 authored Aug 23, 2024
2 parents 31888e4 + 5fa4c22 commit 2b3343a
Show file tree
Hide file tree
Showing 24 changed files with 124 additions and 41 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/aliens/backend/auth/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ public void cancelApplication() {
}
}

public boolean hasPartner() {
return status == MatchingStatus.NOT_APPLIED_MATCHED || status == MatchingStatus.APPLIED_MATCHED;
}

@Override
public String toString() {
return String.format("email: %s, role : %s", this.email, this.role);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ public interface ChatRoomRepository extends JpaRepository<ChatRoom, Long> {
@Modifying
@Query("UPDATE ChatRoom c SET c.status = com.aliens.backend.chat.domain.ChatRoomStatus.CLOSED")
void expireAllChatRooms();

@Modifying
@Query("UPDATE ChatRoom c SET c.status = com.aliens.backend.chat.domain.ChatRoomStatus.OPENED WHERE c.status = com.aliens.backend.chat.domain.ChatRoomStatus.WAITING")
void openWaitingChatRooms();
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ public List<Message> findMessages(Long chatRoomID, String lastMessageId) {
public List<ChatMessageSummary> aggregateMessageSummaries(List<Long> chatRoomIds, Long memberId) {
AggregationOperation match = Aggregation.match(Criteria.where("roomId").in(chatRoomIds));
AggregationOperation group = Aggregation.group("roomId")
.last("roomId").as("roomId")
.last("content").as("lastMessageContent")
.last("sendTime").as("lastMessageTime")
.sum(ConditionalOperators.when(Criteria.where("receiverId").is(memberId).and("isRead").is(false)).then(1).otherwise(0)).as("unreadCount");
.sum(ConditionalOperators.when(Criteria.where("receiverId").is(memberId).and("isRead").is(false)).then(1).otherwise(0)).as("numberOfUnreadMessages");
Aggregation aggregation = Aggregation.newAggregation(match, group);
AggregationResults<ChatMessageSummary> results = mongoTemplate.aggregate(aggregation, "message", ChatMessageSummary.class);
return new ArrayList<>(results.getMappedResults());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.aliens.backend.chat.service;

import com.aliens.backend.chat.domain.ChatRoom;
import com.aliens.backend.chat.domain.ChatRoomStatus;
import com.aliens.backend.global.exception.RestApiException;
import com.aliens.backend.global.response.error.ChatError;
import org.springframework.stereotype.Component;
Expand All @@ -11,21 +12,32 @@
public class ChatAuthValidator {
public void validateRoomFromTopic(String topic, List<ChatRoom> validChatRooms) {
Long roomId = getRoomIdFromTopic(topic);
if(!isValidRoom(validChatRooms, roomId)) {
if(!isUserInRoom(validChatRooms, roomId)) {
throw new RestApiException(ChatError.INVALID_ROOM_ACCESS);
}
}

public void validateRoom(Long roomId, List<ChatRoom> validChatRooms) {
if(!isValidRoom(validChatRooms, roomId)) {
if (!isUserInRoom(validChatRooms, roomId)) {
throw new RestApiException(ChatError.INVALID_ROOM_ACCESS);
}
if (!isRoomOpened(validChatRooms, roomId)) {
throw new RestApiException(ChatError.ROOM_NOT_OPEN);
}
}

private boolean isValidRoom (List<ChatRoom> validChatRooms, Long roomId) {
private boolean isUserInRoom(List<ChatRoom> validChatRooms, Long roomId) {
return validChatRooms.stream().anyMatch(chatRoom -> chatRoom.getId().equals(roomId));
}

private boolean isRoomOpened(List<ChatRoom> validChatRooms, Long roomId) {
return validChatRooms.stream()
.filter(chatRoom -> chatRoom.getId().equals(roomId))
.findFirst()
.map(chatRoom -> chatRoom.getStatus().equals(ChatRoomStatus.OPENED))
.orElse(false);
}

private long getRoomIdFromTopic(String topic) {
return Long.parseLong(topic.split("/")[2]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;
Expand Down Expand Up @@ -174,4 +175,10 @@ private void expireAllChatRooms() {
private ChatRoom findChatRoomsById(final Long chatRoomId) {
return chatRoomRepository.findById(chatRoomId).orElseThrow(() -> new RestApiException(ChatError.CHAT_ROOM_NOT_FOUND));
}

@Scheduled(cron = "${matching.round.update-date}")
@Transactional
public void openWaitingChatRooms() {
chatRoomRepository.openWaitingChatRooms();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ public enum ChatError implements ErrorCode {
INVALID_MESSAGE_TYPE(HttpStatus.BAD_REQUEST, "CH1", "메시지 타입이 잘못되었습니다."),
INVALID_ROOM_ACCESS(HttpStatus.FORBIDDEN, "CH2", "채팅방 접근 권한이 없습니다."),
INVALID_REPORT_CATEGORY(HttpStatus.BAD_REQUEST, "CH3", "신고 카테고리가 잘못되었습니다."),
CHAT_ROOM_NOT_FOUND(HttpStatus.NOT_FOUND, "CH4", "채팅방을 찾을 수 없습니다.")
CHAT_ROOM_NOT_FOUND(HttpStatus.NOT_FOUND, "CH4", "채팅방을 찾을 수 없습니다."),
ROOM_NOT_OPEN(HttpStatus.FORBIDDEN, "CH5", "채팅방이 열리지 않았습니다.")
;

private final HttpStatus httpStatusCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.aliens.backend.mathcing.business;

import com.aliens.backend.global.property.MatchingRuleProperties;
import com.aliens.backend.mathcing.business.model.*;
import com.aliens.backend.mathcing.business.model.LanguageQueue;
import com.aliens.backend.mathcing.business.model.MatchingTypeGroup;
import com.aliens.backend.mathcing.business.model.Participant;
import com.aliens.backend.mathcing.business.model.ParticipantGroup;
import com.aliens.backend.mathcing.controller.dto.request.MatchingOperateRequest;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.List;

@Component
public class MatchingBusiness {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.aliens.backend.auth.domain.Member;
import com.aliens.backend.block.domain.Block;
import com.aliens.backend.global.response.error.MatchingError;
import com.aliens.backend.global.exception.RestApiException;
import com.aliens.backend.global.response.error.MatchingError;
import com.aliens.backend.mathcing.domain.MatchingApplication;
import com.aliens.backend.mathcing.domain.MatchingResult;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import com.aliens.backend.auth.controller.dto.LoginMember;
import com.aliens.backend.global.config.resolver.Login;
import com.aliens.backend.global.response.success.MatchingSuccess;
import com.aliens.backend.global.response.SuccessResponse;
import com.aliens.backend.global.response.success.MatchingSuccess;
import com.aliens.backend.mathcing.controller.dto.response.MatchingResultResponse;
import com.aliens.backend.mathcing.service.MatchingProcessService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.aliens.backend.mathcing.controller.dto.response;

import com.aliens.backend.mathcing.domain.MatchingApplication;
import com.aliens.backend.mathcing.business.model.Language;
import com.aliens.backend.mathcing.domain.MatchingApplication;

public record MatchingApplicationResponse(
Long matchingRound,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.aliens.backend.chat.domain.ChatRoom;
import com.aliens.backend.chat.domain.ChatRoomStatus;
import com.aliens.backend.mathcing.business.model.Language;
import com.aliens.backend.mathcing.business.model.Relationship;
import com.aliens.backend.mathcing.domain.MatchingApplication;
import com.aliens.backend.mathcing.domain.MatchingResult;
import com.aliens.backend.mathcing.business.model.Relationship;
import com.aliens.backend.member.controller.dto.response.MemberPageResponse;

public record MatchingResultResponse(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.aliens.backend.mathcing.domain;

import com.aliens.backend.auth.domain.Member;
import com.aliens.backend.mathcing.business.model.Language;
import com.aliens.backend.mathcing.controller.dto.request.MatchingApplicationRequest;
import com.aliens.backend.mathcing.domain.id.MatchingApplicationId;
import com.aliens.backend.mathcing.business.model.Language;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import com.aliens.backend.auth.domain.Member;
import com.aliens.backend.mathcing.business.model.Participant;
import com.aliens.backend.mathcing.business.model.Partner;
import com.aliens.backend.mathcing.domain.id.MatchingResultId;
import com.aliens.backend.mathcing.business.model.Relationship;
import com.aliens.backend.member.controller.dto.MemberPage;
import com.aliens.backend.mathcing.domain.id.MatchingResultId;
import com.aliens.backend.member.controller.dto.response.MemberPageResponse;
import jakarta.persistence.*;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import org.springframework.data.domain.Persistable;

@Entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import com.aliens.backend.auth.domain.Member;
import com.aliens.backend.mathcing.domain.MatchingRound;
import jakarta.persistence.*;
import jakarta.persistence.Embeddable;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;

import java.io.Serializable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import com.aliens.backend.auth.domain.Member;
import com.aliens.backend.mathcing.domain.MatchingRound;
import jakarta.persistence.*;
import jakarta.persistence.Embeddable;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;

import java.io.Serializable;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.aliens.backend.mathcing.domain.repository;

import com.aliens.backend.mathcing.domain.MatchingResult;
import com.aliens.backend.mathcing.domain.MatchingRound;
import com.aliens.backend.mathcing.domain.id.MatchingResultId;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -15,6 +14,6 @@ public interface MatchingResultRepository extends JpaRepository<MatchingResult,
List<MatchingResult> findAllByRound(Long round);

@Query("SELECT mr FROM MatchingResult mr " +
"WHERE mr.id.matchingRound = :matchingRound AND mr.id.matchingMember.id = :memberId")
List<MatchingResult> findAllByMatchingRoundAndMemberId(MatchingRound matchingRound, Long memberId);
"WHERE mr.id.matchingRound.round = :round AND mr.id.matchingMember.id = :memberId")
List<MatchingResult> findAllByMatchingRoundAndMemberId(Long round, Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.aliens.backend.auth.controller.dto.LoginMember;
import com.aliens.backend.auth.domain.Member;
import com.aliens.backend.auth.domain.repository.MemberRepository;
import com.aliens.backend.global.response.error.MatchingError;
import com.aliens.backend.global.exception.RestApiException;
import com.aliens.backend.global.response.error.MatchingError;
import com.aliens.backend.global.response.error.MemberError;
import com.aliens.backend.global.response.success.MatchingSuccess;
import com.aliens.backend.mathcing.controller.dto.request.MatchingApplicationRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

import com.aliens.backend.auth.controller.dto.LoginMember;
import com.aliens.backend.auth.domain.Member;
import com.aliens.backend.auth.domain.repository.MemberRepository;
import com.aliens.backend.block.domain.Block;
import com.aliens.backend.block.domain.repository.BlockRepository;
import com.aliens.backend.chat.controller.dto.event.ChatRoomExpireEvent;
import com.aliens.backend.chat.domain.ChatParticipant;
import com.aliens.backend.chat.domain.ChatRoom;
import com.aliens.backend.chat.domain.repository.ChatParticipantRepository;
import com.aliens.backend.global.response.error.MatchingError;
import com.aliens.backend.global.exception.RestApiException;
import com.aliens.backend.global.response.error.ChatError;
import com.aliens.backend.global.response.error.MatchingError;
import com.aliens.backend.global.response.error.MemberError;
import com.aliens.backend.mathcing.business.MatchingBusiness;
import com.aliens.backend.mathcing.business.model.Participant;
import com.aliens.backend.mathcing.controller.dto.request.MatchingOperateRequest;
import com.aliens.backend.mathcing.controller.dto.response.MatchingResultResponse;
import com.aliens.backend.mathcing.domain.MatchingApplication;
Expand All @@ -19,13 +22,13 @@
import com.aliens.backend.mathcing.domain.repository.MatchingApplicationRepository;
import com.aliens.backend.mathcing.domain.repository.MatchingResultRepository;
import com.aliens.backend.mathcing.domain.repository.MatchingRoundRepository;
import com.aliens.backend.mathcing.business.model.Participant;
import com.aliens.backend.mathcing.service.event.MatchingEventPublisher;
import com.aliens.backend.member.controller.dto.response.MemberPageResponse;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Clock;
import java.util.List;

@Service
Expand All @@ -37,21 +40,27 @@ public class MatchingProcessService {
private final BlockRepository blockRepository;
private final MatchingEventPublisher eventPublisher;
private final ChatParticipantRepository chatParticipantRepository;
private final MemberRepository memberRepository;
private final Clock clock;

public MatchingProcessService(final MatchingBusiness matchingBusiness,
final MatchingRoundRepository matchingRoundRepository,
final MatchingApplicationRepository matchingApplicationRepository,
final MatchingResultRepository matchingResultRepository,
final BlockRepository blockRepository,
final MatchingEventPublisher eventPublisher,
final ChatParticipantRepository chatParticipantRepository) {
final ChatParticipantRepository chatParticipantRepository,
final MemberRepository memberRepository,
final Clock clock) {
this.matchingRoundRepository = matchingRoundRepository;
this.matchingApplicationRepository = matchingApplicationRepository;
this.matchingResultRepository = matchingResultRepository;
this.matchingBusiness = matchingBusiness;
this.blockRepository = blockRepository;
this.eventPublisher = eventPublisher;
this.chatParticipantRepository = chatParticipantRepository;
this.memberRepository = memberRepository;
this.clock = clock;
}

@Scheduled(cron = "${matching.round.start}")
Expand Down Expand Up @@ -83,9 +92,21 @@ public List<MatchingResultResponse> findMatchingResult(final LoginMember loginMe
.toList();
}

private List<MatchingResult> getMatchingResults(final MatchingRound matchingRound, final LoginMember loginMember) {
Member member = getMember(loginMember);
List<MatchingResult> matchingResults = matchingResultRepository
.findAllByMatchingRoundAndMemberId(matchingRound.getRound(), loginMember.memberId());
if (member.hasPartner() && matchingResults.isEmpty()) {
return matchingResultRepository
.findAllByMatchingRoundAndMemberId(matchingRound.getPreviousRound(), loginMember.memberId());
}
return matchingResults;
}

private ChatParticipant getChatParticipant(final LoginMember loginMember, final MatchingResult result) {
return chatParticipantRepository.findByMemberIdAndMatchedMemberId(loginMember.memberId(), result.getMatchedMemberId())
.orElseThrow(() -> new RestApiException(MatchingError.NOT_FOUND_MATCHING_APPLICATION_INFO)); }
.orElseThrow(() -> new RestApiException(ChatError.CHAT_ROOM_NOT_FOUND));
}

private MatchingApplication findMatchedMemberApplicationByRoundAndMemberId(MatchingResult result) {
return matchingApplicationRepository.findByMatchingRoundAndMemberId(result.getMatchingRound(), result.getMatchedMemberId())
Expand Down Expand Up @@ -129,8 +150,9 @@ private MatchingRound getCurrentRound() {
.orElseThrow(()-> new RestApiException(MatchingError.NOT_FOUND_MATCHING_ROUND));
}

private List<MatchingResult> getMatchingResults(final MatchingRound matchingRound, final LoginMember loginMember) {
return matchingResultRepository.findAllByMatchingRoundAndMemberId(matchingRound, loginMember.memberId());
private Member getMember(LoginMember loginMember) {
return memberRepository.findById(loginMember.memberId())
.orElseThrow(() -> new RestApiException(MemberError.NULL_MEMBER));
}

private List<MatchingApplication> getMatchingApplications(final MatchingRound matchingRound) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.aliens.backend.global.exception.RestApiException;
import com.aliens.backend.global.response.error.MatchingError;
import com.aliens.backend.global.validator.LanguageCheck;
import com.aliens.backend.mathcing.controller.dto.request.MatchingApplicationRequest;
import com.aliens.backend.mathcing.business.model.Language;
import com.aliens.backend.mathcing.controller.dto.request.MatchingApplicationRequest;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.aliens.backend.chat.controller.dto.request.ReadRequest;
import com.aliens.backend.chat.controller.dto.response.ReadResponse;
import com.aliens.backend.chat.domain.ChatRoom;
import com.aliens.backend.chat.domain.ChatRoomStatus;
import com.aliens.backend.chat.domain.Message;
import com.aliens.backend.chat.domain.MessageType;
import com.aliens.backend.global.BaseIntegrationTest;
Expand Down Expand Up @@ -180,6 +181,7 @@ private MessageSendRequest createMessageSendRequest(Long roomId) {
private void setValidRooms() {
ChatRoom chatroom = mock(ChatRoom.class);
when(chatroom.getId()).thenReturn(authorizedRoomId);
when(chatroom.getStatus()).thenReturn(ChatRoomStatus.OPENED);
List<ChatRoom> chatRooms = new ArrayList<>();
chatRooms.add(chatroom);
when(chatService.getChatRooms(member.getId())).thenReturn(chatRooms);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.payload.PayloadDocumentation.*;
import static org.springframework.restdocs.request.RequestDocumentation.partWithName;
import static org.springframework.restdocs.request.RequestDocumentation.requestParts;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down
Loading

0 comments on commit 2b3343a

Please sign in to comment.