Skip to content

Commit

Permalink
Merge pull request #119 from Re-4aliens/fix/#118_change-matching-stat…
Browse files Browse the repository at this point in the history
…us-if-not-matched

Fix/#118 매칭을 신청했으나, 매칭되지 않은 사용자의 매칭 상태 변경기능 추가
  • Loading branch information
mjj111 authored Jun 10, 2024
2 parents 151510e + f9f4d12 commit e57e65a
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 27 deletions.
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);
if (hasMatchedParticipants(participants)) {
eventPublisher.createChatRoom(participants);
eventPublisher.sendNotification(participants);
}
}

private boolean hasMatchedParticipants(List<Participant> participants) {
return !participants.stream().filter(Participant::hasPartner).toList().isEmpty();
}

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

0 comments on commit e57e65a

Please sign in to comment.