using Microsoft.EntityFrameworkCore; using PnvPanel.Application.Common.Interfaces; using PnvPanel.Application.Common.Messaging; using PnvPanel.Application.Common.Models; using PnvPanel.Domain.Billing; namespace PnvPanel.Application.Admin.Users; public sealed class ListUsersQueryHandler(IIdentityService identityService, IAppDbContext dbContext) : IQueryHandler>> { public async Task>> Handle( ListUsersQuery query, CancellationToken cancellationToken ) { var page = query.Page <= 0 ? 1 : query.Page; var pageSize = query.PageSize is <= 0 or > 100 ? 20 : query.PageSize; var result = await identityService.ListUsersAsync( page, pageSize, query.Search, query.RoleId, query.IsActivated, query.IsBlocked, query.BillingExpired, cancellationToken ); // IIdentityService ничего не знает про PaymentRequests (Identity не должен на них ссылаться) — // подмешиваем "заявка на проверке" здесь, чтобы админский список видел ту же защиту от // тревожного "Истекло", что уже показывается пользователю на /billing (PaidUntilBadge.pendingReview). var userIds = result.Items.Select(u => u.Id).ToList(); var pendingUserIds = await dbContext .PaymentRequests.Where(r => userIds.Contains(r.UserId) && r.Kind == PaymentRequestKind.Subscription && r.Status == PaymentRequestStatus.AwaitingConfirmation ) .Select(r => r.UserId) .ToListAsync(cancellationToken); if (pendingUserIds.Count == 0) return Result.Success(result); var pendingSet = pendingUserIds.ToHashSet(); var enrichedItems = result .Items.Select(u => pendingSet.Contains(u.Id) ? u with { BillingPendingReview = true } : u) .ToList(); return Result.Success( new PagedList(enrichedItems, result.Total, result.Page, result.PageSize) ); } }