implemented scheduled worker that processes queued notifications

This commit is contained in:
2026-01-13 16:42:31 +01:00
parent bef418d77d
commit 06aa0ce3cc
14 changed files with 225 additions and 104 deletions
@@ -3,14 +3,19 @@ package SuMoC.backend.dev;
import SuMoC.backend.firebase.FirebaseMessageBuilder;
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.scheduling.ScheduledTasks;
import SuMoC.backend.trend.dto.TrendDetectionDTO;
import SuMoC.backend.trend.services.StatTableUpdateService;
import SuMoC.backend.user.models.User;
import SuMoC.backend.user.repository.UserRepository;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.Message;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
@@ -18,6 +23,8 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
@RestController
@RequestMapping("/dev")
@@ -31,6 +38,8 @@ public class DevController {
private final ForecastService forecastService;
private final FirebaseMessageBuilder firebaseMessageBuilder;
private final ObjectMapper objectMapper;
private final NotificationRepository notificationRepository;
private final UserRepository userRepository;
public DevController(
ScheduledTasks scheduledTasks,
@@ -39,7 +48,8 @@ public class DevController {
MeteoClient meteoClient,
ForecastService forecastService,
FirebaseMessageBuilder firebaseMessageBuilder,
ObjectMapper objectMapper
ObjectMapper objectMapper,
NotificationRepository notificationRepository, UserRepository userRepository
) {
this.scheduledTasks = scheduledTasks;
this.statTableUpdateService = statTableUpdateService;
@@ -48,6 +58,8 @@ public class DevController {
this.forecastService = forecastService;
this.firebaseMessageBuilder = firebaseMessageBuilder;
this.objectMapper = objectMapper;
this.notificationRepository = notificationRepository;
this.userRepository = userRepository;
}
@PostMapping("/trigger-trend-calculation")
@@ -61,6 +73,7 @@ public class DevController {
}
@GetMapping("/test-firebase/{fcm_token}")
@Operation(description = "Immediately sends a test notification of type TREND to the provided FCM token.")
public ResponseEntity<String> testFirebaseConnection(@PathVariable("fcm_token") @NotNull String fcmToken) {
if (firebaseMessaging == null) {
return ResponseEntity.status(503)
@@ -83,6 +96,33 @@ public class DevController {
}
}
@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() {
if (firebaseMessaging == null) {
return ResponseEntity.status(503)
.body("✗ Firebase is not configured (firebase.config.enabled=false)");
}
try {
User user = userRepository.findByEmail("user1@email.com").orElseThrow();
Notification notification = Notification.builder()
.type(NotificationType.TREND)
.data(objectMapper.writeValueAsString(TrendDetectionDTO.mockTrendDetectionDTO()))
.user(user)
.earliestTimeOfDayToSend(LocalTime.now())
.createdAt(LocalDateTime.now())
.status(NotificationStatus.PENDING)
.build();
notificationRepository.save(notification);
return ResponseEntity.ok("✓ Test notification queued.");
} catch (Exception e) {
return ResponseEntity.status(500).body("✗ Failed: " + e.getMessage());
}
}
@GetMapping("/test-meteo")
public void testMeteoClient() {
try {