Files
BackOffice.BFF/src/BackOffice.BFF.WebApi/Common/Mappings/InventoryProfile.cs
T
masoodafar-web f027fd0b06
Build and Deploy / build (push) Failing after 2m12s
Refactor inventory commands and queries for improved clarity and consistency
- Updated ProductType enum values to be more descriptive (RegularProduct, DiscountProduct).
- Removed unnecessary Success and Message properties from response DTOs in AddStock and AdjustStock commands.
- Renamed Note property to Reason in AdjustStock and RecordLoss commands, and added ReferenceNumber.
- Changed InventoryItemId to Id in UpdateInventorySettingsCommand for consistency.
- Updated pagination properties in various queries (PageIndex to Page).
- Enhanced GetAllInventoryItemsQuery and GetLowStockItemsQuery to include sorting options.
- Updated InventoryProfile mappings to reflect changes in DTOs and Protobuf definitions.
- Adjusted Protobuf definitions to align with new property names and structures.
2026-01-03 15:38:22 +03:30

279 lines
13 KiB
C#

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<BffProtos.GetAllWarehousesRequest, GetAllWarehousesQuery>()
.MapWith(src => new GetAllWarehousesQuery
{
IsActive = src.IsActive != null ? src.IsActive.Value : null
});
config.NewConfig<GetAllWarehousesResponseDto, BffProtos.GetAllWarehousesResponse>()
.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<BffProtos.CreateWarehouseRequest, CreateWarehouseCommand>()
.MapWith(src => new CreateWarehouseCommand
{
Name = src.Name,
Code = src.Code,
Address = src.Address,
IsDefault = src.IsDefault,
IsActive = true
});
config.NewConfig<long, BffProtos.CreateWarehouseResponse>()
.MapWith(src => new BffProtos.CreateWarehouseResponse { Id = src });
// =============================================
// GetAllInventoryItems
// =============================================
config.NewConfig<BffProtos.GetAllInventoryItemsRequest, GetAllInventoryItemsQuery>()
.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<GetAllInventoryItemsResponseDto, BffProtos.GetAllInventoryItemsResponse>()
.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<BffProtos.GetLowStockItemsRequest, GetLowStockItemsQuery>()
.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<GetLowStockItemsResponseDto, BffProtos.GetLowStockItemsResponse>()
.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<BffProtos.GetStockMovementsRequest, GetStockMovementsQuery>()
.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<GetStockMovementsResponseDto, BffProtos.GetStockMovementsResponse>()
.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<BffProtos.AddStockRequest, AddStockCommand>()
.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<AddStockResponseDto, BffProtos.AddStockResponse>()
.MapWith(src => new BffProtos.AddStockResponse
{
InventoryItemId = src.InventoryItemId,
NewQuantity = src.NewQuantity
});
// =============================================
// AdjustStock
// =============================================
config.NewConfig<BffProtos.AdjustStockRequest, AdjustStockCommand>()
.MapWith(src => new AdjustStockCommand
{
ProductId = src.ProductId,
ProductType = (int)src.ProductType,
NewQuantity = src.NewQuantity,
Reason = src.Reason,
ReferenceNumber = src.ReferenceNumber
});
config.NewConfig<AdjustStockResponseDto, BffProtos.AdjustStockResponse>()
.MapWith(src => new BffProtos.AdjustStockResponse
{
PreviousQuantity = src.PreviousQuantity,
NewQuantity = src.NewQuantity,
Difference = src.Difference
});
// =============================================
// RecordLoss
// =============================================
config.NewConfig<BffProtos.RecordLossRequest, RecordLossCommand>()
.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<BffProtos.UpdateInventorySettingsRequest, UpdateInventorySettingsCommand>()
.MapWith(src => new UpdateInventorySettingsCommand
{
Id = src.Id,
LowStockThreshold = src.LowStockThreshold,
ReorderPoint = src.ReorderPoint,
MaxStockLevel = src.MaxStockLevel
});
}
}