Files
mws.backend.dotnet/mws.application/Accounts/Commands/DeleteAccount.cs
T
namdhandClaude Sonnet 5 50faf9052c Convert Accounts module to MediatR commands/queries
Replaces IAccountService/AccountService with one command/query per
operation, following the pattern set in the Auth module.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-14 08:38:31 +07:00

35 lines
1.2 KiB
C#

using MediatR;
using Mws.Application.Common;
using Mws.Application.Permissions;
namespace Mws.Application.Accounts;
public record DeleteAccountCommand(Guid ActorUserId, Guid Id) : IRequest;
public class DeleteAccountHandler(IUnitOfWork uow, IPermissionService permissions) : IRequestHandler<DeleteAccountCommand>
{
private const string Screen = "accounts";
public async Task Handle(DeleteAccountCommand command, CancellationToken ct)
{
await permissions.EnsureAsync(command.ActorUserId, Screen, PermissionAction.Delete, ct);
var user = await uow.Users.GetByIdAsync(command.Id, ct)
?? throw new NotFoundException("Account not found");
var soleOwnerProjectIds = await uow.Projects.GetOwnedProjectIdsAsync(command.Id, ct);
foreach (var projectId in soleOwnerProjectIds)
{
var ownerCount = await uow.Projects.CountOwnersAsync(projectId, ct);
if (ownerCount <= 1)
{
throw new BadRequestException("Cannot delete an account that is the sole owner of a project");
}
}
uow.Users.Remove(user);
await uow.SaveChangesAsync(ct);
}
}