2026-02-18 17:11:13 +01:00
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using WorkTracker.Configuration;
|
|
|
|
|
|
|
|
|
|
namespace WorkTracker.Services.Auth;
|
|
|
|
|
|
|
|
|
|
public sealed class SingleUserSeedService : IHostedService
|
|
|
|
|
{
|
2026-03-16 21:54:44 +01:00
|
|
|
private readonly IMongoAuthService authService;
|
2026-02-18 17:11:13 +01:00
|
|
|
private readonly IOptions<SingleUserOptions> options;
|
|
|
|
|
private readonly ILogger<SingleUserSeedService> logger;
|
|
|
|
|
|
|
|
|
|
public SingleUserSeedService(
|
2026-03-16 21:54:44 +01:00
|
|
|
IMongoAuthService authService,
|
2026-02-18 17:11:13 +01:00
|
|
|
IOptions<SingleUserOptions> options,
|
|
|
|
|
ILogger<SingleUserSeedService> logger)
|
|
|
|
|
{
|
2026-03-16 21:54:44 +01:00
|
|
|
this.authService = authService;
|
2026-02-18 17:11:13 +01:00
|
|
|
this.options = options;
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task StartAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
if (!options.Value.SeedOnStartup)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 21:54:44 +01:00
|
|
|
try
|
2026-02-18 17:11:13 +01:00
|
|
|
{
|
2026-03-16 21:54:44 +01:00
|
|
|
await authService.EnsureSeedUserAsync(cancellationToken);
|
2026-02-18 17:11:13 +01:00
|
|
|
}
|
2026-03-16 21:54:44 +01:00
|
|
|
catch (Exception ex)
|
2026-02-18 17:11:13 +01:00
|
|
|
{
|
2026-03-16 21:54:44 +01:00
|
|
|
logger.LogError(ex, "Unable to seed MongoDB single user account {Email}", options.Value.Email);
|
2026-02-18 17:11:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
|
|
|
|
}
|