Implement Inventory Management Service with CRUD operations for warehouses and inventory items, stock operations, and bulk processing capabilities.
Build and Deploy to Kubernetes / build-and-deploy (push) Successful in 1m48s

This commit is contained in:
masoodafar-web
2026-01-02 00:45:37 +03:30
parent f0117eb1d5
commit 1cf501711f
50 changed files with 10699 additions and 101 deletions
@@ -8,10 +8,14 @@ namespace CMSMicroservice.Application.DiscountShopCQ.Commands.CompleteOrderPayme
public class CompleteOrderPaymentCommandHandler : IRequestHandler<CompleteOrderPaymentCommand, CompleteOrderPaymentResponseDto>
{
private readonly IApplicationDbContext _context;
private readonly IInventoryService _inventoryService;
public CompleteOrderPaymentCommandHandler(IApplicationDbContext context)
public CompleteOrderPaymentCommandHandler(
IApplicationDbContext context,
IInventoryService inventoryService)
{
_context = context;
_inventoryService = inventoryService;
}
public async Task<CompleteOrderPaymentResponseDto> Handle(CompleteOrderPaymentCommand request, CancellationToken cancellationToken)
@@ -63,12 +67,18 @@ public class CompleteOrderPaymentCommandHandler : IRequestHandler<CompleteOrderP
userWallet.DiscountBalance -= order.DiscountBalanceUsed;
}
// Update product stock and sale count
// تایید فروش و کسر موجودی از طریق InventoryService
foreach (var orderDetail in order.OrderDetails)
{
var product = orderDetail.Product;
product.RemainingCount -= orderDetail.Count;
product.SaleCount += orderDetail.Count;
await _inventoryService.ConfirmSaleAsync(
orderDetail.ProductId,
ProductType.DiscountProduct,
orderDetail.Count,
order.Id,
cancellationToken);
// افزایش تعداد فروش
orderDetail.Product.SaleCount += orderDetail.Count;
}
await _context.SaveChangesAsync(cancellationToken);
@@ -82,7 +92,17 @@ public class CompleteOrderPaymentCommandHandler : IRequestHandler<CompleteOrderP
}
else
{
// Payment failed
// Payment failed - آزادسازی رزرو
foreach (var orderDetail in order.OrderDetails)
{
await _inventoryService.ReleaseReservationAsync(
orderDetail.ProductId,
ProductType.DiscountProduct,
orderDetail.Count,
order.Id,
cancellationToken);
}
transaction.PaymentStatus = PaymentStatus.Reject;
order.PaymentStatus = PaymentStatus.Reject;
@@ -1,5 +1,6 @@
using CMSMicroservice.Application.Common.Interfaces;
using CMSMicroservice.Domain.Entities.DiscountShop;
using CMSMicroservice.Domain.Enums;
using MediatR;
using Microsoft.EntityFrameworkCore;
@@ -8,10 +9,14 @@ namespace CMSMicroservice.Application.DiscountShopCQ.Commands.CreateDiscountProd
public class CreateDiscountProductCommandHandler : IRequestHandler<CreateDiscountProductCommand, long>
{
private readonly IApplicationDbContext _context;
private readonly IInventoryService _inventoryService;
public CreateDiscountProductCommandHandler(IApplicationDbContext context)
public CreateDiscountProductCommandHandler(
IApplicationDbContext context,
IInventoryService inventoryService)
{
_context = context;
_inventoryService = inventoryService;
}
public async Task<long> Handle(CreateDiscountProductCommand request, CancellationToken cancellationToken)
@@ -35,6 +40,13 @@ public class CreateDiscountProductCommandHandler : IRequestHandler<CreateDiscoun
_context.DiscountProducts.Add(product);
await _context.SaveChangesAsync(cancellationToken);
// ایجاد رکورد موجودی در سیستم انبارداری
await _inventoryService.InitializeInventoryAsync(
product.Id,
ProductType.DiscountProduct,
request.RemainingCount,
ct: cancellationToken);
// Add product categories
if (request.CategoryIds.Any())
{
@@ -11,10 +11,14 @@ namespace CMSMicroservice.Application.DiscountShopCQ.Commands.PlaceOrder;
public class PlaceOrderCommandHandler : IRequestHandler<PlaceOrderCommand, PlaceOrderResponseDto>
{
private readonly IApplicationDbContext _context;
private readonly IInventoryService _inventoryService;
public PlaceOrderCommandHandler(IApplicationDbContext context)
public PlaceOrderCommandHandler(
IApplicationDbContext context,
IInventoryService inventoryService)
{
_context = context;
_inventoryService = inventoryService;
}
public async Task<PlaceOrderResponseDto> Handle(PlaceOrderCommand request, CancellationToken cancellationToken)
@@ -152,6 +156,17 @@ public class PlaceOrderCommandHandler : IRequestHandler<PlaceOrderCommand, Place
_context.DiscountOrderDetails.AddRange(orderDetails);
// رزرو موجودی برای سفارش pending
foreach (var cartItem in cartItems)
{
await _inventoryService.ReserveStockAsync(
cartItem.ProductId,
ProductType.DiscountProduct,
cartItem.Count,
order.Id,
cancellationToken);
}
// Clear cart
_context.DiscountShoppingCarts.RemoveRange(cartItems);