Added coupon processing. Fixed price calculation
This commit is contained in:
parent
3d4f22f6f6
commit
2dd1d1f69c
2 changed files with 48 additions and 21 deletions
|
@ -23,9 +23,9 @@ public class StoreOrderService
|
||||||
var userRepo = scope.ServiceProvider.GetRequiredService<Repository<User>>();
|
var userRepo = scope.ServiceProvider.GetRequiredService<Repository<User>>();
|
||||||
var serviceRepo = scope.ServiceProvider.GetRequiredService<Repository<Service>>();
|
var serviceRepo = scope.ServiceProvider.GetRequiredService<Repository<Service>>();
|
||||||
var couponRepo = scope.ServiceProvider.GetRequiredService<Repository<Coupon>>();
|
var couponRepo = scope.ServiceProvider.GetRequiredService<Repository<Coupon>>();
|
||||||
|
|
||||||
// Ensure the values are safe and loaded by using the created scope to bypass the cache
|
// Ensure the values are safe and loaded by using the created scope to bypass the cache
|
||||||
|
|
||||||
var user = userRepo
|
var user = userRepo
|
||||||
.Get()
|
.Get()
|
||||||
.Include(x => x.CouponUses)
|
.Include(x => x.CouponUses)
|
||||||
|
@ -35,11 +35,11 @@ public class StoreOrderService
|
||||||
if (user == null)
|
if (user == 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");
|
||||||
|
|
||||||
|
|
||||||
var product = productRepo
|
var product = productRepo
|
||||||
.Get()
|
.Get()
|
||||||
.FirstOrDefault(x => x.Id == p.Id);
|
.FirstOrDefault(x => x.Id == p.Id);
|
||||||
|
|
||||||
if (product == null)
|
if (product == 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");
|
||||||
|
|
||||||
|
@ -51,23 +51,23 @@ public class StoreOrderService
|
||||||
coupon = couponRepo
|
coupon = couponRepo
|
||||||
.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");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform checks on selected order
|
// Perform checks on selected order
|
||||||
|
|
||||||
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");
|
||||||
|
@ -82,7 +82,7 @@ public class StoreOrderService
|
||||||
|
|
||||||
if (product.Stock < 1)
|
if (product.Stock < 1)
|
||||||
throw new DisplayException("The product is out of stock");
|
throw new DisplayException("The product is out of stock");
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,7 +90,7 @@ public class StoreOrderService
|
||||||
{
|
{
|
||||||
// Validate to ensure we dont process an illegal order
|
// Validate to ensure we dont process an illegal order
|
||||||
await Validate(u, p, durationMultiplier, c);
|
await Validate(u, p, durationMultiplier, c);
|
||||||
|
|
||||||
// Create scope and get required services
|
// Create scope and get required services
|
||||||
using var scope = ServiceScopeFactory.CreateScope();
|
using var scope = ServiceScopeFactory.CreateScope();
|
||||||
var serviceService = scope.ServiceProvider.GetRequiredService<ServiceService>();
|
var serviceService = scope.ServiceProvider.GetRequiredService<ServiceService>();
|
||||||
|
@ -100,18 +100,45 @@ 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;
|
||||||
|
|
||||||
// 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); });
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -236,7 +236,7 @@ TODO: Add 404 here
|
||||||
if (SelectedProduct == null) // Prevent processing null
|
if (SelectedProduct == null) // Prevent processing null
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Process the order with the selected values
|
// Process the order with the selected values
|
||||||
var service = await StoreService
|
var service = await StoreService
|
||||||
.Order
|
.Order
|
||||||
.Process(
|
.Process(
|
||||||
|
|
Loading…
Reference in a new issue