From 8d3b45bdec05affdf9a4c1f72bd2a012f8626ffe Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Thu, 19 Jul 2018 21:38:55 +0900 Subject: [PATCH] Delete store if no owner --- BTCPayServer/HostedServices/MigratorHostedService.cs | 2 +- BTCPayServer/Services/Stores/StoreRepository.cs | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/BTCPayServer/HostedServices/MigratorHostedService.cs b/BTCPayServer/HostedServices/MigratorHostedService.cs index 2778713bf..af7029c44 100644 --- a/BTCPayServer/HostedServices/MigratorHostedService.cs +++ b/BTCPayServer/HostedServices/MigratorHostedService.cs @@ -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); } diff --git a/BTCPayServer/Services/Stores/StoreRepository.cs b/BTCPayServer/Services/Stores/StoreRepository.cs index f85deb6ed..05ec93de0 100644 --- a/BTCPayServer/Services/Stores/StoreRepository.cs +++ b/BTCPayServer/Services/Stores/StoreRepository.cs @@ -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).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 CreateStore(string ownerId, string name)