Add دندانپزشک + پرستار سالمندان roles (idempotent ensure on startup)
Roles were only seeded on a fresh DB, so existing deployments never got new ones. Introduced a canonical role list + EnsureRolesAsync that runs on every startup and inserts any missing role — so production picks up the two new roles without a manual step. Original 7 keep their order/ids; the two new roles are appended (sort 8-9). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -10,12 +10,42 @@ namespace JobsMedical.Web.Data;
|
||||
/// </summary>
|
||||
public static class SeedData
|
||||
{
|
||||
/// <summary>Canonical role taxonomy (name, category, sort). Add new roles here; they're
|
||||
/// inserted on every startup if missing, so existing DBs pick them up too.</summary>
|
||||
private static readonly (string Name, string Category, int SortOrder)[] CanonicalRoles =
|
||||
{
|
||||
("پزشک عمومی", "پزشک", 1),
|
||||
("پزشک متخصص", "پزشک", 2),
|
||||
("پرستار", "پرستار", 3),
|
||||
("ماما", "ماما", 4),
|
||||
("تکنسین اتاق عمل", "تکنسین", 5),
|
||||
("تکنسین فوریتهای پزشکی", "تکنسین", 6),
|
||||
("کارشناس آزمایشگاه", "تکنسین", 7),
|
||||
("دندانپزشک", "دندانپزشک", 8),
|
||||
("پرستار سالمندان", "پرستار", 9),
|
||||
};
|
||||
|
||||
public static async Task EnsureSeededAsync(AppDbContext db, bool includeDemo = true)
|
||||
{
|
||||
await SeedReferenceAsync(db);
|
||||
await EnsureRolesAsync(db);
|
||||
if (includeDemo) await SeedDemoAsync(db);
|
||||
}
|
||||
|
||||
/// <summary>Idempotently add any canonical role missing from the DB (no-op when all present).</summary>
|
||||
public static async Task EnsureRolesAsync(AppDbContext db)
|
||||
{
|
||||
var existing = await db.Roles.Select(r => r.Name).ToListAsync();
|
||||
var added = false;
|
||||
foreach (var (name, category, sort) in CanonicalRoles)
|
||||
if (!existing.Contains(name))
|
||||
{
|
||||
db.Roles.Add(new Role { Name = name, Category = category, SortOrder = sort, IsActive = true });
|
||||
added = true;
|
||||
}
|
||||
if (added) await db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
// ---------- Reference data (always) ----------
|
||||
public static async Task SeedReferenceAsync(AppDbContext db)
|
||||
{
|
||||
@@ -29,14 +59,7 @@ public static class SeedData
|
||||
new City { Name = "شیراز", Province = "فارس", IsActive = false });
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
db.Roles.AddRange(
|
||||
new Role { Name = "پزشک عمومی", Category = "پزشک", SortOrder = 1 },
|
||||
new Role { Name = "پزشک متخصص", Category = "پزشک", SortOrder = 2 },
|
||||
new Role { Name = "پرستار", Category = "پرستار", SortOrder = 3 },
|
||||
new Role { Name = "ماما", Category = "ماما", SortOrder = 4 },
|
||||
new Role { Name = "تکنسین اتاق عمل", Category = "تکنسین", SortOrder = 5 },
|
||||
new Role { Name = "تکنسین فوریتهای پزشکی", Category = "تکنسین", SortOrder = 6 },
|
||||
new Role { Name = "کارشناس آزمایشگاه", Category = "تکنسین", SortOrder = 7 });
|
||||
// Roles are seeded by EnsureRolesAsync (idempotent, runs every startup).
|
||||
|
||||
foreach (var n in new[] { "سعادتآباد", "شهرک غرب", "ولیعصر / پارکوی", "نارمک",
|
||||
"تهرانپارس", "گیشا / برج میلاد", "ونک", "تجریش" })
|
||||
|
||||
@@ -94,7 +94,8 @@ using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
db.Database.Migrate();
|
||||
await SeedData.SeedReferenceAsync(db); // cities/roles/districts always
|
||||
await SeedData.SeedReferenceAsync(db); // cities/districts on first run
|
||||
await SeedData.EnsureRolesAsync(db); // add any missing roles (idempotent, existing DBs too)
|
||||
// Demo board in Development, or whenever the admin has turned Demo Mode on.
|
||||
var st = await scope.ServiceProvider
|
||||
.GetRequiredService<JobsMedical.Web.Services.Scraping.SettingsService>().GetAsync();
|
||||
|
||||
Reference in New Issue
Block a user