Skip to content

Commit dce08f8

Browse files
authored
[Fix/#77]보상 상태별 필터링 기능 추가 및 습관 상태 스케줄러 추가 (#78)
## 📌 관련 이슈 - close #77 ## ✨ 변경 사항 - 보상 전체 필터링 기능 구현 (RewardStatus ALL 추가) - 일일 습관 상태 초기화 스케줄러 기능 구현 ## 📸 테스트 증명 (필수) 로컬 빌드 완료 ## 📚 리뷰어 참고 사항 - 누락되었던 기능들 다 추가 완료하였습니다. - ?status = ALL 이런 식으로 쿼리 파라미터가 들어오면 전체 보상 조회 - 일일 습관 상태 초기화 스케줄러를 통한 매일 습관 완료 여부 초기화 - 습관 실패와 관련해선 후순위로 개발하기로 하였습니다. ## ✅ 체크리스트 - [x] 브랜치 전략(git flow)을 따랐나요? - [x] 로컬에서 빌드 및 실행이 정상적으로 되나요? - [x] 불필요한 주석이나 더미 코드는 제거했나요? - [x] 컨벤션(커밋 메시지, 코드 스타일)을 지켰나요? - [x] spotlessApply 실행했나요? <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added an "All" reward-status option to view rewards across all statuses. * Added an automatic daily reset that clears habit completion status. * **Behavior Changes** * The "All" selection now returns unfiltered reward results. * Reward ordering updated to prioritize non-complete items before completed ones. * Reward visibility for parent accounts remains unchanged (no rewards shown). <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent a585656 commit dce08f8

6 files changed

Lines changed: 29 additions & 8 deletions

File tree

src/main/java/com/swyp/server/domain/habit/dto/HabitResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public record HabitResponse(
1010
public static HabitResponse from(Habit habit) {
1111

1212
String reward =
13-
(habit.getUser().getUserType().equals(UserType.PARENT)) ? null : habit.getReward();
13+
(habit.getUser().getUserType() == (UserType.PARENT)) ? null : habit.getReward();
1414

1515
return new HabitResponse(
1616
habit.getId(), habit.getTitle(), habit.getDuration(), reward, habit.isCompleted());

src/main/java/com/swyp/server/domain/habit/entity/Habit.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.swyp.server.global.SoftDeletableEntity;
55
import jakarta.persistence.*;
66
import java.time.LocalDateTime;
7+
import java.time.ZoneId;
78
import lombok.AccessLevel;
89
import lombok.Builder;
910
import lombok.Getter;
@@ -52,7 +53,7 @@ public Habit(User user, String title, HabitDuration duration, String reward) {
5253
this.reward = reward;
5354
this.isCompleted = false;
5455
this.status = RewardStatus.REWARD_CHECKING;
55-
this.expiredAt = LocalDateTime.now().plusDays(duration.getDays());
56+
this.expiredAt = LocalDateTime.now(ZoneId.of("Asia/Seoul")).plusDays(duration.getDays());
5657
}
5758

5859
public void updateTitle(String title) {

src/main/java/com/swyp/server/domain/habit/entity/RewardStatus.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ public enum RewardStatus {
99
REWARD_CHECKING("보상 확인중"),
1010
REWARD_WAITING("보상 대기중"),
1111
IN_PROGRESS("진행중"),
12-
COMPLETE("완료");
12+
COMPLETE("완료"),
13+
ALL("전체");
1314

1415
private final String label;
16+
17+
public boolean isAll() {
18+
return this == ALL;
19+
}
1520
}

src/main/java/com/swyp/server/domain/habit/repository/HabitRepository.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ List<Habit> findAllByUserIdAndStatusOrderByIsCompletedAscIdDesc(
2828
"SELECT h FROM Habit h "
2929
+ "WHERE h.user.id IN :userIds "
3030
+ "AND (:status IS NULL OR h.status = :status) "
31-
+ "ORDER BY h.isCompleted ASC, h.id DESC")
31+
+ "ORDER BY "
32+
+ " CASE h.status WHEN 'COMPLETE' THEN 1 ELSE 0 END ASC, "
33+
+ " h.id DESC")
3234
List<Habit> findAllByUserIdsAndStatusOptional(
3335
@Param("userIds") List<Long> userIds, @Param("status") RewardStatus status);
3436

@@ -39,6 +41,10 @@ List<Habit> findAllByUserIdsAndStatusOptional(
3941
+ "AND h.expiredAt < :now")
4042
void updateExpiredHabitsStatus(@Param("now") LocalDateTime now);
4143

44+
@Modifying(clearAutomatically = true)
45+
@Query("UPDATE Habit h SET h.isCompleted = false " + "WHERE h.isCompleted = true")
46+
void resetAllHabits();
47+
4248
// 미완료 습관이 있는 유저 ID 조회
4349
@Query(
4450
"SELECT DISTINCT h.user.id FROM Habit h WHERE h.user.id IN :userIds AND h.isCompleted = false")

src/main/java/com/swyp/server/domain/habit/service/HabitScheduler.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.swyp.server.domain.habit.repository.HabitRepository;
44
import java.time.LocalDateTime;
5+
import java.time.ZoneId;
56
import lombok.RequiredArgsConstructor;
67
import lombok.extern.slf4j.Slf4j;
78
import org.springframework.scheduling.annotation.Scheduled;
@@ -16,9 +17,16 @@ public class HabitScheduler {
1617
private final HabitRepository habitRepository;
1718

1819
@Transactional
19-
@Scheduled(cron = "0 0 0 * * *")
20+
@Scheduled(cron = "0 0 0 * * *", zone = "Asia/Seoul")
2021
public void checkExpiredHabits() {
2122
log.info("만료된 습관 체크 스케줄러 시작");
22-
habitRepository.updateExpiredHabitsStatus(LocalDateTime.now());
23+
habitRepository.updateExpiredHabitsStatus(LocalDateTime.now(ZoneId.of("Asia/Seoul")));
24+
}
25+
26+
@Transactional
27+
@Scheduled(cron = "0 1 0 * * *", zone = "Asia/Seoul")
28+
public void resetDailyHabits() {
29+
log.info("매일 습관 완료 상태 초기화 스케줄러 시작");
30+
habitRepository.resetAllHabits();
2331
}
2432
}

src/main/java/com/swyp/server/domain/habit/service/HabitService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,10 @@ public HabitRewardListResponse getHabitRewards(Long userId, RewardStatus status)
104104

105105
if (targetUserIds.isEmpty()) return HabitRewardListResponse.empty();
106106

107+
RewardStatus filteredStatus = status.isAll() ? null : status;
108+
107109
List<Habit> habits =
108-
habitRepository.findAllByUserIdsAndStatusOptional(targetUserIds, status);
110+
habitRepository.findAllByUserIdsAndStatusOptional(targetUserIds, filteredStatus);
109111

110112
return HabitRewardListResponse.from(habits, user.getUserType());
111113
}
@@ -214,7 +216,6 @@ public void updateHabitRewardStatusToInProgress(
214216
RewardStatus previousStatus = habit.getStatus();
215217

216218
if (previousStatus != RewardStatus.REWARD_CHECKING) {
217-
// 이미 IN_PROGRESS거나 다른 상태라면 즉시 에러 발생!
218219
throw new CustomException(ErrorCode.INVALID_HABIT_STATUS);
219220
}
220221

0 commit comments

Comments
 (0)