Add InsertOrUpdateBillingAddressAsync

This commit is contained in:
Loretta 2024-11-28 17:13:29 +01:00
parent a2a6f2def3
commit 82876de785
2 changed files with 148 additions and 93 deletions

View File

@ -180,7 +180,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
if (placeOrderResult is not { Success: true })
{
logger.Error($"SignalRMessageHandler.HandleProductToAuctionStatusChangedRequest(); (placeOrderResult is not {{ Success: true }})", null, customer);
return;
//return; //TODO: EGYELŐRE HAGYJUK LEZÁRNI AKKOR IS, HA NEM SIKERÜLT AZ ORDERPLACE()! - J.
}
break;

View File

@ -1,5 +1,6 @@
using Nop.Core;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Orders;
using Nop.Plugin.Misc.AuctionPlugin.Domains.DataLayer;
using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos;
@ -9,58 +10,29 @@ using Nop.Services.Catalog;
using Nop.Services.Logging;
using Nop.Services.Orders;
using Nop.Services.Payments;
using System.Linq;
using Nop.Core.Domain.Common;
using Nop.Services.Common;
using Nop.Services.Customers;
using Nop.Services.Shipping;
namespace Nop.Plugin.Misc.AuctionPlugin.Services;
/// <summary>
/// Store pickup point service
/// </summary>
public class AuctionService : IAuctionService
public class AuctionService(
AuctionDbContext ctx,
IProductService productService,
IWorkContext workContext,
IOrderProcessingService orderProcessingService,
IShoppingCartService shoppingCartService,
IStoreContext storeContext,
IPaymentService paymentService,
IAddressService addressService,
ICustomerService customerService,
ILogger logger) : IAuctionService
{
#region Fields
private readonly AuctionDbContext _ctx;
private readonly IProductService _productService;
private readonly IWorkContext _workContext;
private readonly IPaymentService _paymentService;
private readonly IOrderProcessingService _orderProcessingService;
private readonly IShoppingCartService _shoppingCartService;
private readonly IStoreContext _storeContext;
private readonly ILogger _logger;
#endregion
#region Ctor
/// <summary>
/// Ctor
/// </summary>
/// <param name="ctx"></param>
/// <param name="productService"></param>
/// <param name="workContext"></param>
/// <param name="paymentService"></param>
/// <param name="logger"></param>
public AuctionService(
AuctionDbContext ctx,
IProductService productService,
IWorkContext workContext,
IOrderProcessingService orderProcessingService,
IShoppingCartService shoppingCartService,
IStoreContext storeContext,
IPaymentService paymentService,
ILogger logger)
{
_ctx = ctx;
_productService = productService;
_workContext = workContext;
_orderProcessingService = orderProcessingService;
_shoppingCartService = shoppingCartService;
_storeContext = storeContext;
_paymentService = paymentService;
_logger = logger;
}
#endregion
#region Methods
public static decimal GetStepAmount(decimal currentPrice)
@ -112,29 +84,29 @@ public class AuctionService : IAuctionService
}
public async Task ResetProductToAuctionByProductId(int productId)
=> await ResetProductToAuctionAsync(await _ctx.ProductToAuctions.GetByProductId(productId).FirstOrDefaultAsync());
=> await ResetProductToAuctionAsync(await ctx.ProductToAuctions.GetByProductId(productId).FirstOrDefaultAsync());
public Task ResetProductToAuctionByIdAsync(int productToAuctionId, decimal basePrice = 0)
=> ResetProductToAuctionAsync(_ctx.ProductToAuctions.GetById(productToAuctionId), basePrice);
=> ResetProductToAuctionAsync(ctx.ProductToAuctions.GetById(productToAuctionId), basePrice);
public Task ResetProductToAuctionAsync(ProductToAuctionMapping productToAuction, decimal basePrice = 0)
=> _ctx.ResetProductToAuctionAsync(productToAuction, basePrice);
=> ctx.ResetProductToAuctionAsync(productToAuction, basePrice);
public Task<AuctionBid> GetLastAuctionBidByProductToAuctionIdAsync(int productToAuctionId)
=> _ctx.GetLastAuctionBidByProductToAuctionId(productToAuctionId);
=> ctx.GetLastAuctionBidByProductToAuctionId(productToAuctionId);
public Task<int> GetBidsCountByProductToAuctionIdAsync(int productToAuctionId)
=> _ctx.GetBidsCountByProductToAuctionIdAsync(productToAuctionId);
=> ctx.GetBidsCountByProductToAuctionIdAsync(productToAuctionId);
public Task<AuctionBid> RevertAuctionBidByProductToAuctionIdAsync(int productToAuctionId)
=> _ctx.RevertAuctionBidByProductToAuctionId(productToAuctionId);
=> ctx.RevertAuctionBidByProductToAuctionId(productToAuctionId);
public async Task<Product> GetProductById(int productId)
{
var product = await _productService.GetProductByIdAsync(productId);
var product = await productService.GetProductByIdAsync(productId);
if (product != null) return product;
_logger.Error($"AuctionService.GetProductById(); (product == null)", null, await _workContext.GetCurrentCustomerAsync());
logger.Error($"AuctionService.GetProductById(); (product == null)", null, await workContext.GetCurrentCustomerAsync());
return null;
}
@ -168,14 +140,14 @@ public class AuctionService : IAuctionService
public async Task<AuctionBid> GetBidByIdAsync(int bidId)
{
return await _ctx.AuctionBids.GetByIdAsync(bidId);
return await ctx.AuctionBids.GetByIdAsync(bidId);
}
public async Task InsertBidAsync(AuctionBid auctionBid)
{
//if (await ValidateAuctionBid(auctionBid))
{
await _ctx.AuctionBids.InsertAsync(auctionBid, false);
await ctx.AuctionBids.InsertAsync(auctionBid, false);
}
}
@ -183,7 +155,7 @@ public class AuctionService : IAuctionService
{
//if (await ValidateAuctionBid(auctionBid))
{
await _ctx.AuctionBids.UpdateAsync(auctionBid, false);
await ctx.AuctionBids.UpdateAsync(auctionBid, false);
}
}
@ -194,51 +166,134 @@ public class AuctionService : IAuctionService
/// <returns>A task that represents the asynchronous operation</returns>
public async Task DeleteBidAsync(AuctionBid pickupPoint)
{
await _ctx.AuctionBids.DeleteAsync(pickupPoint, false);
await ctx.AuctionBids.DeleteAsync(pickupPoint, false);
}
public async Task<PlaceOrderResult> CreateOrderForWinnerAsync(ProductToAuctionMapping auctionItem)
private async Task<Address> InsertOrUpdateBillingAddressAsync(Customer customer)
{
if (auctionItem is not { AuctionStatus: AuctionStatus.Sold })
try
{
await _logger.InformationAsync($"AuctionService.CreateOrderForWinnerAsync(); (auctionItem is not {{ AuctionStatus: AuctionStatus.Sold }}); auctionItemId: {auctionItem?.Id}; status: {auctionItem?.AuctionStatus}");
var billingAddress = await customerService.GetCustomerBillingAddressAsync(customer);
if (billingAddress == null)
{
billingAddress = CreateAddressFromCustomer(customer);
await addressService.InsertAddressAsync(billingAddress);
customer.ShippingAddressId = billingAddress.Id;
customer.BillingAddressId = billingAddress.Id;
await customerService.UpdateCustomerAsync(customer);
}
else
{
billingAddress = CopyAddressFromCustomer(billingAddress, customer);
await addressService.UpdateAddressAsync(billingAddress);
}
return billingAddress;
}
catch (Exception ex)
{
logger.Error($"AuctionService.InsertOrUpdateBillingAddress()", ex, customer);
}
return null;
}
private static Address CreateAddressFromCustomer(Customer customer)
{
var address = new Address();
return CopyAddressFromCustomer(address, customer);
}
private static Address CopyAddressFromCustomer(Address intoAddress, Customer customer)
{
intoAddress.FirstName = customer.FirstName;
intoAddress.LastName = customer.LastName;
intoAddress.Email = customer.Email;
intoAddress.FaxNumber = customer.Fax;
intoAddress.PhoneNumber = customer.Phone;
intoAddress.CountryId = customer.CountryId;
intoAddress.City = customer.City;
intoAddress.County = customer.County;
intoAddress.Address1 = customer.StreetAddress;
intoAddress.Address2 = customer.StreetAddress2;
intoAddress.StateProvinceId = customer.StateProvinceId;
intoAddress.ZipPostalCode = customer.ZipPostalCode;
intoAddress.Company = customer.Company;
return intoAddress;
}
public async Task<PlaceOrderResult> CreateOrderForWinnerAsync(ProductToAuctionMapping productToAuction)
{
if (productToAuction is not { AuctionStatus: AuctionStatus.Sold })
{
await logger.InformationAsync($"AuctionService.CreateOrderForWinnerAsync(); (productToAuction is not {{ AuctionStatus: AuctionStatus.Sold }}); productToAuctionId: {productToAuction?.Id}; status: {productToAuction?.AuctionStatus}");
return null;
}
Customer currentCustomer = null;
try
{
currentCustomer = await workContext.GetCurrentCustomerAsync();
var billingAddress = await InsertOrUpdateBillingAddressAsync(currentCustomer);
if (billingAddress == null)
{
logger.Error($"AuctionService.CreateOrderForWinnerAsync(); (billingAddress == null); productToAuctionId: {productToAuction.Id}", null, currentCustomer);
return null;
}
var processPaymentRequest = new ProcessPaymentRequest
{
PaymentMethodSystemName = "Payments.CheckMoneyOrder",
CustomerId = auctionItem.WinnerCustomerId,
OrderTotal = auctionItem.CurrentPrice,
CustomerId = productToAuction.WinnerCustomerId,
OrderTotal = productToAuction.CurrentPrice,
};
var currentCustomer = await _workContext.GetCurrentCustomerAsync();
var product = await _productService.GetProductByIdAsync(auctionItem.ProductId);
var currentStore = await _storeContext.GetCurrentStoreAsync();
processPaymentRequest.CustomValues.Add("ProductToAuctionMappingId", productToAuction.Id);
//TODO: valszeg a GenerateOrderGuidAsync-ot kell használni, de nemtom egy példában láttam... - J.
await _paymentService.GenerateOrderGuidAsync(processPaymentRequest);
var product = await productService.GetProductByIdAsync(productToAuction.ProductId);
product.DisableBuyButton = false; //TODO: ezt automatikusan kéne false-ra állítani, mikor Assign-oljuk a ProductToAuctionItem-hez! vagy valami hasonló... - J.
processPaymentRequest.CustomValues.Add("ProductToAuctionMappingId", auctionItem.Id);
var currentStore = await storeContext.GetCurrentStoreAsync();
processPaymentRequest.StoreId = currentStore.Id;
product.DisableBuyButton = false;
await logger.InformationAsync($"AuctionService.CreateOrderForWinnerAsync(); productToAuctionId: {productToAuction.Id}; currentStoreId: {currentStore.Id}", null, currentCustomer);
var cartResult = await _shoppingCartService.AddToCartAsync(currentCustomer, product, ShoppingCartType.ShoppingCart, currentStore.Id);
await paymentService.GenerateOrderGuidAsync(processPaymentRequest);
var placeOrderResult = await _orderProcessingService.PlaceOrderAsync(processPaymentRequest);
//TODO: kitalálni melyik legyen... - J.
await shoppingCartService.ClearShoppingCartAsync(currentCustomer, currentStore.Id);
//var currentProductsInCart = await shoppingCartService.GetShoppingCartAsync(currentCustomer, ShoppingCartType.ShoppingCart, currentStore.Id, product.Id);
//if (currentProductsInCart != null)
//{
// foreach (var shoppingCartItem in currentProductsInCart)
// await shoppingCartService.DeleteShoppingCartItemAsync(shoppingCartItem);
//}
var cartWarnings = await shoppingCartService.AddToCartAsync(currentCustomer, product, ShoppingCartType.ShoppingCart, currentStore.Id);
if (cartWarnings.Count > 0)
{
await logger.InformationAsync($"AuctionService.CreateOrderForWinnerAsync(); cartWarnings: {string.Join("; ", cartWarnings)}", null, currentCustomer);
return null;
}
var placeOrderResult = await orderProcessingService.PlaceOrderAsync(processPaymentRequest);
if (!placeOrderResult.Success) return null;
auctionItem.OrderId = placeOrderResult.PlacedOrder.Id;
auctionItem.OrderGuid = placeOrderResult.PlacedOrder.OrderGuid;
productToAuction.OrderId = placeOrderResult.PlacedOrder.Id;
productToAuction.OrderGuid = placeOrderResult.PlacedOrder.OrderGuid;
return placeOrderResult;
}
catch (Exception ex)
{
_logger.Error($"AuctionService.CreateOrderForWinnerAsync(); Exception; auctionItemId: {auctionItem.Id}", ex);
logger.Error($"AuctionService.CreateOrderForWinnerAsync(); Exception; productToAuctionId: {productToAuction.Id}", ex, currentCustomer);
}
return null;
@ -247,22 +302,22 @@ public class AuctionService : IAuctionService
#region auctions
public async Task InsertAuctionAsync(Auction auction)
{
await _ctx.Auctions.InsertAsync(auction, false);
await ctx.Auctions.InsertAsync(auction, false);
}
public async Task UpdateAuctionAsync(Auction auction)
{
await _ctx.Auctions.UpdateAsync(auction);
await ctx.Auctions.UpdateAsync(auction);
}
public async Task<IList<Auction>> GetAllAuctionsAsync()
{
return await _ctx.Auctions.GetAllAuctionsAsync();
return await ctx.Auctions.GetAllAuctionsAsync();
}
public async Task<List<ProductToAuctionMapping>> GetProductToAuctionsByAuctionIdAsync(int auctionId, bool onlyActiveItems)
{
return await _ctx.ProductToAuctions.GetProductToAuctionsByAuctionId(auctionId, onlyActiveItems).ToListAsync();
return await ctx.ProductToAuctions.GetProductToAuctionsByAuctionId(auctionId, onlyActiveItems).ToListAsync();
}
#endregion
#endregion
@ -276,7 +331,7 @@ public class AuctionService : IAuctionService
foreach (var auctionDtoProductToAuctionDto in auctionDto.ProductToAuctionDtos)
{
auctionDtoProductToAuctionDto.AuctionBidDtos.AddRange(await _ctx.AuctionBids.GetByProductToAuctionId(auctionDtoProductToAuctionDto.Id).OrderByDescending(x => x.Id).Take(maxBidsCount).Select(x => new AuctionBidDto(x)).ToListAsync());
auctionDtoProductToAuctionDto.AuctionBidDtos.AddRange(await ctx.AuctionBids.GetByProductToAuctionId(auctionDtoProductToAuctionDto.Id).OrderByDescending(x => x.Id).Take(maxBidsCount).Select(x => new AuctionBidDto(x)).ToListAsync());
}
return auctionDto;
@ -284,7 +339,7 @@ public class AuctionService : IAuctionService
public async Task<AuctionDto> GetAuctionDtoByIdAsync(int auctionId, bool widthProducts, bool activeProductOnly)
{
var auction = await _ctx.Auctions.GetByIdAsync(auctionId);
var auction = await ctx.Auctions.GetByIdAsync(auctionId);
if (auction == null) return null;
var auctionDto = new AuctionDto(auction);
@ -300,7 +355,7 @@ public class AuctionService : IAuctionService
public async Task<AuctionDto> GetAuctionDtoWithProductByIdAsync(int auctionId, int productId, bool activeProductOnly)
{
var auction = await _ctx.Auctions.GetByIdAsync(auctionId);
var auction = await ctx.Auctions.GetByIdAsync(auctionId);
if (auction == null) return null;
var auctionDto = new AuctionDto(auction);
@ -311,7 +366,7 @@ public class AuctionService : IAuctionService
public async Task<ProductToAuctionDto> GetProductToAuctionDtoByIdAsync(int productToAuctionId)
{
var productToAuction = await _ctx.ProductToAuctions.GetByIdAsync(productToAuctionId);
var productToAuction = await ctx.ProductToAuctions.GetByIdAsync(productToAuctionId);
return productToAuction == null ? null : new ProductToAuctionDto(productToAuction);
}
@ -328,21 +383,21 @@ public class AuctionService : IAuctionService
public async Task<List<ProductToAuctionDto>> GetProductToAuctionDtosByProductIdAsync(int productId)
{
return [..await _ctx.ProductToAuctions.GetByProductId(productId).Select(x=>new ProductToAuctionDto(x)).ToListAsync()];
return [..await ctx.ProductToAuctions.GetByProductId(productId).Select(x=>new ProductToAuctionDto(x)).ToListAsync()];
}
public async Task<AuctionBidDto> GetAuctionBidDtoByIdAsync(int auctionBidId)
{
var auctionBid = await _ctx.AuctionBids.GetByIdAsync(auctionBidId);
var auctionBid = await ctx.AuctionBids.GetByIdAsync(auctionBidId);
return auctionBid == null ? null : new AuctionBidDto(auctionBid);
}
public Task<List<ProductToAuctionMapping>> GetProductToAuctionsByProductIdAsync(int productId)
=> _ctx.GetProductToAuctionsByProductIdAsync(productId);
=> ctx.GetProductToAuctionsByProductIdAsync(productId);
public Task<List<ProductToAuctionMapping>> GetProductToAuctionByAuctionIdAndProductIdAsync(int auctionId, int productId, bool activeProductOnly)
=> _ctx.GetProductToAuctionByAuctionIdAndProductIdAsync(auctionId, productId, activeProductOnly);
=> ctx.GetProductToAuctionByAuctionIdAndProductIdAsync(auctionId, productId, activeProductOnly);
public async Task<ProductToAuctionMapping> AssignProductToAuctionAsync(int productId, decimal startingPrice, decimal bidPrice, int auctionId)
@ -366,11 +421,11 @@ public class AuctionService : IAuctionService
try
{
await _ctx.ProductToAuctions.InsertAsync(mapping);
await ctx.ProductToAuctions.InsertAsync(mapping);
}
catch (Exception ex)
{
await _logger.InformationAsync(ex.ToString());
await logger.InformationAsync(ex.ToString());
}
return mapping;
@ -378,12 +433,12 @@ public class AuctionService : IAuctionService
public async Task<ProductToAuctionMapping> GetProductToAuctionMappingByIdAsync(int productToAuctionMappingId)
{
return await _ctx.ProductToAuctions.GetByIdAsync(productToAuctionMappingId);
return await ctx.ProductToAuctions.GetByIdAsync(productToAuctionMappingId);
}
public async Task UpdateProductToAuctionMappingAsync(ProductToAuctionMapping productToAuctionMapping)
{
await _ctx.ProductToAuctions.UpdateAsync(productToAuctionMapping);
await ctx.ProductToAuctions.UpdateAsync(productToAuctionMapping);
}
#endregion Dtos