Ensure profile created maintenance method

This commit is contained in:
LittleSheep 2025-05-22 01:31:36 +08:00
parent b0a616c17c
commit 2399bf0309
2 changed files with 25 additions and 0 deletions

View File

@ -428,4 +428,13 @@ public class AccountController(
.Take(take)
.ToListAsync();
}
[HttpPost("/maintenance/ensureProfileCreated")]
[Authorize]
[RequiredPermission("maintenance", "accounts.profiles")]
public async Task<ActionResult> EnsureProfileCreated()
{
await accounts.EnsureAccountProfileCreated();
return Ok();
}
}

View File

@ -2,6 +2,7 @@ using System.Globalization;
using System.Reflection;
using DysonNetwork.Sphere.Localization;
using DysonNetwork.Sphere.Permission;
using EFCore.BulkExtensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Localization;
@ -48,4 +49,19 @@ public class AccountService(
.FirstOrDefaultAsync();
return profile?.Level;
}
/// Maintenance methods for server administrator
public async Task EnsureAccountProfileCreated()
{
var accountsId = await db.Accounts.Select(a => a.Id).ToListAsync();
var existingId = await db.AccountProfiles.Select(p => p.AccountId).ToListAsync();
var missingId = accountsId.Except(existingId).ToList();
if (missingId.Count != 0)
{
var newProfiles = missingId.Select(id => new Profile { AccountId = id }).ToList();
await db.BulkInsertAsync(newProfiles);
}
}
}