using Domain.Exceptions;
using Features.Auth.Dtos;
using Features.Auth.Repository;
using Features.Auth.Services;
using MediatR;
namespace Features.Auth.Commands.ConfirmUser;
///
/// Handles by validating the confirmation token and marking the
/// corresponding user account as confirmed.
///
/// Repository used to look up and confirm user accounts.
/// Service used to validate the confirmation token.
public class ConfirmUserHandler(
IAuthRepository authRepository,
ITokenService tokenService
) : IRequestHandler
{
///
/// Thrown when the confirmation token is invalid or expired, or when the associated user account cannot be found.
///
public async Task Handle(ConfirmUserCommand request, CancellationToken cancellationToken)
{
var validatedToken = await tokenService.ValidateConfirmationTokenAsync(request.Token);
var user = await authRepository.ConfirmUserAccountAsync(validatedToken.UserId);
if (user == null)
{
throw new UnauthorizedException("User account not found");
}
return new ConfirmationPayload(user.UserAccountId, DateTime.UtcNow);
}
}