send ad hoc alternative route notifications for recent uploaded trips
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<RouteDTO> 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<RouteDTO> pickBestAlternatives(List<RouteDTO> routes, RoutingPreferencesDTO preferences) {
|
||||
List<RouteDTO> 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) {
|
||||
|
||||
Reference in New Issue
Block a user