1
1
package org .routineade .RoutineAdeServer .service ;
2
2
3
+ import java .time .LocalDate ;
3
4
import java .time .YearMonth ;
4
5
import java .time .format .DateTimeFormatter ;
5
6
import java .util .Arrays ;
15
16
import org .routineade .RoutineAdeServer .domain .Routine ;
16
17
import org .routineade .RoutineAdeServer .domain .User ;
17
18
import org .routineade .RoutineAdeServer .domain .common .Category ;
19
+ import org .routineade .RoutineAdeServer .dto .firebase .FCMNotificationRequest ;
18
20
import org .routineade .RoutineAdeServer .dto .firebase .UserFirebeseTokenSaveRequest ;
21
+ import org .routineade .RoutineAdeServer .dto .routine .GroupRoutineInfo ;
22
+ import org .routineade .RoutineAdeServer .dto .routine .GroupRoutinesGetResponse ;
23
+ import org .routineade .RoutineAdeServer .dto .routine .PersonalRoutineInfo ;
19
24
import org .routineade .RoutineAdeServer .dto .routine .RoutineCategoryStatisticsInfo ;
20
25
import org .routineade .RoutineAdeServer .dto .routine .RoutinesByUserProfileGetResponse ;
26
+ import org .routineade .RoutineAdeServer .dto .routine .RoutinesGetResponse ;
21
27
import org .routineade .RoutineAdeServer .dto .user .UserInfosGetResponse ;
22
28
import org .routineade .RoutineAdeServer .dto .user .UserRoutineCalenderStatisticsGetResponse ;
23
29
import org .routineade .RoutineAdeServer .dto .user .UserRoutineCategoryStatisticsGetResponse ;
24
30
import org .routineade .RoutineAdeServer .dto .user .UserRoutineCompletionStatistics ;
31
+ import org .routineade .RoutineAdeServer .repository .GroupMemberRepository ;
25
32
import org .routineade .RoutineAdeServer .repository .GroupRepository ;
26
33
import org .routineade .RoutineAdeServer .repository .GroupRoutineRepository ;
27
34
import org .routineade .RoutineAdeServer .repository .UserRepository ;
35
+ import org .springframework .scheduling .annotation .Scheduled ;
28
36
import org .springframework .stereotype .Service ;
29
37
import org .springframework .transaction .annotation .Transactional ;
30
38
import org .springframework .web .multipart .MultipartFile ;
@@ -40,6 +48,8 @@ public class UserService {
40
48
private final S3Service s3Service ;
41
49
private final GroupRepository groupRepository ;
42
50
private final GroupRoutineRepository groupRoutineRepository ;
51
+ private final FCMNotificationService fcmNotificationService ;
52
+ private final GroupMemberRepository groupMemberRepository ;
43
53
private static final String BASIC_PROFILE_IMAGE = "https://routineade-ducket.s3.ap-northeast-2.amazonaws.com/BasicProfile.png" ;
44
54
45
55
@ Transactional (readOnly = true )
@@ -168,52 +178,44 @@ private String saveAndGetImage(MultipartFile image) {
168
178
return s3Service .getFileURLFromS3 (uploadName ).toString ();
169
179
}
170
180
171
- // @Scheduled(cron = "0 0 10 * * *")
172
- // private void sendRoutineAlarm() {
173
- // List<User> users = userRepository.findAll();
174
- //
175
- // for (User user : users) {
176
- // RoutinesGetResponse routines = routineService.getRoutines(user,
177
- // LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy.MM.dd")));
178
- //
179
- // if (routines.personalRoutines().isEmpty() && routines.groupRoutines().isEmpty()) {
180
- // continue;
181
- // }
182
- //
183
- // List<PersonalRoutineInfo> personalRoutineInfos = routines.personalRoutines().stream()
184
- // .flatMap(personalRoutine -> personalRoutine.routines().stream())
185
- // .toList();
186
- //
187
- // for (PersonalRoutineInfo pRoutine : personalRoutineInfos) {
188
- // String routineTitle = pRoutine.routineTitle();
189
- //
190
- // // 내용 수정
191
- // FCMNotificationRequest request = FCMNotificationRequest.of(user, "", "");
192
- //
193
- // fcmNotificationService.sendNotificationByToken(request);
194
- // }
195
- //
196
- // for (GroupRoutinesGetResponse groupRoutine : routines.groupRoutines()) {
197
- // Group group = groupRepository.findById(groupRoutine.groupId())
198
- // .orElseThrow(() -> new IllegalArgumentException("해당 ID의 그룹이 존재하지 않습니다."));
199
- // if (groupMemberRepository.findByGroupAndUser(group, user)
200
- // .orElseThrow(() -> new IllegalArgumentException("해당 유저가 해당 그룹의 멤버가 아닙니다!"))
201
- // .getIsGroupAlarmEnabled()) {
202
- // List<GroupRoutineInfo> gRoutines = groupRoutine.groupRoutines().stream()
203
- // .flatMap(gr -> gr.routines().stream())
204
- // .toList();
205
- //
206
- // for (GroupRoutineInfo gRoutine : gRoutines) {
207
- // String routineTitle = gRoutine.routineTitle();
208
- //
209
- // // 내용 수정
210
- // FCMNotificationRequest request = FCMNotificationRequest.of(user, "", "");
211
- //
212
- // fcmNotificationService.sendNotificationByToken(request);
213
- // }
214
- // }
215
- // }
216
- // }
217
- // }
181
+ @ Scheduled (cron = "0 0 10 * * *" )
182
+ public void sendRoutineAlarm () {
183
+ List <User > users = userRepository .findAll ();
184
+
185
+ for (User user : users ) {
186
+ RoutinesGetResponse routines = routineService .getRoutines (user ,
187
+ LocalDate .now ().format (DateTimeFormatter .ofPattern ("yyyy.MM.dd" )));
188
+ if (routines .personalRoutines ().isEmpty () && routines .groupRoutines ().isEmpty ()) {
189
+ continue ;
190
+ }
191
+ List <PersonalRoutineInfo > personalRoutineInfos = routines .personalRoutines ().stream ()
192
+ .flatMap (personalRoutine -> personalRoutine .routines ().stream ())
193
+ .filter (PersonalRoutineInfo ::isAlarmEnabled )
194
+ .toList ();
195
+
196
+ for (PersonalRoutineInfo pRoutine : personalRoutineInfos ) {
197
+ fcmNotificationService .sendNotificationByToken (
198
+ FCMNotificationRequest .of (user , "루틴에이드" , pRoutine .routineTitle () + " 할 시간이에요!" ));
199
+ }
200
+
201
+ for (GroupRoutinesGetResponse groupRoutine : routines .groupRoutines ()) {
202
+ Group group = groupRepository .findById (groupRoutine .groupId ())
203
+ .orElseThrow (() -> new IllegalArgumentException ("해당 ID의 그룹이 존재하지 않습니다." ));
204
+
205
+ if (groupMemberRepository .findByGroupAndUser (group , user )
206
+ .orElseThrow (() -> new IllegalArgumentException ("해당 유저가 해당 그룹의 멤버가 아닙니다!" ))
207
+ .getIsGroupAlarmEnabled ()) {
208
+ List <GroupRoutineInfo > gRoutines = groupRoutine .groupRoutines ().stream ()
209
+ .flatMap (gr -> gr .routines ().stream ())
210
+ .toList ();
211
+
212
+ for (GroupRoutineInfo gRoutine : gRoutines ) {
213
+ fcmNotificationService .sendNotificationByToken (FCMNotificationRequest .of (user , "루틴에이드" ,
214
+ gRoutine .routineTitle () + " 할 시간이에요!" ));
215
+ }
216
+ }
217
+ }
218
+ }
219
+ }
218
220
219
221
}
0 commit comments