2026-04-17 12:28:11 -03:00
|
|
|
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;
|
2026-04-18 10:12:17 -03:00
|
|
|
private readonly TimeProvider _timeProvider;
|
2026-04-17 12:28:11 -03:00
|
|
|
|
|
|
|
|
public ReactivatePuntoDeVentaCommandHandler(
|
|
|
|
|
IPuntoDeVentaRepository repo,
|
|
|
|
|
IMedioRepository medioRepo,
|
2026-04-18 10:12:17 -03:00
|
|
|
IAuditLogger audit,
|
|
|
|
|
TimeProvider timeProvider)
|
2026-04-17 12:28:11 -03:00
|
|
|
{
|
|
|
|
|
_repo = repo;
|
|
|
|
|
_medioRepo = medioRepo;
|
|
|
|
|
_audit = audit;
|
2026-04-18 10:12:17 -03:00
|
|
|
_timeProvider = timeProvider;
|
2026-04-17 12:28:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2026-04-18 10:12:17 -03:00
|
|
|
var now = _timeProvider.GetUtcNow().UtcDateTime;
|
|
|
|
|
var updated = target.WithActivo(true, now);
|
2026-04-17 12:28:11 -03:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|