Refactored event service injection

This commit is contained in:
2026-10-29 06:46:48 +02:00
parent 04b5656fd1
commit 067af804ef
3 changed files with 13 additions and 3 deletions
@@ -20,7 +20,7 @@ public sealed class LateProjectEvent : IEvent
} }
public sealed class LateProjectEventHandler(IDbContextFactory<ApplicationDbContext> contextFactory, public sealed class LateProjectEventHandler(IDbContextFactory<ApplicationDbContext> contextFactory,
ICommunicationsService service) : INotificationHandler<LateProjectEvent> IServiceProvider provider) : INotificationHandler<LateProjectEvent>
{ {
readonly int lateDays = 3; readonly int lateDays = 3;
@@ -28,6 +28,9 @@ public sealed class LateProjectEventHandler(IDbContextFactory<ApplicationDbConte
{ {
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var scope = provider.CreateScope().ServiceProvider;
var service = scope.GetRequiredService<ICommunicationsService>();
var now = DateTime.UtcNow; var now = DateTime.UtcNow;
var overdueCutoff = lateDays > 0 ? now.AddDays(-lateDays) : now; var overdueCutoff = lateDays > 0 ? now.AddDays(-lateDays) : now;
@@ -22,12 +22,15 @@ public sealed class NonCompliantProjectEvent : IEvent
} }
public sealed class NonCompliantProjectEventHandler(IDbContextFactory<ApplicationDbContext> contextFactory, public sealed class NonCompliantProjectEventHandler(IDbContextFactory<ApplicationDbContext> contextFactory,
ICommunicationsService service) : INotificationHandler<NonCompliantProjectEvent> IServiceProvider provider) : INotificationHandler<NonCompliantProjectEvent>
{ {
public async ValueTask Handle(NonCompliantProjectEvent notification, CancellationToken cancellationToken) public async ValueTask Handle(NonCompliantProjectEvent notification, CancellationToken cancellationToken)
{ {
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var scope = provider.CreateScope().ServiceProvider;
var service = scope.GetRequiredService<ICommunicationsService>();
var now = DateTime.UtcNow; var now = DateTime.UtcNow;
// Fetch active/monitored projects with programme, portfolio, and award organisations // Fetch active/monitored projects with programme, portfolio, and award organisations
@@ -18,7 +18,7 @@ public sealed class ProcessEmailEvent : IEvent
} }
public sealed class ProcessEmailEventHandler(IDbContextFactory<ApplicationDbContext> contextFactory, public sealed class ProcessEmailEventHandler(IDbContextFactory<ApplicationDbContext> contextFactory,
IFeatureManager featureManager, EmailService emailService, ISkorubaUserService skorubaUserService) : INotificationHandler<ProcessEmailEvent> IServiceProvider provider, IFeatureManager featureManager) : INotificationHandler<ProcessEmailEvent>
{ {
const int maxBatch = 100; const int maxBatch = 100;
@@ -26,6 +26,10 @@ public sealed class ProcessEmailEventHandler(IDbContextFactory<ApplicationDbCont
{ {
if (!await featureManager.IsEnabledAsync(FeatureEmailProcessor)) return; if (!await featureManager.IsEnabledAsync(FeatureEmailProcessor)) return;
var scope = provider.CreateScope().ServiceProvider;
var emailService = scope.GetRequiredService<EmailService>();
var skorubaUserService = scope.GetRequiredService<ISkorubaUserService>();
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var outgoing = await context.Notifications.AsNoTracking() var outgoing = await context.Notifications.AsNoTracking()