feat(notification-outbox): add Notification EF configuration and DbSet
This commit is contained in:
@@ -0,0 +1,31 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||||
|
using ScadaLink.Commons.Entities.Notifications;
|
||||||
|
|
||||||
|
namespace ScadaLink.ConfigurationDatabase.Configurations;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// EF Core mapping for the central notification outbox entity. <see cref="Notification.TypeData"/>
|
||||||
|
/// and <see cref="Notification.ResolvedTargets"/> are intentionally left unconstrained
|
||||||
|
/// (nullable nvarchar(max)) as they carry variable-length JSON / target snapshots.
|
||||||
|
/// </summary>
|
||||||
|
public class NotificationOutboxConfiguration : IEntityTypeConfiguration<Notification>
|
||||||
|
{
|
||||||
|
public void Configure(EntityTypeBuilder<Notification> builder)
|
||||||
|
{
|
||||||
|
builder.ToTable("Notifications");
|
||||||
|
builder.HasKey(n => n.NotificationId);
|
||||||
|
builder.Property(n => n.NotificationId).HasMaxLength(64);
|
||||||
|
builder.Property(n => n.Type).HasConversion<string>().HasMaxLength(32).IsRequired();
|
||||||
|
builder.Property(n => n.Status).HasConversion<string>().HasMaxLength(32).IsRequired();
|
||||||
|
builder.Property(n => n.ListName).HasMaxLength(200).IsRequired();
|
||||||
|
builder.Property(n => n.Subject).HasMaxLength(1000).IsRequired();
|
||||||
|
builder.Property(n => n.Body).IsRequired();
|
||||||
|
builder.Property(n => n.LastError).HasMaxLength(4000);
|
||||||
|
builder.Property(n => n.SourceSiteId).HasMaxLength(100).IsRequired();
|
||||||
|
builder.Property(n => n.SourceInstanceId).HasMaxLength(200);
|
||||||
|
builder.Property(n => n.SourceScript).HasMaxLength(200);
|
||||||
|
builder.HasIndex(n => new { n.Status, n.NextAttemptAt });
|
||||||
|
builder.HasIndex(n => new { n.SourceSiteId, n.CreatedAt });
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -69,6 +69,7 @@ public class ScadaLinkDbContext : DbContext, IDataProtectionKeyContext
|
|||||||
public DbSet<NotificationList> NotificationLists => Set<NotificationList>();
|
public DbSet<NotificationList> NotificationLists => Set<NotificationList>();
|
||||||
public DbSet<NotificationRecipient> NotificationRecipients => Set<NotificationRecipient>();
|
public DbSet<NotificationRecipient> NotificationRecipients => Set<NotificationRecipient>();
|
||||||
public DbSet<SmtpConfiguration> SmtpConfigurations => Set<SmtpConfiguration>();
|
public DbSet<SmtpConfiguration> SmtpConfigurations => Set<SmtpConfiguration>();
|
||||||
|
public DbSet<Notification> Notifications => Set<Notification>();
|
||||||
|
|
||||||
// Scripts
|
// Scripts
|
||||||
public DbSet<SharedScript> SharedScripts => Set<SharedScript>();
|
public DbSet<SharedScript> SharedScripts => Set<SharedScript>();
|
||||||
|
|||||||
@@ -243,6 +243,97 @@ public class NotificationRepositoryTests : IDisposable
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class NotificationOutboxConfigurationTests : IDisposable
|
||||||
|
{
|
||||||
|
private readonly ScadaLinkDbContext _context;
|
||||||
|
|
||||||
|
public NotificationOutboxConfigurationTests()
|
||||||
|
{
|
||||||
|
_context = SqliteTestHelper.CreateInMemoryContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
_context.Database.CloseConnection();
|
||||||
|
_context.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Notification_FullyPopulated_RoundTrips()
|
||||||
|
{
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
var siteEnqueuedAt = new DateTimeOffset(2026, 5, 19, 8, 0, 0, TimeSpan.Zero);
|
||||||
|
var createdAt = new DateTimeOffset(2026, 5, 19, 8, 0, 5, TimeSpan.Zero);
|
||||||
|
var lastAttemptAt = new DateTimeOffset(2026, 5, 19, 8, 1, 0, TimeSpan.Zero);
|
||||||
|
var nextAttemptAt = new DateTimeOffset(2026, 5, 19, 8, 2, 0, TimeSpan.Zero);
|
||||||
|
var deliveredAt = new DateTimeOffset(2026, 5, 19, 8, 3, 0, TimeSpan.Zero);
|
||||||
|
|
||||||
|
var notification = new Notification(id, NotificationType.Email, "Ops List",
|
||||||
|
"High Tank Level", "Tank 4 exceeded the high level threshold.", "site-north")
|
||||||
|
{
|
||||||
|
TypeData = "{\"channel\":\"email\"}",
|
||||||
|
Status = NotificationStatus.Retrying,
|
||||||
|
RetryCount = 3,
|
||||||
|
LastError = "SMTP timeout",
|
||||||
|
ResolvedTargets = "ops@example.test;duty@example.test",
|
||||||
|
SourceInstanceId = "instance-42",
|
||||||
|
SourceScript = "TankLevelAlarm",
|
||||||
|
SiteEnqueuedAt = siteEnqueuedAt,
|
||||||
|
CreatedAt = createdAt,
|
||||||
|
LastAttemptAt = lastAttemptAt,
|
||||||
|
NextAttemptAt = nextAttemptAt,
|
||||||
|
DeliveredAt = deliveredAt,
|
||||||
|
};
|
||||||
|
|
||||||
|
_context.Notifications.Add(notification);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
_context.ChangeTracker.Clear();
|
||||||
|
|
||||||
|
var loaded = await _context.Notifications.FindAsync(id);
|
||||||
|
|
||||||
|
Assert.NotNull(loaded);
|
||||||
|
Assert.Equal(id, loaded!.NotificationId);
|
||||||
|
Assert.Equal(NotificationType.Email, loaded.Type);
|
||||||
|
Assert.Equal(NotificationStatus.Retrying, loaded.Status);
|
||||||
|
Assert.Equal("Ops List", loaded.ListName);
|
||||||
|
Assert.Equal("High Tank Level", loaded.Subject);
|
||||||
|
Assert.Equal("Tank 4 exceeded the high level threshold.", loaded.Body);
|
||||||
|
Assert.Equal("{\"channel\":\"email\"}", loaded.TypeData);
|
||||||
|
Assert.Equal(3, loaded.RetryCount);
|
||||||
|
Assert.Equal("SMTP timeout", loaded.LastError);
|
||||||
|
Assert.Equal("ops@example.test;duty@example.test", loaded.ResolvedTargets);
|
||||||
|
Assert.Equal("site-north", loaded.SourceSiteId);
|
||||||
|
Assert.Equal("instance-42", loaded.SourceInstanceId);
|
||||||
|
Assert.Equal("TankLevelAlarm", loaded.SourceScript);
|
||||||
|
Assert.Equal(siteEnqueuedAt, loaded.SiteEnqueuedAt);
|
||||||
|
Assert.Equal(createdAt, loaded.CreatedAt);
|
||||||
|
Assert.Equal(lastAttemptAt, loaded.LastAttemptAt);
|
||||||
|
Assert.Equal(nextAttemptAt, loaded.NextAttemptAt);
|
||||||
|
Assert.Equal(deliveredAt, loaded.DeliveredAt);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Notification_StatusPersistsAsString()
|
||||||
|
{
|
||||||
|
var id = Guid.NewGuid().ToString();
|
||||||
|
var notification = new Notification(id, NotificationType.Email, "Ops List",
|
||||||
|
"Subject", "Body", "site-north")
|
||||||
|
{
|
||||||
|
Status = NotificationStatus.Parked,
|
||||||
|
};
|
||||||
|
|
||||||
|
_context.Notifications.Add(notification);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
_context.ChangeTracker.Clear();
|
||||||
|
|
||||||
|
var statusText = await _context.Database
|
||||||
|
.SqlQuery<string>($"SELECT Status AS Value FROM Notifications WHERE NotificationId = {id}")
|
||||||
|
.SingleAsync();
|
||||||
|
|
||||||
|
Assert.Equal("Parked", statusText);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public class SiteRepositoryTests : IDisposable
|
public class SiteRepositoryTests : IDisposable
|
||||||
{
|
{
|
||||||
private readonly ScadaLinkDbContext _context;
|
private readonly ScadaLinkDbContext _context;
|
||||||
|
|||||||
Reference in New Issue
Block a user