mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-01-19 13:43:46 +01:00
Make App Inventory Updater run updates in order
followed the same logic we used in the new auto labelling
This commit is contained in:
parent
3e13e478ad
commit
70d4e98dff
@ -11,22 +11,81 @@ namespace BTCPayServer.HostedServices
|
||||
{
|
||||
public class AppInventoryUpdaterHostedService : EventHostedServiceBase
|
||||
{
|
||||
private readonly AppService _AppService;
|
||||
private readonly EventAggregator _eventAggregator;
|
||||
private readonly AppService _appService;
|
||||
|
||||
protected override void SubscribeToEvents()
|
||||
{
|
||||
Subscribe<InvoiceEvent>();
|
||||
Subscribe<UpdateAppInventory>();
|
||||
}
|
||||
|
||||
public AppInventoryUpdaterHostedService(EventAggregator eventAggregator, AppService appService) : base(
|
||||
eventAggregator)
|
||||
{
|
||||
_AppService = appService;
|
||||
_eventAggregator = eventAggregator;
|
||||
_appService = appService;
|
||||
}
|
||||
|
||||
protected override async Task ProcessEvent(object evt, CancellationToken cancellationToken)
|
||||
{
|
||||
if (evt is InvoiceEvent invoiceEvent)
|
||||
if (evt is UpdateAppInventory updateAppInventory)
|
||||
{
|
||||
//get all apps that were tagged that have manageable inventory that has an item that matches the item code in the invoice
|
||||
var apps = (await _appService.GetApps(updateAppInventory.AppId)).Select(data =>
|
||||
{
|
||||
switch (Enum.Parse<AppType>(data.AppType))
|
||||
{
|
||||
case AppType.PointOfSale:
|
||||
var possettings = data.GetSettings<AppsController.PointOfSaleSettings>();
|
||||
return (Data: data, Settings: (object)possettings,
|
||||
Items: _appService.Parse(possettings.Template, possettings.Currency));
|
||||
case AppType.Crowdfund:
|
||||
var cfsettings = data.GetSettings<CrowdfundSettings>();
|
||||
return (Data: data, Settings: (object)cfsettings,
|
||||
Items: _appService.Parse(cfsettings.PerksTemplate, cfsettings.TargetCurrency));
|
||||
default:
|
||||
return (null, null, null);
|
||||
}
|
||||
}).Where(tuple => tuple.Data != null && tuple.Items.Any(item =>
|
||||
item.Inventory.HasValue &&
|
||||
updateAppInventory.Items.ContainsKey(item.Id)));
|
||||
foreach (var valueTuple in apps)
|
||||
{
|
||||
foreach (var item1 in valueTuple.Items.Where(item =>
|
||||
updateAppInventory.Items.ContainsKey(item.Id)))
|
||||
{
|
||||
if (updateAppInventory.Deduct)
|
||||
{
|
||||
item1.Inventory -= updateAppInventory.Items[item1.Id];
|
||||
}
|
||||
else
|
||||
{
|
||||
item1.Inventory += updateAppInventory.Items[item1.Id];
|
||||
}
|
||||
}
|
||||
|
||||
switch (Enum.Parse<AppType>(valueTuple.Data.AppType))
|
||||
{
|
||||
case AppType.PointOfSale:
|
||||
|
||||
((AppsController.PointOfSaleSettings)valueTuple.Settings).Template =
|
||||
_appService.SerializeTemplate(valueTuple.Items);
|
||||
break;
|
||||
case AppType.Crowdfund:
|
||||
((CrowdfundSettings)valueTuple.Settings).PerksTemplate =
|
||||
_appService.SerializeTemplate(valueTuple.Items);
|
||||
break;
|
||||
default:
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
valueTuple.Data.SetSettings(valueTuple.Settings);
|
||||
await _appService.UpdateOrCreateApp(valueTuple.Data);
|
||||
}
|
||||
|
||||
|
||||
}else if (evt is InvoiceEvent invoiceEvent)
|
||||
{
|
||||
Dictionary<string, int> cartItems = null;
|
||||
bool deduct;
|
||||
@ -44,7 +103,7 @@ namespace BTCPayServer.HostedServices
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if ((!string.IsNullOrEmpty(invoiceEvent.Invoice.ProductInformation.ItemCode) ||
|
||||
AppService.TryParsePosCartItems(invoiceEvent.Invoice.PosData, out cartItems)))
|
||||
{
|
||||
@ -55,80 +114,28 @@ namespace BTCPayServer.HostedServices
|
||||
return;
|
||||
}
|
||||
|
||||
//get all apps that were tagged that have manageable inventory that has an item that matches the item code in the invoice
|
||||
var apps = (await _AppService.GetApps(appIds)).Select(data =>
|
||||
var items = cartItems ?? new Dictionary<string, int>();
|
||||
if (!string.IsNullOrEmpty(invoiceEvent.Invoice.ProductInformation.ItemCode))
|
||||
{
|
||||
switch (Enum.Parse<AppType>(data.AppType))
|
||||
{
|
||||
case AppType.PointOfSale:
|
||||
var possettings = data.GetSettings<AppsController.PointOfSaleSettings>();
|
||||
return (Data: data, Settings: (object)possettings,
|
||||
Items: _AppService.Parse(possettings.Template, possettings.Currency));
|
||||
case AppType.Crowdfund:
|
||||
var cfsettings = data.GetSettings<CrowdfundSettings>();
|
||||
return (Data: data, Settings: (object)cfsettings,
|
||||
Items: _AppService.Parse(cfsettings.PerksTemplate, cfsettings.TargetCurrency));
|
||||
default:
|
||||
return (null, null, null);
|
||||
}
|
||||
}).Where(tuple => tuple.Data != null && tuple.Items.Any(item =>
|
||||
item.Inventory.HasValue &&
|
||||
((!string.IsNullOrEmpty(invoiceEvent.Invoice.ProductInformation.ItemCode) &&
|
||||
item.Id == invoiceEvent.Invoice.ProductInformation.ItemCode) ||
|
||||
(cartItems != null && cartItems.ContainsKey(item.Id)))));
|
||||
foreach (var valueTuple in apps)
|
||||
{
|
||||
foreach (var item1 in valueTuple.Items.Where(item =>
|
||||
((!string.IsNullOrEmpty(invoiceEvent.Invoice.ProductInformation.ItemCode) &&
|
||||
item.Id == invoiceEvent.Invoice.ProductInformation.ItemCode) ||
|
||||
(cartItems != null && cartItems.ContainsKey(item.Id)))))
|
||||
{
|
||||
if (cartItems != null && cartItems.ContainsKey(item1.Id))
|
||||
{
|
||||
if (deduct)
|
||||
{
|
||||
item1.Inventory -= cartItems[item1.Id];
|
||||
}
|
||||
else
|
||||
{
|
||||
item1.Inventory += cartItems[item1.Id];
|
||||
}
|
||||
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(invoiceEvent.Invoice.ProductInformation.ItemCode) &&
|
||||
item1.Id == invoiceEvent.Invoice.ProductInformation.ItemCode)
|
||||
{
|
||||
if (deduct)
|
||||
{
|
||||
item1.Inventory--;
|
||||
}
|
||||
else
|
||||
{
|
||||
item1.Inventory++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch (Enum.Parse<AppType>(valueTuple.Data.AppType))
|
||||
{
|
||||
case AppType.PointOfSale:
|
||||
|
||||
((AppsController.PointOfSaleSettings)valueTuple.Settings).Template =
|
||||
_AppService.SerializeTemplate(valueTuple.Items);
|
||||
break;
|
||||
case AppType.Crowdfund:
|
||||
((CrowdfundSettings)valueTuple.Settings).PerksTemplate =
|
||||
_AppService.SerializeTemplate(valueTuple.Items);
|
||||
break;
|
||||
default:
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
valueTuple.Data.SetSettings(valueTuple.Settings);
|
||||
await _AppService.UpdateOrCreateApp(valueTuple.Data);
|
||||
items.TryAdd(invoiceEvent.Invoice.ProductInformation.ItemCode, 1);
|
||||
}
|
||||
|
||||
_eventAggregator.Publish(new UpdateAppInventory()
|
||||
{
|
||||
Deduct = deduct,
|
||||
Items = items,
|
||||
AppId = appIds
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class UpdateAppInventory
|
||||
{
|
||||
public string[] AppId { get; set; }
|
||||
public Dictionary<string, int> Items { get; set; }
|
||||
public bool Deduct { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user