diff --git a/jaknaeso-core/src/main/java/org/nexters/jaknaesocore/common/support/error/CustomException.java b/jaknaeso-core/src/main/java/org/nexters/jaknaesocore/common/support/error/CustomException.java index b764b720..b26b4a70 100644 --- a/jaknaeso-core/src/main/java/org/nexters/jaknaesocore/common/support/error/CustomException.java +++ b/jaknaeso-core/src/main/java/org/nexters/jaknaesocore/common/support/error/CustomException.java @@ -28,6 +28,8 @@ public class CustomException extends RuntimeException { public static final CustomException FORBIDDEN_ACCESS = new CustomException(ErrorType.FORBIDDEN); public static final CustomException BUNDLE_NOT_FOUND = new CustomException(ErrorType.BUNDLE_NOT_FOUND); + public static final CustomException NOT_PROCEED_ONBOARDING = + new CustomException(ErrorType.NOT_PROCEED_ONBOARDING); private final ErrorType errorType; diff --git a/jaknaeso-core/src/main/java/org/nexters/jaknaesocore/common/support/error/ErrorType.java b/jaknaeso-core/src/main/java/org/nexters/jaknaesocore/common/support/error/ErrorType.java index ce81de17..f3f9f9f1 100644 --- a/jaknaeso-core/src/main/java/org/nexters/jaknaesocore/common/support/error/ErrorType.java +++ b/jaknaeso-core/src/main/java/org/nexters/jaknaesocore/common/support/error/ErrorType.java @@ -46,7 +46,9 @@ public enum ErrorType { ALREADY_COMPLETED_SURVEY_BUNDLE( HttpStatus.BAD_REQUEST, ErrorCode.E400, "이미 완료된 설문 번들입니다.", LogLevel.WARN), - BUNDLE_NOT_FOUND(HttpStatus.NOT_FOUND, ErrorCode.E404, "해당 번들을 찾을 수 없습니다.", LogLevel.WARN); + BUNDLE_NOT_FOUND(HttpStatus.NOT_FOUND, ErrorCode.E404, "해당 번들을 찾을 수 없습니다.", LogLevel.WARN), + NOT_PROCEED_ONBOARDING( + HttpStatus.FORBIDDEN, ErrorCode.E403, "온보딩을 진행해야 접근 가능합니다.", LogLevel.ERROR); private final HttpStatus status; diff --git a/jaknaeso-core/src/main/java/org/nexters/jaknaesocore/domain/survey/dto/SurveyHistoryResponse.java b/jaknaeso-core/src/main/java/org/nexters/jaknaesocore/domain/survey/dto/SurveyHistoryResponse.java index 94ffb78d..e295ed17 100644 --- a/jaknaeso-core/src/main/java/org/nexters/jaknaesocore/domain/survey/dto/SurveyHistoryResponse.java +++ b/jaknaeso-core/src/main/java/org/nexters/jaknaesocore/domain/survey/dto/SurveyHistoryResponse.java @@ -19,8 +19,4 @@ public static SurveyHistoryResponse of( public static SurveyHistoryResponse createNextBundleSurveyHistory(Long bundleId) { return new SurveyHistoryResponse(bundleId, List.of(), 1, false); } - - public static SurveyHistoryResponse createInitialSurveyHistory() { - return new SurveyHistoryResponse(1L, List.of(), 1, false); - } } diff --git a/jaknaeso-core/src/main/java/org/nexters/jaknaesocore/domain/survey/repository/SurveyBundleRepository.java b/jaknaeso-core/src/main/java/org/nexters/jaknaesocore/domain/survey/repository/SurveyBundleRepository.java index e7dfb750..f56d2df0 100644 --- a/jaknaeso-core/src/main/java/org/nexters/jaknaesocore/domain/survey/repository/SurveyBundleRepository.java +++ b/jaknaeso-core/src/main/java/org/nexters/jaknaesocore/domain/survey/repository/SurveyBundleRepository.java @@ -1,10 +1,11 @@ package org.nexters.jaknaesocore.domain.survey.repository; +import java.util.Collection; import java.util.Optional; import org.nexters.jaknaesocore.domain.survey.model.SurveyBundle; import org.springframework.data.jpa.repository.JpaRepository; public interface SurveyBundleRepository extends JpaRepository { - Optional findFirstByIdGreaterThanOrderByIdAsc(final Long id); + Optional findFirstByIdNotInOrderByIdAsc(Collection ids); } diff --git a/jaknaeso-core/src/main/java/org/nexters/jaknaesocore/domain/survey/service/SurveyService.java b/jaknaeso-core/src/main/java/org/nexters/jaknaesocore/domain/survey/service/SurveyService.java index 1c692b55..2b995dee 100644 --- a/jaknaeso-core/src/main/java/org/nexters/jaknaesocore/domain/survey/service/SurveyService.java +++ b/jaknaeso-core/src/main/java/org/nexters/jaknaesocore/domain/survey/service/SurveyService.java @@ -4,7 +4,7 @@ import java.time.LocalDateTime; import java.util.Comparator; import java.util.List; -import java.util.Optional; +import java.util.Set; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import org.nexters.jaknaesocore.common.model.BaseEntity; @@ -48,14 +48,14 @@ private List getSubmittedSurvey(final Long memberId) { @Transactional(readOnly = true) public SurveyHistoryResponse getSurveyHistory(final Long memberId) { - return findLatestSubmission(memberId) - .map(this::classifySubmission) - .orElseGet(SurveyHistoryResponse::createInitialSurveyHistory); + SurveySubmission latestSubmission = findLatestSubmission(memberId); + return classifySubmission(latestSubmission); } - private Optional findLatestSubmission(final Long memberId) { - return surveySubmissionRepository.findTopByMember_IdAndDeletedAtIsNullOrderByCreatedAtDesc( - memberId); + private SurveySubmission findLatestSubmission(final Long memberId) { + return surveySubmissionRepository + .findTopByMember_IdAndDeletedAtIsNullOrderByCreatedAtDesc(memberId) + .orElseThrow(() -> CustomException.NOT_PROCEED_ONBOARDING); } private SurveyHistoryResponse classifySubmission(SurveySubmission latestSubmission) { @@ -69,7 +69,7 @@ private SurveyHistoryResponse classifySubmission(SurveySubmission latestSubmissi } if (bundle.isAllSubmitted(submissions)) { - return getNextBundleHistory(bundle.getId()); + return getNextBundleHistory(submissions); } return createCurrentBundleSurveyHistory(bundle.getId(), submissions, isCompleted); } @@ -89,11 +89,19 @@ private SurveyHistoryResponse createCurrentBundleSurveyHistory( return SurveyHistoryResponse.of(bundleId, historyDetails, historyDetails.size() + 1, false); } - private SurveyHistoryResponse getNextBundleHistory(Long currentBundleId) { - return surveyBundleRepository - .findFirstByIdGreaterThanOrderByIdAsc(currentBundleId) - .map(bundle -> SurveyHistoryResponse.createNextBundleSurveyHistory(bundle.getId())) - .orElseThrow(() -> CustomException.NOT_READY_FOR_NEXT_BUNDLE); + private SurveyHistoryResponse getNextBundleHistory(List submissions) { + Set bundleIds = + submissions.stream() + .map(SurveySubmission::getSurvey) + .map(Survey::getSurveyBundle) + .map(SurveyBundle::getId) + .collect(Collectors.toSet()); + + SurveyBundle nextBundle = + surveyBundleRepository + .findFirstByIdNotInOrderByIdAsc(bundleIds) + .orElseThrow(() -> CustomException.NOT_READY_FOR_NEXT_BUNDLE); + return SurveyHistoryResponse.createNextBundleSurveyHistory(nextBundle.getId()); } @Transactional diff --git a/jaknaeso-core/src/test/java/org/nexters/jaknaesocore/domain/survey/repository/SurveyBundleRepositoryTest.java b/jaknaeso-core/src/test/java/org/nexters/jaknaesocore/domain/survey/repository/SurveyBundleRepositoryTest.java index 310447cc..4da1ccb6 100644 --- a/jaknaeso-core/src/test/java/org/nexters/jaknaesocore/domain/survey/repository/SurveyBundleRepositoryTest.java +++ b/jaknaeso-core/src/test/java/org/nexters/jaknaesocore/domain/survey/repository/SurveyBundleRepositoryTest.java @@ -21,7 +21,7 @@ void tearDown() { } @Test - void 주어진_ID_보다_큰_번들_중_가장_작은_ID를_가진_번들을_찾는다() { + void 주어진_ID_목록에_없는_번들_중_가장_작은_ID를_가진_번들을_찾는다() { // given SurveyBundle surveyBundle1 = new SurveyBundle(); SurveyBundle surveyBundle2 = new SurveyBundle(); @@ -30,9 +30,13 @@ void tearDown() { surveyBundleRepository.saveAll( List.of(surveyBundle1, surveyBundle2, surveyBundle3, surveyBundle4)); + + List excludedIds = List.of(surveyBundle1.getId(), surveyBundle2.getId()); + // when Optional result = - surveyBundleRepository.findFirstByIdGreaterThanOrderByIdAsc(surveyBundle2.getId()); + surveyBundleRepository.findFirstByIdNotInOrderByIdAsc(excludedIds); + // then then(result) .hasValueSatisfying( @@ -40,7 +44,7 @@ void tearDown() { } @Test - void 주어진_ID보다_큰_번들이_없으면_빈_값을_반환한다() { + void 모든_번들이_제외_목록에_있으면_빈_값을_반환한다() { // given SurveyBundle surveyBundle1 = new SurveyBundle(); SurveyBundle surveyBundle2 = new SurveyBundle(); @@ -49,9 +53,18 @@ void tearDown() { surveyBundleRepository.saveAll( List.of(surveyBundle1, surveyBundle2, surveyBundle3, surveyBundle4)); + + List excludedIds = + List.of( + surveyBundle1.getId(), + surveyBundle2.getId(), + surveyBundle3.getId(), + surveyBundle4.getId()); + // when Optional result = - surveyBundleRepository.findFirstByIdGreaterThanOrderByIdAsc(surveyBundle4.getId()); + surveyBundleRepository.findFirstByIdNotInOrderByIdAsc(excludedIds); + // then then(result).isEmpty(); } diff --git a/jaknaeso-core/src/test/java/org/nexters/jaknaesocore/domain/survey/service/SurveyServiceTest.java b/jaknaeso-core/src/test/java/org/nexters/jaknaesocore/domain/survey/service/SurveyServiceTest.java index 7706b2c6..391de6be 100644 --- a/jaknaeso-core/src/test/java/org/nexters/jaknaesocore/domain/survey/service/SurveyServiceTest.java +++ b/jaknaeso-core/src/test/java/org/nexters/jaknaesocore/domain/survey/service/SurveyServiceTest.java @@ -182,7 +182,7 @@ void throwSurveyNotFoundException() { } @Test - void 설문_기록이_없으면_1번_번들을_제공한다() { + void 설문_기록이_없으면_접근_할_수_없() { // given Member member = Member.create("나민혁", "test@test.com"); memberRepository.save(member); @@ -212,12 +212,10 @@ void throwSurveyNotFoundException() { SurveyOption.builder().survey(survey4).scores(scores).content("4점").build(); surveyOptionRepository.saveAll(List.of(option1, option2, option3, option4, option5, option6)); // when - SurveyHistoryResponse response = surveyService.getSurveyHistory(member.getId()); - // then - then(response) - .extracting("bundleId", "nextSurveyIndex", "surveyHistoryDetails") - .containsExactly(1L, 1, List.of()); + thenThrownBy(() -> surveyService.getSurveyHistory(member.getId())) + .isInstanceOf(CustomException.class) + .isEqualTo(CustomException.NOT_PROCEED_ONBOARDING); } @Test