improvements, fixes, etc...

This commit is contained in:
Loretta 2024-12-04 07:54:10 +01:00
parent ea2b318e8c
commit 99f60ad70d
5 changed files with 117 additions and 75 deletions

View File

@ -0,0 +1,8 @@
namespace Nop.Plugin.Misc.AuctionPlugin.Domains.Enums;
public enum ResponseType : byte
{
Error = 0,
ToCaller = 10,
ToAllClients = 20,
}

View File

@ -19,9 +19,9 @@ public class AuctionEventConsumer(IHttpContextAccessor httpContextAccessor, ILog
foreach (var productToAuction in productToAuctions) foreach (var productToAuction in productToAuctions)
{ {
if (await ctx.HasBidByProductToAuctionIdAsync(productToAuction.Id)) if (productToAuction.BidsCount > 0) //await ctx.HasBidByProductToAuctionIdAsync(productToAuction.Id))
{ {
await Logger.InformationAsync($"AuctionEventConsumer.HandleEventAsync<Product>(); HasAuctionBids->continue"); await Logger.InformationAsync($"AuctionEventConsumer.HandleEventAsync<Product>(); HasAuctionBids->continue; productId: {eventMessage.Entity.Id}; productToAuctionId: {productToAuction.Id};");
continue; continue;
} }

View File

@ -12,6 +12,7 @@ using Nop.Services.Catalog;
using Nop.Plugin.Misc.AuctionPlugin.Services; using Nop.Plugin.Misc.AuctionPlugin.Services;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities; using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using static LinqToDB.Reflection.Methods.LinqToDB.Insert;
namespace Nop.Plugin.Misc.AuctionPlugin.Hubs namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
{ {
@ -65,7 +66,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
// Log the message type and data // Log the message type and data
//await _logger.InformationAsync($"AuctionHub.OnConnectedAsync(); Received message of type: {message.MessageType}"); //await _logger.InformationAsync($"AuctionHub.OnConnectedAsync(); Received message of type: {message.MessageType}");
await _signalRMessageHandler.HandleMessage(message); await _signalRMessageHandler.HandleMessage(message, Context.ConnectionId);
} }
} }
} }

View File

@ -1,4 +1,5 @@
using System; using Nop.Plugin.Misc.AuctionPlugin.Domains.Enums;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -10,6 +11,8 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs.Messages
{ {
public string MessageType { get; set; } public string MessageType { get; set; }
public int SenderId { get; set; } public int SenderId { get; set; }
public Guid RequestId { get; set; }
public ResponseType ResponseType { get; set; }
public string Data { get; set; } public string Data { get; set; }
} }
} }

View File

