Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -16,4 +17,6 @@ public interface MatchedHistoryRepository extends JpaRepository<MatchedHistory,

@Query("SELECT m FROM MatchedHistory m WHERE m.student.id = :studentId ORDER BY m.id DESC LIMIT 1")
Optional<MatchedHistory> findMostRecentMatchedHistoryByStudentId(Long studentId);

int countByStudentIdAndCreatedDateBetween(Long studentId, LocalDateTime start, LocalDateTime end);
}
27 changes: 11 additions & 16 deletions src/main/java/com/team/buddyya/match/service/BasicMatchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -259,19 +256,17 @@ private void validateMatchProfileCompleted(Student student) {
}

private void validateMatchRequestTime(Student student) {
Optional<MatchedHistory> 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);
}
}