Files

40 lines
1.5 KiB
C#
Raw Permalink Normal View History

using MediatR;
2026-09-20 16:21:15 +07:00
using mws.backend.dotnet.application.Audit;
2026-08-19 23:25:08 +07:00
using mws.backend.dotnet.application.Common;
using mws.backend.dotnet.domain.Projects;
2026-08-19 23:25:08 +07:00
namespace mws.backend.dotnet.application.Projects;
2026-09-20 16:21:15 +07:00
public record RemoveProjectMemberCommand(Guid UserId, Guid ProjectId, Guid MemberUserId) : IRequest, IAuditableRequest
{
public string Action => "ProjectMember.Remove";
public string EntityType => "ProjectMember";
public Guid? EntityId => MemberUserId;
}
2026-09-20 16:21:15 +07:00
public class RemoveProjectMemberHandler(IUnitOfWork uow, IAuditContext auditContext) : IRequestHandler<RemoveProjectMemberCommand>
{
public async Task Handle(RemoveProjectMemberCommand command, CancellationToken ct)
{
if (await uow.Projects.GetMemberRoleAsync(command.ProjectId, command.UserId, ct) != MemberRole.Owner)
{
throw new ForbiddenException("Only the project owner can remove members");
}
2026-09-20 16:21:15 +07:00
var member = await uow.Projects.GetMemberWithUserAsync(command.ProjectId, command.MemberUserId, ct)
?? throw new NotFoundException("Member not found in project");
var owners = await uow.Projects.CountOwnersAsync(command.ProjectId, ct);
if (member.Role == MemberRole.Owner && owners <= 1)
{
throw new BadRequestException("Cannot remove the last owner of the project");
}
uow.Projects.RemoveMember(member);
await uow.SaveChangesAsync(ct);
2026-09-20 16:21:15 +07:00
auditContext.EntityName = member.User.Username;
}
}