Beispiel #1
0
        public void Can_load_discountRequirementRuleBySystemKeyword()
        {
            EngineContext.Replace(null);
            var rule = _discountPluginManager.LoadPluginBySystemName("TestDiscountRequirementRule");

            rule.Should().NotBeNull();
        }
 public void Can_load_discountRequirementRuleBySystemKeyword()
 {
     RunWithTestServiceProvider(() =>
     {
         var rule = _discountPluginManager.LoadPluginBySystemName("TestDiscountRequirementRule");
         rule.Should().NotBeNull();
     });
 }
Beispiel #3
0
 public void CheckRequirement_Request_Null()
 {
     RunWithTestServiceProvider(() =>
     {
         var rule = _discountPluginManager.LoadPluginBySystemName(SystemName);
         try
         {
             var result = rule.CheckRequirement(null);
         }
         catch (ArgumentException ex)
         {
             Assert.IsInstanceOf <ArgumentNullException>(ex);
             Assert.AreEqual("Value cannot be null. (Parameter 'request')", ex.Message);
             Assert.AreEqual("request", ex.ParamName);
         }
     });
 }
Beispiel #4
0
        /// <summary>
        /// Get discount validation result
        /// </summary>
        /// <param name="requirements">Collection of discount requirement</param>
        /// <param name="groupInteractionType">Interaction type within the group of requirements</param>
        /// <param name="customer">Customer</param>
        /// <param name="errors">Errors</param>
        /// <returns>True if result is valid; otherwise false</returns>
        protected bool GetValidationResult(IEnumerable <DiscountRequirement> requirements,
                                           RequirementGroupInteractionType groupInteractionType, Customer customer, List <string> errors)
        {
            var result = false;

            foreach (var requirement in requirements)
            {
                if (requirement.IsGroup)
                {
                    var childRequirements = GetDiscountRequirementsByParent(requirement);
                    //get child requirements for the group
                    var interactionType = requirement.InteractionType ?? RequirementGroupInteractionType.And;
                    result = GetValidationResult(childRequirements, interactionType, customer, errors);
                }
                else
                {
                    //or try to get validation result for the requirement
                    var requirementRulePlugin = _discountPluginManager
                                                .LoadPluginBySystemName(requirement.DiscountRequirementRuleSystemName, customer, _storeContext.CurrentStore.Id);
                    if (requirementRulePlugin == null)
                    {
                        continue;
                    }

                    var ruleResult = requirementRulePlugin.CheckRequirement(new DiscountRequirementValidationRequest
                    {
                        DiscountRequirementId = requirement.Id,
                        Customer = customer,
                        Store    = _storeContext.CurrentStore
                    });

                    //add validation error
                    if (!ruleResult.IsValid)
                    {
                        var userError = !string.IsNullOrEmpty(ruleResult.UserError)
                            ? ruleResult.UserError
                            : _localizationService.GetResource("ShoppingCart.Discount.CannotBeUsed");
                        errors.Add(userError);
                    }

                    result = ruleResult.IsValid;
                }

                //all requirements must be met, so return false
                if (!result && groupInteractionType == RequirementGroupInteractionType.And)
                {
                    return(false);
                }

                //any of requirements must be met, so return true
                if (result && groupInteractionType == RequirementGroupInteractionType.Or)
                {
                    return(true);
                }
            }

            return(result);
        }
Beispiel #5
0
        /// <summary>
        /// Prepare discount requirement rule models
        /// </summary>
        /// <param name="requirements">Collection of discount requirements</param>
        /// <param name="discount">Discount</param>
        /// <param name="groupInteractionType">Interaction type within the group of requirements</param>
        /// <returns>List of discount requirement rule models</returns>
        public virtual IList <DiscountRequirementRuleModel> PrepareDiscountRequirementRuleModels(ICollection <DiscountRequirement> requirements,
                                                                                                 Discount discount, RequirementGroupInteractionType groupInteractionType)
        {
            if (requirements == null)
            {
                throw new ArgumentNullException(nameof(requirements));
            }

            if (discount == null)
            {
                throw new ArgumentNullException(nameof(discount));
            }

            var lastRequirement = requirements.LastOrDefault();

            return(requirements.Select(requirement =>
            {
                //set common properties
                var requirementModel = new DiscountRequirementRuleModel
                {
                    DiscountRequirementId = requirement.Id,
                    ParentId = requirement.ParentId,
                    IsGroup = requirement.IsGroup,
                    RuleName = requirement.DiscountRequirementRuleSystemName,
                    IsLastInGroup = lastRequirement == null || lastRequirement.Id == requirement.Id,
                    InteractionTypeId = (int)groupInteractionType
                };

                var interactionType = requirement.InteractionType ?? RequirementGroupInteractionType.And;
                requirementModel.AvailableInteractionTypes = interactionType.ToSelectList();

                if (requirement.IsGroup)
                {
                    //get child requirements for the group
                    var childRequirements = _discountService.GetDiscountRequirementsByParent(requirement);

                    requirementModel
                    .ChildRequirements = PrepareDiscountRequirementRuleModels(childRequirements, discount, interactionType);

                    return requirementModel;
                }

                //or try to get name and configuration URL for the requirement
                var requirementRule = _discountPluginManager.LoadPluginBySystemName(requirement.DiscountRequirementRuleSystemName);
                if (requirementRule == null)
                {
                    return null;
                }

                requirementModel.RuleName = requirementRule.PluginDescriptor.FriendlyName;
                requirementModel
                .ConfigurationUrl = requirementRule.GetConfigurationUrl(discount.Id, requirement.Id);

                return requirementModel;
            }).ToList());
        }
        public virtual IActionResult GetDiscountRequirementConfigurationUrl(string systemName, int discountId, int?discountRequirementId)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageDiscounts))
            {
                return(AccessDeniedView());
            }

            if (string.IsNullOrEmpty(systemName))
            {
                throw new ArgumentNullException(nameof(systemName));
            }

            var discountRequirementRule = _discountPluginManager.LoadPluginBySystemName(systemName)
                                          ?? throw new ArgumentException("Discount requirement rule could not be loaded");

            var discount = _discountService.GetDiscountById(discountId)
                           ?? throw new ArgumentException("Discount could not be loaded");

            var url = discountRequirementRule.GetConfigurationUrl(discount.Id, discountRequirementId);

            return(Json(new { url }));
        }
        public void CanLoadDiscountRequirementRuleBySystemKeyword()
        {
            var rule = _discountPluginManager.LoadPluginBySystemName("TestDiscountRequirementRule");

            rule.Should().NotBeNull();
        }