refactor: Remove ICurrentUserService dependency from query handlers and update user ID handling logic
Build and Deploy to Kubernetes / build-and-deploy (push) Failing after 8s

This commit is contained in:
masoodafar-web
2026-02-10 23:01:49 +03:30
parent b42d9e141d
commit d1ca72300d
13 changed files with 77 additions and 95 deletions
@@ -3,7 +3,7 @@ namespace CMSMicroservice.Application.NetworkMembershipCQ.Queries.GetNetworkStat
public class GetNetworkStatisticsQuery : IRequest<GetNetworkStatisticsResponseDto>
{
/// <summary>
/// شناسه کاربر برای محاسبه آمار شبکه او - 0 یا null یعنی کاربر جاری
/// شناسه کاربر برای محاسبه آمار شبکه او - 0 یعنی آمار کل شبکه (root user)
/// </summary>
public long UserId { get; set; }
}
@@ -5,30 +5,33 @@ namespace CMSMicroservice.Application.NetworkMembershipCQ.Queries.GetNetworkStat
public class GetNetworkStatisticsQueryHandler : IRequestHandler<GetNetworkStatisticsQuery, GetNetworkStatisticsResponseDto>
{
private readonly IApplicationDbContext _context;
private readonly ICurrentUserService _currentUser;
public GetNetworkStatisticsQueryHandler(
IApplicationDbContext context,
ICurrentUserService currentUser)
IApplicationDbContext context)
{
_context = context;
_currentUser = currentUser;
}
public async Task<GetNetworkStatisticsResponseDto> Handle(GetNetworkStatisticsQuery request, CancellationToken cancellationToken)
{
// Get userId - use current user if not specified or is 0
var userId = request.UserId == 0
? (long.TryParse(_currentUser.UserId, out var currentUserId) ? currentUserId : 0)
: request.UserId;
// UserId > 0 → stats for that user's network
// UserId == 0 → global stats (find root user of the network)
var userId = request.UserId;
// Get all users (needed for descendant calculation anyway)
var allUsers = await _context.Users.ToListAsync(cancellationToken);
if (userId == 0)
{
throw new UnauthorizedAccessException("User ID not found");
// Find root user (user with no NetworkParentId)
var rootUser = allUsers.FirstOrDefault(x => x.NetworkParentId == null || x.NetworkParentId == 0);
if (rootUser != null)
userId = rootUser.Id;
else if (allUsers.Count > 0)
userId = allUsers.First().Id;
else
throw new InvalidOperationException("No users found in the system");
}
// Get all descendants recursively
var allUsers = await _context.Users.ToListAsync(cancellationToken);
var allDescendants = GetAllDescendants(userId, allUsers);
// Statistics for the user's network (all descendants)
@@ -8,28 +8,23 @@ public class GetNetworkTreeQueryHandler : IRequestHandler<GetNetworkTreeQuery, N
{
private readonly IApplicationDbContext _context;
private readonly ILogger<GetNetworkTreeQueryHandler> _logger;
private readonly ICurrentUserService _currentUser;
public GetNetworkTreeQueryHandler(
IApplicationDbContext context,
ILogger<GetNetworkTreeQueryHandler> logger,
ICurrentUserService currentUser)
ILogger<GetNetworkTreeQueryHandler> logger)
{
_context = context;
_logger = logger;
_currentUser = currentUser;
}
public async Task<NetworkTreeDto?> Handle(GetNetworkTreeQuery request, CancellationToken cancellationToken)
{
// Get userId - use current user if UserId is 0
var userId = request.UserId == 0
? (long.TryParse(_currentUser.UserId, out var currentUserId) ? currentUserId : 0)
: request.UserId;
// UserId must be provided - customer endpoints resolve from JWT before calling
var userId = request.UserId;
if (userId == 0)
{
throw new UnauthorizedAccessException("User ID not found");
throw new ArgumentException("UserId is required for network tree query");
}
// Create a new request with the resolved userId