From 8c3d710253cfe5703fd39165715b62828db0a0f6 Mon Sep 17 00:00:00 2001 From: masoodafar-web Date: Sat, 3 Jan 2026 10:05:47 +0330 Subject: [PATCH] feat(mappings): expand InventoryProfile with comprehensive CQRS mappings for warehouses and inventory items --- .../Common/Mappings/InventoryProfile.cs | 186 +++++++++++++++++- 1 file changed, 185 insertions(+), 1 deletion(-) diff --git a/src/CMSMicroservice.WebApi/Common/Mappings/InventoryProfile.cs b/src/CMSMicroservice.WebApi/Common/Mappings/InventoryProfile.cs index 2a530b8..9bcabe6 100644 --- a/src/CMSMicroservice.WebApi/Common/Mappings/InventoryProfile.cs +++ b/src/CMSMicroservice.WebApi/Common/Mappings/InventoryProfile.cs @@ -1,5 +1,11 @@ using CMSMicroservice.Domain.Entities; using CMSMicroservice.Protobuf.Protos.Inventory; +using CMSMicroservice.Application.Features.Warehouses.Commands; +using CMSMicroservice.Application.Features.Warehouses.Queries; +using CMSMicroservice.Application.Features.InventoryItems.Commands; +using CMSMicroservice.Application.Features.InventoryItems.Queries; +using CMSMicroservice.Application.Features.StockMovements.Commands; +using CMSMicroservice.Application.Features.StockMovements.Queries; using Google.Protobuf.WellKnownTypes; using ProtoProductType = CMSMicroservice.Protobuf.Protos.Inventory.ProductType; using DomainProductType = CMSMicroservice.Domain.Enums.ProductType; @@ -9,12 +15,190 @@ using DomainStockMovementType = CMSMicroservice.Domain.Enums.StockMovementType; namespace CMSMicroservice.WebApi.Common.Mappings; /// -/// Mapster profile برای Inventory entities به Protobuf DTOs +/// Mapster profile برای Inventory Protobuf به CQRS و Entities به DTOs /// public class InventoryProfile : IRegister { void IRegister.Register(TypeAdapterConfig config) { + // ============================================= + // Protobuf Request به CQRS Query/Command Mappings + // ============================================= + + // Warehouse Commands + config.NewConfig() + .MapWith(src => new CreateWarehouseCommand + { + Name = src.Name, + Code = src.Code, + Address = src.Address, + IsDefault = src.IsDefault + }); + + config.NewConfig() + .MapWith(src => new UpdateWarehouseCommand + { + Id = src.Id, + Name = src.Name, + Code = src.Code, + Address = src.Address, + IsActive = src.IsActive + }); + + config.NewConfig() + .MapWith(src => new DeleteWarehouseCommand(src.Id)); + + config.NewConfig() + .MapWith(src => new SetDefaultWarehouseCommand(src.Id)); + + // Warehouse Queries + config.NewConfig() + .MapWith(src => new GetWarehouseByIdQuery(src.Id)); + + config.NewConfig() + .MapWith(src => new GetAllWarehousesQuery()); + + // Inventory Item Queries + config.NewConfig() + .MapWith(src => new GetInventoryItemByIdQuery(src.Id)); + + config.NewConfig() + .MapWith(src => new GetInventoryItemByProductIdQuery(src.ProductId, null)); + + config.NewConfig() + .MapWith(src => new GetInventoryItemByDiscountProductIdQuery(src.ProductId, null)); + + config.NewConfig() + .MapWith(src => new SearchInventoryItemsQuery( + null, // SearchTerm + null, // ProductType + null, // WarehouseId + null, // MinQuantity + null, // MaxQuantity + 0, // Skip + 50 // Take + )); + + config.NewConfig() + .MapWith(src => new GetLowStockItemsQuery(null, 1)); + + // Inventory Item Commands + config.NewConfig() + .MapWith(src => new UpdateInventoryItemCommand + { + Id = src.Id, + LowStockThreshold = src.LowStockThreshold, + ReorderPoint = src.ReorderPoint, + MaxStockLevel = src.MaxStockLevel + }); + + // Stock Operation Commands + config.NewConfig() + .MapWith(src => new IncreaseInventoryCommand + { + ProductId = src.ProductId, + ProductType = (DomainProductType)src.ProductType, + Quantity = src.Quantity, + Note = src.Note, + ReferenceNumber = src.ReferenceNumber, + WarehouseId = src.WarehouseId?.Value ?? 1 + }); + + config.NewConfig() + .MapWith(src => new UpdateInventoryQuantityCommand + { + ProductId = src.ProductId, + ProductType = (DomainProductType)src.ProductType, + NewQuantity = src.NewQuantity, + Note = src.Note, + ReferenceNumber = src.ReferenceNumber, + WarehouseId = src.WarehouseId?.Value ?? 1 + }); + + config.NewConfig() + .MapWith(src => new ReserveInventoryCommand + { + ProductId = src.ProductId, + ProductType = (DomainProductType)src.ProductType, + Quantity = src.Quantity, + Note = src.Note, + ReferenceNumber = src.ReferenceNumber, + OrderId = src.OrderId?.Value, + WarehouseId = src.WarehouseId?.Value ?? 1 + }); + + config.NewConfig() + .MapWith(src => new ReleaseReservedInventoryCommand + { + ProductId = src.ProductId, + ProductType = (DomainProductType)src.ProductType, + Quantity = src.Quantity, + Note = src.Note, + ReferenceNumber = src.ReferenceNumber, + WarehouseId = src.WarehouseId?.Value ?? 1 + }); + + config.NewConfig() + .MapWith(src => new ReduceInventoryCommand + { + ProductId = src.ProductId, + ProductType = (DomainProductType)src.ProductType, + Quantity = src.Quantity, + Note = src.Note, + ReferenceNumber = src.ReferenceNumber, + OrderId = src.OrderId?.Value, + WarehouseId = src.WarehouseId?.Value ?? 1 + }); + + config.NewConfig() + .MapWith(src => new IncreaseInventoryCommand + { + ProductId = src.ProductId, + ProductType = (DomainProductType)src.ProductType, + Quantity = src.Quantity, + Note = !string.IsNullOrEmpty(src.Note) ? src.Note : "Product return", + ReferenceNumber = src.ReferenceNumber, + OrderId = src.OrderId?.Value, + WarehouseId = src.WarehouseId?.Value ?? 1 + }); + + config.NewConfig() + .MapWith(src => new CreateStockMovementCommand + { + ProductId = src.ProductId, + ProductType = (DomainProductType)src.ProductType, + MovementType = DomainStockMovementType.Loss, + Quantity = src.Quantity, + Note = !string.IsNullOrEmpty(src.Note) ? src.Note : "Stock loss/damage", + ReferenceNumber = src.ReferenceNumber, + WarehouseId = src.WarehouseId?.Value ?? 1 + }); + + // Stock Movement Queries + config.NewConfig() + .MapWith(src => new SearchStockMovementsQuery + { + InventoryItemId = src.InventoryItemId?.Value, + ProductId = src.ProductId?.Value, + ProductType = src.ProductType != ProtoProductType.Unspecified ? (DomainProductType?)src.ProductType : null, + MovementType = src.MovementType != ProtoStockMovementType.Unspecified ? (DomainStockMovementType?)src.MovementType : null, + StartDate = src.StartDate?.ToDateTime(), + EndDate = src.EndDate?.ToDateTime(), + Skip = src.Skip, + Take = src.Take > 0 ? src.Take : 50 + }); + + config.NewConfig() + .MapWith(src => new GetInventoryItemMovementHistoryQuery + { + InventoryItemId = src.InventoryItemId, + Skip = (src.Page - 1) * src.PageSize, + Take = src.PageSize > 0 ? src.PageSize : 50 + }); + + // ============================================= + // Entity به Protobuf DTO Mappings + // ============================================= // Warehouse Entity به WarehouseDto config.NewConfig() .Map(dest => dest.Id, src => src.Id)