1
1
package org .websoso .WSSServer .service ;
2
2
3
- import static org .websoso .WSSServer .domain .common .NotificationTypeGroup .FEED ;
4
- import static org .websoso .WSSServer .domain .common .NotificationTypeGroup .NOTICE ;
5
- import static org .websoso .WSSServer .exception .error .CustomNotificationError .NOTIFICATION_ALREADY_READ ;
6
- import static org .websoso .WSSServer .exception .error .CustomNotificationError .NOTIFICATION_NOT_FOUND ;
7
- import static org .websoso .WSSServer .exception .error .CustomNotificationError .NOTIFICATION_READ_FORBIDDEN ;
8
- import static org .websoso .WSSServer .exception .error .CustomNotificationError .NOTIFICATION_TYPE_INVALID ;
9
-
10
- import java .util .List ;
11
- import java .util .Set ;
12
- import java .util .stream .Collectors ;
13
3
import lombok .RequiredArgsConstructor ;
14
4
import org .springframework .data .domain .PageRequest ;
15
5
import org .springframework .data .domain .Slice ;
16
6
import org .springframework .stereotype .Service ;
17
7
import org .springframework .transaction .annotation .Transactional ;
18
- import org .websoso .WSSServer .domain .Notification ;
19
- import org .websoso .WSSServer .domain .NotificationType ;
20
- import org .websoso .WSSServer .domain .ReadNotification ;
21
- import org .websoso .WSSServer .domain .User ;
8
+ import org .websoso .WSSServer .domain .*;
22
9
import org .websoso .WSSServer .domain .common .NotificationTypeGroup ;
23
- import org .websoso .WSSServer .dto .notification .NotificationGetResponse ;
24
- import org .websoso .WSSServer .dto .notification .NotificationInfo ;
25
- import org .websoso .WSSServer .dto .notification .NotificationsGetResponse ;
26
- import org .websoso .WSSServer .dto .notification .NotificationsReadStatusGetResponse ;
10
+ import org .websoso .WSSServer .dto .notification .*;
27
11
import org .websoso .WSSServer .exception .exception .CustomNotificationException ;
12
+ import org .websoso .WSSServer .exception .exception .CustomNotificationTypeException ;
13
+ import org .websoso .WSSServer .notification .FCMService ;
14
+ import org .websoso .WSSServer .notification .dto .FCMMessageRequest ;
28
15
import org .websoso .WSSServer .repository .NotificationRepository ;
16
+ import org .websoso .WSSServer .repository .NotificationTypeRepository ;
29
17
import org .websoso .WSSServer .repository .ReadNotificationRepository ;
18
+ import org .websoso .WSSServer .repository .UserRepository ;
19
+
20
+ import java .util .List ;
21
+ import java .util .Set ;
22
+ import java .util .stream .Collectors ;
23
+
24
+ import static org .websoso .WSSServer .domain .common .NotificationTypeGroup .FEED ;
25
+ import static org .websoso .WSSServer .domain .common .NotificationTypeGroup .NOTICE ;
26
+ import static org .websoso .WSSServer .domain .common .Role .ADMIN ;
27
+ import static org .websoso .WSSServer .exception .error .CustomNotificationError .*;
28
+ import static org .websoso .WSSServer .exception .error .CustomNotificationTypeError .NOTIFICATION_TYPE_NOT_FOUND ;
30
29
31
30
@ Service
32
31
@ RequiredArgsConstructor
@@ -36,6 +35,10 @@ public class NotificationService {
36
35
private static final int DEFAULT_PAGE_NUMBER = 0 ;
37
36
private final NotificationRepository notificationRepository ;
38
37
private final ReadNotificationRepository readNotificationRepository ;
38
+ private final NotificationTypeRepository notificationTypeRepository ;
39
+ private final UserRepository userRepository ;
40
+ private final FCMService fcmService ;
41
+ private final UserService userService ;
39
42
40
43
@ Transactional (readOnly = true )
41
44
public NotificationsReadStatusGetResponse checkNotificationsReadStatus (User user ) {
@@ -109,4 +112,72 @@ private void checkIfNotificationAlreadyRead(User user, Notification notification
109
112
"notifications that the user has already read" );
110
113
}
111
114
}
115
+
116
+ public void createNoticeNotification (User user , NotificationCreateRequest request ) {
117
+ validateAdminPrivilege (user );
118
+ validateNoticeType (request .notificationTypeName ());
119
+
120
+ Notification notification = notificationRepository .save (Notification .create (
121
+ request .notificationTitle (),
122
+ request .notificationBody (),
123
+ request .notificationDetail (),
124
+ request .userId (),
125
+ null ,
126
+ getNotificationTypeOrException (request .notificationTypeName ()))
127
+ );
128
+
129
+ sendNoticePushMessage (request .userId (), notification );
130
+ }
131
+
132
+ private void validateAdminPrivilege (User user ) {
133
+ if (user .getRole () != ADMIN ) {
134
+ throw new CustomNotificationException (NOTIFICATION_ADMIN_ONLY ,
135
+ "User who tried to create, modify, or delete the notice is not an ADMIN." );
136
+ }
137
+ }
138
+
139
+ private void validateNoticeType (String notificationTypeName ) {
140
+ if (!NotificationTypeGroup .isTypeInGroup (notificationTypeName , NOTICE )) {
141
+ throw new CustomNotificationException (NOTIFICATION_NOT_NOTICE_TYPE ,
142
+ "given notification type does not belong to the NOTICE category" );
143
+ }
144
+ }
145
+
146
+ private NotificationType getNotificationTypeOrException (String notificationTypeName ) {
147
+ return notificationTypeRepository
148
+ .findOptionalByNotificationTypeName (notificationTypeName )
149
+ .orElseThrow (() -> new CustomNotificationTypeException (NOTIFICATION_TYPE_NOT_FOUND ,
150
+ "notification type with the given name is not found" ));
151
+ }
152
+
153
+ private void sendNoticePushMessage (Long userId , Notification notification ) {
154
+ FCMMessageRequest fcmMessageRequest = FCMMessageRequest .of (
155
+ notification .getNotificationTitle (),
156
+ notification .getNotificationBody (),
157
+ "" ,
158
+ "notificationDetail" ,
159
+ String .valueOf (notification .getNotificationId ())
160
+ );
161
+
162
+ List <String > targetFCMTokens = getTargetFCMTokens (userId );
163
+
164
+ fcmService .sendMulticastPushMessage (targetFCMTokens , fcmMessageRequest );
165
+ }
166
+
167
+ private List <String > getTargetFCMTokens (Long userId ) {
168
+ if (userId .equals (0L )) {
169
+ return userRepository .findAllByIsPushEnabledTrue ()
170
+ .stream ()
171
+ .flatMap (user -> user .getUserDevices ().stream ())
172
+ .map (UserDevice ::getFcmToken )
173
+ .distinct ()
174
+ .toList ();
175
+ }
176
+ return userService .getUserOrException (userId )
177
+ .getUserDevices ()
178
+ .stream ()
179
+ .map (UserDevice ::getFcmToken )
180
+ .distinct ()
181
+ .toList ();
182
+ }
112
183
}
0 commit comments