feat(application): repository abstraction + DTOs + validators + handlers CRUD PuntosDeVenta con auditoría + retry deadlock

This commit is contained in:
2026-04-17 12:28:11 -03:00
parent 43877bd4a1
commit 50f6f2b67a
36 changed files with 1296 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
namespace SIGCM2.Application.PuntosDeVenta.Update;
public sealed record PuntoDeVentaUpdatedDto(
int Id,
int MedioId,
short NumeroAFIP,
string Nombre,
string? Descripcion,
bool Activo);

View File

@@ -0,0 +1,7 @@
namespace SIGCM2.Application.PuntosDeVenta.Update;
public sealed record UpdatePuntoDeVentaCommand(
int Id,
string Nombre,
short NumeroAFIP,
string? Descripcion);

View File

@@ -0,0 +1,71 @@
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.PuntosDeVenta.Update;
public sealed class UpdatePuntoDeVentaCommandHandler : ICommandHandler<UpdatePuntoDeVentaCommand, PuntoDeVentaUpdatedDto>
{
private readonly IPuntoDeVentaRepository _repo;
private readonly IMedioRepository _medioRepo;
private readonly IAuditLogger _audit;
public UpdatePuntoDeVentaCommandHandler(
IPuntoDeVentaRepository repo,
IMedioRepository medioRepo,
IAuditLogger audit)
{
_repo = repo;
_medioRepo = medioRepo;
_audit = audit;
}
public async Task<PuntoDeVentaUpdatedDto> Handle(UpdatePuntoDeVentaCommand command)
{
var target = await _repo.GetByIdAsync(command.Id)
?? throw new PuntoDeVentaNotFoundException(command.Id);
var medio = await _medioRepo.GetByIdAsync(target.MedioId)
?? throw new MedioNotFoundException(target.MedioId);
if (!medio.Activo)
throw new MedioInactivoException(medio.Id);
// Re-validate uniqueness excluding current entity (REQ-PDV-004)
var exists = await _repo.ExistsByNumeroAFIPInMedioAsync(target.MedioId, command.NumeroAFIP, excludeId: command.Id);
if (exists)
throw new NumeroAFIPDuplicadoException(target.MedioId, command.NumeroAFIP);
var updated = target.WithUpdatedProfile(command.Nombre, command.NumeroAFIP, command.Descripcion);
using var tx = new TransactionScope(
TransactionScopeOption.Required,
new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted },
TransactionScopeAsyncFlowOption.Enabled);
await _repo.UpdateAsync(updated);
await _audit.LogAsync(
action: "punto_de_venta.update",
targetType: "PuntoDeVenta",
targetId: command.Id.ToString(),
metadata: new
{
before = new { target.Nombre, target.NumeroAFIP, target.Descripcion },
after = new { updated.Nombre, updated.NumeroAFIP, updated.Descripcion },
});
tx.Complete();
return new PuntoDeVentaUpdatedDto(
Id: updated.Id,
MedioId: updated.MedioId,
NumeroAFIP: updated.NumeroAFIP,
Nombre: updated.Nombre,
Descripcion: updated.Descripcion,
Activo: updated.Activo);
}
}

View File

@@ -0,0 +1,29 @@
using FluentValidation;
namespace SIGCM2.Application.PuntosDeVenta.Update;
public sealed class UpdatePuntoDeVentaCommandValidator : AbstractValidator<UpdatePuntoDeVentaCommand>
{
private const int NombreMaxLength = 100;
private const int DescripcionMaxLength = 255;
private const short NumeroAFIPMin = 1;
private const short NumeroAFIPMax = 9999;
public UpdatePuntoDeVentaCommandValidator()
{
RuleFor(x => x.Id)
.GreaterThan(0).WithMessage("El id debe ser mayor a 0.");
RuleFor(x => x.NumeroAFIP)
.InclusiveBetween(NumeroAFIPMin, NumeroAFIPMax)
.WithMessage($"El número AFIP debe estar entre {NumeroAFIPMin} y {NumeroAFIPMax}.");
RuleFor(x => x.Nombre)
.NotEmpty().WithMessage("El nombre es requerido.")
.MaximumLength(NombreMaxLength).WithMessage($"El nombre no puede superar los {NombreMaxLength} caracteres.");
RuleFor(x => x.Descripcion)
.MaximumLength(DescripcionMaxLength).WithMessage($"La descripción no puede superar los {DescripcionMaxLength} caracteres.")
.When(x => x.Descripcion is not null);
}
}