diff --git a/Nop.Plugin.Misc.AuctionPlugin/Hubs/SignalRMessageHandler.cs b/Nop.Plugin.Misc.AuctionPlugin/Hubs/SignalRMessageHandler.cs index 52ffa61..ccfbab8 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Hubs/SignalRMessageHandler.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Hubs/SignalRMessageHandler.cs @@ -15,6 +15,7 @@ using Nop.Services.Customers; using DocumentFormat.OpenXml.Wordprocessing; using Mango.Nop.Core.Repositories; using Nop.Core.Caching; +using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities.Interfaces; namespace Nop.Plugin.Misc.AuctionPlugin.Hubs { @@ -110,15 +111,11 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs if (product == null) return; var revertLastBid = await auctionService.RevertAuctionBidByProductToAuctionIdAsync(productToAuction.Id); - if (revertLastBid == null) - { - //TODO: NA ILYENKOR VAN A CUMI... - J. - logger.Warning($"SignalRMessageHandler.HandleRevertAuctionBidRequest(); revertLastBid == null", null, customer); - return; - } - await SetAuctionBidPrice(revertLastBid.BidPrice, productToAuction, product, revertLastBid.CustomerId); - await SendAuctionBidMessageAsync(revertLastBid, productToAuction, product, customer.Id); + if (revertLastBid == null) await ResetProductToAuction(productToAuction); + else await SetAuctionBidPrice(revertLastBid.BidPrice, productToAuction, product, revertLastBid.CustomerId); + + await SendAuctionBidMessageAsync(productToAuction, product, customer.Id); } catch (Exception ex) { @@ -158,11 +155,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs return; } - if (auctionProductStatusRequest.AuctionStatus == AuctionStatus.None) - { - productToAuction.AuctionStatus = AuctionStatus.None; - await auctionService.ResetProductToAuctionAsync(productToAuction, productToAuction.StartingPrice); - } + if (auctionProductStatusRequest.AuctionStatus == AuctionStatus.None) await ResetProductToAuction(productToAuction); else { switch (auctionProductStatusRequest.AuctionStatus) @@ -182,6 +175,13 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs productToAuction.AuctionStatus = AuctionStatus.Sold; productToAuction.CurrentPrice = lastAuctionBid.BidPrice; productToAuction.WinnerCustomerId = lastAuctionBid.CustomerId; + + //productToAuction.OrderGuid = Guid.NewGuid(); //TODO: - J. + + var placeOrderResult = await auctionService.CreateOrderForWinnerAsync(productToAuction); + if (placeOrderResult is not { Success: true }) return; + + //productToAuction.OrderId = placedOrder.PlacedOrder.Id; //TODO: - J. break; case AuctionStatus.NotSold: @@ -217,6 +217,12 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs } } + private Task ResetProductToAuction(ProductToAuctionMapping productToAuction) + { + productToAuction.AuctionStatus = AuctionStatus.None; + return auctionService.ResetProductToAuctionAsync(productToAuction, productToAuction.StartingPrice); + } + private async Task HandleBidRequest(Customer customer, AuctionBidRequest bidRequestMessage) { if (bidRequestMessage == null) @@ -246,22 +252,17 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs var product = await auctionService.GetProductById(bidRequestMessage.ProductId); if (product == null) return; + if (IsValidBidPrice(product.Price, activeProductAuction.CurrentPrice, bidRequestMessage.BidPrice, customer)) return; + var auctionBid = bidRequestMessage.CreateMainEntity(); auctionBid.ProductAuctionMappingId = activeProductAuction.Id; - - //TODO: Validálni, h a bejövő BidPrice valóban a következő licit lépcső! - J. - if (product.Price >= auctionBid.BidPrice || activeProductAuction.CurrentPrice >= auctionBid.BidPrice) - { - logger.Warning($"SignalRMessageHandler.HandleBidRequest(); (product.Price >= bidRequestMessage.BidPrice || activeProductAuction.CurrentPrice >= auctionBid.BidPrice); productPrice: {product.Price}; bidRequestPrice: {auctionBid.BidPrice}; activeProductAuctionPrice: {activeProductAuction.CurrentPrice}", null, customer); - return; - } - + await auctionService.InsertBidAsync(auctionBid); activeProductAuction.AuctionStatus = AuctionStatus.Active; - if (!await SetAuctionBidPrice(bidRequestMessage.BidPrice, activeProductAuction, product, customer.Id)) return; + if (!await SetAuctionBidPrice(auctionBid.BidPrice, activeProductAuction, product, customer.Id)) return; - await SendAuctionBidMessageAsync(auctionBid, activeProductAuction, product, customer.Id); + await SendAuctionBidMessageAsync(activeProductAuction, product, customer.Id); } catch (Exception ex) { @@ -269,10 +270,21 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs } } - private async Task SendAuctionBidMessageAsync(AuctionBid auctionBid, ProductToAuctionMapping productToAuction, Product product, int customerId) + private bool IsValidBidPrice(decimal productPrice, decimal productToAuctionCurrentPrice, decimal bidRequestPrice, Customer customer = null) { - var stepAmount = AuctionService.GetStepAmount(auctionBid.BidPrice); - var nextBidPrice = auctionBid.BidPrice + stepAmount; + if (productPrice >= bidRequestPrice || productToAuctionCurrentPrice >= bidRequestPrice || bidRequestPrice != GetNextBidPrice(productToAuctionCurrentPrice)) + { + logger.Warning($"SignalRMessageHandler.IsValidBidPrice(); (productPrice >= bidRequestPrice || productToAuctionCurrentPrice >= bidRequestPrice || bidRequestPrice != GetNextBidPrice(productToAuctionCurrentPrice)); productPrice: {productPrice}; productToAuctionCurrentPrice: {productToAuctionCurrentPrice}; bidRequestPrice: {bidRequestPrice}", null, customer); + return true; + } + + return false; + } + + private async Task SendAuctionBidMessageAsync(ProductToAuctionMapping productToAuction, Product product, int customerId) + { + var stepAmount = GetStepAmount(productToAuction.CurrentPrice); + var nextBidPrice = GetNextBidPrice(productToAuction.CurrentPrice, stepAmount); var bidMessage = new MessageWrapper { @@ -281,7 +293,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs Data = new BidNotificationMessage(await auctionService.GetAuctionDtoByProductToAuctionIdAsync(productToAuction.Id, true)) //TODO: NE KÉRJÜK LE DB-BŐL!!! - j. { ProductName = product.Name, - CurrentPrice = auctionBid.BidPrice, + CurrentPrice = productToAuction.CurrentPrice, NextStepAmount = stepAmount, NextBidPrice = nextBidPrice, ToasterMessage = "EMPTY", //TODO: - J. @@ -313,6 +325,13 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs return false; } + private static decimal GetStepAmount(decimal currentBidPrice) => AuctionService.GetStepAmount(currentBidPrice); + private static decimal GetNextBidPrice(decimal currentBidPrice) => GetNextBidPrice(currentBidPrice, GetStepAmount(currentBidPrice)); + private static decimal GetNextBidPrice(decimal currentBidPrice, decimal stepAmount) + { + return currentBidPrice + stepAmount; + } + private static bool IsValidRequestAuctionStatus(AuctionStatus newStatus, AuctionStatus oldStatus) { switch (oldStatus) diff --git a/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs b/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs index c1322fd..60b971e 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs @@ -36,8 +36,6 @@ public class AuctionService : IAuctionService /// Ctor /// /// - /// Short term cache manager - /// Cache manager /// /// /// @@ -187,11 +185,35 @@ public class AuctionService : IAuctionService await _ctx.AuctionBids.DeleteAsync(pickupPoint, false); } - public async Task CreateOrderForWinner(ProductToAuctionMapping auctionItem) + public async Task CreateOrderForWinnerAsync(ProductToAuctionMapping auctionItem) { - var processPaymentRequest = new ProcessPaymentRequest(); + if (auctionItem is not { AuctionStatus: AuctionStatus.Sold }) + { + await _logger.InformationAsync($"AuctionService.CreateOrderForWinnerAsync(); (auctionItem is not {{ AuctionStatus: AuctionStatus.Sold }}); auctionItemId: {auctionItem?.Id}; status: {auctionItem?.AuctionStatus}"); + return null; + } + + try + { + var processPaymentRequest = new ProcessPaymentRequest(); + processPaymentRequest.CustomerId = auctionItem.WinnerCustomerId; + processPaymentRequest.CustomValues.Add("ProductToAuctionMappingId", auctionItem.Id); + processPaymentRequest.OrderTotal = auctionItem.CurrentPrice; - await _orderProcessingService.PlaceOrderAsync(processPaymentRequest); + //processPaymentRequest.OrderGuid = productToAuction.OrderGuid; //TODO: - J. + processPaymentRequest.OrderGuid = Guid.NewGuid(); + + var placeOrderResult = await _orderProcessingService.PlaceOrderAsync(processPaymentRequest); + //placeOrderResult.PlacedOrder //TODO:... - J. + + return placeOrderResult.Success ? placeOrderResult : null; + } + catch (Exception ex) + { + _logger.Error($"AuctionService.CreateOrderForWinnerAsync(); Exception; auctionItemId: {auctionItem.Id}", ex); + } + + return null; } #region auctions diff --git a/Nop.Plugin.Misc.AuctionPlugin/Services/IAuctionService.cs b/Nop.Plugin.Misc.AuctionPlugin/Services/IAuctionService.cs index 2628562..bba0e5f 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Services/IAuctionService.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Services/IAuctionService.cs @@ -3,6 +3,7 @@ using Nop.Core; using Nop.Plugin.Misc.AuctionPlugin.Domains; using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos; using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities; +using Nop.Services.Orders; namespace Nop.Plugin.Misc.AuctionPlugin.Services; @@ -59,5 +60,5 @@ public interface IAuctionService Task GetProductToAuctionMappingByIdAsync(int productToAuctionMappingId); Task UpdateProductToAuctionMappingAsync(ProductToAuctionMapping productToAuctionMapping); - Task CreateOrderForWinner(ProductToAuctionMapping productToAuctionMapping); + Task CreateOrderForWinnerAsync(ProductToAuctionMapping productToAuctionMapping); } \ No newline at end of file