using PnvPanel.Application.Auth; using PnvPanel.Application.Common.Interfaces; using PnvPanel.Application.Common.Messaging; using PnvPanel.Application.Common.Models; namespace PnvPanel.Application.Support.ListSelectableRoles; public sealed class ListSelectableRolesQueryHandler( IRoleService roleService, IIdentityService identityService, ICurrentUser currentUser ) : IQueryHandler>> { // Совпадает со значением Infrastructure.Identity.RoleNames.Admin — см. пояснение в // CreateRoleRequestTicketCommandHandler (Application не может ссылаться на Infrastructure). private const string AdminRoleName = "admin"; public async Task>> Handle( ListSelectableRolesQuery query, CancellationToken cancellationToken ) { if (currentUser.UserId is not { } userId) return Result.Failure>(AuthErrors.Unauthorized); var profile = await identityService.GetProfileAsync(userId, cancellationToken); var roles = await roleService.ListRolesAsync(cancellationToken); // Без admin (нельзя запросить) и без текущей роли пользователя (уже есть — нечего запрашивать). var selectable = roles .Where(r => !r.Name.Equals(AdminRoleName, StringComparison.OrdinalIgnoreCase)) .Where(r => profile is null || r.Id != profile.RoleId) .ToList(); return Result.Success>(selectable); } }