Skip to content

Commit

Permalink
VCST-1811: Implement Pack Size Limit (#11)
Browse files Browse the repository at this point in the history
feat: Added Pack Size Policy for cart operations.
  • Loading branch information
OlegoO authored Sep 20, 2024
1 parent 9ad11c6 commit 4026795
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using VirtoCommerce.XCart.Core.Models;

namespace VirtoCommerce.XCart.Core.Specifications;

public class PackSizeLimitSpecification
{
public virtual bool IsSatisfiedBy(CartProduct product, long requestedQuantity)
{
var packSize = product.Product.PackSize;
return packSize == 1 || (requestedQuantity % packSize == 0);
}
}
14 changes: 14 additions & 0 deletions src/VirtoCommerce.XCart.Core/Validators/CartErrorDescriber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,20 @@ public static ValidationFailure ProductExactQuantityError(IEntity entity, int qt
return result;
}

public static CartValidationError ProductPackSizeError(IEntity entity, int qty, int packSize)
{
var result = new CartValidationError(entity, $" Order in packs of {packSize}", "PRODUCT_PACK_SIZE_LIMIT")
{
FormattedMessagePlaceholderValues = new Dictionary<string, object>
{
["qty"] = qty,
["packSize"] = packSize
}
};

return result;
}

public static CartValidationError ProductDuplicateError(string type, string sku, List<string> ids)
{
var result = new CartValidationError(type, sku, $"Duplicate product {sku}", "PRODUCT_DUPLICATE_SKU")
Expand Down
13 changes: 12 additions & 1 deletion src/VirtoCommerce.XCart.Core/Validators/CartLineItemValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@ private void ValidateMinMaxQuantity(ValidationContext<LineItemValidationContext>
{
var minQuantity = cartProduct?.GetMinQuantity();
var maxQuantity = cartProduct?.GetMaxQuantity();
var packSize = cartProduct?.Product.PackSize ?? 1;

if (minQuantity.HasValue && maxQuantity.HasValue)
if (IsPackSizeLimit(cartProduct, lineItem.Quantity))
{
// PRODUCT_PACK_SIZE_LIMIT
context.AddFailure(CartErrorDescriber.ProductPackSizeError(lineItem, lineItem.Quantity, packSize));
}
else if (minQuantity.HasValue && maxQuantity.HasValue)
{
if (IsOutsideMinMaxQuantity(lineItem.Quantity, minQuantity.Value, maxQuantity.Value))
{
Expand Down Expand Up @@ -82,6 +88,11 @@ private void ValidateMinMaxQuantity(ValidationContext<LineItemValidationContext>
}
}

protected virtual bool IsPackSizeLimit(CartProduct cartProduct, int quantity)
{
return !AbstractTypeFactory<PackSizeLimitSpecification>.TryCreateInstance().IsSatisfiedBy(cartProduct, quantity);
}

protected virtual bool IsProductNotBuyable(CartProduct cartProduct)
{
return cartProduct is null || !AbstractTypeFactory<ProductIsBuyableSpecification>.TryCreateInstance().IsSatisfiedBy(cartProduct);
Expand Down
14 changes: 13 additions & 1 deletion src/VirtoCommerce.XCart.Core/Validators/NewCartItemValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ protected virtual void ValidateCartItem(ValidationContext<NewCartItem> context,
if (ValidateLineItemLimit(context, cartProduct, newCartItem)
&& ValidateProductIsBuyable(context, cartProduct)
&& ValidateProductInStock(context, cartProduct, newCartItem)
&& ValidateMinQuantity(context, cartProduct))
&& ValidateMinQuantity(context, cartProduct)
&& ValidatePackSize(context, cartProduct, newCartItem))
{
ValidateMinMaxQuantity(context, cartProduct, newCartItem);
}
Expand Down Expand Up @@ -80,6 +81,17 @@ private bool ValidateMinQuantity(ValidationContext<NewCartItem> context, CartPro
return true;
}

protected virtual bool ValidatePackSize(ValidationContext<NewCartItem> context, CartProduct cartProduct, NewCartItem newCartItem)
{
if (!AbstractTypeFactory<PackSizeLimitSpecification>.TryCreateInstance().IsSatisfiedBy(cartProduct, newCartItem.Quantity))
{
context.AddFailure(CartErrorDescriber.ProductPackSizeError(cartProduct.Product, newCartItem.Quantity, cartProduct.Product.PackSize));
return false;
}

return true;
}

protected virtual bool ValidateProductIsBuyable(ValidationContext<NewCartItem> context, CartProduct cartProduct)
{
if (!AbstractTypeFactory<ProductIsBuyableSpecification>.TryCreateInstance().IsSatisfiedBy(cartProduct))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageReference Include="FluentValidation" Version="11.8.1" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="VirtoCommerce.Xapi.Core" Version="3.802.0" />
<PackageReference Include="VirtoCommerce.XCatalog.Core" Version="3.800.0" />
<PackageReference Include="VirtoCommerce.XCatalog.Core" Version="3.809.0" />
<PackageReference Include="VirtoCommerce.CartModule.Core" Version="3.810.0" />
<PackageReference Include="VirtoCommerce.PaymentModule.Core" Version="3.800.0" />
<PackageReference Include="VirtoCommerce.ShippingModule.Core" Version="3.800.0" />
Expand Down
2 changes: 1 addition & 1 deletion src/VirtoCommerce.XCart.Web/module.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<platformVersion>3.848.0</platformVersion>
<dependencies>
<dependency id="VirtoCommerce.Xapi" version="3.802.0" />
<dependency id="VirtoCommerce.XCatalog" version="3.800.0" />
<dependency id="VirtoCommerce.XCatalog" version="3.809.0" />
<dependency id="VirtoCommerce.Cart" version="3.810.0" />
<dependency id="VirtoCommerce.Inventory" version="3.800.0" />
<dependency id="VirtoCommerce.Marketing" version="3.800.0" />
Expand Down

0 comments on commit 4026795

Please sign in to comment.