public CartShipmentValidator(ShoppingCart cart, ICartService cartService) { RuleSet("strict", () => { RuleFor(x => x).CustomAsync(async(shipment, context, cancellationToken) => { shipment.ValidationErrors.Clear(); var availShippingmethods = await cartService.GetAvailableShippingMethodsAsync(cart); var shipmentShippingMethod = availShippingmethods.FirstOrDefault(sm => shipment.HasSameMethod(sm)); if (shipmentShippingMethod == null) { var unavailableError = new UnavailableError(); shipment.ValidationErrors.Add(unavailableError); context.AddFailure(new ValidationFailure(nameof(shipment.ShipmentMethodCode), "The shipment method is no longer available")); } else if (shipmentShippingMethod.Price != shipment.Price) { var priceChangedError = new PriceError(shipment.Price, shipment.PriceWithTax, shipmentShippingMethod.Price, shipmentShippingMethod.PriceWithTax); shipment.ValidationErrors.Add(priceChangedError); context.AddFailure(new ValidationFailure(nameof(shipment.Price), "The shipment method price is changed")); } }); }); }
public CartLineItemValidator(ShoppingCart cart) { RuleSet("strict", () => { RuleFor(x => x).Custom((lineItem, context) => { lineItem.ValidationErrors.Clear(); if (lineItem.Product == null || !lineItem.Product.IsActive || !lineItem.Product.IsBuyable) { var unavailableError = new UnavailableError(); lineItem.ValidationErrors.Add(unavailableError); context.AddFailure(new ValidationFailure(nameof(lineItem.Product), "The product is not longer available for purchase")); } else if (lineItem.Product.Price == null || lineItem.Product.Price.GetTierPrice(lineItem.Quantity).Price == 0) { var unavailableError = new UnavailableError(); lineItem.ValidationErrors.Add(unavailableError); } else { var isProductAvailable = new ProductIsAvailableSpecification(lineItem.Product).IsSatisfiedBy(lineItem.Quantity); if (!isProductAvailable) { var availableQuantity = lineItem.Product.AvailableQuantity; var qtyError = new QuantityError(availableQuantity); lineItem.ValidationErrors.Add(qtyError); context.AddFailure(new ValidationFailure(nameof(lineItem.Product.AvailableQuantity), "The product available qty is changed")); } var tierPrice = lineItem.Product.Price.GetTierPrice(lineItem.Quantity); if (tierPrice.Price != lineItem.SalePrice) { var priceError = new PriceError(lineItem.SalePrice, lineItem.SalePriceWithTax, tierPrice.Price, tierPrice.PriceWithTax); lineItem.ValidationErrors.Add(priceError); context.AddFailure(new ValidationFailure(nameof(lineItem.SalePrice), "The product price is changed")); } } }); }); }