feat(application): repository abstraction + DTOs + validators + handlers CRUD PuntosDeVenta con auditoría + retry deadlock
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
using SIGCM2.Application.Common;
|
||||
using SIGCM2.Domain.Entities;
|
||||
using SIGCM2.Domain.Enums;
|
||||
|
||||
namespace SIGCM2.Application.Abstractions.Persistence;
|
||||
|
||||
public interface IPuntoDeVentaRepository
|
||||
{
|
||||
Task<int> AddAsync(PuntoDeVenta pdv, CancellationToken ct = default);
|
||||
Task<PuntoDeVenta?> GetByIdAsync(int id, CancellationToken ct = default);
|
||||
Task<bool> ExistsByNumeroAFIPInMedioAsync(int medioId, short numeroAFIP, int? excludeId = null, CancellationToken ct = default);
|
||||
Task UpdateAsync(PuntoDeVenta pdv, CancellationToken ct = default);
|
||||
Task<PagedResult<PuntoDeVenta>> GetPagedAsync(PuntosDeVentaQuery q, CancellationToken ct = default);
|
||||
Task<int> ReservarNumeroAsync(int puntoDeVentaId, TipoComprobante tipo, CancellationToken ct = default);
|
||||
Task<int?> GetUltimoNumeroAsync(int puntoDeVentaId, TipoComprobante tipo, CancellationToken ct = default);
|
||||
}
|
||||
9
src/api/SIGCM2.Application/Common/PuntosDeVentaQuery.cs
Normal file
9
src/api/SIGCM2.Application/Common/PuntosDeVentaQuery.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace SIGCM2.Application.Common;
|
||||
|
||||
/// <summary>Query parameters for listing puntos de venta with optional filters and paging.</summary>
|
||||
public sealed record PuntosDeVentaQuery(
|
||||
int Page,
|
||||
int PageSize,
|
||||
int? MedioId,
|
||||
bool? Activo
|
||||
);
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace SIGCM2.Domain.Exceptions;
|
||||
|
||||
/// <summary>
|
||||
/// Thrown by Infrastructure when a database deadlock (SQL 1205) is detected.
|
||||
/// Allows Application handlers to retry without referencing SqlClient.
|
||||
/// </summary>
|
||||
public sealed class DeadlockTransientException : DomainException
|
||||
{
|
||||
public DeadlockTransientException()
|
||||
: base("Se detectó un deadlock en la base de datos. Reintentando operación.") { }
|
||||
|
||||
public DeadlockTransientException(Exception innerException)
|
||||
: base("Se detectó un deadlock en la base de datos. Reintentando operación.", innerException) { }
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
using NSubstitute;
|
||||
using SIGCM2.Application.Abstractions.Persistence;
|
||||
using SIGCM2.Application.Audit;
|
||||
using SIGCM2.Application.PuntosDeVenta.Create;
|
||||
using SIGCM2.Domain.Entities;
|
||||
using SIGCM2.Domain.Exceptions;
|
||||
|
||||
namespace SIGCM2.Application.Tests.PuntosDeVenta.Create;
|
||||
|
||||
public class CreatePuntoDeVentaCommandHandlerTests
|
||||
{
|
||||
private readonly IPuntoDeVentaRepository _repo = Substitute.For<IPuntoDeVentaRepository>();
|
||||
private readonly IMedioRepository _medioRepo = Substitute.For<IMedioRepository>();
|
||||
private readonly IAuditLogger _audit = Substitute.For<IAuditLogger>();
|
||||
private readonly CreatePuntoDeVentaCommandHandler _handler;
|
||||
|
||||
private static Medio MakeMedio(int id = 5, bool activo = true)
|
||||
=> new(id, "COD" + id, "Medio " + id, TipoMedio.Diario, null, activo, DateTime.UtcNow, null);
|
||||
|
||||
private static CreatePuntoDeVentaCommand ValidCommand() => new(
|
||||
MedioId: 5,
|
||||
NumeroAFIP: 1,
|
||||
Nombre: "PdV Central",
|
||||
Descripcion: null);
|
||||
|
||||
public CreatePuntoDeVentaCommandHandlerTests()
|
||||
{
|
||||
_handler = new CreatePuntoDeVentaCommandHandler(_repo, _medioRepo, _audit);
|
||||
_medioRepo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(MakeMedio(5));
|
||||
_repo.ExistsByNumeroAFIPInMedioAsync(Arg.Any<int>(), Arg.Any<short>(), Arg.Any<int?>(), Arg.Any<CancellationToken>()).Returns(false);
|
||||
_repo.AddAsync(Arg.Any<PuntoDeVenta>(), Arg.Any<CancellationToken>()).Returns(10);
|
||||
}
|
||||
|
||||
// ── medio not found → throws ─────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_MedioNotFound_ThrowsMedioNotFoundException()
|
||||
{
|
||||
_medioRepo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns((Medio?)null);
|
||||
|
||||
await Assert.ThrowsAsync<MedioNotFoundException>(
|
||||
() => _handler.Handle(ValidCommand()));
|
||||
}
|
||||
|
||||
// ── medio inactivo → throws ──────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_MedioInactivo_ThrowsMedioInactivoException()
|
||||
{
|
||||
_medioRepo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(MakeMedio(5, false));
|
||||
|
||||
await Assert.ThrowsAsync<MedioInactivoException>(
|
||||
() => _handler.Handle(ValidCommand()));
|
||||
}
|
||||
|
||||
// ── NumeroAFIP duplicado → throws ────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_NumeroAFIPDuplicado_ThrowsNumeroAFIPDuplicadoException()
|
||||
{
|
||||
_repo.ExistsByNumeroAFIPInMedioAsync(5, 1, null, Arg.Any<CancellationToken>()).Returns(true);
|
||||
|
||||
await Assert.ThrowsAsync<NumeroAFIPDuplicadoException>(
|
||||
() => _handler.Handle(ValidCommand()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_NumeroAFIPDuplicado_DoesNotCallAddAsync()
|
||||
{
|
||||
_repo.ExistsByNumeroAFIPInMedioAsync(Arg.Any<int>(), Arg.Any<short>(), Arg.Any<int?>(), Arg.Any<CancellationToken>()).Returns(true);
|
||||
|
||||
try { await _handler.Handle(ValidCommand()); } catch (NumeroAFIPDuplicadoException) { }
|
||||
|
||||
await _repo.DidNotReceive().AddAsync(Arg.Any<PuntoDeVenta>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
// ── happy path ───────────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_HappyPath_ReturnsDtoWithIdFromRepository()
|
||||
{
|
||||
var result = await _handler.Handle(ValidCommand());
|
||||
|
||||
Assert.Equal(10, result.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_HappyPath_DtoContainsCorrectFields()
|
||||
{
|
||||
var result = await _handler.Handle(ValidCommand());
|
||||
|
||||
Assert.Equal(5, result.MedioId);
|
||||
Assert.Equal(1, result.NumeroAFIP);
|
||||
Assert.Equal("PdV Central", result.Nombre);
|
||||
Assert.True(result.Activo);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_HappyPath_CallsAuditWithCreateAction()
|
||||
{
|
||||
await _handler.Handle(ValidCommand());
|
||||
|
||||
await _audit.Received(1).LogAsync(
|
||||
action: "punto_de_venta.create",
|
||||
targetType: "PuntoDeVenta",
|
||||
targetId: "10",
|
||||
metadata: Arg.Any<object?>(),
|
||||
ct: Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
// ── audit fail-closed ────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_AuditLoggerThrows_ExceptionBubblesUpAndAddNotCommitted()
|
||||
{
|
||||
_audit.LogAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<object?>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Task.FromException(new InvalidOperationException("audit fail")));
|
||||
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(
|
||||
() => _handler.Handle(ValidCommand()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
using NSubstitute;
|
||||
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.Tests.PuntosDeVenta.Deactivate;
|
||||
|
||||
public class DeactivatePuntoDeVentaCommandHandlerTests
|
||||
{
|
||||
private readonly IPuntoDeVentaRepository _repo = Substitute.For<IPuntoDeVentaRepository>();
|
||||
private readonly IMedioRepository _medioRepo = Substitute.For<IMedioRepository>();
|
||||
private readonly IAuditLogger _audit = Substitute.For<IAuditLogger>();
|
||||
private readonly DeactivatePuntoDeVentaCommandHandler _handler;
|
||||
|
||||
private static PuntoDeVenta MakePdv(int id = 10, int medioId = 5, bool activo = true)
|
||||
=> new(id, medioId, 1, "PdV Test", null, activo, DateTime.UtcNow, null);
|
||||
|
||||
private static Medio MakeMedio(int id = 5, bool activo = true)
|
||||
=> new(id, "COD" + id, "Medio " + id, TipoMedio.Diario, null, activo, DateTime.UtcNow, null);
|
||||
|
||||
public DeactivatePuntoDeVentaCommandHandlerTests()
|
||||
{
|
||||
_handler = new DeactivatePuntoDeVentaCommandHandler(_repo, _medioRepo, _audit);
|
||||
_medioRepo.GetByIdAsync(Arg.Any<int>(), Arg.Any<CancellationToken>()).Returns(MakeMedio(5, true));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_NotFound_ThrowsPuntoDeVentaNotFoundException()
|
||||
{
|
||||
_repo.GetByIdAsync(999, Arg.Any<CancellationToken>()).Returns((PuntoDeVenta?)null);
|
||||
|
||||
await Assert.ThrowsAsync<PuntoDeVentaNotFoundException>(
|
||||
() => _handler.Handle(new DeactivatePuntoDeVentaCommand(999)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_AlreadyInactive_IsIdempotentAndDoesNotCallUpdateAsync()
|
||||
{
|
||||
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: false));
|
||||
|
||||
await _handler.Handle(new DeactivatePuntoDeVentaCommand(10));
|
||||
|
||||
await _repo.DidNotReceive().UpdateAsync(Arg.Any<PuntoDeVenta>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_AlreadyInactive_DoesNotWriteAuditEvent()
|
||||
{
|
||||
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: false));
|
||||
|
||||
await _handler.Handle(new DeactivatePuntoDeVentaCommand(10));
|
||||
|
||||
await _audit.DidNotReceive().LogAsync(
|
||||
Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(),
|
||||
Arg.Any<object?>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_ActivePdv_CallsUpdateAsyncWithInactiveEntity()
|
||||
{
|
||||
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: true));
|
||||
|
||||
await _handler.Handle(new DeactivatePuntoDeVentaCommand(10));
|
||||
|
||||
await _repo.Received(1).UpdateAsync(
|
||||
Arg.Is<PuntoDeVenta>(p => !p.Activo),
|
||||
Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_ActivePdv_WritesAuditWithDeactivateAction()
|
||||
{
|
||||
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: true));
|
||||
|
||||
await _handler.Handle(new DeactivatePuntoDeVentaCommand(10));
|
||||
|
||||
await _audit.Received(1).LogAsync(
|
||||
action: "punto_de_venta.deactivate",
|
||||
targetType: "PuntoDeVenta",
|
||||
targetId: "10",
|
||||
metadata: Arg.Any<object?>(),
|
||||
ct: Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using NSubstitute;
|
||||
using SIGCM2.Application.Abstractions.Persistence;
|
||||
using SIGCM2.Application.PuntosDeVenta.GetById;
|
||||
using SIGCM2.Domain.Entities;
|
||||
using SIGCM2.Domain.Exceptions;
|
||||
|
||||
namespace SIGCM2.Application.Tests.PuntosDeVenta.GetById;
|
||||
|
||||
public class GetPuntoDeVentaByIdQueryHandlerTests
|
||||
{
|
||||
private readonly IPuntoDeVentaRepository _repo = Substitute.For<IPuntoDeVentaRepository>();
|
||||
private readonly GetPuntoDeVentaByIdQueryHandler _handler;
|
||||
|
||||
public GetPuntoDeVentaByIdQueryHandlerTests()
|
||||
{
|
||||
_handler = new GetPuntoDeVentaByIdQueryHandler(_repo);
|
||||
}
|
||||
|
||||
private static PuntoDeVenta MakePdv(int id = 5) =>
|
||||
new(id, 2, 3, "PdV " + id, "Desc", true, DateTime.UtcNow, null);
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_NotFound_ThrowsPuntoDeVentaNotFoundException()
|
||||
{
|
||||
_repo.GetByIdAsync(999, Arg.Any<CancellationToken>()).Returns((PuntoDeVenta?)null);
|
||||
|
||||
await Assert.ThrowsAsync<PuntoDeVentaNotFoundException>(
|
||||
() => _handler.Handle(new GetPuntoDeVentaByIdQuery(999)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_HappyPath_ReturnsDtoWithCorrectFields()
|
||||
{
|
||||
var pdv = MakePdv(5);
|
||||
_repo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(pdv);
|
||||
|
||||
var result = await _handler.Handle(new GetPuntoDeVentaByIdQuery(5));
|
||||
|
||||
Assert.Equal(5, result.Id);
|
||||
Assert.Equal(2, result.MedioId);
|
||||
Assert.Equal(3, result.NumeroAFIP);
|
||||
Assert.Equal("PdV 5", result.Nombre);
|
||||
Assert.Equal("Desc", result.Descripcion);
|
||||
Assert.True(result.Activo);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
using NSubstitute;
|
||||
using SIGCM2.Application.Abstractions.Persistence;
|
||||
using SIGCM2.Application.Common;
|
||||
using SIGCM2.Application.PuntosDeVenta.List;
|
||||
using SIGCM2.Domain.Entities;
|
||||
|
||||
namespace SIGCM2.Application.Tests.PuntosDeVenta.List;
|
||||
|
||||
public class ListPuntosDeVentaQueryHandlerTests
|
||||
{
|
||||
private readonly IPuntoDeVentaRepository _repo = Substitute.For<IPuntoDeVentaRepository>();
|
||||
private readonly ListPuntosDeVentaQueryHandler _handler;
|
||||
|
||||
public ListPuntosDeVentaQueryHandlerTests()
|
||||
{
|
||||
_handler = new ListPuntosDeVentaQueryHandler(_repo);
|
||||
}
|
||||
|
||||
private static PuntoDeVenta MakePdv(int id) =>
|
||||
new(id, 5, (short)id, "PdV " + id, null, true, DateTime.UtcNow, null);
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_ReturnsPagedDtoItems()
|
||||
{
|
||||
var items = new List<PuntoDeVenta> { MakePdv(1), MakePdv(2) };
|
||||
var pagedResult = new PagedResult<PuntoDeVenta>(items, 1, 20, 2);
|
||||
|
||||
_repo.GetPagedAsync(Arg.Any<PuntosDeVentaQuery>(), Arg.Any<CancellationToken>())
|
||||
.Returns(pagedResult);
|
||||
|
||||
var result = await _handler.Handle(new ListPuntosDeVentaQuery(1, 20, null, null));
|
||||
|
||||
Assert.Equal(2, result.Total);
|
||||
Assert.Equal(2, result.Items.Count);
|
||||
Assert.Equal(1, result.Items[0].NumeroAFIP);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_ClampsPageSizeToMax100()
|
||||
{
|
||||
_repo.GetPagedAsync(Arg.Any<PuntosDeVentaQuery>(), Arg.Any<CancellationToken>())
|
||||
.Returns(new PagedResult<PuntoDeVenta>([], 1, 100, 0));
|
||||
|
||||
await _handler.Handle(new ListPuntosDeVentaQuery(1, 500, null, null));
|
||||
|
||||
await _repo.Received(1).GetPagedAsync(
|
||||
Arg.Is<PuntosDeVentaQuery>(q => q.PageSize == 100),
|
||||
Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_ClampsPageToMin1()
|
||||
{
|
||||
_repo.GetPagedAsync(Arg.Any<PuntosDeVentaQuery>(), Arg.Any<CancellationToken>())
|
||||
.Returns(new PagedResult<PuntoDeVenta>([], 1, 20, 0));
|
||||
|
||||
await _handler.Handle(new ListPuntosDeVentaQuery(0, 20, null, null));
|
||||
|
||||
await _repo.Received(1).GetPagedAsync(
|
||||
Arg.Is<PuntosDeVentaQuery>(q => q.Page == 1),
|
||||
Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_FiltersByMedioId()
|
||||
{
|
||||
_repo.GetPagedAsync(Arg.Any<PuntosDeVentaQuery>(), Arg.Any<CancellationToken>())
|
||||
.Returns(new PagedResult<PuntoDeVenta>([], 1, 20, 0));
|
||||
|
||||
await _handler.Handle(new ListPuntosDeVentaQuery(1, 20, MedioId: 5, null));
|
||||
|
||||
await _repo.Received(1).GetPagedAsync(
|
||||
Arg.Is<PuntosDeVentaQuery>(q => q.MedioId == 5),
|
||||
Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using NSubstitute;
|
||||
using SIGCM2.Application.Abstractions.Persistence;
|
||||
using SIGCM2.Application.PuntosDeVenta.ProximoNumero;
|
||||
using SIGCM2.Domain.Enums;
|
||||
|
||||
namespace SIGCM2.Application.Tests.PuntosDeVenta.ProximoNumero;
|
||||
|
||||
public class GetProximoNumeroQueryHandlerTests
|
||||
{
|
||||
private readonly IPuntoDeVentaRepository _repo = Substitute.For<IPuntoDeVentaRepository>();
|
||||
private readonly GetProximoNumeroQueryHandler _handler;
|
||||
|
||||
public GetProximoNumeroQueryHandlerTests()
|
||||
{
|
||||
_handler = new GetProximoNumeroQueryHandler(_repo);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_ExistingSequence_ReturnsUltimoNumeroMasUno()
|
||||
{
|
||||
_repo.GetUltimoNumeroAsync(10, TipoComprobante.FacturaA, Arg.Any<CancellationToken>())
|
||||
.Returns(7);
|
||||
|
||||
var result = await _handler.Handle(new GetProximoNumeroQuery(10, TipoComprobante.FacturaA));
|
||||
|
||||
Assert.Equal(TipoComprobante.FacturaA, result.TipoComprobante);
|
||||
Assert.Equal(8, result.ProximoNumero);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_NoExistingSequence_ReturnsOne()
|
||||
{
|
||||
_repo.GetUltimoNumeroAsync(10, TipoComprobante.FacturaB, Arg.Any<CancellationToken>())
|
||||
.Returns((int?)null);
|
||||
|
||||
var result = await _handler.Handle(new GetProximoNumeroQuery(10, TipoComprobante.FacturaB));
|
||||
|
||||
Assert.Equal(1, result.ProximoNumero);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_DoesNotCallReservarNumero()
|
||||
{
|
||||
_repo.GetUltimoNumeroAsync(10, TipoComprobante.FacturaA, Arg.Any<CancellationToken>())
|
||||
.Returns(5);
|
||||
|
||||
await _handler.Handle(new GetProximoNumeroQuery(10, TipoComprobante.FacturaA));
|
||||
|
||||
await _repo.DidNotReceive().ReservarNumeroAsync(
|
||||
Arg.Any<int>(), Arg.Any<TipoComprobante>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
using NSubstitute;
|
||||
using SIGCM2.Application.Abstractions.Persistence;
|
||||
using SIGCM2.Application.Audit;
|
||||
using SIGCM2.Application.PuntosDeVenta.Deactivate;
|
||||
using SIGCM2.Application.PuntosDeVenta.Reactivate;
|
||||
using SIGCM2.Domain.Entities;
|
||||
using SIGCM2.Domain.Exceptions;
|
||||
|
||||
namespace SIGCM2.Application.Tests.PuntosDeVenta.Reactivate;
|
||||
|
||||
public class ReactivatePuntoDeVentaCommandHandlerTests
|
||||
{
|
||||
private readonly IPuntoDeVentaRepository _repo = Substitute.For<IPuntoDeVentaRepository>();
|
||||
private readonly IMedioRepository _medioRepo = Substitute.For<IMedioRepository>();
|
||||
private readonly IAuditLogger _audit = Substitute.For<IAuditLogger>();
|
||||
private readonly ReactivatePuntoDeVentaCommandHandler _handler;
|
||||
|
||||
private static PuntoDeVenta MakePdv(int id = 10, int medioId = 5, bool activo = false)
|
||||
=> new(id, medioId, 1, "PdV Test", null, activo, DateTime.UtcNow, null);
|
||||
|
||||
private static Medio MakeMedio(int id = 5, bool activo = true)
|
||||
=> new(id, "COD" + id, "Medio " + id, TipoMedio.Diario, null, activo, DateTime.UtcNow, null);
|
||||
|
||||
public ReactivatePuntoDeVentaCommandHandlerTests()
|
||||
{
|
||||
_handler = new ReactivatePuntoDeVentaCommandHandler(_repo, _medioRepo, _audit);
|
||||
_medioRepo.GetByIdAsync(Arg.Any<int>(), Arg.Any<CancellationToken>()).Returns(MakeMedio(5, true));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_NotFound_ThrowsPuntoDeVentaNotFoundException()
|
||||
{
|
||||
_repo.GetByIdAsync(999, Arg.Any<CancellationToken>()).Returns((PuntoDeVenta?)null);
|
||||
|
||||
await Assert.ThrowsAsync<PuntoDeVentaNotFoundException>(
|
||||
() => _handler.Handle(new ReactivatePuntoDeVentaCommand(999)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_MedioInactivo_ThrowsMedioInactivoException()
|
||||
{
|
||||
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: false));
|
||||
_medioRepo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(MakeMedio(5, false));
|
||||
|
||||
await Assert.ThrowsAsync<MedioInactivoException>(
|
||||
() => _handler.Handle(new ReactivatePuntoDeVentaCommand(10)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_AlreadyActive_IsIdempotentAndDoesNotCallUpdateAsync()
|
||||
{
|
||||
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: true));
|
||||
|
||||
await _handler.Handle(new ReactivatePuntoDeVentaCommand(10));
|
||||
|
||||
await _repo.DidNotReceive().UpdateAsync(Arg.Any<PuntoDeVenta>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_InactivePdv_CallsUpdateAsyncWithActiveEntity()
|
||||
{
|
||||
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: false));
|
||||
|
||||
await _handler.Handle(new ReactivatePuntoDeVentaCommand(10));
|
||||
|
||||
await _repo.Received(1).UpdateAsync(
|
||||
Arg.Is<PuntoDeVenta>(p => p.Activo),
|
||||
Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_InactivePdv_WritesAuditWithReactivateAction()
|
||||
{
|
||||
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: false));
|
||||
|
||||
await _handler.Handle(new ReactivatePuntoDeVentaCommand(10));
|
||||
|
||||
await _audit.Received(1).LogAsync(
|
||||
action: "punto_de_venta.reactivate",
|
||||
targetType: "PuntoDeVenta",
|
||||
targetId: "10",
|
||||
metadata: Arg.Any<object?>(),
|
||||
ct: Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_MedioInactivo_NoAuditLogged()
|
||||
{
|
||||
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: false));
|
||||
_medioRepo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(MakeMedio(5, false));
|
||||
|
||||
try { await _handler.Handle(new ReactivatePuntoDeVentaCommand(10)); } catch (MedioInactivoException) { }
|
||||
|
||||
await _audit.DidNotReceive().LogAsync(
|
||||
Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(),
|
||||
Arg.Any<object?>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
using NSubstitute;
|
||||
using NSubstitute.ExceptionExtensions;
|
||||
using SIGCM2.Application.Abstractions.Persistence;
|
||||
using SIGCM2.Application.PuntosDeVenta.Reservar;
|
||||
using SIGCM2.Domain.Enums;
|
||||
using SIGCM2.Domain.Exceptions;
|
||||
|
||||
namespace SIGCM2.Application.Tests.PuntosDeVenta.Reservar;
|
||||
|
||||
public class ReservarNumeroCommandHandlerTests
|
||||
{
|
||||
private readonly IPuntoDeVentaRepository _repo = Substitute.For<IPuntoDeVentaRepository>();
|
||||
private readonly ReservarNumeroCommandHandler _handler;
|
||||
|
||||
private static readonly ReservarNumeroCommand ValidCommand =
|
||||
new(PuntoDeVentaId: 10, TipoComprobante: TipoComprobante.FacturaA);
|
||||
|
||||
public ReservarNumeroCommandHandlerTests()
|
||||
{
|
||||
// Use delay = 0 for fast tests
|
||||
_handler = new ReservarNumeroCommandHandler(_repo, deadlockBackoffMs: [0, 0, 0]);
|
||||
}
|
||||
|
||||
// ── happy path ───────────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_HappyPath_ReturnsNumeroReservado()
|
||||
{
|
||||
_repo.ReservarNumeroAsync(10, TipoComprobante.FacturaA, Arg.Any<CancellationToken>())
|
||||
.Returns(7);
|
||||
|
||||
var result = await _handler.Handle(ValidCommand);
|
||||
|
||||
Assert.Equal(TipoComprobante.FacturaA, result.TipoComprobante);
|
||||
Assert.Equal(7, result.NumeroReservado);
|
||||
}
|
||||
|
||||
// ── retry deadlock ────────────────────────────────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_DeadlockTwiceThenSucceeds_ReturnsResult()
|
||||
{
|
||||
var deadlock = new DeadlockTransientException();
|
||||
|
||||
_repo.ReservarNumeroAsync(10, TipoComprobante.FacturaA, Arg.Any<CancellationToken>())
|
||||
.Returns(
|
||||
_ => Task.FromException<int>(deadlock),
|
||||
_ => Task.FromException<int>(deadlock),
|
||||
_ => Task.FromResult(3));
|
||||
|
||||
var result = await _handler.Handle(ValidCommand);
|
||||
|
||||
Assert.Equal(3, result.NumeroReservado);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_DeadlockThreeTimes_BubblesUpDeadlockException()
|
||||
{
|
||||
var deadlock = new DeadlockTransientException();
|
||||
|
||||
_repo.ReservarNumeroAsync(10, TipoComprobante.FacturaA, Arg.Any<CancellationToken>())
|
||||
.Returns(
|
||||
_ => Task.FromException<int>(deadlock),
|
||||
_ => Task.FromException<int>(deadlock),
|
||||
_ => Task.FromException<int>(deadlock));
|
||||
|
||||
await Assert.ThrowsAsync<DeadlockTransientException>(
|
||||
() => _handler.Handle(ValidCommand));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_DeadlockExhaustsBackoff_TriedFourTimesTotal()
|
||||
{
|
||||
// backoff = [0,0,0] → 3 retries → 4 total attempts (1 initial + 3 retries)
|
||||
var deadlock = new DeadlockTransientException();
|
||||
|
||||
_repo.ReservarNumeroAsync(10, TipoComprobante.FacturaA, Arg.Any<CancellationToken>())
|
||||
.Returns(_ => Task.FromException<int>(deadlock));
|
||||
|
||||
try { await _handler.Handle(ValidCommand); } catch (DeadlockTransientException) { }
|
||||
|
||||
await _repo.Received(4).ReservarNumeroAsync(
|
||||
Arg.Any<int>(), Arg.Any<TipoComprobante>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
// ── domain exceptions bubble up without retry ─────────────────────────────
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_PuntoDeVentaInactivo_BubblesUpImmediately()
|
||||
{
|
||||
_repo.ReservarNumeroAsync(10, TipoComprobante.FacturaA, Arg.Any<CancellationToken>())
|
||||
.Throws(new PuntoDeVentaInactivoException(10));
|
||||
|
||||
await Assert.ThrowsAsync<PuntoDeVentaInactivoException>(
|
||||
() => _handler.Handle(ValidCommand));
|
||||
|
||||
await _repo.Received(1).ReservarNumeroAsync(
|
||||
Arg.Any<int>(), Arg.Any<TipoComprobante>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_MedioInactivo_BubblesUpImmediately()
|
||||
{
|
||||
_repo.ReservarNumeroAsync(10, TipoComprobante.FacturaA, Arg.Any<CancellationToken>())
|
||||
.Throws(new MedioInactivoException(5));
|
||||
|
||||
await Assert.ThrowsAsync<MedioInactivoException>(
|
||||
() => _handler.Handle(ValidCommand));
|
||||
|
||||
await _repo.Received(1).ReservarNumeroAsync(
|
||||
Arg.Any<int>(), Arg.Any<TipoComprobante>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_PdvNotFound_BubblesUpImmediately()
|
||||
{
|
||||
_repo.ReservarNumeroAsync(10, TipoComprobante.FacturaA, Arg.Any<CancellationToken>())
|
||||
.Throws(new PuntoDeVentaNotFoundException(10));
|
||||
|
||||
await Assert.ThrowsAsync<PuntoDeVentaNotFoundException>(
|
||||
() => _handler.Handle(ValidCommand));
|
||||
|
||||
await _repo.Received(1).ReservarNumeroAsync(
|
||||
Arg.Any<int>(), Arg.Any<TipoComprobante>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
using NSubstitute;
|
||||
using SIGCM2.Application.Abstractions.Persistence;
|
||||
using SIGCM2.Application.Audit;
|
||||
using SIGCM2.Application.PuntosDeVenta.Update;
|
||||
using SIGCM2.Domain.Entities;
|
||||
using SIGCM2.Domain.Exceptions;
|
||||
|
||||
namespace SIGCM2.Application.Tests.PuntosDeVenta.Update;
|
||||
|
||||
public class UpdatePuntoDeVentaCommandHandlerTests
|
||||
{
|
||||
private readonly IPuntoDeVentaRepository _repo = Substitute.For<IPuntoDeVentaRepository>();
|
||||
private readonly IMedioRepository _medioRepo = Substitute.For<IMedioRepository>();
|
||||
private readonly IAuditLogger _audit = Substitute.For<IAuditLogger>();
|
||||
private readonly UpdatePuntoDeVentaCommandHandler _handler;
|
||||
|
||||
private static PuntoDeVenta MakePdv(int id = 10, int medioId = 5, bool activo = true)
|
||||
=> new(id, medioId, 1, "Original", null, activo, DateTime.UtcNow, null);
|
||||
|
||||
private static Medio MakeMedio(int id = 5, bool activo = true)
|
||||
=> new(id, "COD" + id, "Medio " + id, TipoMedio.Diario, null, activo, DateTime.UtcNow, null);
|
||||
|
||||
private static UpdatePuntoDeVentaCommand ValidCommand(int id = 10) =>
|
||||
new(Id: id, Nombre: "Nuevo Nombre", NumeroAFIP: 3, Descripcion: null);
|
||||
|
||||
public UpdatePuntoDeVentaCommandHandlerTests()
|
||||
{
|
||||
_handler = new UpdatePuntoDeVentaCommandHandler(_repo, _medioRepo, _audit);
|
||||
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10));
|
||||
_medioRepo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(MakeMedio(5));
|
||||
_repo.ExistsByNumeroAFIPInMedioAsync(Arg.Any<int>(), Arg.Any<short>(), Arg.Any<int?>(), Arg.Any<CancellationToken>()).Returns(false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_NotFound_ThrowsPuntoDeVentaNotFoundException()
|
||||
{
|
||||
_repo.GetByIdAsync(999, Arg.Any<CancellationToken>()).Returns((PuntoDeVenta?)null);
|
||||
|
||||
await Assert.ThrowsAsync<PuntoDeVentaNotFoundException>(
|
||||
() => _handler.Handle(new UpdatePuntoDeVentaCommand(999, "X", 1, null)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_MedioInactivo_ThrowsMedioInactivoException()
|
||||
{
|
||||
_medioRepo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(MakeMedio(5, false));
|
||||
|
||||
await Assert.ThrowsAsync<MedioInactivoException>(
|
||||
() => _handler.Handle(ValidCommand()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_NumeroAFIPDuplicado_ThrowsNumeroAFIPDuplicadoException()
|
||||
{
|
||||
_repo.ExistsByNumeroAFIPInMedioAsync(5, 3, 10, Arg.Any<CancellationToken>()).Returns(true);
|
||||
|
||||
await Assert.ThrowsAsync<NumeroAFIPDuplicadoException>(
|
||||
() => _handler.Handle(ValidCommand()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_HappyPath_CallsUpdateAsyncOnce()
|
||||
{
|
||||
await _handler.Handle(ValidCommand());
|
||||
|
||||
await _repo.Received(1).UpdateAsync(Arg.Any<PuntoDeVenta>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_HappyPath_ReturnsDtoWithUpdatedFields()
|
||||
{
|
||||
var result = await _handler.Handle(ValidCommand());
|
||||
|
||||
Assert.Equal(10, result.Id);
|
||||
Assert.Equal("Nuevo Nombre", result.Nombre);
|
||||
Assert.Equal(3, result.NumeroAFIP);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_HappyPath_CallsAuditWithUpdateAction()
|
||||
{
|
||||
await _handler.Handle(ValidCommand());
|
||||
|
||||
await _audit.Received(1).LogAsync(
|
||||
action: "punto_de_venta.update",
|
||||
targetType: "PuntoDeVenta",
|
||||
targetId: "10",
|
||||
metadata: Arg.Any<object?>(),
|
||||
ct: Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_MedioInactivo_NoAuditLogged()
|
||||
{
|
||||
_medioRepo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(MakeMedio(5, false));
|
||||
|
||||
try { await _handler.Handle(ValidCommand()); } catch (MedioInactivoException) { }
|
||||
|
||||
await _audit.DidNotReceive().LogAsync(
|
||||
Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(),
|
||||
Arg.Any<object?>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user