mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2024-11-19 18:11:36 +01:00
51690b47a3
* Automated Transfer processors This PR introduces a few things: * Payouts can now be directly nested under a store instead of through a pull payment. * The Wallet Send screen now has an option to "schedule" instead of simply creating a transaction. When you click on schedule, all transaction destinations are converted into approved payouts. Any options relating to fees or coin selection are discarded. * There is a new concept introduced, called "Transfer Processors". Transfer Processors are services for stores that process payouts that are awaiting payment. Each processor specifies which payment methods it can handle. BTCPay Server will have some forms of transfer processors baked in but it has been designed to allow the Plugin System to provide additional processors. * The initial transfer processors provided are "automated processors", for on chain and lightning payment methods. They can be configured to process payouts every X amount of minutes. For on-chain, this means payments are batched into one transaction, resulting in more efficient and cheaper fees for processing. * * fix build * extract * remove magic string stuff * fix error message when scheduling * Paginate migration * add payout count to payment method tab * remove unused var * add protip * optimzie payout migration dramatically * Remove useless double condition * Fix bunch of warnings * Remove warning * Remove warnigns * Rename to Payout processors * fix typo Co-authored-by: Nicolas Dorier <nicolas.dorier@gmail.com>
91 lines
3.2 KiB
C#
91 lines
3.2 KiB
C#
#nullable enable
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using BTCPayServer.Abstractions.Contracts;
|
|
using BTCPayServer.Data;
|
|
using BTCPayServer.Events;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace BTCPayServer.Services
|
|
{
|
|
public class SettingsRepository : ISettingsRepository
|
|
{
|
|
private readonly ApplicationDbContextFactory _ContextFactory;
|
|
private readonly EventAggregator _EventAggregator;
|
|
private readonly IMemoryCache _memoryCache;
|
|
|
|
public SettingsRepository(ApplicationDbContextFactory contextFactory, EventAggregator eventAggregator, IMemoryCache memoryCache)
|
|
{
|
|
_ContextFactory = contextFactory;
|
|
_EventAggregator = eventAggregator;
|
|
_memoryCache = memoryCache;
|
|
}
|
|
|
|
public async Task<T?> GetSettingAsync<T>(string? name = null) where T : class
|
|
{
|
|
name ??= typeof(T).FullName ?? string.Empty;
|
|
return await _memoryCache.GetOrCreateAsync(GetCacheKey(name), async entry =>
|
|
{
|
|
await using var ctx = _ContextFactory.CreateContext();
|
|
var data = await ctx.Settings.Where(s => s.Id == name).FirstOrDefaultAsync();
|
|
return data == null ? default : Deserialize<T>(data.Value);
|
|
});
|
|
}
|
|
public async Task UpdateSetting<T>(T obj, string? name = null) where T : class
|
|
{
|
|
name ??= typeof(T).FullName ?? string.Empty;
|
|
await using (var ctx = _ContextFactory.CreateContext())
|
|
{
|
|
var settings = UpdateSettingInContext<T>(ctx, obj, name);
|
|
try
|
|
{
|
|
await ctx.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateException)
|
|
{
|
|
ctx.Entry(settings).State = EntityState.Added;
|
|
await ctx.SaveChangesAsync();
|
|
}
|
|
}
|
|
_memoryCache.Set(GetCacheKey(name), obj);
|
|
_EventAggregator.Publish(new SettingsChanged<T>()
|
|
{
|
|
Settings = obj,
|
|
SettingsName = name
|
|
});
|
|
}
|
|
|
|
public SettingData UpdateSettingInContext<T>(ApplicationDbContext ctx, T obj, string? name = null) where T : class
|
|
{
|
|
name ??= obj.GetType().FullName ?? string.Empty;
|
|
_memoryCache.Remove(GetCacheKey(name));
|
|
var settings = new SettingData { Id = name, Value = Serialize(obj) };
|
|
ctx.Attach(settings);
|
|
ctx.Entry(settings).State = EntityState.Modified;
|
|
return settings;
|
|
}
|
|
|
|
private T? Deserialize<T>(string value) where T : class
|
|
{
|
|
return JsonConvert.DeserializeObject<T>(value);
|
|
}
|
|
|
|
private string Serialize<T>(T obj)
|
|
{
|
|
return JsonConvert.SerializeObject(obj);
|
|
}
|
|
|
|
private string GetCacheKey(string name)
|
|
{
|
|
return $"{nameof(SettingsRepository)}_{name}";
|
|
}
|
|
|
|
public async Task<T> WaitSettingsChanged<T>(CancellationToken cancellationToken = default) where T : class
|
|
{
|
|
return (await _EventAggregator.WaitNext<SettingsChanged<T>>(cancellationToken)).Settings;
|
|
}
|
|
}
|
|
}
|