using Domain.Exceptions; using Features.Auth.Identity; using MediatR; using Microsoft.AspNetCore.Identity; namespace Features.Auth.Commands.Account.DeleteAccount; /// Handles by deleting the user via . public class DeleteAccountHandler(UserManager userManager) : IRequestHandler { /// Thrown when the account does not exist. /// /// Thrown when the account cannot be deleted because other records (posts, comments, photos, /// follows) still reference it. /// public async Task Handle(DeleteAccountCommand request, CancellationToken cancellationToken) { ApplicationUser user = await userManager.FindByIdAsync(request.UserAccountId.ToString()) ?? throw new NotFoundException("User account not found"); IdentityResult result = await userManager.DeleteAsync(user); if (!result.Succeeded) { if (result.Errors.Any(e => e.Code == "AccountHasDependentRecords")) throw new ConflictException( "Account cannot be deleted while it still has associated posts, comments, photos, or follows." ); throw new ConflictException( string.Join("; ", result.Errors.Select(e => e.Description)) ); } } }