public static coreModel.Promotion ToCoreModel(this webModel.Promotion promotion)
        {
            var retVal = new DynamicPromotion();

            retVal.InjectFrom(promotion);
            retVal.Coupons = promotion.Coupons;

            if (promotion.DynamicExpression != null && promotion.DynamicExpression.Children != null)
            {
                var conditionExpression = promotion.DynamicExpression.GetConditionExpression();
                retVal.PredicateSerialized = SerializationUtil.SerializeExpression(conditionExpression);
                var rewards = promotion.DynamicExpression.GetRewards();
                retVal.RewardsSerialized = JsonConvert.SerializeObject(rewards, new JsonSerializerSettings {
                    TypeNameHandling = TypeNameHandling.All
                });

                //Clear availableElements in expression (for decrease size)
                promotion.DynamicExpression.AvailableChildren = null;
                var allBlocks = ((DynamicExpression)promotion.DynamicExpression).Traverse(x => x.Children);
                foreach (var block in allBlocks)
                {
                    block.AvailableChildren = null;
                }
                retVal.PredicateVisualTreeSerialized = JsonConvert.SerializeObject(promotion.DynamicExpression);
            }
            return(retVal);
        }
        public static webModel.Promotion ToWebModel(this coreModel.Promotion promotion, PromoDynamicExpressionTree etalonEpressionTree = null)
        {
            var retVal = new webModel.Promotion();

            retVal.InjectFrom(promotion);
            retVal.Coupons = promotion.Coupons;
            retVal.Type    = promotion.GetType().Name;
            var dynamicPromotion = promotion as DynamicPromotion;

            if (dynamicPromotion != null && etalonEpressionTree != null)
            {
                retVal.DynamicExpression = etalonEpressionTree;
                if (!String.IsNullOrEmpty(dynamicPromotion.PredicateVisualTreeSerialized))
                {
                    retVal.DynamicExpression = JsonConvert.DeserializeObject <PromoDynamicExpressionTree>(dynamicPromotion.PredicateVisualTreeSerialized);
                    //Copy available elements from etalon because they not persisted
                    var sourceBlocks = ((DynamicExpression)etalonEpressionTree).Traverse(x => x.Children);
                    var targetBlocks = ((DynamicExpression)retVal.DynamicExpression).Traverse(x => x.Children);
                    foreach (var sourceBlock in sourceBlocks)
                    {
                        foreach (var targetBlock in  targetBlocks.Where(x => x.Id == sourceBlock.Id))
                        {
                            targetBlock.AvailableChildren = sourceBlock.AvailableChildren;
                        }
                    }
                    //copy available elements from etalon
                    retVal.DynamicExpression.AvailableChildren = etalonEpressionTree.AvailableChildren;
                }
            }
            return(retVal);
        }
		public static webModel.Promotion ToWebModel(this coreModel.Promotion promotion, PromoDynamicExpressionTree etalonEpressionTree = null)
		{
			var retVal = new webModel.Promotion();
			retVal.InjectFrom(promotion);
			retVal.Coupons = promotion.Coupons;
			retVal.Type = promotion.GetType().Name;
			var dynamicPromotion = promotion as DynamicPromotion;
			if (dynamicPromotion != null && etalonEpressionTree != null)
			{
				retVal.DynamicExpression = etalonEpressionTree;
				if (!String.IsNullOrEmpty(dynamicPromotion.PredicateVisualTreeSerialized))
				{
					retVal.DynamicExpression = JsonConvert.DeserializeObject<PromoDynamicExpressionTree>(dynamicPromotion.PredicateVisualTreeSerialized);
					//Copy available elements from etalon because they not persisted
					var sourceBlocks = ((DynamicExpression)etalonEpressionTree).Traverse(x => x.Children);
					var targetBlocks = ((DynamicExpression)retVal.DynamicExpression).Traverse(x => x.Children);
					foreach (var sourceBlock in sourceBlocks)
					{
						foreach(var targetBlock in  targetBlocks.Where(x => x.Id == sourceBlock.Id))
						{
							targetBlock.AvailableChildren = sourceBlock.AvailableChildren;
						}
					}
					//copy available elements from etalon
					retVal.DynamicExpression.AvailableChildren = etalonEpressionTree.AvailableChildren;
				}
			}
			return retVal;
		}
        public static Promotion ToCoreModel(this webModel.Promotion promotion, IExpressionSerializer expressionSerializer)
        {
            var result = AbstractTypeFactory <DynamicPromotion> .TryCreateInstance();

            result.InjectFrom(promotion);

            //result.Coupons = promotion.Coupons;

            if (promotion.DynamicExpression?.Children != null)
            {
                var conditionExpression = promotion.DynamicExpression.GetConditionExpression();
                result.PredicateSerialized = expressionSerializer.SerializeExpression(conditionExpression);

                var rewards = promotion.DynamicExpression.GetRewards();
                result.RewardsSerialized = JsonConvert.SerializeObject(rewards, new JsonSerializerSettings {
                    TypeNameHandling = TypeNameHandling.All
                });

                // Clear availableElements in expression to decrease size
                promotion.DynamicExpression.AvailableChildren = null;
                var allBlocks = ((DynamicExpression)promotion.DynamicExpression).Traverse(x => x.Children);
                foreach (var block in allBlocks)
                {
                    block.AvailableChildren = null;
                }

                result.PredicateVisualTreeSerialized = JsonConvert.SerializeObject(promotion.DynamicExpression);
            }

            return(result);
        }
        public IHttpActionResult GetNewDynamicPromotion()
        {
            var retVal = new webModel.Promotion
            {
				Type = typeof(DynamicPromotion).Name,
				DynamicExpression = _marketingExtensionManager.PromotionDynamicExpressionTree,
				IsActive = true
            };
            return Ok(retVal);
        }
        public IHttpActionResult GetNewDynamicPromotion()
        {
            var retVal = new webModel.Promotion
            {
                Type = typeof(DynamicPromotion).Name,
                DynamicExpression = _marketingExtensionManager.PromotionDynamicExpressionTree,
                IsActive          = true
            };

            return(Ok(retVal));
        }
        public IHttpActionResult UpdatePromotions(webModel.Promotion promotion)
        {
            var corePromotion = promotion.ToCoreModel(_expressionSerializer);
            var scopes        = _permissionScopeService.GetObjectPermissionScopeStrings(corePromotion).ToArray();

            if (!_securityService.UserHasAnyPermission(User.Identity.Name, scopes, MarketingPredefinedPermissions.Update))
            {
                throw new HttpResponseException(HttpStatusCode.Unauthorized);
            }
            _promotionService.SavePromotions(new[] { corePromotion });
            return(StatusCode(HttpStatusCode.NoContent));
        }
        public void Can_extend_marketing_promotion_expressiontree_and_create_new_dynamicpromotion()
        {
            var extensionManager = GetPromotionExtensionManager();

            //Register custom dynamic expression in main expression tree now it should be availabe for ui in expression builder
            var blockExpression       = extensionManager.PromotionDynamicExpressionTree as DynamicExpression;
            var blockCatalogCondition = blockExpression.FindChildrenExpression <BlockCatalogCondition>();

            blockCatalogCondition.AvailableChildren.Add(new ConditionItemWithTag());

            var marketingController = GetMarketingController(extensionManager);

            //Create custom promotion
            webModel.Promotion promotion = null;
            var promoResult = (marketingController.GetPromotionById("TaggedProductDiscount") as OkNegotiatedContentResult <webModel.Promotion>);

            if (promoResult != null)
            {
                promotion = promoResult.Content;
            }
            if (promotion == null)
            {
                promotion = (marketingController.GetNewDynamicPromotion() as OkNegotiatedContentResult <webModel.Promotion>).Content;

                promotion.Description = "Buy all product with tag '#FOOTBAL' with 7% discount";
                promotion.Name        = "TaggedProductDiscount";
                promotion.Id          = "TaggedProductDiscount";

                blockExpression       = promotion.DynamicExpression as DynamicExpression;
                blockCatalogCondition = blockExpression.FindChildrenExpression <BlockCatalogCondition>();
                var blockReward = blockExpression.FindChildrenExpression <RewardBlock>();

                var conditionExpr = blockCatalogCondition.FindAvailableExpression <ConditionItemWithTag>();
                conditionExpr.Tags = new string[] { "#FOOTBAL" };
                blockCatalogCondition.Children.Add(conditionExpr);

                var rewardExpr = blockReward.FindAvailableExpression <RewardItemGetOfRel>();
                rewardExpr.Amount = 0.7m;
                blockReward.Children.Add(rewardExpr);

                promotion = (marketingController.CreatePromotion(promotion) as OkNegotiatedContentResult <webModel.Promotion>).Content;
            }

            var cacheManager  = new Moq.Mock <ICacheManager <object> >();
            var marketingEval = new DefaultPromotionEvaluatorImpl(GetMarketingService(), cacheManager.Object);
            var context       = GetPromotionEvaluationContext();

            context.PromoEntries.First().Attributes["tag"] = "#FOOTBAL";
            var result = marketingEval.EvaluatePromotion(context);
        }
        public void Can_create_marketing_dynamicpromotion_using_api()
        {
            var marketingController = GetMarketingController(GetPromotionExtensionManager());

            webModel.Promotion promotion = null;
            var promoResult = (marketingController.GetPromotionById("CartFiveDiscount") as OkNegotiatedContentResult <webModel.Promotion>);

            if (promoResult != null)
            {
                promotion = promoResult.Content;
            }
            if (promotion == null)
            {
                promotion = (marketingController.GetNewDynamicPromotion() as OkNegotiatedContentResult <webModel.Promotion>).Content;

                promotion.Description = "Buy at $100 and get a 5% discount";
                promotion.Name        = "CartFiveDiscount";
                promotion.Id          = "CartFiveDiscount";

                var expressionTree = promotion.DynamicExpression as DynamicExpression;

                //Curreny is USD
                var currencyExpression = expressionTree.FindAvailableExpression <ConditionCurrencyIs>();
                currencyExpression.Currency = "USD";
                expressionTree.Children.Add(currencyExpression);
                //Condition: Cart subtotal great or equal that 100$
                var subtotalExpression = expressionTree.FindAvailableExpression <ConditionCartSubtotalLeast>();
                subtotalExpression.SubTotal = 100;
                expressionTree.Children.Add(subtotalExpression);
                //Reward: Get 5% whole cart discount
                var rewardExpr = expressionTree.FindAvailableExpression <RewardItemGetOfRel>();
                rewardExpr.Amount = 0.5m;
                expressionTree.Children.Add(rewardExpr);

                promotion = (marketingController.CreatePromotion(promotion) as OkNegotiatedContentResult <webModel.Promotion>).Content;
            }

            var cacheManager  = new Moq.Mock <ICacheManager <object> >();
            var marketingEval = new DefaultPromotionEvaluatorImpl(GetMarketingService(), cacheManager.Object);
            var context       = GetPromotionEvaluationContext();
            var result        = marketingEval.EvaluatePromotion(context);
        }
        public static webModel.Promotion ToWebModel(this Promotion promotion, PromoDynamicExpressionTree etalonEpressionTree = null)
        {
            var result = new webModel.Promotion();

            result.InjectFrom(promotion);

            // Workaround for UI: DynamicPromotion type is hardcoded in HTML template
            var dynamicPromotionType = typeof(DynamicPromotion);
            var promotionType        = promotion.GetType();

            result.Type = dynamicPromotionType.IsAssignableFrom(promotionType)
                ? dynamicPromotionType.Name
                : promotionType.Name;

            var dynamicPromotion = promotion as DynamicPromotion;

            if (dynamicPromotion != null && etalonEpressionTree != null)
            {
                result.DynamicExpression = etalonEpressionTree;
                if (!string.IsNullOrEmpty(dynamicPromotion.PredicateVisualTreeSerialized))
                {
                    result.DynamicExpression = JsonConvert.DeserializeObject <PromoDynamicExpressionTree>(dynamicPromotion.PredicateVisualTreeSerialized);

                    // Copy available elements from etalon because they not persisted
                    var sourceBlocks = ((DynamicExpression)etalonEpressionTree).Traverse(x => x.Children);
                    var targetBlocks = ((DynamicExpression)result.DynamicExpression).Traverse(x => x.Children).ToList();

                    foreach (var sourceBlock in sourceBlocks)
                    {
                        foreach (var targetBlock in targetBlocks.Where(x => x.Id == sourceBlock.Id))
                        {
                            targetBlock.AvailableChildren = sourceBlock.AvailableChildren;
                        }
                    }

                    // Copy available elements from etalon
                    result.DynamicExpression.AvailableChildren = etalonEpressionTree.AvailableChildren;
                }
            }

            return(result);
        }
        public IHttpActionResult CreatePromotion(webModel.Promotion promotion)
        {
            var retVal = _promotionService.CreatePromotion(promotion.ToCoreModel());

            return(GetPromotionById(retVal.Id));
        }
 public IHttpActionResult UpdatePromotions(webModel.Promotion promotion)
 {
     _promotionService.UpdatePromotions(new coreModel.Promotion[] { promotion.ToCoreModel() });
     return(StatusCode(HttpStatusCode.NoContent));
 }