Fix missing walletchanged event and add storeremoved event (#4676)

This commit is contained in:
Andrew Camilleri
2023-02-22 05:13:58 +01:00
committed by GitHub
parent e0486aaa24
commit d542a61f5a
4 changed files with 43 additions and 5 deletions

View File

@@ -5,6 +5,7 @@ using BTCPayServer.Abstractions.Extensions;
using BTCPayServer.Client;
using BTCPayServer.Client.Models;
using BTCPayServer.Data;
using BTCPayServer.Events;
using BTCPayServer.Payments;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@@ -90,6 +91,10 @@ namespace BTCPayServer.Controllers.Greenfield
var rawResult = GetExistingBtcLikePaymentMethod(cryptoCode, store);
var result = new OnChainPaymentMethodDataWithSensitiveData(rawResult.CryptoCode, rawResult.DerivationScheme,
rawResult.Enabled, rawResult.Label, rawResult.AccountKeyPath, response.GetMnemonic(), derivationSchemeSettings.PaymentId.ToStringNormalized());
_eventAggregator.Publish(new WalletChangedEvent()
{
WalletId = new WalletId(storeId, cryptoCode)
});
return Ok(result);
}

View File

@@ -7,6 +7,7 @@ using BTCPayServer.Abstractions.Extensions;
using BTCPayServer.Client;
using BTCPayServer.Client.Models;
using BTCPayServer.Data;
using BTCPayServer.Events;
using BTCPayServer.HostedServices;
using BTCPayServer.Payments;
using BTCPayServer.Services;
@@ -34,6 +35,7 @@ namespace BTCPayServer.Controllers.Greenfield
private readonly BTCPayWalletProvider _walletProvider;
private readonly IAuthorizationService _authorizationService;
private readonly ExplorerClientProvider _explorerClientProvider;
private readonly EventAggregator _eventAggregator;
public GreenfieldStoreOnChainPaymentMethodsController(
StoreRepository storeRepository,
@@ -41,13 +43,15 @@ namespace BTCPayServer.Controllers.Greenfield
BTCPayWalletProvider walletProvider,
IAuthorizationService authorizationService,
ExplorerClientProvider explorerClientProvider,
PoliciesSettings policiesSettings)
PoliciesSettings policiesSettings,
EventAggregator eventAggregator)
{
_storeRepository = storeRepository;
_btcPayNetworkProvider = btcPayNetworkProvider;
_walletProvider = walletProvider;
_authorizationService = authorizationService;
_explorerClientProvider = explorerClientProvider;
_eventAggregator = eventAggregator;
PoliciesSettings = policiesSettings;
}
@@ -208,6 +212,10 @@ namespace BTCPayServer.Controllers.Greenfield
var store = Store;
store.SetSupportedPaymentMethod(id, null);
await _storeRepository.UpdateStore(store);
_eventAggregator.Publish(new WalletChangedEvent()
{
WalletId = new WalletId(storeId, cryptoCode)
});
return Ok();
}
@@ -254,6 +262,10 @@ namespace BTCPayServer.Controllers.Greenfield
storeBlob.SetExcluded(id, !request.Enabled);
store.SetStoreBlob(storeBlob);
await _storeRepository.UpdateStore(store);
_eventAggregator.Publish(new WalletChangedEvent()
{
WalletId = new WalletId(storeId, cryptoCode)
});
return Ok(GetExistingBtcLikePaymentMethod(cryptoCode, store));
}
catch

View File

@@ -0,0 +1,14 @@
namespace BTCPayServer.Events;
public class StoreRemovedEvent
{
public StoreRemovedEvent(string storeId)
{
StoreId = storeId;
}
public string StoreId { get; set; }
public override string ToString()
{
return $"Store {StoreId} has been removed";
}
}

View File

@@ -5,6 +5,7 @@ using System.Linq;
using System.Threading.Tasks;
using BTCPayServer.Abstractions.Contracts;
using BTCPayServer.Data;
using BTCPayServer.Events;
using BTCPayServer.Migrations;
using Microsoft.EntityFrameworkCore;
using NBitcoin;
@@ -16,6 +17,7 @@ namespace BTCPayServer.Services.Stores
public class StoreRepository : IStoreRepository
{
private readonly ApplicationDbContextFactory _ContextFactory;
private readonly EventAggregator _eventAggregator;
public JsonSerializerSettings SerializerSettings { get; }
@@ -23,9 +25,10 @@ namespace BTCPayServer.Services.Stores
{
return _ContextFactory.CreateContext();
}
public StoreRepository(ApplicationDbContextFactory contextFactory, JsonSerializerSettings serializerSettings)
public StoreRepository(ApplicationDbContextFactory contextFactory, JsonSerializerSettings serializerSettings, EventAggregator eventAggregator)
{
_ContextFactory = contextFactory ?? throw new ArgumentNullException(nameof(contextFactory));
_eventAggregator = eventAggregator;
SerializerSettings = serializerSettings;
}
@@ -120,14 +123,17 @@ namespace BTCPayServer.Services.Stores
public async Task CleanUnreachableStores()
{
using var ctx = _ContextFactory.CreateContext();
await using var ctx = _ContextFactory.CreateContext();
if (!ctx.Database.SupportDropForeignKey())
return;
foreach (var store in await ctx.Stores.Where(s => !s.UserStores.Where(u => u.Role == StoreRoles.Owner).Any()).ToArrayAsync())
var events = new List<Events.StoreRemovedEvent>();
foreach (var store in await ctx.Stores.Where(s => s.UserStores.All(u => u.Role != StoreRoles.Owner)).ToArrayAsync())
{
ctx.Stores.Remove(store);
events.Add(new Events.StoreRemovedEvent(store.Id));
}
await ctx.SaveChangesAsync();
events.ForEach(e => _eventAggregator.Publish(e));
}
public async Task<bool> RemoveStoreUser(string storeId, string userId)
@@ -147,7 +153,7 @@ namespace BTCPayServer.Services.Stores
private async Task DeleteStoreIfOrphan(string storeId)
{
using var ctx = _ContextFactory.CreateContext();
await using var ctx = _ContextFactory.CreateContext();
if (ctx.Database.SupportDropForeignKey())
{
if (!await ctx.UserStore.Where(u => u.StoreDataId == storeId && u.Role == StoreRoles.Owner).AnyAsync())
@@ -157,6 +163,7 @@ namespace BTCPayServer.Services.Stores
{
ctx.Stores.Remove(store);
await ctx.SaveChangesAsync();
_eventAggregator.Publish(new StoreRemovedEvent(store.Id));
}
}
}