4 command handlers del módulo Roles + Permisos ahora auditan:
| Handler | Action |
|--------------------------------------|------------------------|
| CreateRolCommandHandler | rol.create |
| UpdateRolCommandHandler | rol.update |
| DeactivateRolCommandHandler | rol.deactivate |
| AssignPermisosToRolCommandHandler | rol.permisos_update |
Mismo patrón que B7 (using block + post-commit reads outside scope).
Metadata:
- rol.create: after={Codigo, Nombre, Descripcion}
- rol.update: {before, after} diff
- rol.permisos_update: {before, after} con arrays de codigos ordenados
AssignPermisosToRolCommandHandler captura 'before' leyendo
GetByRolCodigoAsync antes del TransactionScope para poder emitir el diff.
4 test classes actualizados con mock de IAuditLogger.
Suite: 378/378 Application.Tests + 141/141 Api.Tests = 519/519 passing.
Refs: sdd/udt-010-auditoria-trazabilidad/{spec#REQ-RM-AUD, design, tasks#B8}
57 lines
1.9 KiB
C#
57 lines
1.9 KiB
C#
using System.Transactions;
|
|
using SIGCM2.Application.Abstractions;
|
|
using SIGCM2.Application.Abstractions.Persistence;
|
|
using SIGCM2.Application.Audit;
|
|
using SIGCM2.Application.Roles.Dtos;
|
|
using SIGCM2.Domain.Entities;
|
|
using SIGCM2.Domain.Exceptions;
|
|
|
|
namespace SIGCM2.Application.Roles.Create;
|
|
|
|
public sealed class CreateRolCommandHandler : ICommandHandler<CreateRolCommand, RolCreatedDto>
|
|
{
|
|
private readonly IRolRepository _repository;
|
|
private readonly IAuditLogger _audit;
|
|
|
|
public CreateRolCommandHandler(IRolRepository repository, IAuditLogger audit)
|
|
{
|
|
_repository = repository;
|
|
_audit = audit;
|
|
}
|
|
|
|
public async Task<RolCreatedDto> Handle(CreateRolCommand command)
|
|
{
|
|
// Check-then-insert: explicit check produces a clear 409 message.
|
|
// SqlException 2627 (UQ violation) acts as race-condition fallback — caught in ExceptionFilter.
|
|
var existing = await _repository.GetByCodigoAsync(command.Codigo);
|
|
if (existing is not null)
|
|
throw new RolAlreadyExistsException(command.Codigo);
|
|
|
|
var rol = Rol.ForCreation(command.Codigo, command.Nombre, command.Descripcion);
|
|
|
|
int newId;
|
|
using (var tx = new TransactionScope(
|
|
TransactionScopeOption.Required,
|
|
new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted },
|
|
TransactionScopeAsyncFlowOption.Enabled))
|
|
{
|
|
newId = await _repository.AddAsync(rol);
|
|
|
|
await _audit.LogAsync(
|
|
action: "rol.create",
|
|
targetType: "Rol",
|
|
targetId: newId.ToString(),
|
|
metadata: new { after = new { rol.Codigo, rol.Nombre, rol.Descripcion } });
|
|
|
|
tx.Complete();
|
|
}
|
|
|
|
return new RolCreatedDto(
|
|
Id: newId,
|
|
Codigo: rol.Codigo,
|
|
Nombre: rol.Nombre,
|
|
Descripcion: rol.Descripcion,
|
|
Activo: rol.Activo);
|
|
}
|
|
}
|