using BackOffice.BFF.Application.Common.Models; using BackOffice.BFF.Application.InventoryCQ.Commands.AddStock; using BackOffice.BFF.Application.InventoryCQ.Commands.AdjustStock; using BackOffice.BFF.Application.InventoryCQ.Commands.CreateWarehouse; using BackOffice.BFF.Application.InventoryCQ.Commands.RecordLoss; using BackOffice.BFF.Application.InventoryCQ.Commands.UpdateInventorySettings; using BackOffice.BFF.Application.InventoryCQ.Queries.GetAllInventoryItems; using BackOffice.BFF.Application.InventoryCQ.Queries.GetAllWarehouses; using BackOffice.BFF.Application.InventoryCQ.Queries.GetLowStockItems; using BackOffice.BFF.Application.InventoryCQ.Queries.GetStockMovements; using Google.Protobuf.WellKnownTypes; using BffProtos = Foursat.BackOffice.BFF.Inventory.Protos; namespace BackOffice.BFF.WebApi.Common.Mappings; public class InventoryProfile : IRegister { void IRegister.Register(TypeAdapterConfig config) { // ============================================= // GetAllWarehouses // ============================================= config.NewConfig() .MapWith(src => new GetAllWarehousesQuery { IsActive = src.IsActive != null ? src.IsActive.Value : null }); config.NewConfig() .MapWith(src => new BffProtos.GetAllWarehousesResponse { TotalCount = src.TotalCount, Warehouses = { src.Warehouses.Select(w => new BffProtos.WarehouseDto { Id = w.Id, Name = w.Name ?? string.Empty, Code = w.Code ?? string.Empty, Address = w.Address ?? string.Empty, IsDefault = w.IsDefault, IsActive = w.IsActive, Created = w.Created.HasValue ? Timestamp.FromDateTime(DateTime.SpecifyKind(w.Created.Value, DateTimeKind.Utc)) : null, LastModified = w.LastModified.HasValue ? Timestamp.FromDateTime(DateTime.SpecifyKind(w.LastModified.Value, DateTimeKind.Utc)) : null }) } }); // ============================================= // CreateWarehouse // ============================================= config.NewConfig() .MapWith(src => new CreateWarehouseCommand { Name = src.Name, Code = src.Code, Address = src.Address, IsDefault = src.IsDefault, IsActive = true }); config.NewConfig() .MapWith(src => new BffProtos.CreateWarehouseResponse { Id = src }); // ============================================= // GetAllInventoryItems // ============================================= config.NewConfig() .MapWith(src => new GetAllInventoryItemsQuery { Page = src.Page > 0 ? src.Page : 1, PageSize = src.PageSize > 0 ? src.PageSize : 20, WarehouseId = src.WarehouseId, ProductType = src.ProductType != BffProtos.ProductType.Unspecified ? (int)src.ProductType : null, Search = string.IsNullOrWhiteSpace(src.Search) ? null : src.Search, SortBy = string.IsNullOrWhiteSpace(src.SortBy) ? null : src.SortBy, SortDesc = src.SortDesc }); config.NewConfig() .MapWith(src => new BffProtos.GetAllInventoryItemsResponse { TotalCount = src.TotalCount, MetaData = new BackOffice.BFF.Protobuf.Common.MetaData { CurrentPage = src.MetaData.CurrentPage, PageSize = src.MetaData.PageSize, TotalCount = src.MetaData.TotalCount, TotalPage = src.MetaData.TotalPage }, Items = { src.Items.Select(i => new BffProtos.InventoryItemDto { Id = i.Id, ProductId = i.ProductId.HasValue ? i.ProductId.Value : null, DiscountProductId = i.DiscountProductId.HasValue ? i.DiscountProductId.Value : null, ProductType = (BffProtos.ProductType)i.ProductType, ProductTitle = i.ProductTitle ?? string.Empty, ProductPrice = i.ProductPrice, Quantity = i.Quantity, ReservedQuantity = i.ReservedQuantity, AvailableQuantity = i.AvailableQuantity, LowStockThreshold = i.LowStockThreshold, ReorderPoint = i.ReorderPoint, MaxStockLevel = i.MaxStockLevel, LastRestockedAt = i.LastRestockedAt.HasValue ? Timestamp.FromDateTime(DateTime.SpecifyKind(i.LastRestockedAt.Value, DateTimeKind.Utc)) : null, LastSoldAt = i.LastSoldAt.HasValue ? Timestamp.FromDateTime(DateTime.SpecifyKind(i.LastSoldAt.Value, DateTimeKind.Utc)) : null, WarehouseId = i.WarehouseId, WarehouseName = i.WarehouseName ?? string.Empty, IsLowStock = i.IsLowStock, Created = i.Created.HasValue ? Timestamp.FromDateTime(DateTime.SpecifyKind(i.Created.Value, DateTimeKind.Utc)) : null }) } }); // ============================================= // GetLowStockItems // ============================================= config.NewConfig() .MapWith(src => new GetLowStockItemsQuery { Page = src.Page > 0 ? src.Page : 1, PageSize = src.PageSize > 0 ? src.PageSize : 20, WarehouseId = src.WarehouseId, ProductType = src.ProductType != BffProtos.ProductType.Unspecified ? (int)src.ProductType : null }); config.NewConfig() .MapWith(src => new BffProtos.GetLowStockItemsResponse { TotalCount = src.TotalCount, Items = { src.Items.Select(i => new BffProtos.InventoryItemDto { Id = i.Id, ProductId = i.ProductId.HasValue ? i.ProductId.Value : null, DiscountProductId = i.DiscountProductId.HasValue ? i.DiscountProductId.Value : null, ProductType = (BffProtos.ProductType)i.ProductType, ProductTitle = i.ProductTitle ?? string.Empty, ProductPrice = i.ProductPrice, Quantity = i.Quantity, ReservedQuantity = i.ReservedQuantity, AvailableQuantity = i.AvailableQuantity, LowStockThreshold = i.LowStockThreshold, ReorderPoint = i.ReorderPoint, MaxStockLevel = i.MaxStockLevel, WarehouseId = i.WarehouseId, WarehouseName = i.WarehouseName ?? string.Empty, IsLowStock = i.IsLowStock }) } }); // ============================================= // GetStockMovements // ============================================= config.NewConfig() .MapWith(src => new GetStockMovementsQuery { Page = src.Page > 0 ? src.Page : 1, PageSize = src.PageSize > 0 ? src.PageSize : 20, InventoryItemId = src.InventoryItemId, ProductId = src.ProductId, ProductType = src.ProductType != BffProtos.ProductType.Unspecified ? (int)src.ProductType : null, MovementType = src.MovementType != BffProtos.StockMovementType.MovementTypeUnspecified ? (int)src.MovementType : null, FromDate = src.FromDate != null ? src.FromDate.ToDateTime() : null, ToDate = src.ToDate != null ? src.ToDate.ToDateTime() : null }); config.NewConfig() .MapWith(src => new BffProtos.GetStockMovementsResponse { TotalCount = src.TotalCount, MetaData = new BackOffice.BFF.Protobuf.Common.MetaData { CurrentPage = src.MetaData.CurrentPage, PageSize = src.MetaData.PageSize, TotalCount = src.MetaData.TotalCount, TotalPage = src.MetaData.TotalPage }, Movements = { src.Movements.Select(m => new BffProtos.StockMovementDto { Id = m.Id, InventoryItemId = m.InventoryItemId, MovementType = (BffProtos.StockMovementType)m.MovementType, Quantity = m.Quantity, QuantityBefore = m.QuantityBefore, QuantityAfter = m.QuantityAfter, OrderId = m.OrderId.HasValue ? m.OrderId.Value : null, DiscountOrderId = m.DiscountOrderId.HasValue ? m.DiscountOrderId.Value : null, ReferenceNumber = m.ReferenceNumber ?? string.Empty, Note = m.Note ?? string.Empty, PerformedByUserId = m.PerformedByUserId.HasValue ? m.PerformedByUserId.Value : null, Created = Timestamp.FromDateTime(DateTime.SpecifyKind(m.Created, DateTimeKind.Utc)), ProductTitle = m.ProductTitle ?? string.Empty }) } }); // ============================================= // AddStock // ============================================= config.NewConfig() .MapWith(src => new AddStockCommand { ProductId = src.ProductId, ProductType = (int)src.ProductType, Quantity = src.Quantity, ReferenceNumber = src.ReferenceNumber, Note = src.Note, WarehouseId = src.WarehouseId }); config.NewConfig() .MapWith(src => new BffProtos.AddStockResponse { InventoryItemId = src.InventoryItemId, NewQuantity = src.NewQuantity }); // ============================================= // AdjustStock // ============================================= config.NewConfig() .MapWith(src => new AdjustStockCommand { ProductId = src.ProductId, ProductType = (int)src.ProductType, NewQuantity = src.NewQuantity, Reason = src.Reason, ReferenceNumber = src.ReferenceNumber }); config.NewConfig() .MapWith(src => new BffProtos.AdjustStockResponse { PreviousQuantity = src.PreviousQuantity, NewQuantity = src.NewQuantity, Difference = src.Difference }); // ============================================= // RecordLoss // ============================================= config.NewConfig() .MapWith(src => new RecordLossCommand { ProductId = src.ProductId, ProductType = (int)src.ProductType, Quantity = src.Quantity, LossType = (int)src.LossType, Reason = src.Reason, ReferenceNumber = src.ReferenceNumber }); // ============================================= // UpdateInventorySettings // ============================================= config.NewConfig() .MapWith(src => new UpdateInventorySettingsCommand { Id = src.Id, LowStockThreshold = src.LowStockThreshold, ReorderPoint = src.ReorderPoint, MaxStockLevel = src.MaxStockLevel }); } }