Example #1
0
        public async Task <ValidationResult> CheckGiftCard(GetBasketModel basket, string voucherCode)
        {
            // Check if voucher already exists on basket
            // Don't want to actually remove the card from the pool until order has been placed
            if (basket.RedeemedCards.Any(card => card.VoucherCode == voucherCode))
            {
                return(new ValidationResult
                {
                    Success = false,
                    Message = "Voucher already redeemed"
                });
            }

            // Does the voucher exist in the store?
            var giftCard = await this.giftCardRepository.GetGiftCard(voucherCode);

            if (giftCard == null)
            {
                return(new ValidationResult
                {
                    Success = false,
                    Message = "Not a valid voucher code"
                });
            }

            return(new ValidationResult
            {
                Success = true
            });
        }
Example #2
0
        private async Task <List <StockItem> > GetItems(GetBasketModel basket)
        {
            var basketItems = new List <StockItem>();
            var groups      = basket.Items.GroupBy(item => item.ProductId);

            foreach (var group in groups)
            {
                var stockItem = await this.stockItemRepository.GetItem(group.First().ProductId);

                basketItems.AddRange(Enumerable.Repeat(stockItem, group.Count()));
            }

            return(basketItems);
        }
Example #3
0
        public async Task <ValidationResult> CheckOffer(GetBasketModel basket, string offerCode)
        {
            var offer = await this.offerRepository.GetOffer(offerCode);

            if (offer == null)
            {
                return(new ValidationResult
                {
                    Success = false,
                    Message = "Offer code does not exist"
                });
            }

            // Are there items that it applies to
            var basketItems = await this.GetItems(basket);

            var itemTags = basketItems.SelectMany(item => item.Tags);

            if (offer.ApplicableItems.Any() &&
                !itemTags.Intersect(offer.ApplicableItems).Any())
            {
                return(new ValidationResult
                {
                    Success = false,
                    Message = $"There are no products in your basket applicable to voucher {offerCode}"
                });
            }

            // Have we met the minimum-spend threshold
            if (basket.DiscountableTotal < offer.Threshold)
            {
                var difference = offer.Threshold - basket.DiscountableTotal;
                return(new ValidationResult
                {
                    Success = false,
                    Message = $"You have not reached the spend threshold for voucher {offerCode}. Spend another £{difference} to receive £{offer.Value} discount from your basket total"
                });
            }

            return(new ValidationResult
            {
                Success = true
            });
        }