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
Build and Deploy to Kubernetes / build-and-deploy (push) Successful in 1m48s
This commit is contained in:
+21
-2
@@ -1,3 +1,4 @@
|
||||
using CMSMicroservice.Application.Common.Interfaces;
|
||||
using CMSMicroservice.Domain.Events;
|
||||
using CMSMicroservice.Domain.Enums;
|
||||
|
||||
@@ -6,17 +7,22 @@ namespace CMSMicroservice.Application.UserOrderCQ.Commands.CancelOrder;
|
||||
public class CancelOrderCommandHandler : IRequestHandler<CancelOrderCommand, CancelOrderResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly IInventoryService _inventoryService;
|
||||
|
||||
public CancelOrderCommandHandler(IApplicationDbContext context)
|
||||
public CancelOrderCommandHandler(
|
||||
IApplicationDbContext context,
|
||||
IInventoryService inventoryService)
|
||||
{
|
||||
_context = context;
|
||||
_inventoryService = inventoryService;
|
||||
}
|
||||
|
||||
public async Task<CancelOrderResponseDto> Handle(CancelOrderCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// پیدا کردن سفارش
|
||||
// پیدا کردن سفارش با جزئیات
|
||||
var order = await _context.UserOrders
|
||||
.Include(o => o.Transaction)
|
||||
.Include(o => o.FactorDetails)
|
||||
.FirstOrDefaultAsync(o => o.Id == request.OrderId, cancellationToken);
|
||||
|
||||
if (order == null)
|
||||
@@ -35,6 +41,19 @@ public class CancelOrderCommandHandler : IRequestHandler<CancelOrderCommand, Can
|
||||
throw new InvalidOperationException("این سفارش قبلاً لغو شده است");
|
||||
}
|
||||
|
||||
// برگشت موجودی محصولات به انبار
|
||||
foreach (var factorDetail in order.FactorDetails)
|
||||
{
|
||||
await _inventoryService.ProcessReturnAsync(
|
||||
factorDetail.ProductId,
|
||||
ProductType.RegularProduct,
|
||||
factorDetail.Count,
|
||||
order.Id,
|
||||
$"لغو سفارش: {request.CancelReason}",
|
||||
null,
|
||||
cancellationToken);
|
||||
}
|
||||
|
||||
// تغییر وضعیت سفارش
|
||||
order.DeliveryStatus = DeliveryStatus.Cancelled;
|
||||
order.DeliveryDescription = $"لغو شده: {request.CancelReason}";
|
||||
|
||||
+14
-2
@@ -1,3 +1,4 @@
|
||||
using CMSMicroservice.Application.Common.Interfaces;
|
||||
using CMSMicroservice.Domain.Common;
|
||||
using CMSMicroservice.Domain.Enums;
|
||||
using CMSMicroservice.Domain.Events;
|
||||
@@ -10,14 +11,17 @@ public class
|
||||
SubmitShopBuyOrderCommandHandler : IRequestHandler<SubmitShopBuyOrderCommand, SubmitShopBuyOrderResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly IInventoryService _inventoryService;
|
||||
private readonly ILogger<SubmitShopBuyOrderCommandHandler> _logger;
|
||||
private float _vatRate;
|
||||
|
||||
public SubmitShopBuyOrderCommandHandler(
|
||||
IApplicationDbContext context,
|
||||
IInventoryService inventoryService,
|
||||
ILogger<SubmitShopBuyOrderCommandHandler> logger)
|
||||
{
|
||||
_context = context;
|
||||
_inventoryService = inventoryService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
@@ -148,10 +152,18 @@ public class
|
||||
});
|
||||
await _context.FactorDetails.AddRangeAsync(factorDetailsList, cancellationToken);
|
||||
|
||||
// کاهش موجودی محصولات و افزایش تعداد فروش
|
||||
// کاهش موجودی محصولات و افزایش تعداد فروش از طریق InventoryService
|
||||
foreach (var cartItem in user.UserCarts)
|
||||
{
|
||||
cartItem.Product.RemainingCount -= cartItem.Count;
|
||||
// استفاده از سرویس انبارداری برای کسر موجودی
|
||||
await _inventoryService.ConfirmSaleAsync(
|
||||
cartItem.ProductId,
|
||||
ProductType.RegularProduct,
|
||||
cartItem.Count,
|
||||
newOrder.Id,
|
||||
cancellationToken);
|
||||
|
||||
// افزایش تعداد فروش
|
||||
cartItem.Product.SaleCount += cartItem.Count;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user