Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Discount validators - minor changes with UserError

support163db9910f4d
diff --git a/src/Business/Grand.Business.Catalog/Services/Discounts/DiscountService.cs b/src/Business/Grand.Business.Catalog/Services/Discounts/DiscountService.cs
index 26e842a29..7e6b6dc17 100644
--- a/src/Business/Grand.Business.Catalog/Services/Discounts/DiscountService.cs
+++ b/src/Business/Grand.Business.Catalog/Services/Discounts/DiscountService.cs
@@ -28,7 +28,6 @@ namespace Grand.Business.Catalog.Services.Discounts
private readonly IRepository<Discount> _discountRepository;
private readonly IRepository<DiscountCoupon> _discountCouponRepository;
private readonly IRepository<DiscountUsageHistory> _discountUsageHistoryRepository;
- private readonly ITranslationService _translationService;
private readonly ICacheBase _cacheBase;
private readonly IWorkContext _workContext;
private readonly IEnumerable<IDiscountProvider> _discountProviders;
@@ -46,7 +45,6 @@ namespace Grand.Business.Catalog.Services.Discounts
IRepository<Discount> discountRepository,
IRepository<DiscountCoupon> discountCouponRepository,
IRepository<DiscountUsageHistory> discountUsageHistoryRepository,
- ITranslationService translationService,
IWorkContext workContext,
IEnumerable<IDiscountProvider> discountProviders,
IEnumerable<IDiscountAmountProvider> discountAmountProviders,
@@ -56,7 +54,6 @@ namespace Grand.Business.Catalog.Services.Discounts
_discountRepository = discountRepository;
_discountCouponRepository = discountCouponRepository;
_discountUsageHistoryRepository = discountUsageHistoryRepository;
- _translationService = translationService;
_workContext = workContext;
_discountProviders = discountProviders;
_discountAmountProviders = discountAmountProviders;
@@ -176,7 +173,7 @@ namespace Grand.Business.Catalog.Services.Discounts
var usageHistory = await GetAllDiscountUsageHistory(discount.Id);
if (usageHistory.Count > 0)
- throw new ArgumentNullException("Discount was used and have a history");
+ throw new ArgumentException("Discount was used and have a history");
await _discountRepository.DeleteAsync(discount);
@@ -338,7 +335,7 @@ namespace Grand.Business.Catalog.Services.Discounts
}
else
{
- coupon.Qty = coupon.Qty - 1;
+ coupon.Qty -= 1;
coupon.Used = coupon.Qty > 0;
}
await _discountCouponRepository.UpdateAsync(coupon);
@@ -422,7 +419,7 @@ namespace Grand.Business.Catalog.Services.Discounts
//do not allow use discount in the current store
if (discount.LimitedToStores && discount.Stores.All(x => _workContext.CurrentStore.Id != x))
{
- result.UserError = _translationService.GetResource("ShoppingCart.Discount.CannotBeUsedInStore");
+ result.UserErrorResource = "ShoppingCart.Discount.CannotBeUsedInStore";
return result;
}
@@ -460,7 +457,7 @@ namespace Grand.Business.Catalog.Services.Discounts
var hasGiftVouchers = cart.Any(x => x.IsGiftVoucher);
if (hasGiftVouchers)
{
- result.UserError = _translationService.GetResource("ShoppingCart.Discount.CannotBeUsedWithGiftVouchers");
+ result.UserErrorResource = "ShoppingCart.Discount.CannotBeUsedWithGiftVouchers";
return result;
}
}
@@ -471,7 +468,7 @@ namespace Grand.Business.Catalog.Services.Discounts
DateTime startDate = DateTime.SpecifyKind(discount.StartDateUtc.Value, DateTimeKind.Utc);
if (startDate.CompareTo(now) > 0)
{
- result.UserError = _translationService.GetResource("ShoppingCart.Discount.NotStartedYet");
+ result.UserErrorResource = "ShoppingCart.Discount.NotStartedYet";
return result;
}
}
@@ -480,7 +477,7 @@ namespace Grand.Business.Catalog.Services.Discounts
DateTime endDate = DateTime.SpecifyKind(discount.EndDateUtc.Value, DateTimeKind.Utc);
if (endDate.CompareTo(now) < 0)
{
- result.UserError = _translationService.GetResource("ShoppingCart.Discount.Expired");
+ result.UserErrorResource = "ShoppingCart.Discount.Expired";
return result;
}
}
@@ -500,7 +497,7 @@ namespace Grand.Business.Catalog.Services.Discounts
var usedTimes = await GetAllDiscountUsageHistory(discount.Id, customer.Id, null, false, 0, 1);
if (usedTimes.TotalCount >= discount.LimitationTimes)
{
- result.UserError = _translationService.GetResource("ShoppingCart.Discount.CannotBeUsedAnymore");
+ result.UserErrorResource = "ShoppingCart.Discount.CannotBeUsedAnymore";
return result;
}
}
@@ -534,7 +531,7 @@ namespace Grand.Business.Catalog.Services.Discounts
if (singleRequirementRule == null) return result;
var ruleResult = await singleRequirementRule.CheckRequirement(ruleRequest);
if (ruleResult.IsValid) continue;
- result.UserError = ruleResult.UserError;
+ result.UserErrorResource = ruleResult.UserError;
return result;
}
diff --git a/src/Business/Grand.Business.Core/Utilities/Catalog/DiscountValidationResult.cs b/src/Business/Grand.Business.Core/Utilities/Catalog/DiscountValidationResult.cs
index 5a12582cf..f68b93551 100644
--- a/src/Business/Grand.Business.Core/Utilities/Catalog/DiscountValidationResult.cs
+++ b/src/Business/Grand.Business.Core/Utilities/Catalog/DiscountValidationResult.cs
@@ -13,11 +13,12 @@
/// <summary>
/// Gets or sets an error for a customer that should be visible when a coupon code is entered (in case if "IsValid" is set to "false")
/// </summary>
- public string UserError { get; set; }
+ public string UserErrorResource { get; set; }
/// <summary>
/// Gets or sets a coupon code value
/// </summary>
public string CouponCode { get; set; }
}
+
}
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Discounts/DiscountServiceTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Discounts/DiscountServiceTests.cs
index 991528455..706c85ce3 100644
--- a/src/Tests/Grand.Business.Catalog.Tests/Services/Discounts/DiscountServiceTests.cs
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Discounts/DiscountServiceTests.cs
@@ -51,7 +51,7 @@ namespace Grand.Business.Catalog.Tests.Services.Discounts
_discountProviders = new List<IDiscountProvider> { new DiscountProviderTest() };
_discountAmountProviders = new List<IDiscountAmountProvider> { new DiscountAmountProviderTests() };
- _dicountService = new DiscountService(_cacheBase, _repository, _discountCouponRepository, _discountUsageHistoryRepository, _translationServiceMock.Object,
+ _dicountService = new DiscountService(_cacheBase, _repository, _discountCouponRepository, _discountUsageHistoryRepository,
_workContextMock.Object, _discountProviders, _discountAmountProviders, _mediatorMock.Object);
}
diff --git a/src/Web/Grand.Web/Validators/ShoppingCart/DiscountCouponValidator.cs b/src/Web/Grand.Web/Validators/ShoppingCart/DiscountCouponValidator.cs
index 72bc1974c..4ce1b045f 100644
--- a/src/Web/Grand.Web/Validators/ShoppingCart/DiscountCouponValidator.cs
+++ b/src/Web/Grand.Web/Validators/ShoppingCart/DiscountCouponValidator.cs
@@ -48,8 +48,8 @@ namespace Grand.Web.Validators.ShoppingCart
var validationResult = await discountService.ValidateDiscount(discount,
workContext.CurrentCustomer, workContext.WorkingCurrency, x.DiscountCouponCode);
if (!validationResult.IsValid)
- context.AddFailure(!string.IsNullOrEmpty(validationResult.UserError)
- ? validationResult.UserError
+ context.AddFailure(!string.IsNullOrEmpty(validationResult.UserErrorResource)
+ ? translationService.GetResource(validationResult.UserErrorResource)
: translationService.GetResource("ShoppingCart.DiscountCouponCode.WrongDiscount"));
}
else