Skip to content

Commit 4aee04d

Browse files
authored
[Feat/#66] 보상 기반 이벤트 푸시 알림 구현 (#68)
## 📌 관련 이슈 - close #66 ## ✨ 변경 사항 - 자녀가 새 습관 등록 시 연결된 부모에게 즉시 푸시 - 부모가 보상 승인(진행중으로 변경) 시 해당 자녀에게 즉시 푸시 ## 📸 테스트 증명 (필수) ## 📚 리뷰어 참고 사항 - 이벤트 트리거 방식입니다. ## ✅ 체크리스트 - [x] 브랜치 전략(git flow)을 따랐나요? - [x] 로컬에서 빌드 및 실행이 정상적으로 되나요? - [x] 불필요한 주석이나 더미 코드는 제거했나요? - [x] 컨벤션(커밋 메시지, 코드 스타일)을 지켰나요? - [x] spotlessApply 실행했나요? <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Parents now receive notifications when their children create a habit that includes a reward, so parents are promptly informed of new reward-based habits. * Habit owners receive notifications when a habit’s reward progress becomes active (moves into in-progress), keeping owners updated as reward status changes. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 6e8efda commit 4aee04d

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

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

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,23 @@
1010
import com.swyp.server.domain.user.repository.UserRepository;
1111
import com.swyp.server.global.exception.CustomException;
1212
import com.swyp.server.global.exception.ErrorCode;
13+
import com.swyp.server.global.notification.NotificationService;
1314
import java.util.ArrayList;
1415
import java.util.List;
16+
import java.util.Map;
1517
import lombok.RequiredArgsConstructor;
1618
import org.springframework.stereotype.Service;
1719
import org.springframework.transaction.annotation.Transactional;
20+
import org.springframework.transaction.support.TransactionSynchronization;
21+
import org.springframework.transaction.support.TransactionSynchronizationManager;
1822

1923
@Service
2024
@RequiredArgsConstructor
2125
public class HabitService {
2226
private final FamilyRelationService familyRelationService;
2327
private final HabitRepository habitRepository;
2428
private final UserRepository userRepository;
29+
private final NotificationService notificationService;
2530

2631
@Transactional
2732
public Habit createHabit(Long userId, HabitCreateRequest request) {
@@ -46,7 +51,27 @@ public Habit createHabit(Long userId, HabitCreateRequest request) {
4651
.reward(reward)
4752
.build();
4853

49-
return habitRepository.save(habit);
54+
Habit savedHabit = habitRepository.save(habit);
55+
56+
if (user.getUserType() == UserType.CHILD) {
57+
List<Long> parentIds =
58+
familyRelationService.getConnectedMembers(userId).stream()
59+
.filter(m -> m.getUserType() == UserType.PARENT)
60+
.map(User::getId)
61+
.toList();
62+
if (!parentIds.isEmpty()) {
63+
TransactionSynchronizationManager.registerSynchronization(
64+
new TransactionSynchronization() {
65+
@Override
66+
public void afterCommit() {
67+
notificationService.sendToUsers(
68+
parentIds, "해봄", "새로운 보상이 추가됐어요! 지금 바로 수락해 볼까요?", Map.of());
69+
}
70+
});
71+
}
72+
}
73+
74+
return savedHabit;
5075
}
5176

5277
@Transactional(readOnly = true)
@@ -177,10 +202,26 @@ public void updateHabitRewardStatus(
177202
throw new CustomException(ErrorCode.FORBIDDEN);
178203
}
179204

180-
if(!habit.getReward().trim().equals(request.reward().trim())) {
205+
if (habit.getReward() != null
206+
&& !habit.getReward().trim().equals(request.reward().trim())) {
181207
habit.updateReward(request.reward());
182208
}
209+
210+
RewardStatus previousStatus = habit.getStatus();
183211
habit.updateRewardStatus(request.rewardStatus());
212+
213+
if (previousStatus != RewardStatus.IN_PROGRESS
214+
&& request.rewardStatus() == RewardStatus.IN_PROGRESS) {
215+
Long childId = habit.getUser().getId();
216+
TransactionSynchronizationManager.registerSynchronization(
217+
new TransactionSynchronization() {
218+
@Override
219+
public void afterCommit() {
220+
notificationService.sendToUser(
221+
childId, "해봄", "부모님이 보상을 허락해 주셨어요. 보상을 받을 때까지 파이팅!", Map.of());
222+
}
223+
});
224+
}
184225
}
185226

186227
@Transactional

0 commit comments

Comments
 (0)