66import devkor .ontime_back .response .GeneralException ;
77import lombok .RequiredArgsConstructor ;
88import lombok .extern .slf4j .Slf4j ;
9+ import org .jetbrains .annotations .NotNull ;
910import org .springframework .stereotype .Service ;
1011import org .springframework .transaction .annotation .Transactional ;
1112
@@ -81,26 +82,16 @@ public ScheduleDto showScheduleByScheduleId(Long userId, UUID scheduleId) {
8182 // schedule 삭제
8283 @ Transactional
8384 public void deleteSchedule (UUID scheduleId , Long userId ) {
84- Schedule schedule = getScheduleWithAuthorization (scheduleId , userId );
85+ getScheduleWithAuthorization (scheduleId , userId );
8586 NotificationSchedule notification = notificationScheduleRepository .findByScheduleScheduleId (scheduleId )
8687 .orElseThrow (() -> new GeneralException (NOTIFICATION_NOT_FOUND ));
87-
88- cancelAndDeleteNotification (notification );
89- notificationScheduleRepository .flush ();
9088 scheduleRepository .deleteByScheduleId (scheduleId );
9189 }
9290
93- private void cancelAndDeleteNotification (NotificationSchedule notification ) {
94- log .info ("{}에 대한 알림 취소 및 삭제 됨" , notification .getSchedule ().getScheduleName ());
95- notification .disconnectSchedule ();
96- notificationService .cancelScheduledNotification (notification .getId ());
97- notificationScheduleRepository .delete (notification );
98- log .info ("알림 삭제 완료" );
99- }
100-
10191 // schedule 수정
10292 @ Transactional
10393 public void modifySchedule (Long userId , UUID scheduleId , ScheduleModDto scheduleModDto ) {
94+ User user = userRepository .findById (userId ).orElseThrow (() -> new GeneralException (USER_NOT_FOUND ));
10495 Schedule schedule = getScheduleWithAuthorization (scheduleId , userId );
10596
10697 Place place = placeRepository .findByPlaceName (scheduleModDto .getPlaceName ())
@@ -110,10 +101,9 @@ public void modifySchedule(Long userId, UUID scheduleId, ScheduleModDto schedule
110101
111102 scheduleRepository .save (schedule );
112103
113-
114104 NotificationSchedule notification = notificationScheduleRepository .findByScheduleScheduleId (scheduleId )
115105 .orElseThrow (() -> new GeneralException (NOTIFICATION_NOT_FOUND ));
116- LocalDateTime newNotificationTime = scheduleModDto . getScheduleTime (). minusMinutes ( 5 );
106+ LocalDateTime newNotificationTime = getNotificationTime ( schedule , user );
117107 updateAndRescheduleNotification (newNotificationTime , notification );
118108 }
119109
@@ -139,8 +129,10 @@ public void addSchedule(ScheduleAddDto scheduleAddDto, Long userId) {
139129 Schedule schedule = scheduleAddDto .toEntity (user , place );
140130 scheduleRepository .save (schedule );
141131
132+ LocalDateTime notificationTime = getNotificationTime (schedule , user );
133+
142134 NotificationSchedule notification = NotificationSchedule .builder ()
143- .notificationTime (schedule . getScheduleTime (). minusMinutes ( 5 )) // 차후 알림보내야하는 시각으로 수정 필요
135+ .notificationTime (notificationTime )
144136 .isSent (false )
145137 .schedule (schedule )
146138 .build ();
@@ -149,6 +141,18 @@ public void addSchedule(ScheduleAddDto scheduleAddDto, Long userId) {
149141 notificationService .scheduleReminder (notification );
150142 }
151143
144+ private LocalDateTime getNotificationTime (Schedule schedule , User user ) {
145+ Integer preparationTime = calculatePreparationTime (schedule , user );
146+ Integer moveTime = schedule .getMoveTime ();
147+ Integer spareTime = schedule .getScheduleSpareTime () == null ? user .getSpareTime () : schedule .getScheduleSpareTime ();
148+ return schedule .getScheduleTime ().minusMinutes (preparationTime + moveTime + spareTime );
149+ }
150+
151+ private Integer calculatePreparationTime (Schedule schedule , User user ) {
152+ List <PreparationDto > preparationDtos = getPreparations (user .getId (), schedule .getScheduleId ());
153+ return preparationDtos .stream ().map (PreparationDto ::getPreparationTime ).reduce (0 , Integer ::sum );
154+ }
155+
152156 // 지각 히스토리 반환
153157 public List <LatenessHistoryResponse > getLatenessHistory (Long userId ) {
154158 return scheduleRepository .findLatenessHistoryByUserId (userId ).stream ()
0 commit comments