implement full service chain where scheduled task detects regular trips every day and schedules notifications with better alternatives if available
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good

This commit is contained in:
2026-01-16 13:19:19 +01:00
parent b08d9dd22d
commit d099909aab
13 changed files with 299 additions and 24 deletions
@@ -1,14 +1,17 @@
package SuMoC.backend.dev;
import SuMoC.backend.firebase.FirebaseMessageBuilder;
import SuMoC.backend.h3.H3Service;
import SuMoC.backend.meteo.ForecastService;
import SuMoC.backend.meteo.MeteoClient;
import SuMoC.backend.notification.enums.NotificationStatus;
import SuMoC.backend.notification.enums.NotificationType;
import SuMoC.backend.notification.models.Notification;
import SuMoC.backend.notification.repositories.NotificationRepository;
import SuMoC.backend.regulartrip.dtos.DetectedRegularTripDTO;
import SuMoC.backend.routing.dtos.RouteDTO;
import SuMoC.backend.scheduling.ScheduledTasks;
import SuMoC.backend.suggestion.services.RegularTripSuggestionService;
import SuMoC.backend.trend.dto.TrendDetectionDTO;
import SuMoC.backend.trend.services.StatTableUpdateService;
import SuMoC.backend.user.mapper.UserMapper;
@@ -28,6 +31,7 @@ import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
@RestController
@@ -45,6 +49,8 @@ public class DevController {
private final NotificationRepository notificationRepository;
private final UserRepository userRepository;
private final UserMapper userMapper;
private final H3Service h3Service;
private final RegularTripSuggestionService regularTripSuggestionService;
public DevController(
ScheduledTasks scheduledTasks,
@@ -54,7 +60,7 @@ public class DevController {
ForecastService forecastService,
FirebaseMessageBuilder firebaseMessageBuilder,
ObjectMapper objectMapper,
NotificationRepository notificationRepository, UserRepository userRepository, UserMapper userMapper
NotificationRepository notificationRepository, UserRepository userRepository, UserMapper userMapper, H3Service h3Service, RegularTripSuggestionService regularTripSuggestionService
) {
this.scheduledTasks = scheduledTasks;
this.statTableUpdateService = statTableUpdateService;
@@ -66,6 +72,8 @@ public class DevController {
this.notificationRepository = notificationRepository;
this.userRepository = userRepository;
this.userMapper = userMapper;
this.h3Service = h3Service;
this.regularTripSuggestionService = regularTripSuggestionService;
}
@PostMapping("/trigger-trend-calculation")
@@ -112,7 +120,6 @@ public class DevController {
try {
Notification notification = new Notification();
notification.setType(NotificationType.ADHOC_ALTERNATIVE);
// Using TrendDetectionDTO as a placeholder, replace with actual AdhocAlternativeDTO when available
List<RouteDTO> mockRoutes = List.of(RouteDTO.getMockRouteDTO(), RouteDTO.getMockRouteDTO());
notification.setData(objectMapper.writeValueAsString(mockRoutes));
@@ -128,6 +135,57 @@ public class DevController {
}
}
@GetMapping("/test-firebase-regular-trip-notification/{fcm_token}")
@Operation(description = "Immediately sends a test notification of type REGULAR_TRIP_ALTERNATIVE to the provided FCM token.")
public ResponseEntity<String> SendTestRegularTripAlternativeNotificationFirebase(@PathVariable("fcm_token") @NotNull String fcmToken) {
if (firebaseMessaging == null) {
return ResponseEntity.status(503)
.body("✗ Firebase is not configured (firebase.config.enabled=false)");
}
try {
Notification notification = new Notification();
notification.setType(NotificationType.REGULAR_TRIP_ALTERNATIVE);
RouteDTO mockRoute = RouteDTO.getMockRouteDTO();
notification.setData(objectMapper.writeValueAsString(mockRoute));
Message mockRegularTripMessage = firebaseMessageBuilder.generateMessage(notification, fcmToken);
// Send to a topic (no device registration needed)
String message = firebaseMessaging.sendAsync(
mockRegularTripMessage
).get();
return ResponseEntity.ok("✓ Firebase message sent to topic: " + message);
} catch (Exception e) {
return ResponseEntity.status(500).body("✗ Failed: " + e.getMessage());
}
}
@GetMapping("/test-notification-from-detected-regular-trip")
@Operation(description = "Testing endpoint that mocks the detection of regular trips and then tests the alternative calculation and notification scheduling.")
public ResponseEntity<String> testNotificationFromDetectedRegularTrip() {
if (firebaseMessaging == null) {
return ResponseEntity.status(503)
.body("✗ Firebase is not configured (firebase.config.enabled=false)");
}
try {
// we seed user 1 and 2 in the dev data loader
HashMap<Long, List<DetectedRegularTripDTO>> detectedRegularTripsByUser = new HashMap<>();
List<DetectedRegularTripDTO> list1 = List.of(
DetectedRegularTripDTO.getTriggeringMock(h3Service, 1L)
);
List<DetectedRegularTripDTO> list2 = List.of(
DetectedRegularTripDTO.getTriggeringMock(h3Service, 2L)
);
detectedRegularTripsByUser.put(1L, list1);
detectedRegularTripsByUser.put(2L, list2);
regularTripSuggestionService.checkAlternativesAndNotify(detectedRegularTripsByUser);
return ResponseEntity.ok("✓ Test notifications from detected regular trips processed.");
} catch (Exception e) {
return ResponseEntity.status(500).body("✗ Failed: " + e.getMessage());
}
}
@Operation(description = "Immediately queues a notification to the fcm key specified in application-dev.properties to be processed by the notification queue.")
@GetMapping("/queue-test-notification")
public ResponseEntity<String> queueTestNotification() {