add notification for received friendship request

This commit is contained in:
2026-01-30 11:49:53 +01:00
parent 48063d442c
commit 06b35c13b6
6 changed files with 109 additions and 58 deletions
@@ -15,6 +15,10 @@ import java.time.LocalDateTime;
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 {
@@ -25,6 +29,14 @@ public class NotificationSchedulerService {
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<Notifieable> dataBodies,
NotificationType notificationType,
LocalTime earliestTimeOfDayToSend
@@ -53,11 +65,20 @@ public class NotificationSchedulerService {
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
NotificationType notificationType,
LocalTime earliestTimeOfDayToSend
) {
String dataBodyJson;
try {
@@ -66,6 +87,10 @@ public class NotificationSchedulerService {
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()