using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Infrastructure; using Microsoft.AspNetCore.Mvc.Routing; using Nop.Core; using Nop.Services.Configuration; using Nop.Services.Customers; using Nop.Services.Discounts; using Nop.Services.Localization; using Nop.Services.Plugins; namespace Nop.Plugin.DiscountRules.CustomerRoles; public class CustomerRoleDiscountRequirementRule : BasePlugin, IDiscountRequirementRule { #region Fields protected readonly IActionContextAccessor _actionContextAccessor; protected readonly ICustomerService _customerService; protected readonly IDiscountService _discountService; protected readonly ILocalizationService _localizationService; protected readonly ISettingService _settingService; protected readonly IUrlHelperFactory _urlHelperFactory; protected readonly IWebHelper _webHelper; #endregion #region Ctor public CustomerRoleDiscountRequirementRule(IActionContextAccessor actionContextAccessor, IDiscountService discountService, ICustomerService customerService, ILocalizationService localizationService, ISettingService settingService, IUrlHelperFactory urlHelperFactory, IWebHelper webHelper) { _actionContextAccessor = actionContextAccessor; _customerService = customerService; _discountService = discountService; _localizationService = localizationService; _settingService = settingService; _urlHelperFactory = urlHelperFactory; _webHelper = webHelper; } #endregion #region Methods /// /// Check discount requirement /// /// Object that contains all information required to check the requirement (Current customer, discount, etc) /// /// A task that represents the asynchronous operation /// The task result contains the result /// public async Task CheckRequirementAsync(DiscountRequirementValidationRequest request) { ArgumentNullException.ThrowIfNull(request); //invalid by default var result = new DiscountRequirementValidationResult(); if (request.Customer == null) return result; //try to get saved restricted customer role identifier var restrictedRoleId = await _settingService.GetSettingByKeyAsync(string.Format(DiscountRequirementDefaults.SettingsKey, request.DiscountRequirementId)); if (restrictedRoleId == 0) return result; //result is valid if the customer belongs to the restricted role result.IsValid = (await _customerService.GetCustomerRolesAsync(request.Customer)).Any(role => role.Id == restrictedRoleId); return result; } /// /// Get URL for rule configuration /// /// Discount identifier /// Discount requirement identifier (if editing) /// URL public string GetConfigurationUrl(int discountId, int? discountRequirementId) { var urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext); return urlHelper.Action("Configure", "DiscountRulesCustomerRoles", new { discountId = discountId, discountRequirementId = discountRequirementId }, _webHelper.GetCurrentRequestProtocol()); } /// /// Install the plugin /// /// A task that represents the asynchronous operation public override async Task InstallAsync() { //locales await _localizationService.AddOrUpdateLocaleResourceAsync(new Dictionary { ["Plugins.DiscountRules.CustomerRoles.Fields.CustomerRole"] = "Required customer role", ["Plugins.DiscountRules.CustomerRoles.Fields.CustomerRole.Hint"] = "Discount will be applied if customer is in the selected customer role.", ["Plugins.DiscountRules.CustomerRoles.Fields.CustomerRole.Select"] = "Select customer role", ["Plugins.DiscountRules.CustomerRoles.Fields.CustomerRoleId.Required"] = "Customer role is required", ["Plugins.DiscountRules.CustomerRoles.Fields.DiscountId.Required"] = "Discount is required" }); await base.InstallAsync(); } /// /// Uninstall the plugin /// /// A task that represents the asynchronous operation public override async Task UninstallAsync() { //discount requirements var discountRequirements = (await _discountService.GetAllDiscountRequirementsAsync()) .Where(discountRequirement => discountRequirement.DiscountRequirementRuleSystemName == DiscountRequirementDefaults.SystemName); foreach (var discountRequirement in discountRequirements) { await _discountService.DeleteDiscountRequirementAsync(discountRequirement, false); } //locales await _localizationService.DeleteLocaleResourcesAsync("Plugins.DiscountRules.CustomerRoles"); await base.UninstallAsync(); } #endregion }