Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Minor refactoring ShoppingCartService

support669a49e114c1
diff --git a/src/Business/Grand.Business.Checkout/Commands/Handlers/Orders/AddCustomerReservationCommandHandler.cs b/src/Business/Grand.Business.Checkout/Commands/Handlers/Orders/AddCustomerReservationCommandHandler.cs
new file mode 100644
index 000000000..afaba8a79
--- /dev/null
+++ b/src/Business/Grand.Business.Checkout/Commands/Handlers/Orders/AddCustomerReservationCommandHandler.cs
@@ -0,0 +1,107 @@
+using Grand.Business.Core.Commands.Checkout.Orders;
+using Grand.Business.Core.Interfaces.Catalog.Products;
+using Grand.Domain.Catalog;
+using MediatR;
+
+namespace Grand.Business.Checkout.Commands.Handlers.Orders
+{
+ public class AddCustomerReservationCommandHandler : IRequestHandler<AddCustomerReservationCommand, bool>
+ {
+ private readonly IProductReservationService _productReservationService;
+
+ public AddCustomerReservationCommandHandler(IProductReservationService productReservationService)
+ {
+ _productReservationService = productReservationService;
+ }
+
+ public async Task<bool> Handle(AddCustomerReservationCommand request, CancellationToken cancellationToken)
+ {
+ if (request.RentalStartDate.HasValue && request.RentalEndDate.HasValue)
+ {
+ var reservations = await _productReservationService.GetProductReservationsByProductId(request.Product.Id, true, null);
+ var reserved = await _productReservationService.GetCustomerReservationsHelpers(request.Customer.Id);
+ foreach (var item in reserved)
+ {
+ var match = reservations.Where(x => x.Id == item.ReservationId).FirstOrDefault();
+ if (match != null)
+ {
+ reservations.Remove(match);
+ }
+ }
+
+ IGrouping<string, ProductReservation> groupToBook = null;
+
+ var grouped = reservations.GroupBy(x => x.Resource);
+ foreach (var group in grouped)
+ {
+ bool groupCanBeBooked = true;
+ if (request.Product.IncBothDate && request.Product.IntervalUnitId == IntervalUnit.Day)
+ {
+ for (DateTime iterator = request.RentalStartDate.Value; iterator <= request.RentalEndDate.Value; iterator += new TimeSpan(24, 0, 0))
+ {
+ if (!group.Select(x => x.Date).Contains(iterator))
+ {
+ groupCanBeBooked = false;
+ break;
+ }
+ }
+ }
+ else
+ {
+ for (DateTime iterator = request.RentalStartDate.Value; iterator < request.RentalEndDate.Value; iterator += new TimeSpan(24, 0, 0))
+ {
+ if (!group.Select(x => x.Date).Contains(iterator))
+ {
+ groupCanBeBooked = false;
+ break;
+ }
+ }
+ }
+ if (groupCanBeBooked)
+ {
+ groupToBook = group;
+ break;
+ }
+ }
+
+ if (groupToBook != null)
+ {
+ if (request.Product.IncBothDate && request.Product.IntervalUnitId == IntervalUnit.Day)
+ {
+ foreach (var item in groupToBook.Where(x => x.Date >= request.RentalStartDate && x.Date <= request.RentalEndDate))
+ {
+ await _productReservationService.InsertCustomerReservationsHelper(new CustomerReservationsHelper {
+ CustomerId = request.Customer.Id,
+ ReservationId = item.Id,
+ ShoppingCartItemId = request.ShoppingCartItem.Id
+ });
+ }
+ }
+ else
+ {
+ foreach (var item in groupToBook.Where(x => x.Date >= request.RentalStartDate && x.Date < request.RentalEndDate))
+ {
+ await _productReservationService.InsertCustomerReservationsHelper(new CustomerReservationsHelper {
+ CustomerId = request.Customer.Id,
+ ReservationId = item.Id,
+ ShoppingCartItemId = request.ShoppingCartItem.Id
+ });
+ }
+ }
+ }
+
+ }
+
+ if (!string.IsNullOrEmpty(request.ReservationId))
+ {
+ await _productReservationService.InsertCustomerReservationsHelper(new CustomerReservationsHelper {
+ CustomerId = request.Customer.Id,
+ ReservationId = request.ReservationId,
+ ShoppingCartItemId = request.ShoppingCartItem.Id
+ });
+ }
+ return true;
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/src/Business/Grand.Business.Checkout/Events/ShoppingCart/ShoppingCartItemDeletedEventHandler.cs b/src/Business/Grand.Business.Checkout/Events/ShoppingCart/ShoppingCartItemDeletedEventHandler.cs
new file mode 100644
index 000000000..4ca30bc34
--- /dev/null
+++ b/src/Business/Grand.Business.Checkout/Events/ShoppingCart/ShoppingCartItemDeletedEventHandler.cs
@@ -0,0 +1,33 @@
+using Grand.Business.Core.Interfaces.Catalog.Products;
+using Grand.Domain.Orders;
+using Grand.Infrastructure.Events;
+using MediatR;
+
+namespace Grand.Business.Checkout.Events.Shipping
+{
+ /// <summary>
+ /// Update order items
+ /// </summary>
+ public class ShoppingCartItemDeletedEventHandler : INotificationHandler<EntityDeleted<ShoppingCartItem>>
+ {
+
+ private readonly IProductReservationService _productReservationService;
+
+ public ShoppingCartItemDeletedEventHandler(IProductReservationService productReservationService)
+ {
+ _productReservationService = productReservationService;
+ }
+
+ public async Task Handle(EntityDeleted<ShoppingCartItem> notification, CancellationToken cancellationToken)
+ {
+ if ((notification.Entity.RentalStartDateUtc.HasValue && notification.Entity.RentalEndDateUtc.HasValue) || !string.IsNullOrEmpty(notification.Entity.ReservationId))
+ {
+ var reserved = await _productReservationService.GetCustomerReservationsHelperBySciId(notification.Entity.Id);
+ foreach (var res in reserved)
+ {
+ await _productReservationService.DeleteCustomerReservationsHelper(res);
+ }
+ }
+ }
+ }
+}
diff --git a/src/Business/Grand.Business.Checkout/Services/Orders/ShoppingCartService.cs b/src/Business/Grand.Business.Checkout/Services/Orders/ShoppingCartService.cs
index 61665351f..b77bb1f5c 100644
--- a/src/Business/Grand.Business.Checkout/Services/Orders/ShoppingCartService.cs
+++ b/src/Business/Grand.Business.Checkout/Services/Orders/ShoppingCartService.cs
@@ -1,12 +1,11 @@
-using Grand.Business.Core.Interfaces.Catalog.Products;
using Grand.Business.Core.Commands.Checkout.Orders;
using Grand.Business.Core.Events.Checkout.ShoppingCart;
-using Grand.Business.Core.Interfaces.Checkout.CheckoutAttributes;
-using Grand.Business.Core.Interfaces.Checkout.Orders;
using Grand.Business.Core.Extensions;
+using Grand.Business.Core.Interfaces.Catalog.Products;
+using Grand.Business.Core.Interfaces.Checkout.Orders;
using Grand.Business.Core.Interfaces.Common.Directory;
-using Grand.Business.Core.Interfaces.Common.Security;
using Grand.Business.Core.Interfaces.Customers;
+using Grand.Business.Core.Utilities.Checkout;
using Grand.Domain.Catalog;
using Grand.Domain.Common;
using Grand.Domain.Customers;
@@ -14,7 +13,6 @@ using Grand.Domain.Orders;
using Grand.Infrastructure;
using Grand.Infrastructure.Extensions;
using MediatR;
-using Grand.Business.Core.Utilities.Checkout;
namespace Grand.Business.Checkout.Services.Orders
{
@@ -27,14 +25,12 @@ namespace Grand.Business.Checkout.Services.Orders
private readonly IWorkContext _workContext;
private readonly IProductService _productService;
- private readonly ICheckoutAttributeParser _checkoutAttributeParser;
private readonly ICustomerService _customerService;
- private readonly IAclService _aclService;
private readonly IMediator _mediator;
private readonly IUserFieldService _userFieldService;
- private readonly IProductReservationService _productReservationService;
private readonly IShoppingCartValidator _shoppingCartValidator;
private readonly ShoppingCartSettings _shoppingCartSettings;
+
#endregion
#region Ctor
@@ -42,23 +38,17 @@ namespace Grand.Business.Checkout.Services.Orders
public ShoppingCartService(
IWorkContext workContext,
IProductService productService,
- ICheckoutAttributeParser checkoutAttributeParser,
ICustomerService customerService,
- IAclService aclService,
IMediator mediator,
IUserFieldService userFieldService,
- IProductReservationService productReservationService,
IShoppingCartValidator shoppingCartValidator,
ShoppingCartSettings shoppingCartSettings)
{
_workContext = workContext;
_productService = productService;
- _checkoutAttributeParser = checkoutAttributeParser;
_customerService = customerService;
- _aclService = aclService;
_mediator = mediator;
_userFieldService = userFieldService;
- _productReservationService = productReservationService;
_shoppingCartValidator = shoppingCartValidator;
_shoppingCartSettings = shoppingCartSettings;
}
@@ -87,13 +77,11 @@ namespace Grand.Business.Checkout.Services.Orders
foreach (var item in cart)
{
var product = await _productService.GetProductById(item.ProductId);
- if (product == null || !product.Published || !_aclService.Authorize(product, _workContext.CurrentCustomer))
+ if (product == null)
continue;
model.Add(item);
}
-
-
return model;
}
@@ -282,7 +270,16 @@ namespace Grand.Business.Checkout.Services.Orders
//insert new item
shoppingCartItem = await InsertNewItem(customer, product, shoppingCartItem, automaticallyAddRequiredProductsIfEnabled);
}
- await HandleCustomerReservation(customer, product, rentalStartDate, rentalEndDate, shoppingCartItem, reservationId);
+
+ if (product.ProductTypeId == ProductType.Reservation)
+ await _mediator.Send(new AddCustomerReservationCommand() {
+ Customer = customer,
+ Product = product,
+ ShoppingCartItem = shoppingCartItem,
+ RentalStartDate = rentalStartDate,
+ RentalEndDate = rentalEndDate,
+ ReservationId = reservationId,
+ });
//reset checkout info
await _customerService.ResetCheckoutData(customer, storeId);
@@ -324,100 +321,6 @@ namespace Grand.Business.Checkout.Services.Orders
return shoppingCartItem;
}
- private async Task HandleCustomerReservation(
- Customer customer,
- Product product,
- DateTime? rentalStartDate, DateTime? rentalEndDate,
- ShoppingCartItem shoppingCartItem,
- string reservationId)
- {
-
- if (rentalStartDate.HasValue && rentalEndDate.HasValue)
- {
- var reservations = await _productReservationService.GetProductReservationsByProductId(product.Id, true, null);
- var reserved = await _productReservationService.GetCustomerReservationsHelpers(_workContext.CurrentCustomer.Id);
- foreach (var item in reserved)
- {
- var match = reservations.Where(x => x.Id == item.ReservationId).FirstOrDefault();
- if (match != null)
- {
- reservations.Remove(match);
- }
- }
-
- IGrouping<string, ProductReservation> groupToBook = null;
-
- var grouped = reservations.GroupBy(x => x.Resource);
- foreach (var group in grouped)
- {
- bool groupCanBeBooked = true;
- if (product.IncBothDate && product.IntervalUnitId == IntervalUnit.Day)
- {
- for (DateTime iterator = rentalStartDate.Value; iterator <= rentalEndDate.Value; iterator += new TimeSpan(24, 0, 0))
- {
- if (!group.Select(x => x.Date).Contains(iterator))
- {
- groupCanBeBooked = false;
- break;
- }
- }
- }
- else
- {
- for (DateTime iterator = rentalStartDate.Value; iterator < rentalEndDate.Value; iterator += new TimeSpan(24, 0, 0))
- {
- if (!group.Select(x => x.Date).Contains(iterator))
- {
- groupCanBeBooked = false;
- break;
- }
- }
- }
- if (groupCanBeBooked)
- {
- groupToBook = group;
- break;
- }
- }
-
- if (groupToBook != null)
- {
- if (product.IncBothDate && product.IntervalUnitId == IntervalUnit.Day)
- {
- foreach (var item in groupToBook.Where(x => x.Date >= rentalStartDate && x.Date <= rentalEndDate))
- {
- await _productReservationService.InsertCustomerReservationsHelper(new CustomerReservationsHelper {
- CustomerId = customer.Id,
- ReservationId = item.Id,
- ShoppingCartItemId = shoppingCartItem.Id
- });
- }
- }
- else
- {
- foreach (var item in groupToBook.Where(x => x.Date >= rentalStartDate && x.Date < rentalEndDate))
- {
- await _productReservationService.InsertCustomerReservationsHelper(new CustomerReservationsHelper {
- CustomerId = customer.Id,
- ReservationId = item.Id,
- ShoppingCartItemId = shoppingCartItem.Id
- });
- }
- }
- }
-
- }
-
- if (!string.IsNullOrEmpty(reservationId))
- {
- await _productReservationService.InsertCustomerReservationsHelper(new CustomerReservationsHelper {
- CustomerId = customer.Id,
- ReservationId = reservationId,
- ShoppingCartItemId = shoppingCartItem.Id
- });
- }
- }
-
/// <summary>
/// Updates the shopping cart item
/// </summary>
@@ -497,19 +400,6 @@ namespace Grand.Business.Checkout.Services.Orders
if (shoppingCartItem == null)
throw new ArgumentNullException(nameof(shoppingCartItem));
- if ((shoppingCartItem.RentalStartDateUtc.HasValue && shoppingCartItem.RentalEndDateUtc.HasValue) || !string.IsNullOrEmpty(shoppingCartItem.ReservationId))
- {
- var reserved = await _productReservationService.GetCustomerReservationsHelperBySciId(shoppingCartItem.Id);
- foreach (var res in reserved)
- {
- if (res.CustomerId == _workContext.CurrentCustomer.Id)
- {
- await _productReservationService.DeleteCustomerReservationsHelper(res);
- }
- }
- }
- var storeId = shoppingCartItem.StoreId;
-
//reset checkout data
if (resetCheckoutData)
{
@@ -520,21 +410,6 @@ namespace Grand.Business.Checkout.Services.Orders
customer.ShoppingCartItems.Remove(customer.ShoppingCartItems.Where(x => x.Id == shoppingCartItem.Id).FirstOrDefault());
await _customerService.DeleteShoppingCartItem(customer.Id, shoppingCartItem);
- //validate checkout attributes
- if (ensureOnlyActiveCheckoutAttributes &&
- //only for shopping cart items (ignore wishlist)
- shoppingCartItem.ShoppingCartTypeId == ShoppingCartType.ShoppingCart)
- {
- var cart = customer.ShoppingCartItems
- .Where(x => x.ShoppingCartTypeId == ShoppingCartType.ShoppingCart)
- .LimitPerStore(_shoppingCartSettings.SharedCartBetweenStores, storeId)
- .ToList();
-
- var checkoutAttributes = await customer.GetUserField<List<CustomAttribute>>(_userFieldService, SystemCustomerFieldNames.CheckoutAttributes, storeId);
- var newcheckoutAttributes = await _checkoutAttributeParser.EnsureOnlyActiveAttributes(checkoutAttributes, cart);
- await _userFieldService.SaveField(customer, SystemCustomerFieldNames.CheckoutAttributes, newcheckoutAttributes, storeId);
- }
-
//event notification
await _mediator.EntityDeleted(shoppingCartItem);
}
diff --git a/src/Business/Grand.Business.Core/Commands/Checkout/Orders/AddCustomerReservationCommand.cs b/src/Business/Grand.Business.Core/Commands/Checkout/Orders/AddCustomerReservationCommand.cs
new file mode 100644
index 000000000..c780281f5
--- /dev/null
+++ b/src/Business/Grand.Business.Core/Commands/Checkout/Orders/AddCustomerReservationCommand.cs
@@ -0,0 +1,17 @@
+using Grand.Domain.Catalog;
+using Grand.Domain.Customers;
+using Grand.Domain.Orders;
+using MediatR;
+
+namespace Grand.Business.Core.Commands.Checkout.Orders
+{
+ public class AddCustomerReservationCommand : IRequest<bool>
+ {
+ public Customer Customer { get; set; }
+ public Product Product { get; set; }
+ public DateTime? RentalStartDate { get; set; }
+ public DateTime? RentalEndDate { get; set; }
+ public ShoppingCartItem ShoppingCartItem { get; set; }
+ public string ReservationId { get; set; }
+ }
+}