2026-07-22 19:43:13 +00:00
|
|
|
using Heimdall.Application.Common;
|
|
|
|
|
using Heimdall.Application.Common.Persistence;
|
|
|
|
|
using Heimdall.Application.DTOs;
|
|
|
|
|
using Heimdall.Domain.Entities;
|
|
|
|
|
using MediatR;
|
2026-10-21 10:09:14 +02:00
|
|
|
using System.ComponentModel.DataAnnotations;
|
2026-11-04 11:37:53 +01:00
|
|
|
using Heimdall.Application.Services;
|
2026-07-22 19:43:13 +00:00
|
|
|
|
|
|
|
|
namespace Heimdall.Application.Users.Commands.UpdateUser;
|
|
|
|
|
|
|
|
|
|
public class UpdateUserHandler : IRequestHandler<UpdateUserCommand, ServiceResult<UserResponse>>
|
|
|
|
|
{
|
2026-11-04 11:37:53 +01:00
|
|
|
private readonly IIdentityAccountService _userManager;
|
2026-07-22 19:43:13 +00:00
|
|
|
private readonly IUserRepository _repository;
|
|
|
|
|
|
2026-11-04 11:37:53 +01:00
|
|
|
public UpdateUserHandler(IIdentityAccountService userManager, IUserRepository repository)
|
2026-07-22 19:43:13 +00:00
|
|
|
{
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_repository = repository;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<ServiceResult<UserResponse>> Handle(UpdateUserCommand command, CancellationToken ct)
|
|
|
|
|
{
|
2026-11-04 11:37:53 +01:00
|
|
|
var user = await _userManager.FindByIdAsync(command.Id, ct).ConfigureAwait(false);
|
2026-07-22 19:43:13 +00:00
|
|
|
if (user is null)
|
|
|
|
|
return ServiceResult<UserResponse>.NotFound("User not found");
|
|
|
|
|
|
2026-10-25 04:19:19 +01:00
|
|
|
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))
|
|
|
|
|
{
|
2026-11-04 11:37:53 +01:00
|
|
|
var existingUser = await _userManager.FindByEmailAsync(newEmail, ct).ConfigureAwait(false);
|
2026-10-21 10:09:14 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-22 19:43:13 +00:00
|
|
|
user.DisplayName = command.DisplayName ?? user.DisplayName;
|
|
|
|
|
user.Theme = command.Theme ?? user.Theme;
|
2026-10-25 04:19:19 +01:00
|
|
|
user.Language = language ?? user.Language;
|
2026-07-22 19:43:13 +00:00
|
|
|
|
2026-11-04 11:37:53 +01:00
|
|
|
var result = await _userManager.UpdateAsync(user, ct).ConfigureAwait(false);
|
2026-07-22 19:43:13 +00:00
|
|
|
if (!result.Succeeded)
|
|
|
|
|
return ServiceResult<UserResponse>.Failure(
|
2026-11-04 11:37:53 +01:00
|
|
|
string.Join(", ", result.ErrorCodes));
|
2026-07-22 19:43:13 +00:00
|
|
|
|
2026-11-04 10:26:07 +01:00
|
|
|
if (emailChanged)
|
|
|
|
|
{
|
2026-11-04 11:37:53 +01:00
|
|
|
var stamp = await _userManager.UpdateSecurityStampAsync(user, ct).ConfigureAwait(false);
|
|
|
|
|
if (!stamp)
|
2026-11-04 10:26:07 +01:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-27 18:59:12 +00:00
|
|
|
var response = await _repository.GetByIdAsync(user.Id, ct).ConfigureAwait(false);
|
2026-07-22 19:43:13 +00:00
|
|
|
return ServiceResult<UserResponse>.Success(response!);
|
|
|
|
|
}
|
|
|
|
|
}
|