btcpayserver/BTCPayServer/HostedServices/InvoiceEventSaverService.cs
2021-11-23 13:55:34 +09:00

44 lines
1.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using BTCPayServer.Data;
using BTCPayServer.Events;
using BTCPayServer.Logging;
using BTCPayServer.Services.Invoices;
using NBXplorer;
namespace BTCPayServer.HostedServices
{
public class InvoiceEventSaverService : EventHostedServiceBase
{
private readonly InvoiceRepository _invoiceRepository;
public InvoiceEventSaverService(EventAggregator eventAggregator, InvoiceRepository invoiceRepository, Logs logs) : base(
eventAggregator, logs)
{
_invoiceRepository = invoiceRepository;
}
protected override void SubscribeToEvents()
{
Subscribe<InvoiceEvent>();
Subscribe<InvoiceDataChangedEvent>();
Subscribe<InvoiceStopWatchedEvent>();
Subscribe<InvoiceIPNEvent>();
}
protected override async Task ProcessEvent(object evt, CancellationToken cancellationToken)
{
var e = (IHasInvoiceId)evt;
var severity = InvoiceEventData.EventSeverity.Info;
if (evt is InvoiceIPNEvent ipn)
{
severity = string.IsNullOrEmpty(ipn.Error) ? InvoiceEventData.EventSeverity.Success
: InvoiceEventData.EventSeverity.Error;
}
await _invoiceRepository.AddInvoiceEvent(e.InvoiceId, e, severity);
}
}
}