From a81fb39a3235eb7f6e4884e042ccb4eb337731fc Mon Sep 17 00:00:00 2001 From: masoodafar-web Date: Sat, 3 Jan 2026 09:02:09 +0330 Subject: [PATCH] feat(mappings): add InventoryProfile for mapping Inventory entities to Protobuf DTOs --- .../Common/Mappings/InventoryProfile.cs | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/CMSMicroservice.WebApi/Common/Mappings/InventoryProfile.cs diff --git a/src/CMSMicroservice.WebApi/Common/Mappings/InventoryProfile.cs b/src/CMSMicroservice.WebApi/Common/Mappings/InventoryProfile.cs new file mode 100644 index 0000000..2a530b8 --- /dev/null +++ b/src/CMSMicroservice.WebApi/Common/Mappings/InventoryProfile.cs @@ -0,0 +1,63 @@ +using CMSMicroservice.Domain.Entities; +using CMSMicroservice.Protobuf.Protos.Inventory; +using Google.Protobuf.WellKnownTypes; +using ProtoProductType = CMSMicroservice.Protobuf.Protos.Inventory.ProductType; +using DomainProductType = CMSMicroservice.Domain.Enums.ProductType; +using ProtoStockMovementType = CMSMicroservice.Protobuf.Protos.Inventory.StockMovementType; +using DomainStockMovementType = CMSMicroservice.Domain.Enums.StockMovementType; + +namespace CMSMicroservice.WebApi.Common.Mappings; + +/// +/// Mapster profile برای Inventory entities به Protobuf DTOs +/// +public class InventoryProfile : IRegister +{ + void IRegister.Register(TypeAdapterConfig config) + { + // Warehouse Entity به WarehouseDto + config.NewConfig() + .Map(dest => dest.Id, src => src.Id) + .Map(dest => dest.Name, src => src.Name) + .Map(dest => dest.Code, src => src.Code ?? string.Empty) + .Map(dest => dest.Address, src => src.Address ?? string.Empty) + .Map(dest => dest.IsDefault, src => src.IsDefault) + .Map(dest => dest.IsActive, src => src.IsActive); + + // InventoryItem Entity به InventoryItemDto + config.NewConfig() + .Map(dest => dest.Id, src => src.Id) + .Map(dest => dest.WarehouseId, src => src.WarehouseId) + .Map(dest => dest.WarehouseName, src => src.Warehouse != null ? src.Warehouse.Name : string.Empty) + .Map(dest => dest.ProductId, src => src.ProductId.HasValue ? new Int64Value { Value = src.ProductId.Value } : null) + .Map(dest => dest.DiscountProductId, src => src.DiscountProductId.HasValue ? new Int64Value { Value = src.DiscountProductId.Value } : null) + .Map(dest => dest.ProductType, src => (ProtoProductType)src.ProductType) + .Map(dest => dest.Quantity, src => src.Quantity) + .Map(dest => dest.ReservedQuantity, src => src.ReservedQuantity) + .Map(dest => dest.AvailableQuantity, src => src.AvailableQuantity) + .Map(dest => dest.LowStockThreshold, src => src.LowStockThreshold) + .Map(dest => dest.ReorderPoint, src => src.ReorderPoint) + .Map(dest => dest.MaxStockLevel, src => src.MaxStockLevel) + .Map(dest => dest.LastRestockedAt, src => src.LastRestockedAt.HasValue ? Timestamp.FromDateTime(src.LastRestockedAt.Value.ToUniversalTime()) : null) + .Map(dest => dest.LastSoldAt, src => src.LastSoldAt.HasValue ? Timestamp.FromDateTime(src.LastSoldAt.Value.ToUniversalTime()) : null) + .Map(dest => dest.ProductTitle, src => src.Product != null ? src.Product.Title : (src.DiscountProduct != null ? src.DiscountProduct.Title : string.Empty)) + .Map(dest => dest.ProductPrice, src => src.Product != null ? src.Product.Price : (src.DiscountProduct != null ? src.DiscountProduct.Price : 0)) + .Map(dest => dest.Created, src => Timestamp.FromDateTime(src.Created.ToUniversalTime())); + + // StockMovement Entity به StockMovementDto + config.NewConfig() + .Map(dest => dest.Id, src => src.Id) + .Map(dest => dest.InventoryItemId, src => src.InventoryItemId) + .Map(dest => dest.MovementType, src => (ProtoStockMovementType)src.MovementType) + .Map(dest => dest.Quantity, src => src.Quantity) + .Map(dest => dest.QuantityBefore, src => src.QuantityBefore) + .Map(dest => dest.QuantityAfter, src => src.QuantityAfter) + .Map(dest => dest.Note, src => src.Note ?? string.Empty) + .Map(dest => dest.ReferenceNumber, src => src.ReferenceNumber ?? string.Empty) + .Map(dest => dest.OrderId, src => src.OrderId.HasValue ? new Int64Value { Value = src.OrderId.Value } : null) + .Map(dest => dest.DiscountOrderId, src => src.DiscountOrderId.HasValue ? new Int64Value { Value = src.DiscountOrderId.Value } : null) + .Map(dest => dest.PerformedByUserId, src => src.PerformedByUserId.HasValue ? new Int64Value { Value = src.PerformedByUserId.Value } : null) + .Map(dest => dest.Created, src => Timestamp.FromDateTime(src.Created.ToUniversalTime())) + .Map(dest => dest.ProductTitle, src => src.InventoryItem != null && src.InventoryItem.Product != null ? src.InventoryItem.Product.Title : (src.InventoryItem != null && src.InventoryItem.DiscountProduct != null ? src.InventoryItem.DiscountProduct.Title : string.Empty)); + } +}