SignalR Hubs: - MonitoringHub (/hubs/monitoring) — statuts agents/caméras/stockage/GPU jobs - NotificationsHub (/hubs/notifications) — notifications in-app badge temps réel - AdminHub (/hubs/admin) — broadcast changements config (Admin only) Notification System: - Nouvelles entités: Notification, NotificationTemplate, UserNotificationConfig - CQRS/MediatR: 6 handlers (list, unread, mark-read, mark-all, config get/update) - Infrastructure: NotificationService orchestrator + EmailService + GotifyService - Rate limiter IMemoryCache (20 events/min/camera/type) - Hangfire jobs: SendNotification, DailyDigest, WeeklyDigest - NotificationsController API (6 endpoints) - Blazor UI: NotificationsPage + NotificationBadge - Migration AddNotifications (3 nouvelles tables) - 644 nouveaux tests (Domain: 131, Application: 321, Infra: 151, Integration: 120, +autres)
113 lines
5.2 KiB
C#
113 lines
5.2 KiB
C#
using Heimdall.Application.Common;
|
|
using Heimdall.Domain.Entities;
|
|
using Heimdall.Infrastructure.Data.Interceptors;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
|
|
|
namespace Heimdall.Infrastructure.Data;
|
|
|
|
public class AppDbContext : IdentityDbContext<AppUser, AppRole, Guid>, IApplicationDbContext
|
|
{
|
|
private readonly AuditInterceptor _auditInterceptor;
|
|
|
|
public AppDbContext(
|
|
DbContextOptions<AppDbContext> options,
|
|
AuditInterceptor auditInterceptor) : base(options)
|
|
{
|
|
_auditInterceptor = auditInterceptor;
|
|
}
|
|
|
|
// Domain entities
|
|
public DbSet<Site> Sites => Set<Site>();
|
|
public DbSet<Agent> Agents => Set<Agent>();
|
|
public DbSet<Camera> Cameras => Set<Camera>();
|
|
public DbSet<CameraCredentials> CameraCredentials => Set<CameraCredentials>();
|
|
public DbSet<CameraDetectionProfile> CameraDetectionProfiles => Set<CameraDetectionProfile>();
|
|
public DbSet<UserSite> UserSites => Set<UserSite>();
|
|
public DbSet<ApiKey> ApiKeys => Set<ApiKey>();
|
|
public DbSet<AuditLog> AuditLogs => Set<AuditLog>();
|
|
public DbSet<AppSetting> AppSettings => Set<AppSetting>();
|
|
public DbSet<AgentSetting> AgentSettings => Set<AgentSetting>();
|
|
public DbSet<Group> Groups => Set<Group>();
|
|
public DbSet<UserGroup> UserGroups => Set<UserGroup>();
|
|
|
|
// Discovery entities
|
|
public DbSet<CameraDiscoveryResult> CameraDiscoveryResults => Set<CameraDiscoveryResult>();
|
|
public DbSet<DiscoveredStreamUrl> DiscoveredStreams => Set<DiscoveredStreamUrl>();
|
|
|
|
// Assignment entities
|
|
public DbSet<CameraAssignment> CameraAssignments => Set<CameraAssignment>();
|
|
|
|
// Event entities
|
|
public DbSet<CameraEvent> CameraEvents => Set<CameraEvent>();
|
|
|
|
// Notification entities
|
|
public DbSet<Notification> Notifications => Set<Notification>();
|
|
public DbSet<NotificationTemplate> NotificationTemplates => Set<NotificationTemplate>();
|
|
public DbSet<UserNotificationConfig> UserNotificationConfigs => Set<UserNotificationConfig>();
|
|
|
|
// Security entities
|
|
public DbSet<RevokedRefreshToken> RevokedRefreshTokens => Set<RevokedRefreshToken>();
|
|
|
|
// Person entities
|
|
public DbSet<Person> Persons => Set<Person>();
|
|
public DbSet<KnownFace> KnownFaces => Set<KnownFace>();
|
|
|
|
public async Task SaveChangesWithRetryAsync(int maxRetries = 3, CancellationToken ct = default)
|
|
{
|
|
await DbContextExtensions.SaveChangesWithRetryAsync(this, maxRetries, ct);
|
|
}
|
|
|
|
protected override void OnModelCreating(ModelBuilder builder)
|
|
{
|
|
base.OnModelCreating(builder);
|
|
|
|
builder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly);
|
|
|
|
// Global soft-delete filter — principal entities
|
|
builder.Entity<Site>().HasQueryFilter(e => !e.IsDeleted);
|
|
builder.Entity<Agent>().HasQueryFilter(e => !e.IsDeleted);
|
|
builder.Entity<Camera>().HasQueryFilter(e => !e.IsDeleted);
|
|
|
|
// Matching query filters on dependent entities to prevent
|
|
// "required navigation with query filter" EF Core warnings.
|
|
// Without these, loading an AgentSetting whose Agent was soft-deleted
|
|
// would cause a null reference on the required navigation.
|
|
builder.Entity<AgentSetting>().HasQueryFilter(e => e.Agent == null || !e.Agent.IsDeleted);
|
|
builder.Entity<CameraCredentials>().HasQueryFilter(e => e.Camera == null || !e.Camera.IsDeleted);
|
|
builder.Entity<CameraDetectionProfile>().HasQueryFilter(e => e.Camera == null || !e.Camera.IsDeleted);
|
|
builder.Entity<UserSite>().HasQueryFilter(e => e.Site == null || !e.Site.IsDeleted);
|
|
|
|
// Matching query filters for dependent entities sharing required
|
|
// navigations to soft-deletable entities. These prevent EF Core
|
|
// warnings (10622) about required ends with global query filters.
|
|
builder.Entity<CameraAssignment>().HasQueryFilter(e => e.Agent == null || !e.Agent.IsDeleted);
|
|
builder.Entity<CameraDiscoveryResult>().HasQueryFilter(e => e.Site == null || !e.Site.IsDeleted);
|
|
builder.Entity<CameraEvent>().HasQueryFilter(e => e.Camera == null || !e.Camera.IsDeleted);
|
|
builder.Entity<DiscoveredStreamUrl>().HasQueryFilter(e => e.DiscoveryResult == null || e.DiscoveryResult.Site == null || !e.DiscoveryResult.Site.IsDeleted);
|
|
|
|
// Group soft-delete filters
|
|
builder.Entity<Group>().HasQueryFilter(e => !e.IsDeleted);
|
|
builder.Entity<UserGroup>().HasQueryFilter(e => e.Group == null || !e.Group.IsDeleted);
|
|
}
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
optionsBuilder.AddInterceptors(
|
|
_auditInterceptor);
|
|
|
|
// Suppress informational EF Core messages that create noise in
|
|
// error tracking (GlitchTip #538). Redundant index warnings are
|
|
// automatically handled by EF Core — it simply skips creating FK
|
|
// indexes that are already covered by composite indexes/PKs.
|
|
optionsBuilder.ConfigureWarnings(warnings =>
|
|
{
|
|
warnings.Ignore(CoreEventId.RedundantIndexRemoved);
|
|
});
|
|
|
|
base.OnConfiguring(optionsBuilder);
|
|
}
|
|
}
|