fix: auto-create default branch on cafe registration + backfill existing
CI/CD / CI · API (dotnet build + test) (push) Successful in 42s
CI/CD / CI · Admin API (dotnet build) (push) Successful in 34s
CI/CD / CI · Dashboard (tsc) (push) Successful in 1m3s
CI/CD / CI · Admin Web (tsc) (push) Successful in 34s
CI/CD / CI · Website (tsc) (push) Successful in 44s
CI/CD / CI · Koja (tsc) (push) Successful in 49s
CI/CD / Deploy · all services (push) Has been cancelled
CI/CD / CI · API (dotnet build + test) (push) Successful in 42s
CI/CD / CI · Admin API (dotnet build) (push) Successful in 34s
CI/CD / CI · Dashboard (tsc) (push) Successful in 1m3s
CI/CD / CI · Admin Web (tsc) (push) Successful in 34s
CI/CD / CI · Website (tsc) (push) Successful in 44s
CI/CD / CI · Koja (tsc) (push) Successful in 49s
CI/CD / Deploy · all services (push) Has been cancelled
- VerifyRegisterAsync: create a Branch named after the café alongside the Café and Owner, so new owners can use the dashboard immediately without hitting the "select a branch" gate - PlatformDataSeeder: EnsureDefaultBranchesAsync runs on every boot and creates a default branch for any existing café that has none (covers cafés registered before this fix) Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -367,6 +367,15 @@ public class AuthService : IAuthService
|
||||
PlanTier = PlanTier.Free,
|
||||
};
|
||||
|
||||
// Auto-create a default main branch so the owner can start using the
|
||||
// dashboard immediately without hitting the "select a branch" gate.
|
||||
var defaultBranch = new Branch
|
||||
{
|
||||
CafeId = cafe.Id,
|
||||
Name = cafeName,
|
||||
IsActive = true,
|
||||
};
|
||||
|
||||
var owner = new Employee
|
||||
{
|
||||
CafeId = cafe.Id,
|
||||
@@ -376,6 +385,7 @@ public class AuthService : IAuthService
|
||||
};
|
||||
|
||||
_db.Cafes.Add(cafe);
|
||||
_db.Branches.Add(defaultBranch);
|
||||
_db.Employees.Add(owner);
|
||||
await _db.SaveChangesAsync(cancellationToken);
|
||||
|
||||
|
||||
@@ -125,8 +125,46 @@ public static class PlatformDataSeeder
|
||||
await EnsureCatalogUpgradesAsync(db, logger);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ensures every café has at least one active branch. Idempotent.
|
||||
/// Creates a default branch named after the café for any café that has none.
|
||||
/// </summary>
|
||||
private static async Task EnsureDefaultBranchesAsync(AppDbContext db, ILogger logger)
|
||||
{
|
||||
// Load café IDs that have zero branches in one query
|
||||
var cafeIdsWithBranches = await db.Branches
|
||||
.Where(b => b.DeletedAt == null)
|
||||
.Select(b => b.CafeId)
|
||||
.Distinct()
|
||||
.ToListAsync();
|
||||
|
||||
var cafesWithoutBranch = await db.Cafes
|
||||
.Where(c => c.DeletedAt == null && !cafeIdsWithBranches.Contains(c.Id))
|
||||
.Select(c => new { c.Id, c.Name })
|
||||
.ToListAsync();
|
||||
|
||||
if (cafesWithoutBranch.Count == 0) return;
|
||||
|
||||
foreach (var cafe in cafesWithoutBranch)
|
||||
{
|
||||
db.Branches.Add(new Branch
|
||||
{
|
||||
CafeId = cafe.Id,
|
||||
Name = cafe.Name,
|
||||
IsActive = true,
|
||||
});
|
||||
}
|
||||
|
||||
await db.SaveChangesAsync();
|
||||
logger.LogInformation("Created default branch for {Count} café(s) that had none", cafesWithoutBranch.Count);
|
||||
}
|
||||
|
||||
private static async Task EnsureCatalogUpgradesAsync(AppDbContext db, ILogger logger)
|
||||
{
|
||||
// Ensure every café has at least one branch. Cafés registered before the
|
||||
// auto-branch feature was added are patched on the first boot after upgrade.
|
||||
await EnsureDefaultBranchesAsync(db, logger);
|
||||
|
||||
var featureAdds = new[]
|
||||
{
|
||||
("menu_3d", "منوی سهبعدی", "3D menu", "growth"),
|
||||
|
||||
Reference in New Issue
Block a user