diff --git a/src/main/java/sopt/org/hmh/domain/challenge/service/ChallengeFacade.java b/src/main/java/sopt/org/hmh/domain/challenge/service/ChallengeFacade.java index fe3b415e..bd989581 100644 --- a/src/main/java/sopt/org/hmh/domain/challenge/service/ChallengeFacade.java +++ b/src/main/java/sopt/org/hmh/domain/challenge/service/ChallengeFacade.java @@ -29,11 +29,10 @@ public class ChallengeFacade { @Transactional public void startNewChallenge(NewChallengeOrder newChallengeOrder) { Challenge newChallenge = challengeService.addChallenge(newChallengeOrder.toChallengeEntity()); - userService.changeCurrentChallengeIdByUserId(newChallengeOrder.getUserId(), newChallenge.getId()); - dailyChallengeService.addDailyChallenge(newChallenge); this.addAppsByNewChallengeOrder(newChallengeOrder, newChallenge); + userService.changeCurrentChallengeIdByUserId(newChallengeOrder.getUserId(), newChallenge.getId()); } private void addAppsByNewChallengeOrder(NewChallengeOrder newChallengeOrder, Challenge newChallenge) { diff --git a/src/main/java/sopt/org/hmh/domain/dailychallenge/controller/DailyChallengeController.java b/src/main/java/sopt/org/hmh/domain/dailychallenge/controller/DailyChallengeController.java index f8d900ea..1e4c0f42 100644 --- a/src/main/java/sopt/org/hmh/domain/dailychallenge/controller/DailyChallengeController.java +++ b/src/main/java/sopt/org/hmh/domain/dailychallenge/controller/DailyChallengeController.java @@ -34,7 +34,7 @@ public ResponseEntity> orderAddHistoryDa return ResponseEntity .status(DailyChallengeSuccess.SEND_FINISHED_DAILY_CHALLENGE_SUCCESS.getHttpStatus()) .body(BaseResponse.success(DailyChallengeSuccess.SEND_FINISHED_DAILY_CHALLENGE_SUCCESS, - new ChallengeStatusesResponse(dailyChallengeFacade.addFinishedDailyChallengeHistory(userId, request, os)))); + new ChallengeStatusesResponse(dailyChallengeFacade.addFinishedDailyChallengeHistory(userId, request, os, timeZone)))); } @Override @@ -47,6 +47,6 @@ public ResponseEntity> orderChangeStatus return ResponseEntity .status(DailyChallengeSuccess.SEND_FINISHED_DAILY_CHALLENGE_SUCCESS.getHttpStatus()) .body(BaseResponse.success(DailyChallengeSuccess.SEND_FINISHED_DAILY_CHALLENGE_SUCCESS, - new ChallengeStatusesResponse(dailyChallengeFacade.changeDailyChallengeStatusByIsSuccess(userId, request)))); + new ChallengeStatusesResponse(dailyChallengeFacade.changeDailyChallengeStatusByIsSuccess(userId, request, timeZone)))); } } \ No newline at end of file diff --git a/src/main/java/sopt/org/hmh/domain/dailychallenge/domain/exception/DailyChallengeError.java b/src/main/java/sopt/org/hmh/domain/dailychallenge/domain/exception/DailyChallengeError.java index c6eafc5b..1870f246 100644 --- a/src/main/java/sopt/org/hmh/domain/dailychallenge/domain/exception/DailyChallengeError.java +++ b/src/main/java/sopt/org/hmh/domain/dailychallenge/domain/exception/DailyChallengeError.java @@ -10,7 +10,8 @@ public enum DailyChallengeError implements ErrorBase { DAILY_CHALLENGE_NOT_FOUND(HttpStatus.NOT_FOUND, "일별 챌린지를 찾을 수 없습니다."), DAILY_CHALLENGE_PERIOD_INDEX_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 인덱스의 일별 챌린지를 찾을 수 없습니다."), DAILY_CHALLENGE_ALREADY_PROCESSED(HttpStatus.BAD_REQUEST, "이미 처리된 일별 챌린지입니다."), - DAILY_CHALLENGE_ALREADY_EXISTS(HttpStatus.BAD_REQUEST, "이미 존재하는 일별 챌린지입니다."); + DAILY_CHALLENGE_ALREADY_EXISTS(HttpStatus.BAD_REQUEST, "이미 존재하는 일별 챌린지입니다."), + PERIOD_INDEX_NOT_VALID(HttpStatus.BAD_REQUEST, "지난 날의 정보만 전송할 수 있습니다."); private final HttpStatus status; private final String errorMessage; diff --git a/src/main/java/sopt/org/hmh/domain/dailychallenge/service/DailyChallengeFacade.java b/src/main/java/sopt/org/hmh/domain/dailychallenge/service/DailyChallengeFacade.java index ae44fee0..3a27bf9e 100644 --- a/src/main/java/sopt/org/hmh/domain/dailychallenge/service/DailyChallengeFacade.java +++ b/src/main/java/sopt/org/hmh/domain/dailychallenge/service/DailyChallengeFacade.java @@ -1,5 +1,7 @@ package sopt.org.hmh.domain.dailychallenge.service; +import java.time.LocalDate; +import java.time.ZoneId; import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -23,10 +25,13 @@ public class DailyChallengeFacade { private final UserService userService; @Transactional - public List addFinishedDailyChallengeHistory(Long userId, FinishedDailyChallengeListRequest request, String os) { + public List addFinishedDailyChallengeHistory(Long userId, FinishedDailyChallengeListRequest request, String os, String timeZone) { Challenge challenge = challengeService.findByIdOrElseThrow(userService.getCurrentChallengeIdByUserId(userId)); + Integer todayIndex = dailyChallengeService.calculateTodayIndex(challenge, LocalDate.now(ZoneId.of(timeZone))); request.finishedDailyChallenges().forEach(challengeRequest -> { + dailyChallengeService.validatePeriodIndex(challengeRequest.challengePeriodIndex(), todayIndex); + DailyChallenge dailyChallenge = dailyChallengeService .findDailyChallengeByChallengePeriodIndex(challenge, challengeRequest.challengePeriodIndex()); dailyChallengeService.changeStatusByCurrentStatus(dailyChallenge); @@ -37,10 +42,13 @@ public List addFinishedDailyChallengeHistory(Long userId, FinishedDailyC } @Transactional - public List changeDailyChallengeStatusByIsSuccess(Long userId, FinishedDailyChallengeStatusListRequest request) { + public List changeDailyChallengeStatusByIsSuccess(Long userId, FinishedDailyChallengeStatusListRequest request, String timeZone) { Challenge challenge = challengeService.findByIdOrElseThrow(userService.getCurrentChallengeIdByUserId(userId)); + Integer todayIndex = dailyChallengeService.calculateTodayIndex(challenge, LocalDate.now(ZoneId.of(timeZone))); request.finishedDailyChallenges().forEach(challengeRequest -> { + dailyChallengeService.validatePeriodIndex(challengeRequest.challengePeriodIndex(), todayIndex); + DailyChallenge dailyChallenge = dailyChallengeService .findDailyChallengeByChallengePeriodIndex(challenge, challengeRequest.challengePeriodIndex()); if (challengeRequest.isSuccess()) { diff --git a/src/main/java/sopt/org/hmh/domain/dailychallenge/service/DailyChallengeService.java b/src/main/java/sopt/org/hmh/domain/dailychallenge/service/DailyChallengeService.java index 8186daae..69b5166f 100644 --- a/src/main/java/sopt/org/hmh/domain/dailychallenge/service/DailyChallengeService.java +++ b/src/main/java/sopt/org/hmh/domain/dailychallenge/service/DailyChallengeService.java @@ -1,6 +1,7 @@ package sopt.org.hmh.domain.dailychallenge.service; import java.time.LocalDate; +import java.time.temporal.ChronoUnit; import java.util.List; import java.util.Optional; import java.util.stream.IntStream; @@ -73,6 +74,10 @@ private void validateDuplicateDailyChallenge(Challenge challenge) { } } + public void validatePeriodIndex(Integer periodIndex, Integer todayIndex) { + if (periodIndex >= todayIndex) throw new DailyChallengeException(DailyChallengeError.PERIOD_INDEX_NOT_VALID); + } + private List createDailyChallengeByChallengePeriod(Challenge challenge) { LocalDate startDate = challenge.getStartDate(); Long userId = challenge.getUserId(); @@ -106,4 +111,10 @@ private void changeChallengeDateOfDailyChallenges(List dailyChal dailyChallenges.get(i).changeChallengeDate(challengeDate.plusDays(i)); } } + + public Integer calculateTodayIndex(Challenge challenge, LocalDate now) { + final int COMPLETED_CHALLENGE_INDEX = -1; + int daysBetween = (int) ChronoUnit.DAYS.between(challenge.getStartDate(), now); + return (daysBetween >= challenge.getPeriod()) ? COMPLETED_CHALLENGE_INDEX : daysBetween; + } } \ No newline at end of file