public void Expired_Promo_Should_Not_Be_Applied_To_CartItem_For_Matching_SKU_And_Quantity()
        {
            // arrange
            _mockProductService.Setup(method => method.GetProductsFromStore())
            .Returns(new List <ProductDto> {
                new ProductDto {
                    SKU = "A", Price = 50
                },
                new ProductDto {
                    SKU = "B", Price = 30
                },
            });
            var products = _mockProductService.Object.GetProductsFromStore();

            _mockCartService.Setup(method => method.GetCartItems())
            .Returns(new List <CartItemDto> {
                new CartItemDto {
                    SKU = "A", Quantity = 3, UnitPrice = 50
                },
            });
            var cartItems   = _mockCartService.Object.GetCartItems();
            var promoOffers = new List <PromoOffer> {
                new PromoOffer
                {
                    PromotionOfferId          = "1",
                    PromotionOfferDescription = "3 A's for 130",
                    ValidTill = DateTime.Today.AddMonths(-1),
                    PromoRule = new PromoRule {
                        IsForDifferentItems = false, Quantity = 3, SKUs = new List <string> {
                            "A"
                        }, PromoResult = new PromoResult {
                            OffFixedPrice = 130
                        }
                    },
                },
            };
            var appliedPromoOffer = promoOffers.First();
            // act
            var cartItemsWithOfferPrice = _promoCalculator.CalculateOfferPrice(_promoCalculator.ApplyPromoRule(cartItems, promoOffers), promoOffers);
            var cartItem_SKU_A          = cartItemsWithOfferPrice.FirstOrDefault(p => p.SKU == "A");

            // assert
            Assert.False(cartItem_SKU_A.PromoApplied);
            Assert.Equal(cartItem_SKU_A.ActualPrice, cartItem_SKU_A.UnitPrice * cartItem_SKU_A.Quantity);
            Assert.NotEqual(cartItem_SKU_A.OfferPrice, appliedPromoOffer.PromoRule.PromoResult.OffFixedPrice + ((cartItem_SKU_A.Quantity - appliedPromoOffer.PromoRule.Quantity) * cartItem_SKU_A.UnitPrice));
        }
Example #2
0
        public CartDto Checkout(CartDto cart)
        {
            var products  = _productService.GetProductsFromStore();
            var cartItems = cart.CartItems != null && cart.CartItems.Count() > 0 ? cart.CartItems : _cartService.GetCartItems(); // TO DO: This will be the cart.CartItems

            cartItems.ForEach(item =>
            {
                if (!products.Select(p => p.SKU).Contains(item.SKU))
                {
                    throw new Exception("Invalid item added to Cart.");
                }
                item.UnitPrice = products.First(p => p.SKU == item.SKU).Price;
            });
            var promoOffers             = _promoRuleService.GetPromoRules();
            var cartItemsWithOfferPrice = _promoCalculator.CalculateOfferPrice(_promoCalculator.ApplyPromoRule(cartItems, promoOffers), promoOffers);
            var cartDto = new CartDto
            {
                CartId    = !string.IsNullOrEmpty(cart.CartId) ? cart.CartId : Guid.NewGuid().ToString(),
                CartItems = cartItemsWithOfferPrice
            };

            return(cartDto);
        }