@ -30,68 +30,73 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
{ {
private readonly Mutex _handleMessageMutex = new(); private readonly Mutex _handleMessageMutex = new();
public async Task HandleMessage(MessageWrapper message) public async Task HandleMessage(MessageWrapper messageWrapper, string connectionId)
{ {
var customer = await workContext.GetCurrentCustomerAsync(); var customer = await workContext.GetCurrentCustomerAsync();
if (message?.Data == null) if (messageWrapper?.Data == null)
{ {
logger.Error($"SignalRMessageHandler.HandleMessage(); message?.Data == null", null, customer); logger.Error($"SignalRMessageHandler.HandleMessage(); message?.Data == null; connectionId: {connectionId}", null, customer);
return; return;
} }
await logger.InformationAsync($"SignalRMessageHandler.HandleMessage(); MessageType: {message.MessageType}; jsonData: {message.Data}", null, customer); if (customer == null || messageWrapper.SenderId <= 0 || messageWrapper.SenderId != customer.Id || await customerService.IsGuestAsync(customer))
if (customer == null || message.SenderId <= 0 || message.SenderId != customer.Id || await customerService.IsGuestAsync(customer))
{ {
logger.Error($"SignalRMessageHandler.HandleMessage(); (customer == null || message.SenderId <= 0 || message.SenderId != customer.Id || IsGuestAsync() == true)", null, customer); logger.Error($"SignalRMessageHandler.HandleMessage(); (customer == null || message.SenderId <= 0 || message.SenderId != customer.Id || IsGuestAsync() == true); connectionId: {connectionId}", null, customer);
return; return;
} }
await logger.InformationAsync($"SignalRMessageHandler.HandleMessage(); Before lock; MessageType: {messageWrapper.MessageType}; connectionId: {connectionId}; jsonData: {messageWrapper.Data}", null, customer);
//TODO: az összes request-et egy base-ből származtatni és beletenni az AuctionRequestMode-ot! - J. //TODO: az összes request-et egy base-ből származtatni és beletenni az AuctionRequestMode-ot! - J.
using (_handleMessageMutex.UseWaitOne()) using (_handleMessageMutex.UseWaitOne())
{ {
try try
{ {
if (message.MessageType == "BidRequestMessage") await HandleBidRequest(customer, message.Data.JsonTo<AuctionBidRequest>()); await logger.InformationAsync($"SignalRMessageHandler.HandleMessage(); Enter lock; MessageType: {messageWrapper.MessageType}; connectionId: {connectionId}", null, customer);
if (messageWrapper.MessageType == "BidRequestMessage") await HandleBidRequest(customer, messageWrapper);
else else
{ {
if (!await customerService.IsAdminAsync(customer)) if (!await customerService.IsAdminAsync(customer))
{ {
logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); IsAdminAsync() == false!", null, customer); logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); IsAdminAsync() == false; connectionId: {connectionId}", null, customer);
return; return;
} }
switch (message.MessageType) switch (messageWrapper.MessageType)
{ {
case nameof(AuctionProductStatusRequest): case nameof(AuctionProductStatusRequest):
await HandleProductToAuctionStatusChangedRequest(customer, message.Data.JsonTo<AuctionProductStatusRequest>()); await HandleProductToAuctionStatusChangedRequest(customer, messageWrapper);
break; break;
case nameof(RevertAuctionBidRequest): case nameof(RevertAuctionBidRequest):
await HandleRevertAuctionBidRequest(customer, message.Data.JsonTo<RevertAuctionBidRequest>()); await HandleRevertAuctionBidRequest(customer, messageWrapper);
break; break;
default: default:
// Add other message types here // Add other message types here
await logger.ErrorAsync($"SignalRMessageHandler.HandleMessage(); Unknown message type; MessageType: {message.MessageType}", null, customer); await logger.ErrorAsync($"SignalRMessageHandler.HandleMessage(); Unknown message type; MessageType: {messageWrapper.MessageType}; connectionId: {connectionId}", null, customer);
break; break;
} }
} }
await logger.InformationAsync($"SignalRMessageHandler.HandleMessage(); Exit lock; MessageType: {messageWrapper.MessageType}; connectionId: {connectionId}", null, customer);
} }
catch (Exception ex) catch (Exception ex)
{ {
logger.Error($"SignalRMessageHandler.HandleMessage(); switch (message.MessageType); MessageType: {message.MessageType}; Exception: {ex.Message}", ex, customer); logger.Error($"SignalRMessageHandler.HandleMessage(); switch (message.MessageType); MessageType: {messageWrapper.MessageType}; connectionId: {connectionId}; Exception: {ex.Message}", ex, customer);
} }
} }
} }
private async Task HandleRevertAuctionBidRequest(Customer customer, RevertAuctionBidRequest revertAuctionBidRequest) private async Task<ResponseType> HandleRevertAuctionBidRequest(Customer customer, MessageWrapper messageWrapper)
{ {
var revertAuctionBidRequest = messageWrapper.Data.JsonTo<RevertAuctionBidRequest>();
if (revertAuctionBidRequest == null) if (revertAuctionBidRequest == null)
{ {
logger.Error($"SignalRMessageHandler.HandleRevertAuctionBidRequest(); auctionProductStatusRequest == null", null, customer); logger.Error($"SignalRMessageHandler.HandleRevertAuctionBidRequest(); auctionProductStatusRequest == null", null, customer);
return; return ResponseType.Error;
} }
try try
@ -100,13 +105,13 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
if (productToAuction is not { AuctionStatus: AuctionStatus.Pause }) if (productToAuction is not { AuctionStatus: AuctionStatus.Pause })
{ {
logger.Warning($"SignalRMessageHandler.HandleRevertAuctionBidRequest(); (productToAuction is not {{ AuctionStatus: AuctionStatus.Pause }}); AuctionStatus: {productToAuction?.AuctionStatus}", null, customer); logger.Warning($"SignalRMessageHandler.HandleRevertAuctionBidRequest(); (productToAuction is not {{ AuctionStatus: AuctionStatus.Pause }}); AuctionStatus: {productToAuction?.AuctionStatus}", null, customer);
return; return ResponseType.Error;
} }
await logger.InformationAsync($"SignalRMessageHandler.HandleRevertAuctionBidRequest(); (productToAuction is not {{ AuctionStatus: AuctionStatus.Pause }}); AuctionStatus: {productToAuction.AuctionStatus}", null, customer); await logger.InformationAsync($"SignalRMessageHandler.HandleRevertAuctionBidRequest(); (productToAuction is not {{ AuctionStatus: AuctionStatus.Pause }}); AuctionStatus: {productToAuction.AuctionStatus}", null, customer);
var product = await auctionService.GetProductById(productToAuction.ProductId); var product = await auctionService.GetProductById(productToAuction.ProductId);
if (product == null) return; if (product == null) return ResponseType.Error;
var revertLastBid = await auctionService.RevertAuctionBidByProductToAuctionIdAsync(productToAuction.Id); var revertLastBid = await auctionService.RevertAuctionBidByProductToAuctionIdAsync(productToAuction.Id);
@ -117,47 +122,52 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
await SetAuctionBidPrice(revertLastBid.BidPrice, productToAuction, product, revertLastBid.CustomerId); await SetAuctionBidPrice(revertLastBid.BidPrice, productToAuction, product, revertLastBid.CustomerId);
} }
await SendAuctionBidMessageAsync(productToAuction, product, customer.Id); await SendAuctionBidMessageAsync(messageWrapper, productToAuction, product, customer.Id);
return ResponseType.ToAllClients;
} }
catch (Exception ex) catch (Exception ex)
{ {
logger.Error($"SignalRMessageHandler.HandleRevertAuctionBidRequest(); Exception: {ex.Message}", ex, customer); logger.Error($"SignalRMessageHandler.HandleRevertAuctionBidRequest(); Exception: {ex.Message}", ex, customer);
} }
return ResponseType.Error;
} }
private async Task HandleProductToAuctionStatusChangedRequest(Customer customer, AuctionProductStatusRequest auctionProductStatusRequest) private async Task<ResponseType> HandleProductToAuctionStatusChangedRequest(Customer customer, MessageWrapper messageWrapper)
{ {
var auctionProductStatusRequest = messageWrapper.Data.JsonTo<AuctionProductStatusRequest>();
if (auctionProductStatusRequest == null) if (auctionProductStatusRequest == null)
{ {
logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); auctionProductStatusRequest == null", null, customer); logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); auctionProductStatusRequest == null", null, customer);
return; return ResponseType.Error;;
} }
try try
{ {
var responseType = ResponseType.ToAllClients;
await logger.InformationAsync($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); ProductToAuctionMappingId: {auctionProductStatusRequest.ProductToAuctionId}; Status: {auctionProductStatusRequest.AuctionStatus}({(int)auctionProductStatusRequest.AuctionStatus})", null, customer); await logger.InformationAsync($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); ProductToAuctionMappingId: {auctionProductStatusRequest.ProductToAuctionId}; Status: {auctionProductStatusRequest.AuctionStatus}({(int)auctionProductStatusRequest.AuctionStatus})", null, customer);
var productToAuction = await auctionService.GetProductToAuctionMappingByIdAsync(auctionProductStatusRequest.ProductToAuctionId); var productToAuction = await auctionService.GetProductToAuctionMappingByIdAsync(auctionProductStatusRequest.ProductToAuctionId);
if (productToAuction == null) if (productToAuction == null)
{ {
logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); (productToAuction == null)", null, customer); logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); (productToAuction == null)", null, customer);
return; return ResponseType.Error;;
} }
var auction = await auctionService.GetAuctionByIdAsync(productToAuction.AuctionId); var auction = await auctionService.GetAuctionByIdAsync(productToAuction.AuctionId);
if (auction == null || auction.Closed) if (auction == null || auction.Closed)
{ {
logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); (auction == null || auction.Closed); Closed: {auction?.Closed}", null, customer); logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); (auction == null || auction.Closed); Closed: {auction?.Closed}", null, customer);
return; return ResponseType.Error;;
} }
if (!IsValidRequestAuctionStatus(auctionProductStatusRequest.AuctionStatus, productToAuction.AuctionStatus)) if (!IsValidRequestAuctionStatus(auctionProductStatusRequest.AuctionStatus, productToAuction.AuctionStatus))
{ {
responseType = ResponseType.ToCaller;
logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); RequestAuctionStatusIsValid() == false; newStatus: {auctionProductStatusRequest.AuctionStatus}; oldStatus: {productToAuction.AuctionStatus}", null, customer); logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); RequestAuctionStatusIsValid() == false; newStatus: {auctionProductStatusRequest.AuctionStatus}; oldStatus: {productToAuction.AuctionStatus}", null, customer);
return; //return ResponseType.Error;;
} }
else if (auctionProductStatusRequest.AuctionStatus == AuctionStatus.None) await ResetProductToAuction(productToAuction, auction.CategoryId);
if (auctionProductStatusRequest.AuctionStatus == AuctionStatus.None) await ResetProductToAuction(productToAuction, auction.CategoryId);
else else
{ {
switch (auctionProductStatusRequest.AuctionStatus) switch (auctionProductStatusRequest.AuctionStatus)
@ -198,7 +208,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
default: default:
logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); AuctionStatus not found; (auctionProductStatusRequest.AuctionStatus == {auctionProductStatusRequest.AuctionStatus})", null, customer); logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); AuctionStatus not found; (auctionProductStatusRequest.AuctionStatus == {auctionProductStatusRequest.AuctionStatus})", null, customer);
return; return ResponseType.Error;;
} }
await auctionService.UpdateProductToAuctionMappingAsync(productToAuction); await auctionService.UpdateProductToAuctionMappingAsync(productToAuction);
@ -209,46 +219,41 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
{ {
MessageType = nameof(ProductToAuctionStatusNotification), MessageType = nameof(ProductToAuctionStatusNotification),
SenderId = customer.Id, SenderId = customer.Id,
ResponseType = responseType,
Data = new ProductToAuctionStatusNotification(await auctionService.GetAuctionDtoByProductToAuctionIdAsync(productToAuction.Id, true), bidsCount, "EMPTY").ToJson() Data = new ProductToAuctionStatusNotification(await auctionService.GetAuctionDtoByProductToAuctionIdAsync(productToAuction.Id, true), bidsCount, "EMPTY").ToJson()
}; };
await hubContext.Clients.All.SendAsync("send", productToauctionChangedNotification.ToJson()); await SendMessageToClientsAsync(productToauctionChangedNotification, responseType == ResponseType.ToCaller);
//TODO: gond van ha az Admin valamit módosít és újrazár egy régi ProductToAuction-t!!!! AuctionRequestMode.Normal...- J. //TODO: gond van ha az Admin valamit módosít és újrazár egy régi ProductToAuction-t!!!! AuctionRequestMode.Normal...- J.
if (/*AuctionRequestMode.Normal &&*/ auction.AuctionType == AuctionType.AutomaticNext && productToAuction.AuctionStatus is AuctionStatus.Sold or AuctionStatus.NotSold) if (responseType== ResponseType.ToAllClients && /*AuctionRequestMode.Normal &&*/ auction.AuctionType == AuctionType.AutomaticNext && productToAuction.AuctionStatus is AuctionStatus.Sold or AuctionStatus.NotSold)
await StepNextProductToAuctionAsync(customer, productToAuction, auctionProductStatusRequest); await StepNextProductToAuctionAsync(messageWrapper, customer, productToAuction, auctionProductStatusRequest);
return ResponseType.ToAllClients;
} }
catch (Exception ex) catch (Exception ex)
{ {
logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); Exception: {ex.Message}", ex, customer); logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); Exception: {ex.Message}", ex, customer);
} }
return ResponseType.Error;
} }
private async Task StepNextProductToAuctionAsync(Customer customer, ProductToAuctionMapping productToAuction, AuctionProductStatusRequest auctionProductStatusRequest) private async Task SendMessageToClientsAsync(MessageWrapper messageWrapper, bool sendToCaller = false)
{ {
var nextProductToAuction = await auctionService.GetNextProductToAuctionByAuctionIdAsync(productToAuction.AuctionId); //if (sendToCaller) await hubContext.Clients.Caller.SendAsync("send", bidMessage.ToJson());
if (nextProductToAuction == null) return; //else
await hubContext.Clients.All.SendAsync("send", messageWrapper.ToJson());
await logger.InformationAsync($"SignalRMessageHandler.StepNextProductToAuctionAsync(); NextProductToAuctionId: {nextProductToAuction.Id};", null, customer);
if (nextProductToAuction.AuctionStatus != AuctionStatus.None)
{
await logger.WarningAsync($"SignalRMessageHandler.StepNextProductToAuctionAsync(); (nextProductToAuction.AuctionStatus != AuctionStatus.None); NextProductToAuctionId: {nextProductToAuction.Id}; Status: {nextProductToAuction.AuctionStatus}({(int)nextProductToAuction.AuctionStatus})", null, customer);
return; //TODO: Ilyenkor mi legyen?! - J.
}
auctionProductStatusRequest.AuctionStatus = AuctionStatus.Active;
auctionProductStatusRequest.ProductToAuctionId = nextProductToAuction.Id;
await HandleProductToAuctionStatusChangedRequest(customer, auctionProductStatusRequest);
} }
private async Task HandleBidRequest(Customer customer, AuctionBidRequest bidRequestMessage)
private async Task<ResponseType> HandleBidRequest(Customer customer, MessageWrapper messageWrapper)
{ {
var bidRequestMessage = messageWrapper.Data.JsonTo<AuctionBidRequest>();
if (bidRequestMessage == null) if (bidRequestMessage == null)
{ {
logger.Error($"SignalRMessageHandler.HandleBidRequest(); (bidRequestMessage == null)", null, customer); logger.Error($"SignalRMessageHandler.HandleBidRequest(); (bidRequestMessage == null)", null, customer);
return; return ResponseType.Error;;
} }
try try
@ -259,20 +264,25 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
if (auction == null || auction.Closed) if (auction == null || auction.Closed)
{ {
logger.Warning($"SignalRMessageHandler.HandleBidRequest(); (auction == null || auction.Closed); Closed: {auction?.Closed}", null, customer); logger.Warning($"SignalRMessageHandler.HandleBidRequest(); (auction == null || auction.Closed); Closed: {auction?.Closed}", null, customer);
return; return ResponseType.Error;;
} }
var product = await auctionService.GetProductById(bidRequestMessage.ProductId);
if (product == null) return ResponseType.Error;;
var activeProductAuction = (await auctionService.GetProductToAuctionByAuctionIdAndProductIdAsync(bidRequestMessage.AuctionId, bidRequestMessage.ProductId, true)).FirstOrDefault(); var activeProductAuction = (await auctionService.GetProductToAuctionByAuctionIdAndProductIdAsync(bidRequestMessage.AuctionId, bidRequestMessage.ProductId, true)).FirstOrDefault();
if (activeProductAuction is not { IsActiveItem: true } || (activeProductAuction.WinnerCustomerId == customer.Id && !await customerService.IsAdminAsync(customer))) if (activeProductAuction is not { IsActiveItem: true } || (activeProductAuction.WinnerCustomerId == customer.Id && !await customerService.IsAdminAsync(customer)))
{ {
logger.Warning($"SignalRMessageHandler.HandleBidRequest(); (activeProductAuction is not {{ IsActiveItem: true }} || activeProductAuction.WinnerCustomerId == customer.Id && !await customerService.IsAdminAsync(customer)); AuctionStatus: {activeProductAuction?.AuctionStatus}; WinnerCustomerId: {activeProductAuction?.WinnerCustomerId}", null, customer); logger.Warning($"SignalRMessageHandler.HandleBidRequest(); (activeProductAuction is not {{ IsActiveItem: true }} || activeProductAuction.WinnerCustomerId == customer.Id && !await customerService.IsAdminAsync(customer)); AuctionStatus: {activeProductAuction?.AuctionStatus}; WinnerCustomerId: {activeProductAuction?.WinnerCustomerId}", null, customer);
return; await SendAuctionBidMessageAsync(messageWrapper, activeProductAuction, product, customer.Id);//, false);
return ResponseType.ToCaller;
} }
var product = await auctionService.GetProductById(bidRequestMessage.ProductId); if (!IsValidBidPrice(activeProductAuction.CurrentPrice, bidRequestMessage.BidPrice, activeProductAuction.BidsCount > 0, customer))
if (product == null) return; {
await SendAuctionBidMessageAsync(messageWrapper, activeProductAuction, product, customer.Id);//, false);
if (!IsValidBidPrice(activeProductAuction.CurrentPrice, bidRequestMessage.BidPrice, activeProductAuction.BidsCount > 0, customer)) return; return ResponseType.ToCaller;
}
var auctionBid = bidRequestMessage.CreateMainEntity(); var auctionBid = bidRequestMessage.CreateMainEntity();
auctionBid.ProductAuctionMappingId = activeProductAuction.Id; auctionBid.ProductAuctionMappingId = activeProductAuction.Id;
@ -281,14 +291,17 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
activeProductAuction.BidsCount++; activeProductAuction.BidsCount++;
activeProductAuction.AuctionStatus = AuctionStatus.Active; activeProductAuction.AuctionStatus = AuctionStatus.Active;
if (!await SetAuctionBidPrice(auctionBid.BidPrice, activeProductAuction, product, customer.Id)) return; if (!await SetAuctionBidPrice(auctionBid.BidPrice, activeProductAuction, product, customer.Id)) return ResponseType.Error;
await SendAuctionBidMessageAsync(activeProductAuction, product, customer.Id); await SendAuctionBidMessageAsync(messageWrapper, activeProductAuction, product, customer.Id);
return ResponseType.ToAllClients;
} }
catch (Exception ex) catch (Exception ex)
{ {
logger.Error($"SignalRMessageHandler.HandleBidRequest(); Exception: {ex.Message}", ex, customer); logger.Error($"SignalRMessageHandler.HandleBidRequest(); Exception: {ex.Message}", ex, customer);
} }
return ResponseType.Error;
} }
private async Task<bool> SetAuctionBidPrice(decimal bidPrice, ProductToAuctionMapping productToAuction, Product product, int lastBidCustomerId) private async Task<bool> SetAuctionBidPrice(decimal bidPrice, ProductToAuctionMapping productToAuction, Product product, int lastBidCustomerId)
@ -313,27 +326,44 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
return false; return false;
} }
private async Task SendAuctionBidMessageAsync(ProductToAuctionMapping productToAuction, Product product, int customerId) private async Task SendAuctionBidMessageAsync(MessageWrapper messageWrapper, ProductToAuctionMapping productToAuction, Product product, int customerId, bool sendToCaller = false)
{ {
var bidsCount = productToAuction.BidsCount;//await auctionService.GetBidsCountByProductToAuctionIdAsync(productToAuction.Id); var bidsCount = productToAuction.BidsCount; //await auctionService.GetBidsCountByProductToAuctionIdAsync(productToAuction.Id);
var stepAmount = GetStepAmount(productToAuction.CurrentPrice); var stepAmount = GetStepAmount(productToAuction.CurrentPrice);
var nextBidPrice = GetNextBidPrice(productToAuction.CurrentPrice, stepAmount, bidsCount > 0); var nextBidPrice = GetNextBidPrice(productToAuction.CurrentPrice, stepAmount, bidsCount > 0);
var bidMessage = new MessageWrapper messageWrapper.SenderId = customerId;
messageWrapper.ResponseType = sendToCaller ? ResponseType.ToCaller : ResponseType.ToAllClients;
messageWrapper.MessageType = "bidNotification";
messageWrapper.Data = new BidNotificationMessage(await auctionService.GetAuctionDtoByProductToAuctionIdAsync(productToAuction.Id, true), bidsCount, "EMPTY") //TODO: NE KÉRJÜK LE DB-BŐL!!! - J.
{ {
MessageType = "bidNotification", ProductName = product.Name,
SenderId = customerId, CurrentPrice = productToAuction.CurrentPrice,
Data = new BidNotificationMessage(await auctionService.GetAuctionDtoByProductToAuctionIdAsync(productToAuction.Id, true), bidsCount, "EMPTY") //TODO: NE KÉRJÜK LE DB-BŐL!!! - J. NextStepAmount = stepAmount,
{ NextBidPrice = nextBidPrice,
ProductName = product.Name, }.ToJson();
CurrentPrice = productToAuction.CurrentPrice,
NextStepAmount = stepAmount,
NextBidPrice = nextBidPrice,
}.ToJson()
};
await hubContext.Clients.All.SendAsync("send", bidMessage.ToJson()); await SendMessageToClientsAsync(messageWrapper, sendToCaller);
}
private async Task StepNextProductToAuctionAsync(MessageWrapper messageWrapper, Customer customer, ProductToAuctionMapping productToAuction, AuctionProductStatusRequest auctionProductStatusRequest)
{
var nextProductToAuction = await auctionService.GetNextProductToAuctionByAuctionIdAsync(productToAuction.AuctionId);
if (nextProductToAuction == null) return;
await logger.InformationAsync($"SignalRMessageHandler.StepNextProductToAuctionAsync(); NextProductToAuctionId: {nextProductToAuction.Id};", null, customer);
if (nextProductToAuction.AuctionStatus != AuctionStatus.None)
{
await logger.WarningAsync($"SignalRMessageHandler.StepNextProductToAuctionAsync(); (nextProductToAuction.AuctionStatus != AuctionStatus.None); NextProductToAuctionId: {nextProductToAuction.Id}; Status: {nextProductToAuction.AuctionStatus}({(int)nextProductToAuction.AuctionStatus})", null, customer);
return; //TODO: Ilyenkor mi legyen?! - J.
}
auctionProductStatusRequest.AuctionStatus = AuctionStatus.Active;
auctionProductStatusRequest.ProductToAuctionId = nextProductToAuction.Id;
await HandleProductToAuctionStatusChangedRequest(customer, messageWrapper);
} }
private async Task ResetProductToAuction(ProductToAuctionMapping productToAuction, int? categoryId = null) private async Task ResetProductToAuction(ProductToAuctionMapping productToAuction, int? categoryId = null)