Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ontime-back/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dependencies {
implementation 'com.fasterxml.jackson.core:jackson-databind'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'com.mysql:mysql-connector-j'
implementation 'mysql:mysql-connector-java:8.0.33'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void scheduleReminder(NotificationSchedule notificationSchedule) {
}

ScheduledFuture<?> future = taskScheduler.schedule(
() -> sendReminder(notificationSchedule, "약속 5분 전입니다"),
() -> sendReminder(notificationSchedule, "준비 시작해야 합니다.(현재 시각: 약속시각 - (여유시간 + 이동시간 + 총준비시간) )"),
Date.from(reminderTime.atZone(ZoneId.systemDefault()).toInstant())
);

Expand All @@ -68,7 +68,8 @@ public void sendReminder(NotificationSchedule notificationSchedule, String messa

if (userId != null) {
UserSetting userSetting = userSettingRepository.findByUserId(userId)
.orElseThrow(() -> new IllegalArgumentException("No UserSetting found in schedule's user"));// Repository 메서드 가정
.orElseThrow(() -> new IllegalArgumentException("No UserSetting found in schedule's user"));
log.debug("사용자 알림 전송 설정 여부: " + userSetting.getIsNotificationsEnabled());

if (Boolean.TRUE.equals(userSetting.getIsNotificationsEnabled())) {
sendNotificationToUser(notificationSchedule.getSchedule(), message);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package devkor.ontime_back.service;

import devkor.ontime_back.dto.PreparationDto;
import devkor.ontime_back.entity.NotificationSchedule;
import devkor.ontime_back.entity.PreparationSchedule;
import devkor.ontime_back.entity.Schedule;
import devkor.ontime_back.entity.User;
import devkor.ontime_back.global.jwt.JwtTokenProvider;
import devkor.ontime_back.repository.NotificationScheduleRepository;
import devkor.ontime_back.repository.PreparationScheduleRepository;
import devkor.ontime_back.repository.ScheduleRepository;
import devkor.ontime_back.repository.UserRepository;
Expand All @@ -13,24 +16,26 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

import static devkor.ontime_back.response.ErrorCode.SCHEDULE_NOT_FOUND;
import static devkor.ontime_back.response.ErrorCode.UNAUTHORIZED_ACCESS;
import static devkor.ontime_back.response.ErrorCode.*;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
@Slf4j
public class PreparationScheduleService {
private final ScheduleService scheduleService;
private final PreparationScheduleRepository preparationScheduleRepository;
private final UserRepository userRepository;
private final ScheduleRepository scheduleRepository;
private final JwtTokenProvider jwtTokenProvider;
private final NotificationScheduleRepository notificationScheduleRepository;


@Transactional
Expand Down Expand Up @@ -87,5 +92,13 @@ protected void handlePreparationSchedules(Long userId, UUID scheduleId, List<Pre
});

preparationScheduleRepository.saveAll(preparationSchedules);

NotificationSchedule notification = notificationScheduleRepository.findByScheduleScheduleId(scheduleId)
.orElseThrow(() -> new GeneralException(NOTIFICATION_NOT_FOUND));

LocalDateTime notificationTime = scheduleService.getNotificationTime(schedule, schedule.getUser());
log.info("Notification Time(변경된): " + notificationTime);

scheduleService.updateAndRescheduleNotification(notificationTime, notification);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void modifySchedule(Long userId, UUID scheduleId, ScheduleModDto schedule
updateAndRescheduleNotification(newNotificationTime, notification);
}

private void updateAndRescheduleNotification(LocalDateTime newNotificationTime, NotificationSchedule notification) {
public void updateAndRescheduleNotification(LocalDateTime newNotificationTime, NotificationSchedule notification) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private에서 public으로 바꾸신 배경이 궁금한데 설명해주실 수 있을까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preparationScheduleService에서 해당 메서드 사용하게 하기 위해서 변경했습니다.
(준비과정 수정 시에도 TaskScheduler의 스케줄을 리스케줄링해줘야해서 preparationScheduleService에서 해당 메서드 사용합니다)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 그렇군요! 확인했습니다!!

if(newNotificationTime == notification.getNotificationTime()) return;

notificationService.cancelScheduledNotification(notification.getId());
Expand Down Expand Up @@ -141,7 +141,7 @@ public void addSchedule(ScheduleAddDto scheduleAddDto, Long userId) {
notificationService.scheduleReminder(notification);
}

private LocalDateTime getNotificationTime(Schedule schedule, User user) {
public LocalDateTime getNotificationTime(Schedule schedule, User user) {
Integer preparationTime = calculatePreparationTime(schedule, user);
Integer moveTime = schedule.getMoveTime();
Integer spareTime = schedule.getScheduleSpareTime() == null ? user.getSpareTime() : schedule.getScheduleSpareTime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public class PreparationScheduleServiceTest {
@Autowired
private UserRepository userRepository;

@Autowired
private NotificationScheduleRepository notificationScheduleRepository;

@Autowired
private PreparationScheduleService preparationScheduleService;

Expand All @@ -63,6 +66,7 @@ void makePreparationSchedules_withoutDeletingExisting() {
.punctualityScore(-1f)
.scheduleCountAfterReset(0)
.latenessCountAfterReset(0)
.spareTime(5)
.build();
userRepository.save(newUser);

Expand All @@ -83,6 +87,12 @@ void makePreparationSchedules_withoutDeletingExisting() {
.build();
scheduleRepository.save(addedSchedule1);

NotificationSchedule notificationSchedule = NotificationSchedule.builder()
.schedule(addedSchedule1)
.notificationTime(LocalDateTime.of(2025, 1, 1, 0, 0))
.build();
notificationScheduleRepository.save(notificationSchedule);

UUID preparationSchedule1Id = UUID.randomUUID();
UUID preparationSchedule2Id = UUID.randomUUID();

Expand Down Expand Up @@ -112,6 +122,7 @@ void updatePreparationSchedules_withDeletingExisting() {
.punctualityScore(-1f)
.scheduleCountAfterReset(0)
.latenessCountAfterReset(0)
.spareTime(5)
.build();
userRepository.save(newUser);

Expand All @@ -132,6 +143,12 @@ void updatePreparationSchedules_withDeletingExisting() {
.build();
scheduleRepository.save(addedSchedule1);

NotificationSchedule notificationSchedule = NotificationSchedule.builder()
.schedule(addedSchedule1)
.notificationTime(LocalDateTime.of(2025, 1, 1, 0, 0))
.build();
notificationScheduleRepository.save(notificationSchedule);

PreparationSchedule preparationSchedule3 = preparationScheduleRepository.save(new PreparationSchedule(
UUID.randomUUID(), addedSchedule1, "화장", 10, null));
PreparationSchedule preparationSchedule2= preparationScheduleRepository.save(new PreparationSchedule(
Expand Down Expand Up @@ -168,6 +185,7 @@ void handlePreparationSchedules_withoutDeletingExisting() {
.punctualityScore(-1f)
.scheduleCountAfterReset(0)
.latenessCountAfterReset(0)
.spareTime(5)
.build();
userRepository.save(newUser);

Expand All @@ -188,6 +206,12 @@ void handlePreparationSchedules_withoutDeletingExisting() {
.build();
scheduleRepository.save(addedSchedule1);

NotificationSchedule notificationSchedule = NotificationSchedule.builder()
.schedule(addedSchedule1)
.notificationTime(LocalDateTime.of(2025, 1, 1, 0, 0))
.build();
notificationScheduleRepository.save(notificationSchedule);

UUID preparationSchedule1Id = UUID.randomUUID();
UUID preparationSchedule2Id = UUID.randomUUID();

Expand Down Expand Up @@ -217,6 +241,7 @@ void handlePreparationSchedules_withDeletingExisting() {
.punctualityScore(-1f)
.scheduleCountAfterReset(0)
.latenessCountAfterReset(0)
.spareTime(5)
.build();
userRepository.save(newUser);

Expand All @@ -237,6 +262,12 @@ void handlePreparationSchedules_withDeletingExisting() {
.build();
scheduleRepository.save(addedSchedule1);

NotificationSchedule notificationSchedule = NotificationSchedule.builder()
.schedule(addedSchedule1)
.notificationTime(LocalDateTime.of(2025, 1, 1, 0, 0))
.build();
notificationScheduleRepository.save(notificationSchedule);

PreparationSchedule preparationSchedule3 = preparationScheduleRepository.save(new PreparationSchedule(
UUID.randomUUID(), addedSchedule1, "화장", 10, null));
PreparationSchedule preparationSchedule2= preparationScheduleRepository.save(new PreparationSchedule(
Expand Down