feat(application): Rubros commands/queries + RubroTreeBuilder + audit (CAT-001)

This commit is contained in:
2026-04-18 19:25:35 -03:00
parent dcb2e5ada6
commit d9fc9a2867
26 changed files with 1330 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
using SIGCM2.Domain.Entities;
namespace SIGCM2.Application.Abstractions.Persistence;
public interface IRubroRepository
{
Task<int> AddAsync(Rubro rubro, CancellationToken ct = default);
Task<Rubro?> GetByIdAsync(int id, CancellationToken ct = default);
Task<IReadOnlyList<Rubro>> GetAllAsync(bool incluirInactivos, CancellationToken ct = default);
/// <summary>
/// Returns all descendants of rootId via recursive CTE (used only by MoveRubro for cycle detection).
/// </summary>
Task<IReadOnlyList<Rubro>> GetDescendantsAsync(int rootId, CancellationToken ct = default);
Task UpdateAsync(Rubro rubro, CancellationToken ct = default);
/// <summary>
/// Returns the count of active children for the given parentId.
/// Used by soft-delete to guard against deleting non-leaf rubros.
/// </summary>
Task<int> CountActiveChildrenAsync(int id, CancellationToken ct = default);
/// <summary>
/// Returns MAX(Orden)+1 among siblings of the given parentId (0 if no siblings).
/// Used for append-on-create ordering.
/// </summary>
Task<int> GetMaxOrdenAsync(int? parentId, CancellationToken ct = default);
/// <summary>
/// Returns true if an active Rubro with the same Nombre (CI) exists under the same parentId,
/// optionally excluding the Rubro with the given id (for rename operations).
/// </summary>
Task<bool> ExistsByNombreUnderParentAsync(int? parentId, string nombre, int? excludeId, CancellationToken ct = default);
/// <summary>
/// Returns the depth of the given parentId (0 if parentId is null = root level).
/// Uses a recursive CTE going upward through ancestors.
/// </summary>
Task<int> GetDepthAsync(int? parentId, CancellationToken ct = default);
}

View File

@@ -0,0 +1,47 @@
using SIGCM2.Application.Rubros.Dtos;
using SIGCM2.Domain.Entities;
namespace SIGCM2.Application.Rubros.Common;
/// <summary>
/// Builds an N-ary tree from a flat list of Rubro entities in O(n) time.
/// Algorithm: (1) optionally filter inactivos, (2) group by ParentId into a dictionary,
/// (3) recursively assemble roots (ParentId==null) attaching children sorted by Orden ASC.
/// </summary>
public static class RubroTreeBuilder
{
public static IReadOnlyList<RubroTreeNodeDto> Build(
IEnumerable<Rubro> flat,
bool incluirInactivos)
{
var filtered = incluirInactivos
? flat.ToList()
: flat.Where(r => r.Activo).ToList();
// Group by ParentId → each bucket sorted by Orden ASC
// Use ToLookup (handles the int? key safely) instead of ToDictionary
var byParent = filtered.ToLookup(r => r.ParentId);
RubroTreeNodeDto Map(Rubro r)
{
var children = byParent[(int?)r.Id]
.OrderBy(x => x.Orden)
.Select(Map)
.ToList();
return new RubroTreeNodeDto(
Id: r.Id,
Nombre: r.Nombre,
Orden: r.Orden,
Activo: r.Activo,
ParentId: r.ParentId,
TarifarioBaseId: r.TarifarioBaseId,
Hijos: children);
}
return byParent[null]
.OrderBy(r => r.Orden)
.Select(Map)
.ToList();
}
}

View File

@@ -0,0 +1,6 @@
namespace SIGCM2.Application.Rubros.Create;
public sealed record CreateRubroCommand(
string Nombre,
int? ParentId,
int? TarifarioBaseId);

View File

@@ -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);
}
}

View File

@@ -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);

View File

@@ -0,0 +1,3 @@
namespace SIGCM2.Application.Rubros.Deactivate;
public sealed record DeactivateRubroCommand(int Id);

View File

