feature: In-App Simulation Runner (Backend)
This commit is contained in:
@@ -37,6 +37,7 @@ public class AppDbContext : DbContext
|
||||
public DbSet<AlertFeedback> AlertFeedbacks => Set<AlertFeedback>();
|
||||
public DbSet<AlertQualityMetric> AlertQualityMetrics => Set<AlertQualityMetric>();
|
||||
public DbSet<RefreshToken> RefreshTokens => Set<RefreshToken>();
|
||||
public DbSet<SimulationRun> SimulationRuns => Set<SimulationRun>();
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
|
||||
@@ -25,6 +25,7 @@ public class PatientConfiguration : IEntityTypeConfiguration<Patient>
|
||||
.HasColumnName("name_search_token")
|
||||
.HasMaxLength(64);
|
||||
builder.Property(p => p.CreatedAt).HasColumnName("created_at").HasDefaultValueSql("NOW()");
|
||||
builder.Property(p => p.IsSimulated).HasColumnName("is_simulated").HasDefaultValue(false);
|
||||
|
||||
// MRN uses exact-match unique index — MRN lookups are always equality checks,
|
||||
// never LIKE/ILIKE. A B-tree unique index satisfies O(log n) point lookup.
|
||||
@@ -33,5 +34,9 @@ public class PatientConfiguration : IEntityTypeConfiguration<Patient>
|
||||
// at this scale (pg_trgm GIN would be warranted at >500k patients).
|
||||
builder.HasIndex(p => p.Mrn).IsUnique();
|
||||
builder.HasIndex(p => p.NameSearchToken);
|
||||
// Filtered index keeps Phase 38 simulated-patient purge cheap.
|
||||
builder.HasIndex(p => p.IsSimulated)
|
||||
.HasDatabaseName("IX_Patients_IsSimulated")
|
||||
.HasFilter("is_simulated = true");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
public class SimulationRunConfiguration : IEntityTypeConfiguration<SimulationRun>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<SimulationRun> builder)
|
||||
{
|
||||
builder.ToTable("simulation_runs");
|
||||
builder.HasKey(r => r.Id);
|
||||
builder.Property(r => r.Id).HasColumnName("id").HasDefaultValueSql("gen_random_uuid()");
|
||||
builder.Property(r => r.ScenarioId).HasColumnName("scenario_id").HasMaxLength(100).IsRequired();
|
||||
builder.Property(r => r.ScenarioName).HasColumnName("scenario_name").HasMaxLength(200).IsRequired();
|
||||
builder.Property(r => r.Speed).HasColumnName("speed");
|
||||
builder.Property(r => r.Status).HasColumnName("status").HasMaxLength(20).IsRequired()
|
||||
.HasConversion(
|
||||
v => v.ToDbString(),
|
||||
v => SimulationRunStatusExtensions.FromDbString(v));
|
||||
builder.Property(r => r.PatientId).HasColumnName("patient_id");
|
||||
builder.Property(r => r.EncounterId).HasColumnName("encounter_id");
|
||||
builder.Property(r => r.StartedByUserId).HasColumnName("started_by_user_id").HasMaxLength(100).IsRequired();
|
||||
builder.Property(r => r.StartedAt).HasColumnName("started_at");
|
||||
builder.Property(r => r.CompletedAt).HasColumnName("completed_at");
|
||||
builder.Property(r => r.ObservationsSent).HasColumnName("observations_sent");
|
||||
builder.Property(r => r.MedicationsSent).HasColumnName("medications_sent");
|
||||
builder.Property(r => r.OrdersPlaced).HasColumnName("orders_placed");
|
||||
builder.Property(r => r.LastOffsetMinutes).HasColumnName("last_offset_minutes");
|
||||
builder.Property(r => r.TotalOffsetMinutes).HasColumnName("total_offset_minutes");
|
||||
builder.Property(r => r.FailureReason).HasColumnName("failure_reason").HasMaxLength(2000);
|
||||
|
||||
builder.HasIndex(r => new { r.Status, r.StartedAt })
|
||||
.IsDescending(false, true)
|
||||
.HasDatabaseName("IX_simulation_runs_status_started_at");
|
||||
}
|
||||
}
|
||||
@@ -2,49 +2,85 @@ using Microsoft.EntityFrameworkCore;
|
||||
|
||||
public static class UserSeeder
|
||||
{
|
||||
public static async Task SeedAsync(AppDbContext db)
|
||||
public static readonly Guid SimulationRunnerUserId =
|
||||
Guid.Parse("55555555-5555-5555-5555-555555555555");
|
||||
|
||||
public static async Task SeedAsync(
|
||||
AppDbContext db,
|
||||
bool simulationEnabled = false,
|
||||
string? simulationRunnerPassword = null)
|
||||
{
|
||||
if (await db.ClinicalUsers.AnyAsync())
|
||||
if (!await db.ClinicalUsers.AnyAsync())
|
||||
{
|
||||
db.ClinicalUsers.AddRange(
|
||||
new ClinicalUser
|
||||
{
|
||||
Id = Guid.Parse("11111111-1111-1111-1111-111111111111"),
|
||||
Username = "nurse.demo",
|
||||
PasswordHash = BCrypt.Net.BCrypt.HashPassword("DemoNurse1!"),
|
||||
DisplayName = "Demo Nurse",
|
||||
Role = ClinicalRole.Nurse,
|
||||
CreatedAt = DateTimeOffset.UtcNow
|
||||
},
|
||||
new ClinicalUser
|
||||
{
|
||||
Id = Guid.Parse("22222222-2222-2222-2222-222222222222"),
|
||||
Username = "physician.demo",
|
||||
PasswordHash = BCrypt.Net.BCrypt.HashPassword("DemoPhysician1!"),
|
||||
DisplayName = "Dr. Demo Physician",
|
||||
Role = ClinicalRole.Physician,
|
||||
CreatedAt = DateTimeOffset.UtcNow
|
||||
},
|
||||
new ClinicalUser
|
||||
{
|
||||
Id = Guid.Parse("33333333-3333-3333-3333-333333333333"),
|
||||
Username = "admin.demo",
|
||||
PasswordHash = BCrypt.Net.BCrypt.HashPassword("DemoAdmin1!"),
|
||||
DisplayName = "Demo Admin",
|
||||
Role = ClinicalRole.Admin,
|
||||
CreatedAt = DateTimeOffset.UtcNow
|
||||
},
|
||||
new ClinicalUser
|
||||
{
|
||||
Id = Guid.Parse("44444444-4444-4444-4444-444444444444"),
|
||||
Username = "integration.mirth",
|
||||
PasswordHash = BCrypt.Net.BCrypt.HashPassword("MirthIntegration1!"),
|
||||
DisplayName = "Mirth Connect",
|
||||
Role = ClinicalRole.Integration,
|
||||
CreatedAt = DateTimeOffset.UtcNow
|
||||
});
|
||||
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
if (simulationEnabled)
|
||||
await EnsureSimulationRunnerAsync(db, simulationRunnerPassword);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Seeds the loopback simulation runner account when Simulation:Enabled.
|
||||
/// Idempotent — safe to call on an existing database that already has demo users.
|
||||
/// </summary>
|
||||
public static async Task EnsureSimulationRunnerAsync(
|
||||
AppDbContext db, string? password)
|
||||
{
|
||||
if (await db.ClinicalUsers.AnyAsync(u => u.Username == "simulation.runner"))
|
||||
return;
|
||||
|
||||
db.ClinicalUsers.AddRange(
|
||||
new ClinicalUser
|
||||
{
|
||||
Id = Guid.Parse("11111111-1111-1111-1111-111111111111"),
|
||||
Username = "nurse.demo",
|
||||
PasswordHash = BCrypt.Net.BCrypt.HashPassword("DemoNurse1!"),
|
||||
DisplayName = "Demo Nurse",
|
||||
Role = ClinicalRole.Nurse,
|
||||
CreatedAt = DateTimeOffset.UtcNow
|
||||
},
|
||||
new ClinicalUser
|
||||
{
|
||||
Id = Guid.Parse("22222222-2222-2222-2222-222222222222"),
|
||||
Username = "physician.demo",
|
||||
PasswordHash = BCrypt.Net.BCrypt.HashPassword("DemoPhysician1!"),
|
||||
DisplayName = "Dr. Demo Physician",
|
||||
Role = ClinicalRole.Physician,
|
||||
CreatedAt = DateTimeOffset.UtcNow
|
||||
},
|
||||
new ClinicalUser
|
||||
{
|
||||
Id = Guid.Parse("33333333-3333-3333-3333-333333333333"),
|
||||
Username = "admin.demo",
|
||||
PasswordHash = BCrypt.Net.BCrypt.HashPassword("DemoAdmin1!"),
|
||||
DisplayName = "Demo Admin",
|
||||
Role = ClinicalRole.Admin,
|
||||
CreatedAt = DateTimeOffset.UtcNow
|
||||
},
|
||||
new ClinicalUser
|
||||
{
|
||||
Id = Guid.Parse("44444444-4444-4444-4444-444444444444"),
|
||||
Username = "integration.mirth",
|
||||
PasswordHash = BCrypt.Net.BCrypt.HashPassword("MirthIntegration1!"),
|
||||
DisplayName = "Mirth Connect",
|
||||
Role = ClinicalRole.Integration,
|
||||
CreatedAt = DateTimeOffset.UtcNow
|
||||
});
|
||||
if (string.IsNullOrWhiteSpace(password))
|
||||
throw new InvalidOperationException(
|
||||
"Simulation:Enabled requires Simulation:RunnerPassword to seed simulation.runner.");
|
||||
|
||||
db.ClinicalUsers.Add(new ClinicalUser
|
||||
{
|
||||
Id = SimulationRunnerUserId,
|
||||
Username = "simulation.runner",
|
||||
PasswordHash = BCrypt.Net.BCrypt.HashPassword(password),
|
||||
DisplayName = "Simulation Runner",
|
||||
Role = ClinicalRole.Integration,
|
||||
CreatedAt = DateTimeOffset.UtcNow
|
||||
});
|
||||
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user