feat: auto-create inventory records for new products + migration worker
Build and Deploy to Kubernetes / build-and-deploy (push) Successful in 8m52s
Build and Deploy to Kubernetes / build-and-deploy (push) Successful in 8m52s
- CreateNewProductsCommandHandler: inject IInventoryService, auto-init inventory record with qty=0 when a new regular product is created (DiscountProduct handler already had this) - InventoryInitializerService: one-time BackgroundService that runs on startup, finds existing products without InventoryItem records, and creates them (migration for legacy products) - Register InventoryInitializerService in DI
This commit is contained in:
+18
@@ -1,6 +1,7 @@
|
||||
using CMSMicroservice.Application.Common.FileManager;
|
||||
using CMSMicroservice.Application.Common.Interfaces;
|
||||
using CMSMicroservice.Domain.Entities;
|
||||
using CMSMicroservice.Domain.Enums;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace CMSMicroservice.Application.ProductsCQ.Commands.CreateNewProducts;
|
||||
@@ -9,15 +10,18 @@ public class CreateNewProductsCommandHandler : IRequestHandler<CreateNewProducts
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly IFileManager _fileManager;
|
||||
private readonly IInventoryService _inventoryService;
|
||||
private readonly ILogger<CreateNewProductsCommandHandler> _logger;
|
||||
|
||||
public CreateNewProductsCommandHandler(
|
||||
IApplicationDbContext context,
|
||||
IFileManager fileManager,
|
||||
IInventoryService inventoryService,
|
||||
ILogger<CreateNewProductsCommandHandler> logger)
|
||||
{
|
||||
_context = context;
|
||||
_fileManager = fileManager;
|
||||
_inventoryService = inventoryService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
@@ -70,6 +74,20 @@ public class CreateNewProductsCommandHandler : IRequestHandler<CreateNewProducts
|
||||
await _context.Products.AddAsync(entity, cancellationToken);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
// ایجاد رکورد موجودی در سیستم انبارداری
|
||||
try
|
||||
{
|
||||
await _inventoryService.InitializeInventoryAsync(
|
||||
entity.Id,
|
||||
ProductType.RegularProduct,
|
||||
0, // موجودی اولیه صفر — باید از طریق Inventory اضافه شود
|
||||
ct: cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to initialize inventory for Product Id={ProductId}", entity.Id);
|
||||
}
|
||||
|
||||
// Handle category assignments
|
||||
if (request.CategoryIds is { Count: > 0 })
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user