Files
Heimdall/src/Heimdall.Application/Users/Commands/UpdateUser/UpdateUserHandler.cs
T

89 lines
3.6 KiB
C#
Raw Normal View History

using Heimdall.Application.Common;
using Heimdall.Application.Common.Persistence;
using Heimdall.Application.DTOs;
using Heimdall.Domain.Entities;
using MediatR;
using Microsoft.AspNetCore.Identity;
2026-10-21 10:09:14 +02:00
using System.ComponentModel.DataAnnotations;
namespace Heimdall.Application.Users.Commands.UpdateUser;
public class UpdateUserHandler : IRequestHandler<UpdateUserCommand, ServiceResult<UserResponse>>
{
private readonly UserManager<AppUser> _userManager;
private readonly IUserRepository _repository;
public UpdateUserHandler(UserManager<AppUser> userManager, IUserRepository repository)
{
_userManager = userManager;
_repository = repository;
}
public async Task<ServiceResult<UserResponse>> Handle(UpdateUserCommand command, CancellationToken ct)
{
var user = await _userManager.FindByIdAsync(command.Id.ToString()).ConfigureAwait(false);
if (user is null)
return ServiceResult<UserResponse>.NotFound("User not found");
var language = command.Language?.Trim().ToLowerInvariant();
if (language is not null && language is not ("fr" or "en"))
return ServiceResult<UserResponse>.Failure("Langue non prise en charge", 400);
2026-10-21 10:09:14 +02:00
var oldEmail = user.Email;
var oldEmailConfirmed = user.EmailConfirmed;
var emailChanged = false;
if (command.Email is not null)
{
var newEmail = command.Email.Trim();
if (!new EmailAddressAttribute().IsValid(newEmail))
return ServiceResult<UserResponse>.Failure("Adresse email invalide", 400);
if (!string.Equals(oldEmail, newEmail, StringComparison.OrdinalIgnoreCase))
{
var existingUser = await _userManager.FindByEmailAsync(newEmail).ConfigureAwait(false);
if (existingUser is not null && existingUser.Id != user.Id)
return ServiceResult<UserResponse>.Failure("Cette adresse email est déjà utilisée", 409);
user.Email = newEmail;
user.UserName = newEmail;
user.EmailConfirmed = false;
emailChanged = true;
}
}
user.DisplayName = command.DisplayName ?? user.DisplayName;
user.Theme = command.Theme ?? user.Theme;
user.Language = language ?? user.Language;
var result = await _userManager.UpdateAsync(user).ConfigureAwait(false);
if (!result.Succeeded)
return ServiceResult<UserResponse>.Failure(
string.Join(", ", result.Errors.Select(e => e.Description)));
if (emailChanged)
{
var stamp = await _userManager.UpdateSecurityStampAsync(user).ConfigureAwait(false);
if (!stamp.Succeeded)
return ServiceResult<UserResponse>.Failure("Email updated but existing sessions could not be revoked", 500);
}
2026-10-21 10:09:14 +02:00
if (emailChanged)
{
await _repository.AddAuditLogAsync(command.ActorId, "User.EmailUpdated", "User", user.Id.ToString(),
oldValue: System.Text.Json.JsonSerializer.Serialize(new
{
email = oldEmail,
emailConfirmed = oldEmailConfirmed
}),
newValue: System.Text.Json.JsonSerializer.Serialize(new
{
email = user.Email,
emailConfirmed = user.EmailConfirmed
}),
ct: ct).ConfigureAwait(false);
}
var response = await _repository.GetByIdAsync(user.Id, ct).ConfigureAwait(false);
return ServiceResult<UserResponse>.Success(response!);
}
}