@@ -0,0 +1,58 @@
using System.Transactions;
using SIGCM2.Application.Abstractions;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Audit;
using SIGCM2.Domain.Exceptions;
namespace SIGCM2.Application.Rubros.Deactivate;
public sealed class DeactivateRubroCommandHandler : ICommandHandler<DeactivateRubroCommand, RubroStatusDto>
{
private readonly IRubroRepository _repo;
private readonly IAuditLogger _audit;
private readonly TimeProvider _timeProvider;
public DeactivateRubroCommandHandler(
IRubroRepository repo,
IAuditLogger audit,
TimeProvider timeProvider)
{
_repo = repo;
_audit = audit;
_timeProvider = timeProvider;
}
public async Task<RubroStatusDto> Handle(DeactivateRubroCommand command)
{
var target = await _repo.GetByIdAsync(command.Id)
?? throw new RubroNotFoundException(command.Id);
var activeChildren = await _repo.CountActiveChildrenAsync(command.Id);
if (activeChildren > 0)
throw new RubroTieneHijosActivosException(command.Id, activeChildren);
var deactivated = target.WithActivo(false, _timeProvider);
using var tx = new TransactionScope(
TransactionScopeOption.Required,
new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted },
TransactionScopeAsyncFlowOption.Enabled);
await _repo.UpdateAsync(deactivated);
await _audit.LogAsync(
action: "rubro.deleted",
targetType: "Rubro",
targetId: command.Id.ToString(),
metadata: new
{
rubroId = command.Id,
nombre = target.Nombre,
activeChildrenCount = 0,
});
tx.Complete();
return new RubroStatusDto(Id: deactivated.Id, Activo: deactivated.Activo);
}
}

View File

@@ -0,0 +1,3 @@
namespace SIGCM2.Application.Rubros.Deactivate;
public sealed record RubroStatusDto(int Id, bool Activo);

View File

@@ -0,0 +1,13 @@
namespace SIGCM2.Application.Rubros.Dtos;
/// <summary>
/// Represents a single node in the N-ary Rubro tree returned by GetRubroTreeQuery.
/// </summary>
public sealed record RubroTreeNodeDto(
int Id,
string Nombre,
int Orden,
bool Activo,
int? ParentId,
int? TarifarioBaseId,
IReadOnlyList<RubroTreeNodeDto> Hijos);

View File

@@ -0,0 +1,3 @@
namespace SIGCM2.Application.Rubros.GetById;
public sealed record GetRubroByIdQuery(int Id);

View File

@@ -0,0 +1,31 @@
using SIGCM2.Application.Abstractions;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Domain.Exceptions;
namespace SIGCM2.Application.Rubros.GetById;
public sealed class GetRubroByIdQueryHandler : ICommandHandler<GetRubroByIdQuery, RubroDetailDto>
{
private readonly IRubroRepository _repo;
public GetRubroByIdQueryHandler(IRubroRepository repo)
{
_repo = repo;
}
public async Task<RubroDetailDto> Handle(GetRubroByIdQuery query)
{
var rubro = await _repo.GetByIdAsync(query.Id)
?? throw new RubroNotFoundException(query.Id);
return new RubroDetailDto(
Id: rubro.Id,
Nombre: rubro.Nombre,
ParentId: rubro.ParentId,
Orden: rubro.Orden,
Activo: rubro.Activo,
TarifarioBaseId: rubro.TarifarioBaseId,
FechaCreacion: rubro.FechaCreacion,
FechaModificacion: rubro.FechaModificacion);
}
}

View File

@@ -0,0 +1,11 @@
namespace SIGCM2.Application.Rubros.GetById;
public sealed record RubroDetailDto(
int Id,
string Nombre,
int? ParentId,
int Orden,
bool Activo,
int? TarifarioBaseId,
DateTime FechaCreacion,
DateTime? FechaModificacion);

View File

@@ -0,0 +1,3 @@
namespace SIGCM2.Application.Rubros.GetTree;
public sealed record GetRubroTreeQuery(bool IncluirInactivos);

View File

@@ -0,0 +1,22 @@
using SIGCM2.Application.Abstractions;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Rubros.Common;
using SIGCM2.Application.Rubros.Dtos;
namespace SIGCM2.Application.Rubros.GetTree;
public sealed class GetRubroTreeQueryHandler : ICommandHandler<GetRubroTreeQuery, IReadOnlyList<RubroTreeNodeDto>>
{
private readonly IRubroRepository _repo;
public GetRubroTreeQueryHandler(IRubroRepository repo)
{
_repo = repo;
}
public async Task<IReadOnlyList<RubroTreeNodeDto>> Handle(GetRubroTreeQuery query)
{
var all = await _repo.GetAllAsync(query.IncluirInactivos);
return RubroTreeBuilder.Build(all, query.IncluirInactivos);
}
}

View File

@@ -0,0 +1,3 @@
namespace SIGCM2.Application.Rubros.Move;
public sealed record MoveRubroCommand(int Id, int? NuevoParentId, int NuevoOrden);

View File

