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)
70 lines
2.4 KiB
C#
70 lines
2.4 KiB
C#
using Heimdall.Application.Services;
|
|
using Heimdall.Infrastructure.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Heimdall.Infrastructure.Jobs;
|
|
|
|
/// <summary>
|
|
/// Hangfire recurring job that sends a weekly digest email to users who have it enabled.
|
|
/// Scheduled Monday at 8:00.
|
|
/// Uses IDbContextFactory to avoid sharing a DbContext across Hangfire worker threads.
|
|
/// </summary>
|
|
public class WeeklyDigestJob
|
|
{
|
|
private readonly IDbContextFactory<AppDbContext> _dbFactory;
|
|
private readonly IEmailService _emailService;
|
|
private readonly ILogger<WeeklyDigestJob> _logger;
|
|
|
|
public WeeklyDigestJob(
|
|
IDbContextFactory<AppDbContext> dbFactory,
|
|
IEmailService emailService,
|
|
ILogger<WeeklyDigestJob> logger)
|
|
{
|
|
_dbFactory = dbFactory;
|
|
_emailService = emailService;
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task ExecuteAsync(CancellationToken ct = default)
|
|
{
|
|
_logger.LogInformation("Starting weekly digest job");
|
|
|
|
await using var db = await _dbFactory.CreateDbContextAsync(ct);
|
|
|
|
var configs = await db.UserNotificationConfigs
|
|
.Where(c => c.WeeklyDigest && c.EmailEnabled && c.EmailAddress != null)
|
|
.ToListAsync(ct);
|
|
|
|
var lastWeek = DateTime.UtcNow.Date.AddDays(-7);
|
|
|
|
foreach (var config in configs)
|
|
{
|
|
try
|
|
{
|
|
var eventCount = await db.CameraEvents
|
|
.CountAsync(e => e.CreatedAt >= lastWeek, ct);
|
|
|
|
var subject = "Heimdall — Résumé hebdomadaire";
|
|
var body = $"""
|
|
<h2>Résumé hebdomadaire Heimdall</h2>
|
|
<p>Bonjour,</p>
|
|
<ul>
|
|
<li>Événements détectés cette semaine : <strong>{eventCount}</strong></li>
|
|
</ul>
|
|
<p>Cordialement,<br/>L'équipe Heimdall</p>
|
|
""";
|
|
|
|
await _emailService.SendEmailAsync(config.EmailAddress!, subject, body, ct);
|
|
_logger.LogDebug("Weekly digest sent to {Email}", config.EmailAddress);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Failed to send weekly digest to {Email}", config.EmailAddress);
|
|
}
|
|
}
|
|
|
|
_logger.LogInformation("Weekly digest completed: {Count} emails sent", configs.Count);
|
|
}
|
|
}
|