From 8ee6992f88d84907c99c3261056e61e43ee4afce Mon Sep 17 00:00:00 2001 From: David Neuhauss Date: Wed, 14 Jan 2026 15:34:59 +0100 Subject: [PATCH] send ad hoc alternative route notifications for recent uploaded trips --- .../NotificationSchedulerService.java | 28 +++++++++ .../services/AdHocSuggestionService.java | 57 +++++++++++++++++-- 2 files changed, 80 insertions(+), 5 deletions(-) diff --git a/src/main/java/SuMoC/backend/notification/services/NotificationSchedulerService.java b/src/main/java/SuMoC/backend/notification/services/NotificationSchedulerService.java index 346d8a7..e5bdbea 100644 --- a/src/main/java/SuMoC/backend/notification/services/NotificationSchedulerService.java +++ b/src/main/java/SuMoC/backend/notification/services/NotificationSchedulerService.java @@ -52,4 +52,32 @@ public class NotificationSchedulerService { notificationRepository.saveAll(notifications); } + + 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); + } + + User userProxy = entityManager.getReference(User.class, userId); + + Notification notification = Notification.builder() + .user(userProxy) + .type(notificationType) + .data(dataBodyJson) + .earliestTimeOfDayToSend(earliestTimeOfDayToSend) + .status(NotificationStatus.PENDING) + .createdAt(LocalDateTime.now()) + .failedSendAttempts(0) + .build(); + + notificationRepository.save(notification); + } } diff --git a/src/main/java/SuMoC/backend/suggestion/services/AdHocSuggestionService.java b/src/main/java/SuMoC/backend/suggestion/services/AdHocSuggestionService.java index f6de03c..ad90735 100644 --- a/src/main/java/SuMoC/backend/suggestion/services/AdHocSuggestionService.java +++ b/src/main/java/SuMoC/backend/suggestion/services/AdHocSuggestionService.java @@ -1,16 +1,22 @@ package SuMoC.backend.suggestion.services; +import SuMoC.backend.notification.enums.NotificationType; +import SuMoC.backend.notification.services.NotificationSchedulerService; +import SuMoC.backend.routing.dtos.RouteDTO; import SuMoC.backend.routing.dtos.RoutingAlternativeRequestDTO; import SuMoC.backend.routing.dtos.RoutingPreferencesDTO; import SuMoC.backend.routing.services.RoutingService; import SuMoC.backend.suggestion.config.AdHocSuggestionProperties; +import SuMoC.backend.trip.Modality; import SuMoC.backend.trip.models.Trip; import lombok.RequiredArgsConstructor; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import java.time.Instant; +import java.time.LocalTime; +import java.util.List; @Service @RequiredArgsConstructor @@ -20,6 +26,8 @@ public class AdHocSuggestionService { private final AdHocSuggestionProperties adHocSuggestionProperties; + private final NotificationSchedulerService notificationSchedulerService; + /** * Receives a trip a user has just uploaded, if it is a recent trip and there is a more sustainable alternative, * schedules a notification to push to the user. @@ -27,14 +35,53 @@ public class AdHocSuggestionService { @Async public void checkNewTripForNotifiedAlternativeSuggestion(Trip newTrip) { if (!isRecentTrip(newTrip)) return; + if (!(newTrip.getMainModality().equals(Modality.CAR) || newTrip.getMainModality().equals(Modality.MOTORCYCLE) || newTrip.getMainModality().equals(Modality.TAXI))) { + return ; // only suggest alternatives for trips mainly done by car, motorcycle or taxi + } var viableAlternatives = routingService.determineAlternativeRoutesAdHoc(alternativeRequestFromTrip(newTrip)); - // check if there are viable alternative routings - // if yes, generate notification - // TODO notification generator - // schedule send of notification - // TODO sending of notification + if (!viableAlternatives.isEmpty()) { + // Since notification bodies have a size limit, we only send the best alternative + // TODO we assume default preferences, in the future we should store user preferences and use them here + RoutingPreferencesDTO preferencesDTO = new RoutingPreferencesDTO(); + List bestAlternatives = pickBestAlternatives(viableAlternatives, preferencesDTO); + + notificationSchedulerService.schedule( + newTrip.getUser().getId(), + bestAlternatives, + NotificationType.ADHOC_ALTERNATIVE, + LocalTime.now() + ); + } + } + + /** + * Picks the best alternative route, if user does not prefer physical activity, picks the fastes route. + * If user prefers physical activity, picks fastest of all modalities plus fastest physical activity only route. + */ + private List pickBestAlternatives(List routes, RoutingPreferencesDTO preferences) { + List bestAlternatives = new java.util.ArrayList<>(); + if (preferences.isPreferPhysicalActivity()) { + // find fastest physical activity only route + routes.stream() + .filter(RouteDTO::isPhysicalActivity) + .min((r1, r2) -> Long.compare( + r1.getDurationSeconds(), + r2.getDurationSeconds() + )).ifPresent(bestAlternatives::add); + } + // find fastest route overall + routes.stream() + .min((r1, r2) -> Long.compare( + r1.getDurationSeconds(), + r2.getDurationSeconds() + )).ifPresent(fastestRoute -> { + if (!bestAlternatives.contains(fastestRoute)) { + bestAlternatives.add(fastestRoute); + } + }); + return bestAlternatives; } private boolean isRecentTrip(Trip trip) {