Skip to content

Commit

Permalink
Closes #898 Trim whitespace from Discount Code
Browse files Browse the repository at this point in the history
  • Loading branch information
markwoerdenweber committed Nov 29, 2023
1 parent a9087d2 commit 088d8c6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
Expand Up @@ -199,9 +199,9 @@ public virtual async Task<IEnumerable<Discount>> GetAllDiscountsAsync(DiscountTy
}

public virtual async Task<bool> IsDiscountValidAsync(
Discount discount,
Customer customer,
string couponCodeToValidate,
Discount discount,
Customer customer,
string couponCodeToValidate,
Store store = null,
DiscountValidationFlags flags = DiscountValidationFlags.All)
{
Expand All @@ -216,7 +216,7 @@ public virtual async Task<IEnumerable<Discount>> GetAllDiscountsAsync(DiscountTy
}

// Check coupon code.
if (discount.RequiresCouponCode && (discount.CouponCode.IsEmpty() || !discount.CouponCode.EqualsNoCase(couponCodeToValidate)))
if (discount.RequiresCouponCode && (discount.CouponCode.IsEmpty() || !discount.CouponCode.Trim().EqualsNoCase(couponCodeToValidate)))
{
return Cached(false);
}
Expand All @@ -235,7 +235,7 @@ public virtual async Task<IEnumerable<Discount>> GetAllDiscountsAsync(DiscountTy
ShoppingCart cart = null;

// Do not to apply discounts if there are gift cards in the cart cause the customer could "earn" money through that.
if (flags.HasFlag(DiscountValidationFlags.GiftCards) &&
if (flags.HasFlag(DiscountValidationFlags.GiftCards) &&
(discount.DiscountType == DiscountType.AssignedToOrderTotal || discount.DiscountType == DiscountType.AssignedToOrderSubTotal))
{
cart = await _cartService.Value.GetCartAsync(customer, ShoppingCartType.ShoppingCart, store.Id);
Expand Down Expand Up @@ -347,7 +347,7 @@ protected virtual async Task<bool> CheckDiscountLimitationsAsync(Discount discou
return false;
}
}

class DiscountKey : Tuple<Discount, Customer, string, Store, DiscountValidationFlags>
{
public DiscountKey(Discount discount, Customer customer, string customerCouponCode, Store store, DiscountValidationFlags flags)
Expand Down
Expand Up @@ -465,7 +465,9 @@ public virtual async Task<(bool Applied, Discount AppliedDiscount)> ApplyDiscoun
return (false, null);
}

var discount = await _db.Discounts.FirstOrDefaultAsync(x => x.CouponCode == couponCode);
couponCode = couponCode.Trim();

var discount = await _db.Discounts.FirstOrDefaultAsync(x => x.CouponCode.Trim().Equals(couponCode));
if (discount == null || !discount.RequiresCouponCode || !await _discountService.IsDiscountValidAsync(discount, cart.Customer, couponCode))
{
return (false, null);
Expand Down
Expand Up @@ -139,6 +139,8 @@ public async Task<IActionResult> Create(DiscountModel model, bool continueEditin
if (ModelState.IsValid)
{
var discount = await MapperFactory.MapAsync<DiscountModel, Discount>(model);
discount.CouponCode = discount.CouponCode?.Trim();

_db.Discounts.Add(discount);
await _db.SaveChangesAsync();

Expand Down Expand Up @@ -207,6 +209,9 @@ public async Task<IActionResult> Edit(DiscountModel model, bool continueEditing)
if (ModelState.IsValid)
{
await MapperFactory.MapAsync(model, discount);

discount.CouponCode = discount.CouponCode?.Trim();

await ApplyLocales(model, discount);
await _ruleService.ApplyRuleSetMappingsAsync(discount, model.SelectedRuleSetIds);

Expand Down

0 comments on commit 088d8c6

Please sign in to comment.