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

Fix/#118 매칭을 신청했으나, 매칭되지 않은 사용자의 매칭 상태 변경기능 추가 #119

Merged
merged 13 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
13 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/main/java/com/aliens/backend/auth/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,9 @@ public String getFcmToken() {
}

public void applyMatch() {
if (status.equals(MatchingStatus.NOT_APPLIED_MATCHED)) {
if (status == MatchingStatus.NOT_APPLIED_MATCHED) {
status = MatchingStatus.APPLIED_MATCHED;
}
if (status.equals(MatchingStatus.NOT_APPLIED_NOT_MATCHED)) {
} else if (status == MatchingStatus.NOT_APPLIED_NOT_MATCHED) {
status = MatchingStatus.APPLIED_NOT_MATCHED;
}
}
Expand All @@ -138,19 +137,17 @@ public void matched() {
}

public void expireMatch() {
if (status.equals(MatchingStatus.APPLIED_MATCHED)) {
if (status == MatchingStatus.APPLIED_MATCHED) {
status = MatchingStatus.APPLIED_NOT_MATCHED;
}
if (status.equals(MatchingStatus.NOT_APPLIED_MATCHED)) {
} else if (status == MatchingStatus.NOT_APPLIED_MATCHED || status == MatchingStatus.APPLIED_NOT_MATCHED) {
status = MatchingStatus.NOT_APPLIED_NOT_MATCHED;
}
}

public void cancelApplication() {
if (status.equals(MatchingStatus.APPLIED_MATCHED)) {
if (status == MatchingStatus.APPLIED_MATCHED) {
status = MatchingStatus.NOT_APPLIED_MATCHED;
}
if (status.equals(MatchingStatus.APPLIED_NOT_MATCHED)) {
} else if (status == MatchingStatus.APPLIED_NOT_MATCHED) {
status = MatchingStatus.NOT_APPLIED_NOT_MATCHED;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public enum MatchingError implements ErrorCode {
NOT_FOUND_PREFER_LANGUAGE(HttpStatus.NOT_FOUND, "MA4", "선호 언어를 찾을 수 없음"),
INVALID_LANGUAGE_INPUT(HttpStatus.BAD_REQUEST, "MA5", "두 선호 언어가 같을 수 없음"),
DUPLICATE_MATCHING_APPLICATION(HttpStatus.BAD_REQUEST, "MA6", "중복된 매칭 신청"),

;

private final HttpStatus httpStatusCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,8 @@ public boolean hasBlocked(Participant participant) {
public boolean hasPartner() {
return !partners.isEmpty();
}

public void expireMatch() {
member.expireMatch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public void modifyTo(final MatchingApplicationRequest matchingApplicationRequest
this.secondPreferLanguage = matchingApplicationRequest.secondPreferLanguage();
}

public void expireMatch() {
Member member = getMember();
member.expireMatch();
}

public MatchingApplicationId getId() {
return id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ public void matchEach() {
matchedMember.matched();
}

public void expireMatch() {
Member matchingMember = getMatchingMember();
Member matchedMember = getMatchedMember();
matchingMember.expireMatch();
matchedMember.expireMatch();
}

public MatchingRound getMatchingRound() {
return id.getMatchingRound();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

@Repository
public interface MatchingApplicationRepository extends JpaRepository<MatchingApplication, MatchingApplicationId> {
@Query("SELECT ma FROM MatchingApplication ma WHERE ma.id.matchingRound = :matchingRound")
List<MatchingApplication> findAllByMatchingRound(MatchingRound matchingRound);
@Query("SELECT ma FROM MatchingApplication ma WHERE ma.id.matchingRound.round = :round")
List<MatchingApplication> findAllByRound(Long round);

@Query("SELECT ma FROM MatchingApplication ma WHERE ma.id.matchingRound = :matchingRound AND ma.id.member.id = :memberId")
Optional<MatchingApplication> findByMatchingRoundAndMemberId(MatchingRound matchingRound, Long memberId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@
public interface MatchingRoundRepository extends JpaRepository<MatchingRound, Long> {
@Query("SELECT mr FROM MatchingRound mr WHERE mr.round = (SELECT MAX(mr.round) FROM MatchingRound mr)")
Optional<MatchingRound> findCurrentRound();

Optional<MatchingRound> findMatchingRoundByRound(Long round);
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ public List<MatchingResultResponse> findMatchingResult(final LoginMember loginMe
.toList();
}


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)); }
Expand All @@ -92,12 +91,12 @@ private MatchingApplication findMatchedMemberApplicationByRoundAndMemberId(Match
.orElseThrow(() -> new RestApiException(MatchingError.NOT_FOUND_MATCHING_APPLICATION_INFO));
}


@Scheduled(cron = "${matching.round.end}")
@Transactional
public void expireMatching() {
List<MatchingResult> previousMatchingResults = getPreviousMatchingResults();
previousMatchingResults.forEach(MatchingResult::expireMatch);
List<MatchingApplication> previousMatchingApplications = getPreviousMatchingApplications();
previousMatchingApplications.forEach(MatchingApplication::expireMatch);
eventPublisher.expireChatRoom(previousMatchingResults);
}

Expand All @@ -106,8 +105,17 @@ private void saveMatchingResult(final MatchingRound matchingRound, final List<Pa
.flatMap(participant -> participant.partners().stream()
.map(partner -> MatchingResult.from(matchingRound, participant, partner)))
.forEach(this::matchBetween);
eventPublisher.createChatRoom(participants);
eventPublisher.sendNotification(participants);
participants.stream()
.filter(participant -> !participant.hasPartner())
.forEach(Participant::expireMatch);
Comment on lines +108 to +110
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

이번에 추가된 부분입니다~
매칭 되지 않은 사용자들의 매칭 신청을 만료 시키는 로직이에용

if (hasMatchedParticipants(participants)) {
eventPublisher.createChatRoom(participants);
eventPublisher.sendNotification(participants);
}
}

private boolean hasMatchedParticipants(List<Participant> participants) {
return !participants.stream().filter(Participant::hasPartner).toList().isEmpty();
}
Comment on lines +111 to 119
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fcm 알림을 보낼때, 보낼 대상이 한명도 없으면 에러가 터져서 추가한 메서드 입니다!
이번 회차에 매칭된 사용자들이 없으면 채팅방 생성 이벤트 및 알림 이벤트는 호출도 하지 않도록 설계했습니다!


private void checkHasApplied(final List<MatchingResult> matchingResults) {
Expand All @@ -126,7 +134,7 @@ private List<MatchingResult> getMatchingResults(final MatchingRound matchingRoun
}

private List<MatchingApplication> getMatchingApplications(final MatchingRound matchingRound) {
return matchingApplicationRepository.findAllByMatchingRound(matchingRound);
return matchingApplicationRepository.findAllByRound(matchingRound.getRound());
}

private List<MatchingResult> getPreviousMatchingResults() {
Expand All @@ -135,6 +143,12 @@ private List<MatchingResult> getPreviousMatchingResults() {
return matchingResultRepository.findAllByRound(previousRound);
}

private List<MatchingApplication> getPreviousMatchingApplications() {
MatchingRound currentRound = getCurrentRound();
Long previousRound = currentRound.getPreviousRound();
return matchingApplicationRepository.findAllByRound(previousRound);
}

private List<Block> getBlockListByMatchingApplications(final List<MatchingApplication> matchingApplications) {
List<Block> blockHistory = matchingApplications.stream()
.map(MatchingApplication::getMember)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,20 @@ void applyNextMatchAndCancel() {
assertThat(result).isEqualTo(MatchingStatus.NOT_APPLIED_MATCHED.getMessage());
}

@Test
@DisplayName("매칭을 신청했으나, 매칭되지 않은 사용자의 매칭 상태가 정상적으로 변경됨")
void changeMatchingStatusIfNotMatched() {
// given
matchingApplicationService.saveParticipant(loginMember, matchingApplicationRequest);

// when
dummyGenerator.operateMatching();

// then
Member notMatchedMember = getMemberById(member.getId());
assertThat(notMatchedMember.getStatus()).isEqualTo(MatchingStatus.NOT_APPLIED_NOT_MATCHED.getMessage());
}

@Test
@DisplayName("지정 시간 외 매칭 신청시, 에러 발생")
void applyMatchIfNotValidTime() {
Expand Down
Loading