regular trip calculation: batch db writes for improved performance

This commit is contained in:
2026-02-09 13:56:59 +01:00
parent a401b9a6c1
commit f7d7f6bc53
6 changed files with 139 additions and 14 deletions
@@ -3,6 +3,7 @@ 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.interfaces.NotifieableWithTime;
import SuMoC.backend.notification.models.Notification;
import SuMoC.backend.notification.repositories.NotificationRepository;
import SuMoC.backend.user.models.User;
@@ -65,6 +66,33 @@ public class NotificationSchedulerService {
notificationRepository.saveAll(notifications);
}
public void scheduleAll(List<NotifieableWithTime> dataBodies,
NotificationType notificationType
) {
List<Notification> 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(dataBody.getEarliestTimeOfDayToSend())
.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.