package SuMoC.backend.notification.services; import SuMoC.backend.notification.enums.NotificationStatus; import SuMoC.backend.notification.enums.NotificationType; import SuMoC.backend.notification.interfaces.Notifieable; import SuMoC.backend.notification.models.Notification; import SuMoC.backend.notification.repositories.NotificationRepository; import SuMoC.backend.user.models.User; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.persistence.EntityManager; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.time.Instant; import java.time.LocalTime; import java.util.List; /** * This service is the main entry point to schedule notifications to be sent to a users' mobile device. * It creates Notification entities in the database with status PENDING which are then later processed by the notification worker. */ @Service @RequiredArgsConstructor public class NotificationSchedulerService { private final ObjectMapper objectMapper; private final NotificationRepository notificationRepository; private final EntityManager entityManager; /** * Can be used to schedule multiple notifications at once. Notifieable interface allows sending to multiple users. * dataBodies need to be serializable to JSON. * * @param dataBodies * @param notificationType * @param earliestTimeOfDayToSend */ public void scheduleAll(List dataBodies, NotificationType notificationType, LocalTime earliestTimeOfDayToSend ) { List notifications = dataBodies.stream().map(dataBody -> { String dataBodyJson; try { dataBodyJson = objectMapper.writeValueAsString(dataBody); } catch (Exception e) { throw new RuntimeException("Failed to serialize notification data body", e); } User userProxy = entityManager.getReference(User.class, dataBody.getUserId()); return Notification.builder() .user(userProxy) .type(notificationType) .data(dataBodyJson) .earliestTimeOfDayToSend(earliestTimeOfDayToSend) .status(NotificationStatus.PENDING) .createdAt(Instant.now()) .failedSendAttempts(0) .build(); }).toList(); notificationRepository.saveAll(notifications); } /** * Schedules a notification to be sent to the specified user. * Data body needs to be serializable to JSON. * * @param userId * @param dataBody * @param notificationType * @param earliestTimeOfDayToSend TOD from which on the notification can be sent. If null, send as soon as possible. */ public void schedule( Long userId, Object dataBody, NotificationType notificationType, LocalTime earliestTimeOfDayToSend ) { String dataBodyJson; try { dataBodyJson = objectMapper.writeValueAsString(dataBody); } catch (Exception e) { throw new RuntimeException("Failed to serialize notification data body", e); } if (earliestTimeOfDayToSend == null) { earliestTimeOfDayToSend = LocalTime.now(); } User userProxy = entityManager.getReference(User.class, userId); Notification notification = Notification.builder() .user(userProxy) .type(notificationType) .data(dataBodyJson) .earliestTimeOfDayToSend(earliestTimeOfDayToSend) .status(NotificationStatus.PENDING) .createdAt(Instant.now()) .failedSendAttempts(0) .build(); notificationRepository.save(notification); } }