feat(application): Rubros commands/queries + RubroTreeBuilder + audit (CAT-001)
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
namespace SIGCM2.Application.Rubros.Create;
|
||||
|
||||
public sealed record CreateRubroCommand(
|
||||
string Nombre,
|
||||
int? ParentId,
|
||||
int? TarifarioBaseId);
|
||||
@@ -0,0 +1,91 @@
|
||||
using System.Transactions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SIGCM2.Application.Abstractions;
|
||||
using SIGCM2.Application.Abstractions.Persistence;
|
||||
using SIGCM2.Application.Audit;
|
||||
using SIGCM2.Domain.Entities;
|
||||
using SIGCM2.Domain.Exceptions;
|
||||
|
||||
namespace SIGCM2.Application.Rubros.Create;
|
||||
|
||||
public sealed class CreateRubroCommandHandler : ICommandHandler<CreateRubroCommand, RubroCreatedDto>
|
||||
{
|
||||
private readonly IRubroRepository _repo;
|
||||
private readonly IAuditLogger _audit;
|
||||
private readonly TimeProvider _timeProvider;
|
||||
private readonly RubrosOptions _options;
|
||||
|
||||
public CreateRubroCommandHandler(
|
||||
IRubroRepository repo,
|
||||
IAuditLogger audit,
|
||||
TimeProvider timeProvider,
|
||||
IOptions<RubrosOptions> options)
|
||||
{
|
||||
_repo = repo;
|
||||
_audit = audit;
|
||||
_timeProvider = timeProvider;
|
||||
_options = options.Value;
|
||||
}
|
||||
|
||||
public async Task<RubroCreatedDto> Handle(CreateRubroCommand command)
|
||||
{
|
||||
// Validate parent exists and is active (if provided)
|
||||
if (command.ParentId.HasValue)
|
||||
{
|
||||
var parent = await _repo.GetByIdAsync(command.ParentId.Value);
|
||||
if (parent is null)
|
||||
throw new RubroNotFoundException(command.ParentId.Value);
|
||||
if (!parent.Activo)
|
||||
throw new RubroPadreInactivoException(command.ParentId.Value);
|
||||
|
||||
// Depth check: parent's depth + 1 must not exceed MaxDepth
|
||||
var parentDepth = await _repo.GetDepthAsync(command.ParentId);
|
||||
var newDepth = parentDepth + 1;
|
||||
if (newDepth > _options.MaxDepth)
|
||||
throw new RubroMaxDepthExceededException(newDepth, _options.MaxDepth);
|
||||
}
|
||||
|
||||
// Duplicate name check (CI) under same parent
|
||||
var exists = await _repo.ExistsByNombreUnderParentAsync(command.ParentId, command.Nombre, excludeId: null);
|
||||
if (exists)
|
||||
throw new RubroNombreDuplicadoEnPadreException(command.Nombre, command.ParentId);
|
||||
|
||||
// Determine Orden = MAX+1 among siblings
|
||||
var orden = await _repo.GetMaxOrdenAsync(command.ParentId);
|
||||
|
||||
var now = _timeProvider.GetUtcNow().UtcDateTime;
|
||||
var rubro = Rubro.ForCreation(command.Nombre, command.ParentId, orden, command.TarifarioBaseId, _timeProvider);
|
||||
|
||||
using var tx = new TransactionScope(
|
||||
TransactionScopeOption.Required,
|
||||
new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted },
|
||||
TransactionScopeAsyncFlowOption.Enabled);
|
||||
|
||||
var newId = await _repo.AddAsync(rubro);
|
||||
|
||||
await _audit.LogAsync(
|
||||
action: "rubro.created",
|
||||
targetType: "Rubro",
|
||||
targetId: newId.ToString(),
|
||||
metadata: new
|
||||
{
|
||||
after = new
|
||||
{
|
||||
rubro.Nombre,
|
||||
rubro.ParentId,
|
||||
rubro.Orden,
|
||||
rubro.TarifarioBaseId,
|
||||
},
|
||||
});
|
||||
|
||||
tx.Complete();
|
||||
|
||||
return new RubroCreatedDto(
|
||||
Id: newId,
|
||||
Nombre: rubro.Nombre,
|
||||
ParentId: rubro.ParentId,
|
||||
Orden: rubro.Orden,
|
||||
Activo: rubro.Activo,
|
||||
TarifarioBaseId: rubro.TarifarioBaseId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace SIGCM2.Application.Rubros.Create;
|
||||
|
||||
public sealed record RubroCreatedDto(
|
||||
int Id,
|
||||
string Nombre,
|
||||
int? ParentId,
|
||||
int Orden,
|
||||
bool Activo,
|
||||
int? TarifarioBaseId);
|
||||
Reference in New Issue
Block a user