Added coupon processing. Fixed price calculation

This commit is contained in:
Marcel Baumgartner
2023-10-22 22:02:45 +02:00
parent 3d4f22f6f6
commit 2dd1d1f69c
2 changed files with 48 additions and 21 deletions

View File

@@ -52,7 +52,7 @@ public class StoreOrderService
.Get() .Get()
.FirstOrDefault(x => x.Id == coupon.Id); .FirstOrDefault(x => x.Id == coupon.Id);
if(coupon == null) if (coupon == null)
throw new DisplayException("Unsafe value detected. Please reload the page to proceed"); throw new DisplayException("Unsafe value detected. Please reload the page to proceed");
} }
@@ -61,13 +61,13 @@ public class StoreOrderService
if (coupon != null && user.CouponUses.Any(x => x.Coupon.Id == coupon.Id)) if (coupon != null && user.CouponUses.Any(x => x.Coupon.Id == coupon.Id))
throw new DisplayException("Coupon already used"); throw new DisplayException("Coupon already used");
if (coupon != null && coupon.Amount == 0) if (coupon != null && coupon.Amount < 1)
throw new DisplayException("No coupon uses left"); throw new DisplayException("No coupon uses left");
var price = product.Price * durationMultiplier; var price = product.Price * durationMultiplier;
if (coupon != null) if (coupon != null)
price = Math.Round(price * coupon.Percent / 100, 2); price = Math.Round(price - (price * coupon.Percent / 100), 2);
if (user.Balance < price) if (user.Balance < price)
throw new DisplayException("Order is too expensive"); throw new DisplayException("Order is too expensive");
@@ -100,7 +100,7 @@ public class StoreOrderService
var price = p.Price * durationMultiplier; var price = p.Price * durationMultiplier;
if (c != null) if (c != null)
price = Math.Round(price * c.Percent / 100, 2); price = Math.Round(price - (price * c.Percent / 100), 2);
// Calculate duration // Calculate duration
var duration = durationMultiplier * p.Duration; var duration = durationMultiplier * p.Duration;
@@ -108,10 +108,37 @@ public class StoreOrderService
// Add transaction // Add transaction
await transactionService.Add(u, -1 * price, $"Bought product '{p.Name}' for {duration} days"); await transactionService.Add(u, -1 * price, $"Bought product '{p.Name}' for {duration} days");
// Create service // Process coupon if used
return await serviceService.Admin.Create(u, p, service => if (c != null)
{ {
service.RenewAt = DateTime.UtcNow.AddDays(duration); // Remove one use from the coupon
var couponRepo = scope.ServiceProvider.GetRequiredService<Repository<Coupon>>();
var coupon = couponRepo
.Get()
.First(x => x.Id == c.Id);
coupon.Amount--;
couponRepo.Update(coupon);
// Add coupon use to user
var userRepo = scope.ServiceProvider.GetRequiredService<Repository<User>>();
var user = userRepo
.Get()
.Include(x => x.CouponUses)
.First(x => x.Id == u.Id);
user.CouponUses.Add(new ()
{
Coupon = coupon
}); });
userRepo.Update(user);
}
// Create service
return await serviceService.Admin.Create(u, p,
service => { service.RenewAt = DateTime.UtcNow.AddDays(duration); });
} }
} }

View File

@@ -75,7 +75,7 @@ TODO: Add 404 here
if (SelectedCoupon == null) if (SelectedCoupon == null)
actualPrice = defaultPrice; actualPrice = defaultPrice;
else else
actualPrice = Math.Round(defaultPrice * SelectedCoupon.Percent / 100, 2); actualPrice = Math.Round(defaultPrice - (defaultPrice * SelectedCoupon.Percent / 100), 2);
var currency = ConfigService.Get().Store.Currency; var currency = ConfigService.Get().Store.Currency;
} }