fix: Implement product lookup for AddStock and AdjustStock methods in InventoryService
Build and Deploy to Kubernetes / build-and-deploy (push) Has been cancelled
Build and Deploy to Kubernetes / build-and-deploy (push) Has been cancelled
This commit is contained in:
@@ -19,16 +19,20 @@ using CMSMicroservice.Application.StockMovementCQ.Commands.CreateStockMovement;
|
||||
using CMSMicroservice.Application.StockMovementCQ.Queries.GetStockMovements;
|
||||
using CMSMicroservice.Application.StockMovementCQ.Queries.GetStockMovementsByInventoryItem;
|
||||
using Google.Protobuf.WellKnownTypes;
|
||||
using Grpc.Core;
|
||||
using MediatR;
|
||||
|
||||
namespace CMSMicroservice.WebApi.Services;
|
||||
|
||||
public class InventoryService : InventoryContract.InventoryContractBase
|
||||
{
|
||||
private readonly IDispatchRequestToCQRS _dispatchRequestToCQRS;
|
||||
private readonly IMediator _mediator;
|
||||
|
||||
public InventoryService(IDispatchRequestToCQRS dispatchRequestToCQRS)
|
||||
public InventoryService(IDispatchRequestToCQRS dispatchRequestToCQRS, IMediator mediator)
|
||||
{
|
||||
_dispatchRequestToCQRS = dispatchRequestToCQRS;
|
||||
_mediator = mediator;
|
||||
}
|
||||
|
||||
// ========== Warehouse Management ==========
|
||||
@@ -91,19 +95,112 @@ public class InventoryService : InventoryContract.InventoryContractBase
|
||||
}
|
||||
|
||||
// ========== Stock Operations ==========
|
||||
// Note: These operations need lookup by ProductId/ProductType which requires additional implementation
|
||||
// For now, returning stub responses - actual implementation needs custom handlers
|
||||
|
||||
public override Task<AddStockResponse> AddStock(AddStockRequest request, ServerCallContext context)
|
||||
public override async Task<AddStockResponse> AddStock(AddStockRequest request, ServerCallContext context)
|
||||
{
|
||||
// TODO: Implement with product lookup
|
||||
throw new RpcException(new Status(StatusCode.Unimplemented, "AddStock requires product lookup - not yet implemented"));
|
||||
// Lookup InventoryItem by ProductId + ProductType
|
||||
var inventoryItem = await _mediator.Send(
|
||||
new GetInventoryByProductQuery
|
||||
{
|
||||
ProductId = request.ProductId,
|
||||
ProductType = (int)request.ProductType,
|
||||
WarehouseId = request.WarehouseId?.Value
|
||||
},
|
||||
context.CancellationToken);
|
||||
|
||||
if (inventoryItem == null)
|
||||
{
|
||||
throw new RpcException(new Status(StatusCode.NotFound,
|
||||
$"Inventory item not found for ProductId={request.ProductId}, ProductType={request.ProductType}"));
|
||||
}
|
||||
|
||||
public override Task<AdjustStockResponse> AdjustStock(AdjustStockRequest request, ServerCallContext context)
|
||||
// Execute IncreaseInventoryCommand
|
||||
var response = await _mediator.Send(
|
||||
new IncreaseInventoryCommand
|
||||
{
|
||||
// TODO: Implement with product lookup
|
||||
throw new RpcException(new Status(StatusCode.Unimplemented, "AdjustStock requires product lookup - not yet implemented"));
|
||||
Id = inventoryItem.Id,
|
||||
Quantity = request.Quantity,
|
||||
ReferenceNumber = request.ReferenceNumber,
|
||||
Note = request.Note
|
||||
},
|
||||
context.CancellationToken);
|
||||
|
||||
return new AddStockResponse
|
||||
{
|
||||
InventoryItemId = response.Id,
|
||||
NewQuantity = response.NewQuantity
|
||||
};
|
||||
}
|
||||
|
||||
public override async Task<AdjustStockResponse> AdjustStock(AdjustStockRequest request, ServerCallContext context)
|
||||
{
|
||||
// Lookup InventoryItem
|
||||
var inventoryItem = await _mediator.Send(
|
||||
new GetInventoryByProductQuery
|
||||
{
|
||||
ProductId = request.ProductId,
|
||||
ProductType = (int)request.ProductType,
|
||||
WarehouseId = request.WarehouseId?.Value
|
||||
},
|
||||
context.CancellationToken);
|
||||
|
||||
if (inventoryItem == null)
|
||||
{
|
||||
throw new RpcException(new Status(StatusCode.NotFound,
|
||||
$"Inventory item not found for ProductId={request.ProductId}, ProductType={request.ProductType}"));
|
||||
}
|
||||
|
||||
// Determine if increase or decrease
|
||||
var difference = request.NewQuantity - inventoryItem.Quantity;
|
||||
|
||||
if (difference > 0)
|
||||
{
|
||||
var response = await _mediator.Send(
|
||||
new IncreaseInventoryCommand
|
||||
{
|
||||
Id = inventoryItem.Id,
|
||||
Quantity = difference,
|
||||
ReferenceNumber = request.ReferenceNumber,
|
||||
Note = request.Reason
|
||||
},
|
||||
context.CancellationToken);
|
||||
|
||||
return new AdjustStockResponse
|
||||
{
|
||||
InventoryItemId = response.Id,
|
||||
OldQuantity = inventoryItem.Quantity,
|
||||
NewQuantity = response.NewQuantity
|
||||
};
|
||||
}
|
||||
else if (difference < 0)
|
||||
{
|
||||
var response = await _mediator.Send(
|
||||
new ReduceInventoryCommand
|
||||
{
|
||||
Id = inventoryItem.Id,
|
||||
Quantity = Math.Abs(difference),
|
||||
FromReserved = false,
|
||||
ReferenceNumber = request.ReferenceNumber,
|
||||
Note = request.Reason
|
||||
},
|
||||
context.CancellationToken);
|
||||
|
||||
return new AdjustStockResponse
|
||||
{
|
||||
InventoryItemId = response.Id,
|
||||
OldQuantity = inventoryItem.Quantity,
|
||||
NewQuantity = response.NewQuantity
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
return new AdjustStockResponse
|
||||
{
|
||||
InventoryItemId = inventoryItem.Id,
|
||||
OldQuantity = inventoryItem.Quantity,
|
||||
NewQuantity = inventoryItem.Quantity
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public override Task<ReserveStockResponse> ReserveStock(ReserveStockRequest request, ServerCallContext context)
|
||||
@@ -144,10 +241,75 @@ public class InventoryService : InventoryContract.InventoryContractBase
|
||||
throw new RpcException(new Status(StatusCode.Unimplemented, "BulkAddStock requires product lookup - not yet implemented"));
|
||||
}
|
||||
|
||||
public override Task<BulkAdjustStockResponse> BulkAdjustStock(BulkAdjustStockRequest request, ServerCallContext context)
|
||||
public override async Task<AdjustStockResponse> AdjustStock(AdjustStockRequest request, ServerCallContext context)
|
||||
{
|
||||
// TODO: Implement with product lookup
|
||||
throw new RpcException(new Status(StatusCode.Unimplemented, "BulkAdjustStock requires product lookup - not yet implemented"));
|
||||
// Lookup InventoryItem
|
||||
var inventoryItem = await _mediator.Send(
|
||||
new GetInventoryByProductQuery
|
||||
{
|
||||
ProductId = request.ProductId,
|
||||
ProductType = (int)request.ProductType,
|
||||
WarehouseId = request.WarehouseId?.Value
|
||||
},
|
||||
context.CancellationToken);
|
||||
|
||||
if (inventoryItem == null)
|
||||
{
|
||||
throw new RpcException(new Status(StatusCode.NotFound,
|
||||
$"Inventory item not found for ProductId={request.ProductId}, ProductType={request.ProductType}"));
|
||||
}
|
||||
|
||||
// Determine if increase or decrease
|
||||
var difference = request.NewQuantity - inventoryItem.Quantity;
|
||||
|
||||
if (difference > 0)
|
||||
{
|
||||
var response = await _mediator.Send(
|
||||
new IncreaseInventoryCommand
|
||||
{
|
||||
Id = inventoryItem.Id,
|
||||
Quantity = difference,
|
||||
ReferenceNumber = request.ReferenceNumber,
|
||||
Note = request.Reason
|
||||
},
|
||||
context.CancellationToken);
|
||||
|
||||
return new AdjustStockResponse
|
||||
{
|
||||
InventoryItemId = response.Id,
|
||||
OldQuantity = inventoryItem.Quantity,
|
||||
NewQuantity = response.NewQuantity
|
||||
};
|
||||
}
|
||||
else if (difference < 0)
|
||||
{
|
||||
var response = await _mediator.Send(
|
||||
new ReduceInventoryCommand
|
||||
{
|
||||
Id = inventoryItem.Id,
|
||||
Quantity = Math.Abs(difference),
|
||||
FromReserved = false,
|
||||
ReferenceNumber = request.ReferenceNumber,
|
||||
Note = request.Reason
|
||||
},
|
||||
context.CancellationToken);
|
||||
|
||||
return new AdjustStockResponse
|
||||
{
|
||||
InventoryItemId = response.Id,
|
||||
OldQuantity = inventoryItem.Quantity,
|
||||
NewQuantity = response.NewQuantity
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
return new AdjustStockResponse
|
||||
{
|
||||
InventoryItemId = inventoryItem.Id,
|
||||
OldQuantity = inventoryItem.Quantity,
|
||||
NewQuantity = inventoryItem.Quantity
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// ========== Stock Movements ==========
|
||||
|
||||
Reference in New Issue
Block a user