diff --git a/src/main/java/com/team/buddyya/match/exception/MatchExceptionType.java b/src/main/java/com/team/buddyya/match/exception/MatchExceptionType.java index 5393b2b5..d83bc5ea 100644 --- a/src/main/java/com/team/buddyya/match/exception/MatchExceptionType.java +++ b/src/main/java/com/team/buddyya/match/exception/MatchExceptionType.java @@ -11,7 +11,7 @@ public enum MatchExceptionType implements BaseExceptionType { MATCH_PROFILE_NOT_FOUND(6003, HttpStatus.NOT_FOUND, "Matching profile not found."), MATCH_PROFILE_NOT_COMPLETED(6004, HttpStatus.BAD_REQUEST, "Matching profile is not completed."), MATCH_HISTORY_NOT_FOUND(6005, HttpStatus.NOT_FOUND, "Recent matched history not found."), - MATCH_REQUEST_TIME_INVALID(6006, HttpStatus.BAD_REQUEST, "You can only request a match once per day."); + MATCH_REQUEST_TIME_INVALID(6006, HttpStatus.BAD_REQUEST, "You can only request a match twice per day."); private final int errorCode; private final HttpStatus httpStatus; diff --git a/src/main/java/com/team/buddyya/match/repository/MatchedHistoryRepository.java b/src/main/java/com/team/buddyya/match/repository/MatchedHistoryRepository.java index 4f7f3934..30c5adc0 100644 --- a/src/main/java/com/team/buddyya/match/repository/MatchedHistoryRepository.java +++ b/src/main/java/com/team/buddyya/match/repository/MatchedHistoryRepository.java @@ -5,6 +5,7 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import java.time.LocalDateTime; import java.util.Optional; import java.util.Set; @@ -16,4 +17,6 @@ public interface MatchedHistoryRepository extends JpaRepository findMostRecentMatchedHistoryByStudentId(Long studentId); + + int countByStudentIdAndCreatedDateBetween(Long studentId, LocalDateTime start, LocalDateTime end); } diff --git a/src/main/java/com/team/buddyya/match/service/BasicMatchService.java b/src/main/java/com/team/buddyya/match/service/BasicMatchService.java index d77ac438..9c2df554 100644 --- a/src/main/java/com/team/buddyya/match/service/BasicMatchService.java +++ b/src/main/java/com/team/buddyya/match/service/BasicMatchService.java @@ -48,6 +48,7 @@ public class BasicMatchService implements MatchService { private static final Long CHINESE_LANGUAGE_ID = 3L; + private static final int MAX_MATCHES_PER_DAY = 2; private final MatchRequestRepository matchRequestRepository; private final MatchedHistoryRepository matchedHistoryRepository; @@ -191,15 +192,11 @@ private boolean isUniversityMatch(UniversityType matchRequestPreference, UniversityType requesterPreference, Long requesterUniversityId, Long matchRequestUniversityId) { - if (requesterPreference == UniversityType.SAME_UNIVERSITY && - matchRequestPreference == UniversityType.SAME_UNIVERSITY) { - return requesterUniversityId.equals(matchRequestUniversityId); - } if (requesterPreference == UniversityType.DIFFERENT_UNIVERSITY && matchRequestPreference == UniversityType.DIFFERENT_UNIVERSITY) { - return !requesterUniversityId.equals(matchRequestUniversityId); + return true; } - return false; + return requesterUniversityId.equals(matchRequestUniversityId); } private boolean isGenderMatch(GenderType matchRequestPreference, @@ -259,19 +256,17 @@ private void validateMatchProfileCompleted(Student student) { } private void validateMatchRequestTime(Student student) { - Optional recentMatchedHistory = - matchedHistoryRepository.findMostRecentMatchedHistoryByStudentId(student.getId()); - if (recentMatchedHistory.isPresent()) { - LocalDateTime matchedAt = recentMatchedHistory.get().getCreatedDate(); - LocalDate today = LocalDate.now(); - LocalDate matchedDate = matchedAt.toLocalDate(); - if (matchedDate.isEqual(today)) { - throw new MatchException(MatchExceptionType.MATCH_REQUEST_TIME_INVALID); - } + LocalDate today = LocalDate.now(); + LocalDateTime startOfDay = today.atStartOfDay(); + LocalDateTime endOfDay = today.plusDays(1).atStartOfDay(); + int todayMatchCount = matchedHistoryRepository + .countByStudentIdAndCreatedDateBetween(student.getId(), startOfDay, endOfDay); + if (todayMatchCount >= MAX_MATCHES_PER_DAY) { + throw new MatchException(MatchExceptionType.MATCH_REQUEST_TIME_INVALID); } } - public boolean checkChineseAvailability(Student student){ + public boolean checkChineseAvailability(Student student) { return studentLanguageRepository.existsByStudentAndLanguage_Id(student, CHINESE_LANGUAGE_ID); } }