2026-07-26 20:45:35 +00:00
|
|
|
using Heimdall.Application.Services;
|
2026-10-25 05:59:18 +01:00
|
|
|
using Heimdall.Application.Localization;
|
2026-07-26 20:45:35 +00:00
|
|
|
using Heimdall.Infrastructure.Data;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.Infrastructure.Jobs;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Hangfire recurring job that sends a daily digest email to users who have it enabled.
|
|
|
|
|
/// Scheduled daily at 8:00.
|
|
|
|
|
/// Uses IDbContextFactory to avoid sharing a DbContext across Hangfire worker threads.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class DailyDigestJob
|
|
|
|
|
{
|
|
|
|
|
private readonly IDbContextFactory<AppDbContext> _dbFactory;
|
|
|
|
|
private readonly IEmailService _emailService;
|
2026-10-21 18:59:50 +02:00
|
|
|
private readonly ICameraAccessService _cameraAccess;
|
2026-07-26 20:45:35 +00:00
|
|
|
private readonly ILogger<DailyDigestJob> _logger;
|
|
|
|
|
|
|
|
|
|
public DailyDigestJob(
|
|
|
|
|
IDbContextFactory<AppDbContext> dbFactory,
|
|
|
|
|
IEmailService emailService,
|
2026-10-21 18:59:50 +02:00
|
|
|
ICameraAccessService cameraAccess,
|
2026-07-26 20:45:35 +00:00
|
|
|
ILogger<DailyDigestJob> logger)
|
|
|
|
|
{
|
|
|
|
|
_dbFactory = dbFactory;
|
|
|
|
|
_emailService = emailService;
|
2026-10-21 18:59:50 +02:00
|
|
|
_cameraAccess = cameraAccess;
|
2026-07-26 20:45:35 +00:00
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task ExecuteAsync(CancellationToken ct = default)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Starting daily digest job");
|
|
|
|
|
|
|
|
|
|
await using var db = await _dbFactory.CreateDbContextAsync(ct);
|
|
|
|
|
|
|
|
|
|
var configs = await db.UserNotificationConfigs
|
|
|
|
|
.Where(c => c.DailyDigest && c.EmailEnabled && c.EmailAddress != null)
|
|
|
|
|
.ToListAsync(ct);
|
2026-10-25 05:59:18 +01:00
|
|
|
var userIds = configs.Select(config => config.UserId).Distinct().ToArray();
|
|
|
|
|
var languages = await db.Users.AsNoTracking()
|
|
|
|
|
.Where(user => userIds.Contains(user.Id))
|
|
|
|
|
.ToDictionaryAsync(user => user.Id, user => user.Language, ct);
|
2026-07-26 20:45:35 +00:00
|
|
|
|
2026-10-21 18:59:50 +02:00
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
var from = now.Date;
|
2026-07-26 20:45:35 +00:00
|
|
|
|
|
|
|
|
foreach (var config in configs)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2026-10-21 18:59:50 +02:00
|
|
|
var cameraIds = await _cameraAccess
|
|
|
|
|
.GetUserAccessibleCameraIdsAsync(config.UserId, ct)
|
|
|
|
|
.ConfigureAwait(false);
|
2026-07-27 05:33:53 +00:00
|
|
|
|
|
|
|
|
var eventCount = cameraIds.Count > 0
|
|
|
|
|
? await db.CameraEvents
|
2026-10-21 18:59:50 +02:00
|
|
|
.CountAsync(e => e.OccurredAt >= from && e.OccurredAt < now && cameraIds.Contains(e.CameraId), ct)
|
2026-07-27 05:33:53 +00:00
|
|
|
: 0;
|
2026-07-26 20:45:35 +00:00
|
|
|
|
|
|
|
|
var unreadNotifications = await db.Notifications
|
|
|
|
|
.CountAsync(n => n.UserId == config.UserId && !n.IsRead, ct);
|
|
|
|
|
|
2026-10-25 05:59:18 +01:00
|
|
|
var language = languages.GetValueOrDefault(config.UserId, config.Language);
|
|
|
|
|
var subject = ServerText.Get("Digest.Daily.Subject", language);
|
2026-07-26 20:45:35 +00:00
|
|
|
var body = $"""
|
2026-10-25 05:59:18 +01:00
|
|
|
<h2>{ServerText.Get("Digest.Daily.Title", language)}</h2>
|
|
|
|
|
<p>{ServerText.Get("Digest.Greeting", language)}</p>
|
2026-07-26 20:45:35 +00:00
|
|
|
<ul>
|
2026-10-25 05:59:18 +01:00
|
|
|
<li>{ServerText.Get("Digest.Daily.Events", language)} <strong>{eventCount}</strong></li>
|
|
|
|
|
<li>{ServerText.Get("Digest.UnreadNotifications", language)} <strong>{unreadNotifications}</strong></li>
|
2026-07-26 20:45:35 +00:00
|
|
|
</ul>
|
2026-10-25 05:59:18 +01:00
|
|
|
<p>{ServerText.Get("Digest.Signoff", language)}</p>
|
2026-07-26 20:45:35 +00:00
|
|
|
""";
|
|
|
|
|
|
2026-07-27 18:59:12 +00:00
|
|
|
await _emailService.SendEmailAsync(config.EmailAddress!, subject, body, ct).ConfigureAwait(false);
|
2026-07-26 20:45:35 +00:00
|
|
|
_logger.LogDebug("Daily digest sent to {Email}", config.EmailAddress);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Failed to send daily digest to {Email}", config.EmailAddress);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("Daily digest completed: {Count} emails sent", configs.Count);
|
|
|
|
|
}
|
|
|
|
|
}
|