Delete store if no owner

This commit is contained in:
nicolas.dorier 2018-07-19 21:38:55 +09:00
parent bbd19a96ec
commit 8d3b45bdec
2 changed files with 5 additions and 1 deletions

View File

@ -67,7 +67,7 @@ namespace BTCPayServer.HostedServices
{
if (!ctx.Database.SupportDropForeignKey())
return;
foreach (var store in await ctx.Stores.Where(s => s.UserStores.Count() == 0).ToArrayAsync())
foreach (var store in await ctx.Stores.Where(s => s.UserStores.Where(u => u.Role == StoreRoles.Owner).Count() == 0).ToArrayAsync())
{
ctx.Stores.Remove(store);
}

View File

@ -114,13 +114,17 @@ namespace BTCPayServer.Services.Stores
public async Task RemoveStoreUser(string storeId, string userId)
{
bool delete = false;
using (var ctx = _ContextFactory.CreateContext())
{
var userStore = new UserStore() { StoreDataId = storeId, ApplicationUserId = userId };
ctx.UserStore.Add(userStore);
ctx.Entry<UserStore>(userStore).State = EntityState.Deleted;
await ctx.SaveChangesAsync();
delete = await ctx.UserStore.Where(u => u.StoreDataId == storeId && u.Role == StoreRoles.Owner).CountAsync() == 0;
}
if (delete)
await DeleteStore(storeId);
}
public async Task<StoreData> CreateStore(string ownerId, string name)