77import org .example .backend .domain .lecture .repository .LectureRepository ;
88import org .example .backend .domain .notification .converter .NotificationConverter ;
99import org .example .backend .domain .notification .dto .response .NotificationResponseDTO ;
10+ import org .example .backend .domain .notification .entity .AlarmType ;
1011import org .example .backend .domain .notification .entity .Notification ;
1112import org .example .backend .domain .notification .repository .NotificationRepository ;
13+ import org .example .backend .domain .notificationSetting .service .FcmService ;
14+ import org .example .backend .domain .notificationSetting .service .NotificationTemplateService ;
15+ import org .example .backend .global .userdeviceToken .repository .UserDeviceTokenRepository ;
1216import org .springframework .stereotype .Service ;
17+ import org .springframework .transaction .annotation .Transactional ;
1318
1419import java .util .List ;
1520import java .util .UUID ;
@@ -20,7 +25,10 @@ public class NotificationService implements NotificationServiceImpl{
2025 private final NotificationRepository notificationRepository ;
2126 private final LectureRepository lectureRepository ;
2227 private final ClassroomRepository classroomRepository ;
23- private final NotificationConverter notificationConverter ;;
28+ private final NotificationConverter notificationConverter ;
29+ private final NotificationTemplateService templateService ;
30+ private final UserDeviceTokenRepository tokenRepository ;
31+ private final FcmService fcmService ;
2432
2533 public List <NotificationResponseDTO > getNotificationsByUserId (UUID userId ) {
2634 List <Notification > notificationList =
@@ -39,4 +47,32 @@ public List<NotificationResponseDTO> getNotificationsByUserId(UUID userId) {
3947 })
4048 .toList ();
4149 }
50+
51+ public void sendAlarmToProfessor (UUID lectureId , AlarmType type , String senderName , String extra ) {
52+ Lecture lecture = lectureRepository .findById (lectureId )
53+ .orElseThrow (() -> new RuntimeException ("Lecture not found" ));
54+
55+ UUID professorId = lecture .getClassroom ().getProfessor ().getId ();
56+
57+ String title = templateService .getTitle (type );
58+ String body = templateService .getBody (type , senderName , extra );
59+
60+ var tokens = tokenRepository .findAllByUserIdAndIsActiveTrue (professorId );
61+ tokens .forEach (token ->
62+ fcmService .sendNotification (token .getFcmToken (), title , body )
63+ );
64+
65+ Notification notification = Notification .builder ()
66+ .user (lecture .getClassroom ().getProfessor ())
67+ .lecture (lecture )
68+ .alarmType (type )
69+ .isRead (false )
70+ .build ();
71+ notificationRepository .save (notification );
72+ }
73+
74+ @ Transactional
75+ public void markAllAsRead (UUID userId ) {
76+ notificationRepository .markAllAsReadByUserId (userId );
77+ }
4278}
0 commit comments