add demo endpoint for notification mocks
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
This commit is contained in:
@@ -1,17 +1,21 @@
|
||||
package SuMoC.backend.dev;
|
||||
|
||||
import SuMoC.backend.firebase.MessageBuilder;
|
||||
import SuMoC.backend.meteo.ForecastService;
|
||||
import SuMoC.backend.meteo.MeteoClient;
|
||||
import SuMoC.backend.notification.enums.NotificationType;
|
||||
import SuMoC.backend.notification.models.Notification;
|
||||
import SuMoC.backend.scheduling.ScheduledTasks;
|
||||
import SuMoC.backend.trend.dto.TrendDetectionDTO;
|
||||
import SuMoC.backend.trend.services.StatTableUpdateService;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.firebase.messaging.FirebaseMessaging;
|
||||
import com.google.firebase.messaging.Message;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@@ -25,18 +29,25 @@ public class DevController {
|
||||
private final FirebaseMessaging firebaseMessaging;
|
||||
private final MeteoClient meteoClient;
|
||||
private final ForecastService forecastService;
|
||||
private final MessageBuilder messageBuilder;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public DevController(
|
||||
ScheduledTasks scheduledTasks,
|
||||
StatTableUpdateService statTableUpdateService,
|
||||
@Autowired(required = false) FirebaseMessaging firebaseMessaging,
|
||||
MeteoClient meteoClient,
|
||||
ForecastService forecastService) {
|
||||
ForecastService forecastService,
|
||||
MessageBuilder messageBuilder,
|
||||
ObjectMapper objectMapper
|
||||
) {
|
||||
this.scheduledTasks = scheduledTasks;
|
||||
this.statTableUpdateService = statTableUpdateService;
|
||||
this.firebaseMessaging = firebaseMessaging;
|
||||
this.meteoClient = meteoClient;
|
||||
this.forecastService = forecastService;
|
||||
this.messageBuilder = messageBuilder;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
@PostMapping("/trigger-trend-calculation")
|
||||
@@ -49,19 +60,22 @@ public class DevController {
|
||||
statTableUpdateService.updateWeeklyMobilityStatsForAllUsers(day3WeeksAgo);
|
||||
}
|
||||
|
||||
@GetMapping("/test-firebase")
|
||||
public ResponseEntity<String> testFirebaseConnection() {
|
||||
@GetMapping("/test-firebase/{fcm_token}")
|
||||
public ResponseEntity<String> testFirebaseConnection(@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.TREND);
|
||||
notification.setData(objectMapper.writeValueAsString(TrendDetectionDTO.mockTrendDetectionDTO()));
|
||||
|
||||
Message mockTrendMessage = messageBuilder.generateMessage(notification, fcmToken);
|
||||
|
||||
// Send to a topic (no device registration needed)
|
||||
String message = firebaseMessaging.sendAsync(
|
||||
com.google.firebase.messaging.Message.builder()
|
||||
.setTopic("test-topic")
|
||||
.putData("test", "connection-check")
|
||||
.build()
|
||||
mockTrendMessage
|
||||
).get();
|
||||
return ResponseEntity.ok("✓ Firebase message sent to topic: " + message);
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package SuMoC.backend.firebase;
|
||||
|
||||
import SuMoC.backend.notification.models.Notification;
|
||||
import com.google.firebase.messaging.Message;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class MessageBuilder {
|
||||
|
||||
public Message generateMessage(Notification notification, String overrideToken) {
|
||||
// TODO get users token from database
|
||||
String token = "fEfi8Ao_SQiqURpniIT5cF:APA91bHdfhPIpoSC4Bw7HHr4LzszBWvkyJ4sX0jkAr03gG1XTfRkVxJvLEmV0DiiYzRHN-l2LZVgHicxeWGQek-qYvAkJylfXHxn902nGZOgQ5jyNYqN58M";
|
||||
|
||||
if (overrideToken != null && !overrideToken.isEmpty()) {
|
||||
token = overrideToken;
|
||||
}
|
||||
|
||||
var builder = Message.builder().setToken(token);
|
||||
builder.putData("type", notification.getType().toString());
|
||||
builder.putData("data", notification.getData());
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package SuMoC.backend.notification;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/notifications")
|
||||
public class NotificationController {
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package SuMoC.backend.notification.enums;
|
||||
|
||||
/**
|
||||
* Notification type that will be included in the data sent to the mobile devices.
|
||||
*/
|
||||
public enum NotificationType {
|
||||
/**
|
||||
* Informing about a change in mobility trends, e.g. driving less car.
|
||||
*/
|
||||
TREND,
|
||||
/**
|
||||
* An alternative suggestion following the upload of a recent trip.
|
||||
*/
|
||||
ADHOC_ALTERNATIVE,
|
||||
/**
|
||||
* An alternative suggestion that refers to a detected regular trip.
|
||||
* E.g. user drives by car every monday at 8 am and there is a sustainable alternative.
|
||||
*/
|
||||
REGULAR_TRIP_ALTERNATIVE
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package SuMoC.backend.notification.factories;
|
||||
|
||||
import SuMoC.backend.notification.models.Notification;
|
||||
import SuMoC.backend.trend.TrendDetection;
|
||||
import SuMoC.backend.trend.dto.TrendDetectionDTO;
|
||||
import SuMoC.backend.trend.services.TrendMessageGenerator;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -12,7 +12,7 @@ public class TrendNotificationFactory {
|
||||
|
||||
private final TrendMessageGenerator trendMessageGenerator;
|
||||
|
||||
public Notification createNotification(TrendDetection trend) {
|
||||
public Notification createNotification(TrendDetectionDTO trend) {
|
||||
return Notification.builder()
|
||||
.user(trend.getUser())
|
||||
.build();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package SuMoC.backend.notification.models;
|
||||
|
||||
import SuMoC.backend.notification.enums.NotificationStatus;
|
||||
import SuMoC.backend.notification.enums.NotificationType;
|
||||
import SuMoC.backend.user.models.User;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.*;
|
||||
@@ -25,7 +26,10 @@ public class Notification {
|
||||
private User user;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String message;
|
||||
private NotificationType type;
|
||||
|
||||
@Column(columnDefinition = "TEXT")
|
||||
private String data; // Store serialized DTO as JSON
|
||||
|
||||
/**
|
||||
* Time of day from which on a notification might be sent.
|
||||
|
||||
@@ -3,7 +3,7 @@ package SuMoC.backend.notification.services;
|
||||
import SuMoC.backend.notification.factories.TrendNotificationFactory;
|
||||
import SuMoC.backend.notification.models.Notification;
|
||||
import SuMoC.backend.notification.repositories.NotificationRepository;
|
||||
import SuMoC.backend.trend.TrendDetection;
|
||||
import SuMoC.backend.trend.dto.TrendDetectionDTO;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -17,7 +17,7 @@ public class TrendNotificationService {
|
||||
|
||||
private final TrendNotificationFactory trendNotificationFactory;
|
||||
|
||||
public List<Notification> generateTrendNotifications(List<TrendDetection> trendList) {
|
||||
public List<Notification> generateTrendNotifications(List<TrendDetectionDTO> trendList) {
|
||||
List<Notification> notificationList = trendList.stream().map(trendNotificationFactory::createNotification).toList();
|
||||
|
||||
return List.of();
|
||||
|
||||
@@ -98,6 +98,7 @@ public class SecurityConfiguration {
|
||||
auth.requestMatchers("/friendship/**").hasAnyRole("ADMIN", "USER");
|
||||
auth.requestMatchers("/trip/**").hasAnyRole("USER", "ADMIN");
|
||||
auth.requestMatchers("/geocoding/**").hasAnyRole("USER", "ADMIN");
|
||||
auth.requestMatchers("/notifications/**").hasAnyRole("USER", "ADMIN");
|
||||
//auth.requestMatchers("/error").permitAll();
|
||||
auth.anyRequest().authenticated();
|
||||
|
||||
|
||||
+13
-2
@@ -1,4 +1,4 @@
|
||||
package SuMoC.backend.trend;
|
||||
package SuMoC.backend.trend.dto;
|
||||
|
||||
import SuMoC.backend.trend.enums.*;
|
||||
import SuMoC.backend.user.models.User;
|
||||
@@ -9,7 +9,7 @@ import lombok.Data;
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
public class TrendDetection {
|
||||
public class TrendDetectionDTO {
|
||||
private User user;
|
||||
private TrendTimespan timespan;
|
||||
private TrendType type;
|
||||
@@ -17,6 +17,17 @@ public class TrendDetection {
|
||||
private DeltaType deltaType;
|
||||
private double changeValue;
|
||||
|
||||
public static TrendDetectionDTO mockTrendDetectionDTO() {
|
||||
return TrendDetectionDTO.builder()
|
||||
.user(null) // Replace with a mock User if needed
|
||||
.timespan(TrendTimespan.WEEKLY)
|
||||
.type(TrendType.INCREASING)
|
||||
.attribute(StatAttribute.CAR_DISTANCE)
|
||||
.deltaType(DeltaType.ABSOLUTE)
|
||||
.changeValue(15.0)
|
||||
.build();
|
||||
}
|
||||
|
||||
public TrendConnotation getTrendConnotation() {
|
||||
if (this.type == TrendType.INCREASING) {
|
||||
return switch (attribute) {
|
||||
@@ -1,8 +1,8 @@
|
||||
package SuMoC.backend.trend.services;
|
||||
|
||||
import SuMoC.backend.trend.TrendDetection;
|
||||
import SuMoC.backend.trend.WeeklyMobilityStatsRepository;
|
||||
import SuMoC.backend.trend.config.TrendDetectionProperties;
|
||||
import SuMoC.backend.trend.dto.TrendDetectionDTO;
|
||||
import SuMoC.backend.trend.enums.DeltaType;
|
||||
import SuMoC.backend.trend.enums.StatAttribute;
|
||||
import SuMoC.backend.trend.enums.TrendTimespan;
|
||||
@@ -24,7 +24,7 @@ public class TrendDetectorService {
|
||||
|
||||
private final TrendDetectionProperties trendDetectionProperties;
|
||||
|
||||
public List<TrendDetection> detectWeeklyTrends(Integer year, Integer weekNumber, StatAttribute attribute) {
|
||||
public List<TrendDetectionDTO> detectWeeklyTrends(Integer year, Integer weekNumber, StatAttribute attribute) {
|
||||
// Fetch stats for current and previous week
|
||||
List<WeeklyMobilityStats> previousWeekStatsList;
|
||||
if (weekNumber > 1) {
|
||||
@@ -43,7 +43,7 @@ public class TrendDetectorService {
|
||||
.collect(Collectors.toMap(stats -> stats.getUser().getId(), stats -> stats));
|
||||
|
||||
// Detect trends for provided attribute
|
||||
List<TrendDetection> detectedTrends = new ArrayList<>();
|
||||
List<TrendDetectionDTO> detectedTrends = new ArrayList<>();
|
||||
for (Map.Entry<Long, WeeklyMobilityStats> entry : currentWeekStats.entrySet()) {
|
||||
Long userId = entry.getKey();
|
||||
WeeklyMobilityStats currentStats = entry.getValue();
|
||||
@@ -57,7 +57,7 @@ public class TrendDetectorService {
|
||||
double percentageChange = ((currentValue - previousValue) / previousValue) * 100.0;
|
||||
|
||||
if (percentageChange >= trendDetectionProperties.getPositiveThreshold()) {
|
||||
detectedTrends.add(new TrendDetection(
|
||||
detectedTrends.add(new TrendDetectionDTO(
|
||||
currentStats.getUser(),
|
||||
TrendTimespan.WEEKLY,
|
||||
TrendType.INCREASING,
|
||||
@@ -66,7 +66,7 @@ public class TrendDetectorService {
|
||||
percentageChange
|
||||
));
|
||||
} else if (percentageChange <= trendDetectionProperties.getNegativeThreshold()) {
|
||||
detectedTrends.add(new TrendDetection(
|
||||
detectedTrends.add(new TrendDetectionDTO(
|
||||
previousStats.getUser(),
|
||||
TrendTimespan.WEEKLY,
|
||||
TrendType.DECREASING,
|
||||
|
||||
Reference in New Issue
Block a user