feat(application): repository abstraction + DTOs + validators + handlers CRUD PuntosDeVenta con auditoría + retry deadlock
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
namespace SIGCM2.Application.PuntosDeVenta.Create;
|
||||
|
||||
public sealed record CreatePuntoDeVentaCommand(
|
||||
int MedioId,
|
||||
short NumeroAFIP,
|
||||
string Nombre,
|
||||
string? Descripcion);
|
||||
@@ -0,0 +1,75 @@
|
||||
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.Create;
|
||||
|
||||
public sealed class CreatePuntoDeVentaCommandHandler : ICommandHandler<CreatePuntoDeVentaCommand, PuntoDeVentaCreatedDto>
|
||||
{
|
||||
private readonly IPuntoDeVentaRepository _repo;
|
||||
private readonly IMedioRepository _medioRepo;
|
||||
private readonly IAuditLogger _audit;
|
||||
|
||||
public CreatePuntoDeVentaCommandHandler(
|
||||
IPuntoDeVentaRepository repo,
|
||||
IMedioRepository medioRepo,
|
||||
IAuditLogger audit)
|
||||
{
|
||||
_repo = repo;
|
||||
_medioRepo = medioRepo;
|
||||
_audit = audit;
|
||||
}
|
||||
|
||||
public async Task<PuntoDeVentaCreatedDto> Handle(CreatePuntoDeVentaCommand command)
|
||||
{
|
||||
// Validate medio exists and is active (REQ-PDV-001, -002)
|
||||
var medio = await _medioRepo.GetByIdAsync(command.MedioId)
|
||||
?? throw new MedioNotFoundException(command.MedioId);
|
||||
|
||||
if (!medio.Activo)
|
||||
throw new MedioInactivoException(medio.Id);
|
||||
|
||||
// Check uniqueness NumeroAFIP within Medio (REQ-PDV-003)
|
||||
var exists = await _repo.ExistsByNumeroAFIPInMedioAsync(command.MedioId, command.NumeroAFIP, excludeId: null);
|
||||
if (exists)
|
||||
throw new NumeroAFIPDuplicadoException(command.MedioId, command.NumeroAFIP);
|
||||
|
||||
var pdv = PuntoDeVenta.ForCreation(command.MedioId, command.NumeroAFIP, command.Nombre, command.Descripcion);
|
||||
|
||||
using var tx = new TransactionScope(
|
||||
TransactionScopeOption.Required,
|
||||
new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted },
|
||||
TransactionScopeAsyncFlowOption.Enabled);
|
||||
|
||||
var newId = await _repo.AddAsync(pdv);
|
||||
|
||||
// fail-closed: if LogAsync throws, tx rolls back (REQ-PDV-010)
|
||||
await _audit.LogAsync(
|
||||
action: "punto_de_venta.create",
|
||||
targetType: "PuntoDeVenta",
|
||||
targetId: newId.ToString(),
|
||||
metadata: new
|
||||
{
|
||||
after = new
|
||||
{
|
||||
pdv.MedioId,
|
||||
pdv.NumeroAFIP,
|
||||
pdv.Nombre,
|
||||
pdv.Descripcion,
|
||||
},
|
||||
});
|
||||
|
||||
tx.Complete();
|
||||
|
||||
return new PuntoDeVentaCreatedDto(
|
||||
Id: newId,
|
||||
MedioId: pdv.MedioId,
|
||||
NumeroAFIP: pdv.NumeroAFIP,
|
||||
Nombre: pdv.Nombre,
|
||||
Descripcion: pdv.Descripcion,
|
||||
Activo: pdv.Activo);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace SIGCM2.Application.PuntosDeVenta.Create;
|
||||
|
||||
public sealed class CreatePuntoDeVentaCommandValidator : AbstractValidator<CreatePuntoDeVentaCommand>
|
||||
{
|
||||
private const int NombreMaxLength = 100;
|
||||
private const int DescripcionMaxLength = 255;
|
||||
private const short NumeroAFIPMin = 1;
|
||||
private const short NumeroAFIPMax = 9999;
|
||||
|
||||
public CreatePuntoDeVentaCommandValidator()
|
||||
{
|
||||
RuleFor(x => x.MedioId)
|
||||
.GreaterThan(0).WithMessage("El medioId 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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace SIGCM2.Application.PuntosDeVenta.Create;
|
||||
|
||||
public sealed record PuntoDeVentaCreatedDto(
|
||||
int Id,
|
||||
int MedioId,
|
||||
short NumeroAFIP,
|
||||
string Nombre,
|
||||
string? Descripcion,
|
||||
bool Activo);
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace SIGCM2.Application.PuntosDeVenta.Deactivate;
|
||||
|
||||
public sealed record DeactivatePuntoDeVentaCommand(int Id);
|
||||
@@ -0,0 +1,53 @@
|
||||
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.Deactivate;
|
||||
|
||||
public sealed class DeactivatePuntoDeVentaCommandHandler : ICommandHandler<DeactivatePuntoDeVentaCommand, PuntoDeVentaStatusDto>
|
||||
{
|
||||
private readonly IPuntoDeVentaRepository _repo;
|
||||
private readonly IMedioRepository _medioRepo;
|
||||
private readonly IAuditLogger _audit;
|
||||
|
||||
public DeactivatePuntoDeVentaCommandHandler(
|
||||
IPuntoDeVentaRepository repo,
|
||||
IMedioRepository medioRepo,
|
||||
IAuditLogger audit)
|
||||
{
|
||||
_repo = repo;
|
||||
_medioRepo = medioRepo;
|
||||
_audit = audit;
|
||||
}
|
||||
|
||||
public async Task<PuntoDeVentaStatusDto> Handle(DeactivatePuntoDeVentaCommand command)
|
||||
{
|
||||
var target = await _repo.GetByIdAsync(command.Id)
|
||||
?? throw new PuntoDeVentaNotFoundException(command.Id);
|
||||
|
||||
// Idempotent: already inactive → return as-is without writing an audit event
|
||||
if (!target.Activo)
|
||||
return new PuntoDeVentaStatusDto(target.Id, target.NumeroAFIP, target.Activo);
|
||||
|
||||
var updated = target.WithActivo(false);
|
||||
|
||||
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.deactivate",
|
||||
targetType: "PuntoDeVenta",
|
||||
targetId: command.Id.ToString());
|
||||
|
||||
tx.Complete();
|
||||
|
||||
return new PuntoDeVentaStatusDto(updated.Id, updated.NumeroAFIP, updated.Activo);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace SIGCM2.Application.PuntosDeVenta.Deactivate;
|
||||
|
||||
public sealed record PuntoDeVentaStatusDto(int Id, short NumeroAFIP, bool Activo);
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace SIGCM2.Application.PuntosDeVenta.GetById;
|
||||
|
||||
public sealed record GetPuntoDeVentaByIdQuery(int Id);
|
||||
@@ -0,0 +1,31 @@
|
||||
using SIGCM2.Application.Abstractions;
|
||||
using SIGCM2.Application.Abstractions.Persistence;
|
||||
using SIGCM2.Domain.Exceptions;
|
||||
|
||||
namespace SIGCM2.Application.PuntosDeVenta.GetById;
|
||||
|
||||
public sealed class GetPuntoDeVentaByIdQueryHandler : ICommandHandler<GetPuntoDeVentaByIdQuery, PuntoDeVentaDetailDto>
|
||||
{
|
||||
private readonly IPuntoDeVentaRepository _repo;
|
||||
|
||||
public GetPuntoDeVentaByIdQueryHandler(IPuntoDeVentaRepository repo)
|
||||
{
|
||||
_repo = repo;
|
||||
}
|
||||
|
||||
public async Task<PuntoDeVentaDetailDto> Handle(GetPuntoDeVentaByIdQuery query)
|
||||
{
|
||||
var pdv = await _repo.GetByIdAsync(query.Id)
|
||||
?? throw new PuntoDeVentaNotFoundException(query.Id);
|
||||
|
||||
return new PuntoDeVentaDetailDto(
|
||||
Id: pdv.Id,
|
||||
MedioId: pdv.MedioId,
|
||||
NumeroAFIP: pdv.NumeroAFIP,
|
||||
Nombre: pdv.Nombre,
|
||||
Descripcion: pdv.Descripcion,
|
||||
Activo: pdv.Activo,
|
||||
FechaCreacion: pdv.FechaCreacion,
|
||||
FechaModificacion: pdv.FechaModificacion);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace SIGCM2.Application.PuntosDeVenta.GetById;
|
||||
|
||||
public sealed record PuntoDeVentaDetailDto(
|
||||
int Id,
|
||||
int MedioId,
|
||||
short NumeroAFIP,
|
||||
string Nombre,
|
||||
string? Descripcion,
|
||||
bool Activo,
|
||||
DateTime FechaCreacion,
|
||||
DateTime? FechaModificacion);
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace SIGCM2.Application.PuntosDeVenta.List;
|
||||
|
||||
public sealed record ListPuntosDeVentaQuery(
|
||||
int Page,
|
||||
int PageSize,
|
||||
int? MedioId,
|
||||
bool? Activo);
|
||||
@@ -0,0 +1,30 @@
|
||||
using SIGCM2.Application.Abstractions;
|
||||
using SIGCM2.Application.Abstractions.Persistence;
|
||||
using SIGCM2.Application.Common;
|
||||
|
||||
namespace SIGCM2.Application.PuntosDeVenta.List;
|
||||
|
||||
public sealed class ListPuntosDeVentaQueryHandler : ICommandHandler<ListPuntosDeVentaQuery, PagedResult<PuntoDeVentaListItemDto>>
|
||||
{
|
||||
private readonly IPuntoDeVentaRepository _repo;
|
||||
|
||||
public ListPuntosDeVentaQueryHandler(IPuntoDeVentaRepository repo)
|
||||
{
|
||||
_repo = repo;
|
||||
}
|
||||
|
||||
public async Task<PagedResult<PuntoDeVentaListItemDto>> Handle(ListPuntosDeVentaQuery query)
|
||||
{
|
||||
var page = Math.Max(1, query.Page);
|
||||
var pageSize = Math.Clamp(query.PageSize, 1, 100);
|
||||
|
||||
var repoQuery = new PuntosDeVentaQuery(page, pageSize, query.MedioId, query.Activo);
|
||||
var paged = await _repo.GetPagedAsync(repoQuery);
|
||||
|
||||
var items = paged.Items
|
||||
.Select(p => new PuntoDeVentaListItemDto(p.Id, p.MedioId, p.NumeroAFIP, p.Nombre, p.Activo))
|
||||
.ToList();
|
||||
|
||||
return new PagedResult<PuntoDeVentaListItemDto>(items, paged.Page, paged.PageSize, paged.Total);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace SIGCM2.Application.PuntosDeVenta.List;
|
||||
|
||||
public sealed record PuntoDeVentaListItemDto(
|
||||
int Id,
|
||||
int MedioId,
|
||||
short NumeroAFIP,
|
||||
string Nombre,
|
||||
bool Activo);
|
||||
@@ -0,0 +1,5 @@
|
||||
using SIGCM2.Domain.Enums;
|
||||
|
||||
namespace SIGCM2.Application.PuntosDeVenta.ProximoNumero;
|
||||
|
||||
public sealed record GetProximoNumeroQuery(int PuntoDeVentaId, TipoComprobante TipoComprobante);
|
||||
@@ -0,0 +1,27 @@
|
||||
using SIGCM2.Application.Abstractions;
|
||||
using SIGCM2.Application.Abstractions.Persistence;
|
||||
|
||||
namespace SIGCM2.Application.PuntosDeVenta.ProximoNumero;
|
||||
|
||||
/// <summary>
|
||||
/// Consulta el próximo número disponible sin reservarlo (read-only, REQ-SEC-CMB-005).
|
||||
/// Retorna UltimoNumero+1; si no existe fila devuelve 1.
|
||||
/// </summary>
|
||||
public sealed class GetProximoNumeroQueryHandler : ICommandHandler<GetProximoNumeroQuery, ProximoNumeroDto>
|
||||
{
|
||||
private readonly IPuntoDeVentaRepository _repo;
|
||||
|
||||
public GetProximoNumeroQueryHandler(IPuntoDeVentaRepository repo)
|
||||
{
|
||||
_repo = repo;
|
||||
}
|
||||
|
||||
public async Task<ProximoNumeroDto> Handle(GetProximoNumeroQuery query)
|
||||
{
|
||||
var ultimoNumero = await _repo.GetUltimoNumeroAsync(query.PuntoDeVentaId, query.TipoComprobante);
|
||||
|
||||
var proximo = ultimoNumero.HasValue ? ultimoNumero.Value + 1 : 1;
|
||||
|
||||
return new ProximoNumeroDto(query.TipoComprobante, proximo);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
using SIGCM2.Domain.Enums;
|
||||
|
||||
namespace SIGCM2.Application.PuntosDeVenta.ProximoNumero;
|
||||
|
||||
public sealed record ProximoNumeroDto(TipoComprobante TipoComprobante, int ProximoNumero);
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace SIGCM2.Application.PuntosDeVenta.Reactivate;
|
||||
|
||||
public sealed record ReactivatePuntoDeVentaCommand(int Id);
|
||||
@@ -0,0 +1,60 @@
|
||||
using System.Transactions;
|
||||
using SIGCM2.Application.Abstractions;
|
||||
using SIGCM2.Application.Abstractions.Persistence;
|
||||
using SIGCM2.Application.Audit;
|
||||
using SIGCM2.Application.PuntosDeVenta.Deactivate;
|
||||
using SIGCM2.Domain.Entities;
|
||||
using SIGCM2.Domain.Exceptions;
|
||||
|
||||
namespace SIGCM2.Application.PuntosDeVenta.Reactivate;
|
||||
|
||||
public sealed class ReactivatePuntoDeVentaCommandHandler : ICommandHandler<ReactivatePuntoDeVentaCommand, PuntoDeVentaStatusDto>
|
||||
{
|
||||
private readonly IPuntoDeVentaRepository _repo;
|
||||
private readonly IMedioRepository _medioRepo;
|
||||
private readonly IAuditLogger _audit;
|
||||
|
||||
public ReactivatePuntoDeVentaCommandHandler(
|
||||
IPuntoDeVentaRepository repo,
|
||||
IMedioRepository medioRepo,
|
||||
IAuditLogger audit)
|
||||
{
|
||||
_repo = repo;
|
||||
_medioRepo = medioRepo;
|
||||
_audit = audit;
|
||||
}
|
||||
|
||||
public async Task<PuntoDeVentaStatusDto> Handle(ReactivatePuntoDeVentaCommand 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);
|
||||
|
||||
// Idempotent: already active → return as-is without writing an audit event
|
||||
if (target.Activo)
|
||||
return new PuntoDeVentaStatusDto(target.Id, target.NumeroAFIP, target.Activo);
|
||||
|
||||
var updated = target.WithActivo(true);
|
||||
|
||||
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.reactivate",
|
||||
targetType: "PuntoDeVenta",
|
||||
targetId: command.Id.ToString());
|
||||
|
||||
tx.Complete();
|
||||
|
||||
return new PuntoDeVentaStatusDto(updated.Id, updated.NumeroAFIP, updated.Activo);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
using SIGCM2.Domain.Enums;
|
||||
|
||||
namespace SIGCM2.Application.PuntosDeVenta.Reservar;
|
||||
|
||||
public sealed record ReservaNumeroDto(TipoComprobante TipoComprobante, int NumeroReservado);
|
||||
@@ -0,0 +1,5 @@
|
||||
using SIGCM2.Domain.Enums;
|
||||
|
||||
namespace SIGCM2.Application.PuntosDeVenta.Reservar;
|
||||
|
||||
public sealed record ReservarNumeroCommand(int PuntoDeVentaId, TipoComprobante TipoComprobante);
|
||||
@@ -0,0 +1,53 @@
|
||||
using SIGCM2.Application.Abstractions;
|
||||
using SIGCM2.Application.Abstractions.Persistence;
|
||||
using SIGCM2.Domain.Exceptions;
|
||||
|
||||
namespace SIGCM2.Application.PuntosDeVenta.Reservar;
|
||||
|
||||
/// <summary>
|
||||
/// Reserva el próximo número correlativo para (PdvId × TipoComprobante) ejecutando
|
||||
/// usp_ReservarNumeroComprobante vía el repositorio.
|
||||
///
|
||||
/// NOTAS DE DISEÑO (AD4, AD9):
|
||||
/// - NO se envuelve en TransactionScope: el SP ya es atómico bajo SERIALIZABLE.
|
||||
/// Un TransactionScope ambiente aquí escalaría a DTC → innecesario.
|
||||
/// - NO usa Polly: no está en el proyecto. Retry deadlock con bucle simple.
|
||||
/// - Infrastructure traduce SqlException 1205 → DeadlockTransientException.
|
||||
/// - Backoff en ms: [50, 150, 450] — 3 intentos máximo.
|
||||
/// - La auditoría de reservas corre solo vía Temporal Tables (AD8).
|
||||
/// </summary>
|
||||
public sealed class ReservarNumeroCommandHandler : ICommandHandler<ReservarNumeroCommand, ReservaNumeroDto>
|
||||
{
|
||||
private readonly IPuntoDeVentaRepository _repo;
|
||||
private readonly int[] _deadlockBackoffMs;
|
||||
|
||||
private static readonly int[] DefaultBackoffMs = [50, 150, 450];
|
||||
|
||||
public ReservarNumeroCommandHandler(IPuntoDeVentaRepository repo)
|
||||
: this(repo, DefaultBackoffMs) { }
|
||||
|
||||
/// <summary>Constructor with custom backoff for testing (e.g., [0,0,0] for fast tests).</summary>
|
||||
public ReservarNumeroCommandHandler(IPuntoDeVentaRepository repo, int[] deadlockBackoffMs)
|
||||
{
|
||||
_repo = repo;
|
||||
_deadlockBackoffMs = deadlockBackoffMs;
|
||||
}
|
||||
|
||||
public async Task<ReservaNumeroDto> Handle(ReservarNumeroCommand command)
|
||||
{
|
||||
for (var i = 0; ; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
var numero = await _repo.ReservarNumeroAsync(command.PuntoDeVentaId, command.TipoComprobante);
|
||||
return new ReservaNumeroDto(command.TipoComprobante, numero);
|
||||
}
|
||||
catch (DeadlockTransientException) when (i < _deadlockBackoffMs.Length)
|
||||
{
|
||||
// Deadlock — retry with backoff
|
||||
await Task.Delay(_deadlockBackoffMs[i]);
|
||||
}
|
||||
// All other exceptions bubble up immediately
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace SIGCM2.Application.PuntosDeVenta.Update;
|
||||
|
||||
public sealed record UpdatePuntoDeVentaCommand(
|
||||
int Id,
|
||||
string Nombre,
|
||||
short NumeroAFIP,
|
||||
string? Descripcion);
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user