Files
BackOffice.BFF/src/BackOffice.BFF.Application/InventoryCQ/Queries/GetStockMovements/GetStockMovementsQueryHandler.cs
T
masoodafar-web f6f943947c
Build and Deploy / build (push) Successful in 1m46s
feat: Implement Inventory Management Commands and Queries
- Added CreateWarehouseCommand and CreateWarehouseCommandHandler for warehouse creation.
- Introduced RecordLossCommand and RecordLossCommandHandler to handle stock loss recording.
- Created UpdateInventorySettingsCommand and its handler for updating inventory settings.
- Implemented GetAllInventoryItemsQuery and its handler to retrieve inventory items with pagination.
- Added GetAllWarehousesQuery and handler for fetching all warehouses.
- Developed GetLowStockItemsQuery and handler to get items below a specified stock threshold.
- Implemented GetStockMovementsQuery and handler for retrieving stock movement records.
- Created mappings for inventory-related requests and responses in InventoryProfile.
- Developed InventoryService to handle gRPC requests for inventory operations.
- Added Protobuf definitions for inventory management services and messages.
2026-01-03 07:37:32 +03:30

92 lines
3.9 KiB
C#

using CMSMicroservice.Protobuf.Protos.Inventory;
using Google.Protobuf.WellKnownTypes;
namespace BackOffice.BFF.Application.InventoryCQ.Queries.GetStockMovements;
public class GetStockMovementsQueryHandler : IRequestHandler<GetStockMovementsQuery, GetStockMovementsResponseDto>
{
private readonly IApplicationContractContext _context;
public GetStockMovementsQueryHandler(IApplicationContractContext context)
{
_context = context;
}
public async Task<GetStockMovementsResponseDto> Handle(GetStockMovementsQuery request, CancellationToken cancellationToken)
{
var protoRequest = new GetStockMovementsRequest
{
Page = request.PageIndex,
PageSize = request.PageSize
};
if (request.InventoryItemId.HasValue)
protoRequest.InventoryItemId = request.InventoryItemId.Value;
if (request.ProductId.HasValue)
protoRequest.ProductId = request.ProductId.Value;
if (request.ProductType.HasValue)
protoRequest.ProductType = (ProductType)request.ProductType.Value;
if (request.MovementType.HasValue)
protoRequest.MovementType = (StockMovementType)request.MovementType.Value;
if (request.FromDate.HasValue)
protoRequest.FromDate = Timestamp.FromDateTime(request.FromDate.Value.ToUniversalTime());
if (request.ToDate.HasValue)
protoRequest.ToDate = Timestamp.FromDateTime(request.ToDate.Value.ToUniversalTime());
var response = await _context.Inventory.GetStockMovementsAsync(protoRequest, cancellationToken: cancellationToken);
return new GetStockMovementsResponseDto
{
TotalCount = response.TotalCount,
MetaData = new MetaData
{
TotalCount = response.TotalCount,
PageSize = request.PageSize,
CurrentPage = request.PageIndex,
TotalPage = (int)Math.Ceiling((double)response.TotalCount / request.PageSize)
},
Movements = response.Movements.Select(m => new StockMovementDto
{
Id = m.Id,
InventoryItemId = m.InventoryItemId,
ProductName = m.ProductTitle,
MovementType = (int)m.MovementType,
MovementTypeName = GetMovementTypeName(m.MovementType),
Quantity = m.Quantity,
QuantityBefore = m.QuantityBefore,
QuantityAfter = m.QuantityAfter,
OrderId = m.OrderId,
DiscountOrderId = m.DiscountOrderId,
ReferenceNumber = string.IsNullOrEmpty(m.ReferenceNumber) ? null : m.ReferenceNumber,
Note = string.IsNullOrEmpty(m.Note) ? null : m.Note,
PerformedByUserId = m.PerformedByUserId,
PerformedByUserName = null,
CreatedAt = m.Created?.ToDateTime() ?? DateTime.MinValue
}).ToList()
};
}
private static string GetMovementTypeName(StockMovementType type) => type switch
{
StockMovementType.InitialStock => "موجودی اولیه",
StockMovementType.Restock => "ورود کالا",
StockMovementType.Return => "برگشت از مشتری",
StockMovementType.Sale => "فروش",
StockMovementType.AdjustmentIncrease => "تعدیل افزایشی",
StockMovementType.AdjustmentDecrease => "تعدیل کاهشی",
StockMovementType.Reserved => "رزرو",
StockMovementType.Released => "آزادسازی رزرو",
StockMovementType.Loss => "مفقودی",
StockMovementType.Damaged => "ضایعات",
StockMovementType.Expired => "منقضی",
StockMovementType.TransferOut => "انتقال به انبار",
StockMovementType.TransferIn => "انتقال از انبار",
_ => "نامشخص"
};
}