public void TestScenarioA()
        {
            PromotionTypeFactory promotionTypeFactory = new PromotionTypeFactory();
            List <char>          sampleCartItems      = new List <char>();

            sampleCartItems.Add('A');
            sampleCartItems.Add('B');
            sampleCartItems.Add('C');
            IPromotionType promotion1      = PromotionTypeFactory.GetPromotion((int)PromotionTypes.NItemsSKU);
            IPromotionType promotion2      = PromotionTypeFactory.GetPromotion((int)PromotionTypes.SKU1andSKU2);
            decimal        finalOrderValue = promotion1.CaliculatePromotionValue(sampleCartItems) + promotion2.CaliculatePromotionValue(sampleCartItems);

            Assert.AreEqual(finalOrderValue, 100);
        }
 public PromotionEngineController(IPromotionType promotionType)
 {
     this._promotionType = promotionType;
 }
Exemplo n.º 3
0
        public async Task <CartOrderContractAfterPromotionApplied> Handle(Query request, CancellationToken cancellationToken)
        {
            try
            {
                //Collect Valid SKUs for Promotion
                var promotions = JsonConvert.DeserializeObject <Pramotions>(JsonConvert.SerializeObject(
                                                                                JObject.Parse(File.ReadAllText(FileName)),
                                                                                Formatting.Indented));

                List <SKU> tempCartPromoApplied      = new List <SKU>();
                Dictionary <string, string> SKU      = new Dictionary <string, string>();
                Dictionary <string, string> OPERATOR = new Dictionary <string, string>();
                Dictionary <string, int>    QTY      = new Dictionary <string, int>();

                //"Open Close Principal" can be applied here
                //This can be refactored in a way that this class dont need to modify when new Promotion Type introduce
                //Leaving this implementation as is because of time constraint
                foreach (var promotionsModel in promotions.PromotionModels)
                {
                    if (promotionsModel.Type == PromotionType.OnOneSKU.ToString())
                    {
                        //This engine object can be loaded runtime by implementing Factory Pattern
                        _promotionHandler = new OnOneSKUPromotion();
                        foreach (var orderItem in request.CartOrderContratContract.CartOrder)
                        {
                            foreach (var sku in promotionsModel.SKUCollection)
                            {
                                if (sku.Value == orderItem.SKUId)
                                {
                                    var result = _promotionHandler.CalculatePromotionForItem(PromotionType.OnOneSKU,
                                                                                             promotionsModel, orderItem);
                                }
                            }
                        }
                    }

                    if (promotionsModel.Type == PromotionType.OnMultipleSKUsFixedPrice.ToString())
                    {
                        //This engine object can be loaded runtime by implementing Factory Pattern
                        _promotionHandler = new OnMultipleSKUsFixedPrice();
                        foreach (var orderItem in request.CartOrderContratContract.CartOrder)
                        {
                            foreach (var sku in promotionsModel.SKUCollection)
                            {
                                if (sku.Value == orderItem.SKUId)
                                {
                                    var result = _promotionHandler.CalculatePromotionForItem(PromotionType.OnOneSKU,
                                                                                             promotionsModel, orderItem);
                                }
                            }
                        }
                    }
                }

                await Task.CompletedTask;
                return(new CartOrderContractAfterPromotionApplied());
            }
            catch (Exception ex)
            {
                _logger.Error(ex, $"Failed to handle feed for {request}", request);
                throw;
            }
        }