switch to using stored users routing preferences in all routing services

This commit is contained in:
2026-01-22 16:29:56 +01:00
parent fb9276ca15
commit 76e9251001
5 changed files with 40 additions and 17 deletions
@@ -2,11 +2,11 @@ package SuMoC.backend.routing.controller;
import SuMoC.backend.routing.dtos.RouteDTO;
import SuMoC.backend.routing.dtos.RoutingAlternativeRequestDTO;
import SuMoC.backend.routing.dtos.RoutingPreferencesDTO;
import SuMoC.backend.routing.services.RoutingPreferencesService;
import SuMoC.backend.routing.services.RoutingService;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@@ -20,25 +20,22 @@ import java.util.logging.Logger;
@RestController
@RequestMapping("/routing/alternative")
@RequiredArgsConstructor
public class RoutingController {
private final Logger logger = Logger.getLogger(RoutingController.class.getName());
private final RoutingService routingService;
@Autowired
public RoutingController(RoutingService routingService) {
this.routingService = routingService;
}
private final RoutingPreferencesService routingPreferencesService;
@PostMapping("/request")
@Schema(description = "Calculates alternative, sustainable routes for the request. The delta of departure and arrival in the requestdto is used as baseline to determine if alternatives are acceptable.")
public ResponseEntity<List<RouteDTO>> requestAlternativeRoutes(@Valid @RequestBody RoutingAlternativeRequestDTO routingAlternativeRequestDTO) {
try {
if (routingAlternativeRequestDTO.getRoutingPreferencesDTO() == null) { // Use default preferences if none are provided
// TODO we might want to also save users preferences in the future
var preferences = new RoutingPreferencesDTO(); // use default preferences
routingAlternativeRequestDTO.setRoutingPreferencesDTO(preferences);
if (routingAlternativeRequestDTO.getRoutingPreferencesDTO() == null) {
// This automatically uses default preferences for unauthenticated users
routingAlternativeRequestDTO.setRoutingPreferencesDTO(routingPreferencesService.getRoutingPreferencesForCurrentUser());
}
var routeDTOs = routingService.determineAlternativeRoutesOnUserRequest(routingAlternativeRequestDTO);
@@ -5,6 +5,7 @@ import SuMoC.backend.routing.dtos.RoutingPreferencesDTO;
import SuMoC.backend.routing.mapper.RoutingPreferencesMapper;
import SuMoC.backend.routing.models.RoutingPreferences;
import SuMoC.backend.routing.repository.RoutingPreferencesRepository;
import SuMoC.backend.user.service.CurrentUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@@ -18,6 +19,8 @@ public class RoutingPreferencesService {
private final RoutingPreferencesMapper routingPreferencesMapper;
private final CurrentUserService currentUserService;
public void updateUserRoutingPreferences(Long userId, RoutingPreferencesDTO newPreferencesDTO) {
Optional<RoutingPreferences> optionalPreferences = routingPreferencesRepository.findByUserId(userId);
@@ -29,6 +32,9 @@ public class RoutingPreferencesService {
routingPreferencesRepository.save(preferences);
}
/**
* Get routing preferences for a specific user.
*/
public RoutingPreferencesDTO getUserRoutingPreferences(Long userId) {
Optional<RoutingPreferences> optionalPreferences = routingPreferencesRepository.findByUserId(userId);
@@ -41,4 +47,16 @@ public class RoutingPreferencesService {
}
return preferencesDTO;
}
/**
* Gets routing preferences for the current user, if user is logged in returns his preferences, if not logged in returns default preferences.
*/
public RoutingPreferencesDTO getRoutingPreferencesForCurrentUser() {
if (currentUserService.isAuthenticated()) {
var currentUser = currentUserService.get();
return getUserRoutingPreferences(currentUser.getId());
} else {
return routingPreferencesMapper.toRoutingPreferencesDTO(RoutingPreferences.getDefaultPreferences());
}
}
}
@@ -6,6 +6,7 @@ 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.RoutingPreferencesService;
import SuMoC.backend.routing.services.RoutingService;
import SuMoC.backend.suggestion.config.AdHocSuggestionProperties;
import SuMoC.backend.trip.Modality;
@@ -24,6 +25,8 @@ public class AdHocSuggestionService {
private final RoutingService routingService;
private final RoutingPreferencesService routingPreferencesService;
private final AdHocSuggestionProperties adHocSuggestionProperties;
private final NotificationSchedulerService notificationSchedulerService;
@@ -36,15 +39,14 @@ public class AdHocSuggestionService {
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
return; // only suggest alternatives for trips mainly done by car, motorcycle or taxi
}
var viableAlternatives = routingService.determineAlternativeRoutesAdHoc(alternativeRequestFromTrip(newTrip));
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();
RoutingPreferencesDTO preferencesDTO = routingPreferencesService.getUserRoutingPreferences(newTrip.getUser().getId());
List<RouteDTO> bestAlternatives = pickBestAlternatives(viableAlternatives, preferencesDTO);
notificationSchedulerService.schedule(
@@ -90,8 +92,7 @@ public class AdHocSuggestionService {
}
private RoutingAlternativeRequestDTO alternativeRequestFromTrip(Trip trip) {
// TODO for now we assume the preferences here, it would be better if we store these per user
var preferences = new RoutingPreferencesDTO();
var preferences = routingPreferencesService.getUserRoutingPreferences(trip.getUser().getId());
return RoutingAlternativeRequestDTO.builder()
.originLat(trip.getStartLatitude())
.originLon(trip.getStartLongitude())
@@ -7,6 +7,7 @@ import SuMoC.backend.regulartrip.dtos.DetectedRegularTripDTO;
import SuMoC.backend.regulartrip.services.RegularTripDetectionService;
import SuMoC.backend.routing.dtos.RoutingAlternativeRequestDTO;
import SuMoC.backend.routing.dtos.RoutingPreferencesDTO;
import SuMoC.backend.routing.services.RoutingPreferencesService;
import SuMoC.backend.routing.services.RoutingService;
import SuMoC.backend.suggestion.config.RegularTripSuggestionProperties;
import com.uber.h3core.util.LatLng;
@@ -26,6 +27,8 @@ public class RegularTripSuggestionService {
private final RoutingService routingService;
private final RoutingPreferencesService routingPreferencesService;
private final RegularTripSuggestionProperties properties;
private final NotificationSchedulerService notificationSchedulerService;
@@ -46,8 +49,7 @@ public class RegularTripSuggestionService {
public void checkAlternativesAndNotify(HashMap<Long, List<DetectedRegularTripDTO>> detectedRegularTripsByUser) {
for (Long userId : detectedRegularTripsByUser.keySet()) {
var detectedRegularTrips = detectedRegularTripsByUser.get(userId);
// TODO for now we use default preferences, in the future we should store user preferences and use them here
RoutingPreferencesDTO preferencesDTO = new RoutingPreferencesDTO();
RoutingPreferencesDTO preferencesDTO = routingPreferencesService.getUserRoutingPreferences(userId);
// For each detected regular trip, determine alternatives and notify the user if applicable
for (var detectedRegularTrip : detectedRegularTrips) {
@@ -31,4 +31,9 @@ public class CurrentUserService {
HttpStatus.NOT_FOUND,
"Der Nutzer existiert nicht"));
}
public boolean isAuthenticated() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
return auth != null && auth.isAuthenticated();
}
}