2026-08-17 20:58:08 +07:00
|
|
|
using MediatR;
|
2026-08-19 23:25:08 +07:00
|
|
|
using mws.backend.dotnet.application.Common;
|
|
|
|
|
using mws.backend.dotnet.domain.Projects;
|
2026-08-17 20:58:08 +07:00
|
|
|
|
2026-08-19 23:25:08 +07:00
|
|
|
namespace mws.backend.dotnet.application.Projects;
|
2026-08-17 20:58:08 +07:00
|
|
|
|
|
|
|
|
public record RemoveProjectMemberCommand(Guid UserId, Guid ProjectId, Guid MemberUserId) : IRequest;
|
|
|
|
|
|
|
|
|
|
public class RemoveProjectMemberHandler(IUnitOfWork uow) : 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");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var member = await uow.Projects.GetMemberAsync(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);
|
|
|
|
|
}
|
|
|
|
|
}
|