@@ -0,0 +1,92 @@
using System.Transactions;
using Microsoft.Extensions.Options;
using SIGCM2.Application.Abstractions;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Audit;
using SIGCM2.Domain.Exceptions;
namespace SIGCM2.Application.Rubros.Move;
public sealed class MoveRubroCommandHandler : ICommandHandler<MoveRubroCommand, RubroMovedDto>
{
private readonly IRubroRepository _repo;
private readonly IAuditLogger _audit;
private readonly TimeProvider _timeProvider;
private readonly RubrosOptions _options;
public MoveRubroCommandHandler(
IRubroRepository repo,
IAuditLogger audit,
TimeProvider timeProvider,
IOptions<RubrosOptions> options)
{
_repo = repo;
_audit = audit;
_timeProvider = timeProvider;
_options = options.Value;
}
public async Task<RubroMovedDto> Handle(MoveRubroCommand command)
{
var target = await _repo.GetByIdAsync(command.Id)
?? throw new RubroNotFoundException(command.Id);
var anteriorParentId = target.ParentId;
// Cycle check: nuevoParentId must not be in descendants of target
if (command.NuevoParentId.HasValue)
{
var descendants = await _repo.GetDescendantsAsync(command.Id);
if (descendants.Any(d => d.Id == command.NuevoParentId.Value))
throw new RubroCycleDetectedException(command.Id, command.NuevoParentId.Value);
// New parent must exist and be active
var newParent = await _repo.GetByIdAsync(command.NuevoParentId.Value)
?? throw new RubroNotFoundException(command.NuevoParentId.Value);
if (!newParent.Activo)
throw new RubroPadreInactivoException(command.NuevoParentId.Value);
// Depth check
var parentDepth = await _repo.GetDepthAsync(command.NuevoParentId);
var newDepth = parentDepth + 1;
if (newDepth > _options.MaxDepth)
throw new RubroMaxDepthExceededException(newDepth, _options.MaxDepth);
}
// Duplicate name check under new parent (excluding self)
var exists = await _repo.ExistsByNombreUnderParentAsync(command.NuevoParentId, target.Nombre, excludeId: command.Id);
if (exists)
throw new RubroNombreDuplicadoEnPadreException(target.Nombre, command.NuevoParentId);
var moved = target.WithMoved(command.NuevoParentId, command.NuevoOrden, _timeProvider);
using var tx = new TransactionScope(
TransactionScopeOption.Required,
new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted },
TransactionScopeAsyncFlowOption.Enabled);
await _repo.UpdateAsync(moved);
await _audit.LogAsync(
action: "rubro.moved",
targetType: "Rubro",
targetId: command.Id.ToString(),
metadata: new
{
anteriorParentId,
nuevoParentId = command.NuevoParentId,
anteriorOrden = target.Orden,
nuevoOrden = command.NuevoOrden,
});
tx.Complete();
return new RubroMovedDto(
Id: moved.Id,
Nombre: moved.Nombre,
ParentId: moved.ParentId,
Orden: moved.Orden,
Activo: moved.Activo);
}
}

View File

@@ -0,0 +1,8 @@
namespace SIGCM2.Application.Rubros.Move;
public sealed record RubroMovedDto(
int Id,
string Nombre,
int? ParentId,
int Orden,
bool Activo);

View File

@@ -0,0 +1,9 @@
namespace SIGCM2.Application.Rubros.Update;
public sealed record RubroUpdatedDto(
int Id,
string Nombre,
int? ParentId,
int Orden,
bool Activo,
int? TarifarioBaseId);

View File

@@ -0,0 +1,3 @@
namespace SIGCM2.Application.Rubros.Update;
public sealed record UpdateRubroCommand(int Id, string Nombre);

View File

@@ -0,0 +1,65 @@
using System.Transactions;
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.Update;
public sealed class UpdateRubroCommandHandler : ICommandHandler<UpdateRubroCommand, RubroUpdatedDto>
{
private readonly IRubroRepository _repo;
private readonly IAuditLogger _audit;
private readonly TimeProvider _timeProvider;
public UpdateRubroCommandHandler(
IRubroRepository repo,
IAuditLogger audit,
TimeProvider timeProvider)
{
_repo = repo;
_audit = audit;
_timeProvider = timeProvider;
}
public async Task<RubroUpdatedDto> Handle(UpdateRubroCommand command)
{
var target = await _repo.GetByIdAsync(command.Id)
?? throw new RubroNotFoundException(command.Id);
// Duplicate name check (CI, excluding self)
var exists = await _repo.ExistsByNombreUnderParentAsync(target.ParentId, command.Nombre, excludeId: command.Id);
if (exists)
throw new RubroNombreDuplicadoEnPadreException(command.Nombre, target.ParentId);
var updated = target.WithRenamed(command.Nombre, _timeProvider);
using var tx = new TransactionScope(
TransactionScopeOption.Required,
new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted },
TransactionScopeAsyncFlowOption.Enabled);
await _repo.UpdateAsync(updated);
await _audit.LogAsync(
action: "rubro.updated",
targetType: "Rubro",
targetId: command.Id.ToString(),
metadata: new
{
before = new { target.Nombre },
after = new { updated.Nombre },
});
tx.Complete();
return new RubroUpdatedDto(
Id: updated.Id,
Nombre: updated.Nombre,
ParentId: updated.ParentId,
Orden: updated.Orden,
Activo: updated.Activo,
TarifarioBaseId: updated.TarifarioBaseId);
}
}