basic firebase connection + notification structure
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
package SuMoC.backend.dev;
|
||||
|
||||
import SuMoC.backend.scheduling.ScheduledTasks;
|
||||
import SuMoC.backend.trenddetection.services.StatTableUpdateService;
|
||||
import SuMoC.backend.trend.services.StatTableUpdateService;
|
||||
import com.google.firebase.messaging.FirebaseMessaging;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
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;
|
||||
@@ -20,6 +23,8 @@ public class DevController {
|
||||
|
||||
private final StatTableUpdateService statTableUpdateService;
|
||||
|
||||
private final FirebaseMessaging firebaseMessaging;
|
||||
|
||||
@PostMapping("/trigger-trend-calculation")
|
||||
public void triggerTrendCalculation() {
|
||||
LocalDate dayOfLastWeek = LocalDate.now().minusDays(7);
|
||||
@@ -29,4 +34,21 @@ public class DevController {
|
||||
statTableUpdateService.updateWeeklyMobilityStatsForAllUsers(day2WeeksAgo);
|
||||
statTableUpdateService.updateWeeklyMobilityStatsForAllUsers(day3WeeksAgo);
|
||||
}
|
||||
|
||||
@GetMapping("/test-firebase")
|
||||
public ResponseEntity<String> testFirebaseConnection() {
|
||||
try {
|
||||
// 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()
|
||||
).get();
|
||||
return ResponseEntity.ok("✓ Firebase message sent to topic: " + message);
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.status(500).body("✗ Failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package SuMoC.backend.firebase;
|
||||
|
||||
import com.google.auth.oauth2.GoogleCredentials;
|
||||
import com.google.firebase.FirebaseApp;
|
||||
import com.google.firebase.FirebaseOptions;
|
||||
import com.google.firebase.messaging.FirebaseMessaging;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(FirebaseProperties.class)
|
||||
@RequiredArgsConstructor
|
||||
public class FirebaseConfiguration {
|
||||
|
||||
private final FirebaseProperties firebaseProperties;
|
||||
|
||||
@Bean
|
||||
FirebaseMessaging firebaseMessaging(FirebaseApp firebaseApp) {
|
||||
return FirebaseMessaging.getInstance(firebaseApp);
|
||||
}
|
||||
|
||||
@Bean
|
||||
FirebaseApp firebaseApp(GoogleCredentials credentials) {
|
||||
FirebaseOptions options = FirebaseOptions.builder()
|
||||
.setCredentials(credentials)
|
||||
.build();
|
||||
|
||||
return FirebaseApp.initializeApp(options);
|
||||
}
|
||||
|
||||
@Bean
|
||||
GoogleCredentials googleCredentials(FirebaseProperties firebaseProperties) throws IOException {
|
||||
if (firebaseProperties.serviceAccount() != null) {
|
||||
try (InputStream is = firebaseProperties.serviceAccount().getInputStream()) {
|
||||
return GoogleCredentials.fromStream(is);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
} else {
|
||||
// Use standard credentials chain. Useful when running inside GKE
|
||||
return GoogleCredentials.getApplicationDefault();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package SuMoC.backend.firebase;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
@ConfigurationProperties(prefix = "firebase.config")
|
||||
public record FirebaseProperties(
|
||||
Resource serviceAccount
|
||||
) {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package SuMoC.backend.notification.factories;
|
||||
|
||||
import SuMoC.backend.notification.models.Notification;
|
||||
import SuMoC.backend.trend.TrendDetection;
|
||||
import SuMoC.backend.trend.services.TrendMessageGenerator;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class TrendNotificationFactory {
|
||||
|
||||
private final TrendMessageGenerator trendMessageGenerator;
|
||||
|
||||
public Notification createNotification(TrendDetection trend) {
|
||||
return Notification.builder()
|
||||
.user(trend.getUser())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,7 @@ package SuMoC.backend.notification.models;
|
||||
import SuMoC.backend.notification.enums.NotificationStatus;
|
||||
import SuMoC.backend.user.models.User;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
@@ -13,6 +12,9 @@ import java.time.LocalTime;
|
||||
@Table(name = "notifications")
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Notification {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package SuMoC.backend.notification.repositories;
|
||||
|
||||
import SuMoC.backend.notification.models.Notification;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface NotificationRepository extends JpaRepository<Notification, Long> {
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
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 lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class TrendNotificationService {
|
||||
|
||||
private final NotificationRepository notificationRepository;
|
||||
|
||||
private final TrendNotificationFactory trendNotificationFactory;
|
||||
|
||||
public List<Notification> generateTrendNotifications(List<TrendDetection> trendList) {
|
||||
List<Notification> notificationList = trendList.stream().map(trendNotificationFactory::createNotification).toList();
|
||||
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package SuMoC.backend.scheduling;
|
||||
|
||||
import SuMoC.backend.trenddetection.services.StatTableUpdateService;
|
||||
import SuMoC.backend.trend.services.StatTableUpdateService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
+4
-3
@@ -1,6 +1,7 @@
|
||||
package SuMoC.backend.trenddetection;
|
||||
package SuMoC.backend.trend;
|
||||
|
||||
import SuMoC.backend.trenddetection.enums.*;
|
||||
import SuMoC.backend.trend.enums.*;
|
||||
import SuMoC.backend.user.models.User;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
@@ -9,7 +10,7 @@ import lombok.Data;
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
public class TrendDetection {
|
||||
private Long userId;
|
||||
private User user;
|
||||
private TrendTimespan timespan;
|
||||
private TrendType type;
|
||||
private StatAttribute attribute;
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
package SuMoC.backend.trenddetection;
|
||||
package SuMoC.backend.trend;
|
||||
|
||||
import SuMoC.backend.trenddetection.models.WeeklyMobilityStats;
|
||||
import SuMoC.backend.trenddetection.models.WeeklyMobilityStatsId;
|
||||
import SuMoC.backend.trend.models.WeeklyMobilityStats;
|
||||
import SuMoC.backend.trend.models.WeeklyMobilityStatsId;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package SuMoC.backend.trenddetection.config;
|
||||
package SuMoC.backend.trend.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package SuMoC.backend.trenddetection.enums;
|
||||
package SuMoC.backend.trend.enums;
|
||||
|
||||
public enum DeltaType {
|
||||
ABSOLUTE,
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package SuMoC.backend.trenddetection.enums;
|
||||
package SuMoC.backend.trend.enums;
|
||||
|
||||
/**
|
||||
* Represents statistical attributes a detected trend can pertain to.
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package SuMoC.backend.trenddetection.enums;
|
||||
package SuMoC.backend.trend.enums;
|
||||
|
||||
public enum TrendConnotation {
|
||||
POSITIVE,
|
||||
@@ -0,0 +1,5 @@
|
||||
package SuMoC.backend.trend.enums;
|
||||
|
||||
public enum TrendTimespan {
|
||||
WEEKLY,
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package SuMoC.backend.trenddetection.enums;
|
||||
package SuMoC.backend.trend.enums;
|
||||
|
||||
public enum TrendType {
|
||||
INCREASING,
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
package SuMoC.backend.trenddetection.models;
|
||||
package SuMoC.backend.trend.models;
|
||||
|
||||
import SuMoC.backend.trenddetection.enums.StatAttribute;
|
||||
import SuMoC.backend.trend.enums.StatAttribute;
|
||||
import SuMoC.backend.user.models.User;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.*;
|
||||
+2
-1
@@ -1,9 +1,10 @@
|
||||
package SuMoC.backend.trenddetection.models;
|
||||
package SuMoC.backend.trend.models;
|
||||
|
||||
import SuMoC.backend.user.models.User;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@AllArgsConstructor
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
package SuMoC.backend.trenddetection.services;
|
||||
package SuMoC.backend.trend.services;
|
||||
|
||||
import SuMoC.backend.trenddetection.WeeklyMobilityStatsRepository;
|
||||
import SuMoC.backend.trenddetection.statcalculation.WeeklyMobilityStatsCalculator;
|
||||
import SuMoC.backend.trend.WeeklyMobilityStatsRepository;
|
||||
import SuMoC.backend.trend.statcalculation.WeeklyMobilityStatsCalculator;
|
||||
import SuMoC.backend.trip.TripRepository;
|
||||
import SuMoC.backend.trip.models.Trip;
|
||||
import jakarta.transaction.Transactional;
|
||||
+11
-11
@@ -1,13 +1,13 @@
|
||||
package SuMoC.backend.trenddetection.services;
|
||||
package SuMoC.backend.trend.services;
|
||||
|
||||
import SuMoC.backend.trenddetection.TrendDetection;
|
||||
import SuMoC.backend.trenddetection.WeeklyMobilityStatsRepository;
|
||||
import SuMoC.backend.trenddetection.config.TrendDetectionProperties;
|
||||
import SuMoC.backend.trenddetection.enums.DeltaType;
|
||||
import SuMoC.backend.trenddetection.enums.StatAttribute;
|
||||
import SuMoC.backend.trenddetection.enums.TrendTimespan;
|
||||
import SuMoC.backend.trenddetection.enums.TrendType;
|
||||
import SuMoC.backend.trenddetection.models.WeeklyMobilityStats;
|
||||
import SuMoC.backend.trend.TrendDetection;
|
||||
import SuMoC.backend.trend.WeeklyMobilityStatsRepository;
|
||||
import SuMoC.backend.trend.config.TrendDetectionProperties;
|
||||
import SuMoC.backend.trend.enums.DeltaType;
|
||||
import SuMoC.backend.trend.enums.StatAttribute;
|
||||
import SuMoC.backend.trend.enums.TrendTimespan;
|
||||
import SuMoC.backend.trend.enums.TrendType;
|
||||
import SuMoC.backend.trend.models.WeeklyMobilityStats;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -58,7 +58,7 @@ public class TrendDetectorService {
|
||||
|
||||
if (percentageChange >= trendDetectionProperties.getPositiveThreshold()) {
|
||||
detectedTrends.add(new TrendDetection(
|
||||
userId,
|
||||
currentStats.getUser(),
|
||||
TrendTimespan.WEEKLY,
|
||||
TrendType.INCREASING,
|
||||
StatAttribute.CAR_DISTANCE,
|
||||
@@ -67,7 +67,7 @@ public class TrendDetectorService {
|
||||
));
|
||||
} else if (percentageChange <= trendDetectionProperties.getNegativeThreshold()) {
|
||||
detectedTrends.add(new TrendDetection(
|
||||
userId,
|
||||
previousStats.getUser(),
|
||||
TrendTimespan.WEEKLY,
|
||||
TrendType.DECREASING,
|
||||
StatAttribute.CAR_DISTANCE,
|
||||
@@ -0,0 +1,8 @@
|
||||
package SuMoC.backend.trend.services;
|
||||
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class TrendMessageGenerator {
|
||||
}
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
package SuMoC.backend.trenddetection.statcalculation;
|
||||
package SuMoC.backend.trend.statcalculation;
|
||||
|
||||
import SuMoC.backend.trenddetection.models.WeeklyMobilityStats;
|
||||
import SuMoC.backend.trend.models.WeeklyMobilityStats;
|
||||
import SuMoC.backend.trip.models.Trip;
|
||||
import SuMoC.backend.trip.models.TripSegment;
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
package SuMoC.backend.trenddetection.enums;
|
||||
|
||||
public enum TrendTimespan {
|
||||
WEEKLY,
|
||||
}
|
||||
Reference in New Issue
Block a user