using Microsoft.EntityFrameworkCore; using Novelly.Api.Data; namespace Novelly.Api.Users; public static class ServiceUser { public static readonly Guid Id = new("9f1d6f2c-6d1b-4d3e-9a54-0f2b6f8a7c11"); public const string Email = "service@novelly.local"; public const string DisplayName = "Novelly Service"; public static async Task EnsureSeededAsync(INovelDbContext db, string? serviceApiKey, ILogger logger, CancellationToken ct = default) { if (string.IsNullOrWhiteSpace(serviceApiKey)) { logger.LogInformation("No service api key configured; the service user {UserId} was not seeded", Id); return null; } var existing = await db.Users.FirstOrDefaultAsync(u => u.Id == Id, ct); if (existing is not null) return existing; var user = new NovellyUser { Id = Id, UserName = Email, NormalizedUserName = Email.ToUpperInvariant(), Email = Email, NormalizedEmail = Email.ToUpperInvariant(), EmailConfirmed = true, DisplayName = DisplayName, GlobalRole = GlobalRole.Admin, SecurityStamp = Guid.NewGuid().ToString("N"), ConcurrencyStamp = Guid.NewGuid().ToString("N") }; db.Users.Add(user); await db.SaveChangesAsync(ct); logger.LogInformation("Seeded the service user {UserId} with global role {GlobalRole}", user.Id, user.GlobalRole); return user; } }