Merge branch 'kub-stage' into production
Build and Deploy to Production / build-and-deploy (push) Successful in 19m44s
Build and Deploy to Production / build-and-deploy (push) Successful in 19m44s
This commit is contained in:
@@ -29,12 +29,14 @@ public class UserOrderService : UserOrderContract.UserOrderContractBase
|
||||
private readonly ISender _sender;
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
private readonly IInventoryService _inventoryService;
|
||||
|
||||
public UserOrderService(ISender sender, IApplicationDbContext context, ICurrentUserService currentUserService)
|
||||
public UserOrderService(ISender sender, IApplicationDbContext context, ICurrentUserService currentUserService, IInventoryService inventoryService)
|
||||
{
|
||||
_sender = sender;
|
||||
_context = context;
|
||||
_currentUserService = currentUserService;
|
||||
_inventoryService = inventoryService;
|
||||
}
|
||||
[RequiresPermission(PermissionNames.OrdersCreate)]
|
||||
public override async Task<CreateNewUserOrderResponse> CreateNewUserOrder(CreateNewUserOrderRequest request, ServerCallContext context)
|
||||
@@ -285,6 +287,18 @@ public class UserOrderService : UserOrderContract.UserOrderContractBase
|
||||
throw new RpcException(new Status(StatusCode.InvalidArgument,
|
||||
$"مبلغ نامعتبر است. محاسبه شده: {totalAmount}, دریافتی: {request.TotalAmount}"));
|
||||
}
|
||||
|
||||
// Validate stock availability for all cart items before any financial operation
|
||||
foreach (var cartItem in cartItems)
|
||||
{
|
||||
var available = await _inventoryService.GetAvailableQuantityAsync(
|
||||
cartItem.ProductId, ProductType.RegularProduct, ct: context.CancellationToken);
|
||||
if (available < cartItem.Count)
|
||||
{
|
||||
throw new RpcException(new Status(StatusCode.FailedPrecondition,
|
||||
$"موجودی «{cartItem.Product.Title}» کافی نیست. موجود: {available}، درخواستی: {cartItem.Count}"));
|
||||
}
|
||||
}
|
||||
|
||||
// Get user's wallet
|
||||
var wallet = await _context.UserWallets
|
||||
@@ -338,7 +352,7 @@ public class UserOrderService : UserOrderContract.UserOrderContractBase
|
||||
_context.UserWalletHistories.Add(walletLog);
|
||||
|
||||
// ═══ Magic Wallet: Entry / Exit Trigger ═══
|
||||
// Q24: آستانه <= 1,000,000 ریال به جای == 0 (چون قیمت محصولات متفاوت است)
|
||||
// Q24: آستانه <= 1,00,000 تومان به جای == 0 (چون قیمت محصولات متفاوت است)
|
||||
if (wallet.Balance <= SystemConstants.MagicWalletEntryThreshold)
|
||||
{
|
||||
var user = await _context.Users
|
||||
@@ -461,6 +475,18 @@ public class UserOrderService : UserOrderContract.UserOrderContractBase
|
||||
cartItem.IsDeleted = true;
|
||||
}
|
||||
|
||||
await _context.SaveChangesAsync(context.CancellationToken);
|
||||
|
||||
// Deduct inventory and update SaleCount for each purchased item
|
||||
foreach (var cartItem in cartItems)
|
||||
{
|
||||
await _inventoryService.ConfirmSaleAsync(
|
||||
cartItem.ProductId, ProductType.RegularProduct,
|
||||
cartItem.Count, order.Id, context.CancellationToken);
|
||||
|
||||
cartItem.Product.SaleCount += cartItem.Count;
|
||||
}
|
||||
|
||||
await _context.SaveChangesAsync(context.CancellationToken);
|
||||
|
||||
return new SubmitShopBuyOrderResponse
|
||||
|
||||
Reference in New Issue
Block a user