From 70d4e98dff5dc29aaa56e0a3f7d988d2dd7ec17d Mon Sep 17 00:00:00 2001 From: Kukks Date: Wed, 29 Apr 2020 14:51:16 +0200 Subject: [PATCH] Make App Inventory Updater run updates in order followed the same logic we used in the new auto labelling --- .../AppInventoryUpdaterHostedService.cs | 155 +++++++++--------- 1 file changed, 81 insertions(+), 74 deletions(-) diff --git a/BTCPayServer/HostedServices/AppInventoryUpdaterHostedService.cs b/BTCPayServer/HostedServices/AppInventoryUpdaterHostedService.cs index 6348e53be..1a2ba2b74 100644 --- a/BTCPayServer/HostedServices/AppInventoryUpdaterHostedService.cs +++ b/BTCPayServer/HostedServices/AppInventoryUpdaterHostedService.cs @@ -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(); + Subscribe(); } 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(data.AppType)) + { + case AppType.PointOfSale: + var possettings = data.GetSettings(); + return (Data: data, Settings: (object)possettings, + Items: _appService.Parse(possettings.Template, possettings.Currency)); + case AppType.Crowdfund: + var cfsettings = data.GetSettings(); + 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(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 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(); + if (!string.IsNullOrEmpty(invoiceEvent.Invoice.ProductInformation.ItemCode)) { - switch (Enum.Parse(data.AppType)) - { - case AppType.PointOfSale: - var possettings = data.GetSettings(); - return (Data: data, Settings: (object)possettings, - Items: _AppService.Parse(possettings.Template, possettings.Currency)); - case AppType.Crowdfund: - var cfsettings = data.GetSettings(); - 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(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 Items { get; set; } + public bool Deduct { get; set; } + } } }