6
6
import devkor .ontime_back .response .GeneralException ;
7
7
import lombok .RequiredArgsConstructor ;
8
8
import lombok .extern .slf4j .Slf4j ;
9
+ import org .jetbrains .annotations .NotNull ;
9
10
import org .springframework .stereotype .Service ;
10
11
import org .springframework .transaction .annotation .Transactional ;
11
12
@@ -81,26 +82,16 @@ public ScheduleDto showScheduleByScheduleId(Long userId, UUID scheduleId) {
81
82
// schedule 삭제
82
83
@ Transactional
83
84
public void deleteSchedule (UUID scheduleId , Long userId ) {
84
- Schedule schedule = getScheduleWithAuthorization (scheduleId , userId );
85
+ getScheduleWithAuthorization (scheduleId , userId );
85
86
NotificationSchedule notification = notificationScheduleRepository .findByScheduleScheduleId (scheduleId )
86
87
.orElseThrow (() -> new GeneralException (NOTIFICATION_NOT_FOUND ));
87
-
88
- cancelAndDeleteNotification (notification );
89
- notificationScheduleRepository .flush ();
90
88
scheduleRepository .deleteByScheduleId (scheduleId );
91
89
}
92
90
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
-
101
91
// schedule 수정
102
92
@ Transactional
103
93
public void modifySchedule (Long userId , UUID scheduleId , ScheduleModDto scheduleModDto ) {
94
+ User user = userRepository .findById (userId ).orElseThrow (() -> new GeneralException (USER_NOT_FOUND ));
104
95
Schedule schedule = getScheduleWithAuthorization (scheduleId , userId );
105
96
106
97
Place place = placeRepository .findByPlaceName (scheduleModDto .getPlaceName ())
@@ -110,10 +101,9 @@ public void modifySchedule(Long userId, UUID scheduleId, ScheduleModDto schedule
110
101
111
102
scheduleRepository .save (schedule );
112
103
113
-
114
104
NotificationSchedule notification = notificationScheduleRepository .findByScheduleScheduleId (scheduleId )
115
105
.orElseThrow (() -> new GeneralException (NOTIFICATION_NOT_FOUND ));
116
- LocalDateTime newNotificationTime = scheduleModDto . getScheduleTime (). minusMinutes ( 5 );
106
+ LocalDateTime newNotificationTime = getNotificationTime ( schedule , user );
117
107
updateAndRescheduleNotification (newNotificationTime , notification );
118
108
}
119
109
@@ -139,8 +129,10 @@ public void addSchedule(ScheduleAddDto scheduleAddDto, Long userId) {
139
129
Schedule schedule = scheduleAddDto .toEntity (user , place );
140
130
scheduleRepository .save (schedule );
141
131
132
+ LocalDateTime notificationTime = getNotificationTime (schedule , user );
133
+
142
134
NotificationSchedule notification = NotificationSchedule .builder ()
143
- .notificationTime (schedule . getScheduleTime (). minusMinutes ( 5 )) // 차후 알림보내야하는 시각으로 수정 필요
135
+ .notificationTime (notificationTime )
144
136
.isSent (false )
145
137
.schedule (schedule )
146
138
.build ();
@@ -149,6 +141,18 @@ public void addSchedule(ScheduleAddDto scheduleAddDto, Long userId) {
149
141
notificationService .scheduleReminder (notification );
150
142
}
151
143
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
+
152
156
// 지각 히스토리 반환
153
157
public List <LatenessHistoryResponse > getLatenessHistory (Long userId ) {
154
158
return scheduleRepository .findLatenessHistoryByUserId (userId ).stream ()
0 commit comments