Skip to content

Commit

Permalink
[#59] feat: 진행 할 설문 목록 조회 로직 변경
Browse files Browse the repository at this point in the history
- 온보딩이 필수이기 때문에 온보딩을 하지않으면 접근권한 없음
- 다음 번들 찾는 로직을 진행하지않은 번들중에서 찾도록 변경
  • Loading branch information
NaMinhyeok committed Feb 13, 2025
1 parent d9b9d3d commit 6371c1a
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<SurveyBundle, Long> {

Optional<SurveyBundle> findFirstByIdGreaterThanOrderByIdAsc(final Long id);
Optional<SurveyBundle> findFirstByIdNotInOrderByIdAsc(Collection<Long> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,14 +48,14 @@ private List<Survey> 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<SurveySubmission> 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) {
Expand All @@ -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);
}
Expand All @@ -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<SurveySubmission> submissions) {
Set<Long> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void tearDown() {
}

@Test
void 주어진_ID_보다_큰_번들_중_가장_작은_ID를_가진_번들을_찾는다() {
void 주어진_ID_목록에_없는_번들_중_가장_작은_ID를_가진_번들을_찾는다() {
// given
SurveyBundle surveyBundle1 = new SurveyBundle();
SurveyBundle surveyBundle2 = new SurveyBundle();
Expand All @@ -30,17 +30,21 @@ void tearDown() {

surveyBundleRepository.saveAll(
List.of(surveyBundle1, surveyBundle2, surveyBundle3, surveyBundle4));

List<Long> excludedIds = List.of(surveyBundle1.getId(), surveyBundle2.getId());

// when
Optional<SurveyBundle> result =
surveyBundleRepository.findFirstByIdGreaterThanOrderByIdAsc(surveyBundle2.getId());
surveyBundleRepository.findFirstByIdNotInOrderByIdAsc(excludedIds);

// then
then(result)
.hasValueSatisfying(
surveyBundle -> then(surveyBundle.getId()).isEqualTo(surveyBundle3.getId()));
}

@Test
void 주어진_ID보다_큰_번들이_없으면_빈_값을_반환한다() {
void 모든_번들이_제외_목록에_있으면_빈_값을_반환한다() {
// given
SurveyBundle surveyBundle1 = new SurveyBundle();
SurveyBundle surveyBundle2 = new SurveyBundle();
Expand All @@ -49,9 +53,18 @@ void tearDown() {

surveyBundleRepository.saveAll(
List.of(surveyBundle1, surveyBundle2, surveyBundle3, surveyBundle4));

List<Long> excludedIds =
List.of(
surveyBundle1.getId(),
surveyBundle2.getId(),
surveyBundle3.getId(),
surveyBundle4.getId());

// when
Optional<SurveyBundle> result =
surveyBundleRepository.findFirstByIdGreaterThanOrderByIdAsc(surveyBundle4.getId());
surveyBundleRepository.findFirstByIdNotInOrderByIdAsc(excludedIds);

// then
then(result).isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ void throwSurveyNotFoundException() {
}

@Test
void 설문_기록이_없으면_1번_번들을_제공한다() {
void 설문_기록이_없으면_접근_할_수_없() {
// given
Member member = Member.create("나민혁", "[email protected]");
memberRepository.save(member);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6371c1a

Please sign in to comment.