mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2026-01-04 14:44:30 +01:00
Fix bug where store was not properly deleted
This commit is contained in:
@@ -114,17 +114,34 @@ 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;
|
||||
|
||||
}
|
||||
await DeleteStoreIfOrphan(storeId);
|
||||
}
|
||||
|
||||
private async Task DeleteStoreIfOrphan(string storeId)
|
||||
{
|
||||
using (var ctx = _ContextFactory.CreateContext())
|
||||
{
|
||||
if (ctx.Database.SupportDropForeignKey())
|
||||
{
|
||||
if (await ctx.UserStore.Where(u => u.StoreDataId == storeId && u.Role == StoreRoles.Owner).CountAsync() == 0)
|
||||
{
|
||||
var store = await ctx.Stores.FindAsync(storeId);
|
||||
if (store != null)
|
||||
{
|
||||
ctx.Stores.Remove(store);
|
||||
await ctx.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (delete)
|
||||
await DeleteStore(storeId);
|
||||
}
|
||||
|
||||
public async Task<StoreData> CreateStore(string ownerId, string name)
|
||||
@@ -162,6 +179,7 @@ namespace BTCPayServer.Services.Stores
|
||||
ctx.UserStore.Remove(storeUser);
|
||||
await ctx.SaveChangesAsync();
|
||||
}
|
||||
await DeleteStoreIfOrphan(storeId);
|
||||
}
|
||||
|
||||
public async Task UpdateStore(StoreData store)
|
||||
|
||||
Reference in New Issue
Block a user