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
Build and Deploy to Kubernetes / build-and-deploy (push) Failing after 8s
This commit is contained in:
+4
-13
@@ -4,16 +4,13 @@ public class GetUserCommissionPayoutsQueryHandler : IRequestHandler<GetUserCommi
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly IWeekDefinitionRepository _weekDefinitionRepository;
|
||||
private readonly ICurrentUserService _currentUser;
|
||||
|
||||
public GetUserCommissionPayoutsQueryHandler(
|
||||
IApplicationDbContext context,
|
||||
IWeekDefinitionRepository weekDefinitionRepository,
|
||||
ICurrentUserService currentUser)
|
||||
IWeekDefinitionRepository weekDefinitionRepository)
|
||||
{
|
||||
_context = context;
|
||||
_weekDefinitionRepository = weekDefinitionRepository;
|
||||
_currentUser = currentUser;
|
||||
}
|
||||
|
||||
public async Task<GetUserCommissionPayoutsResponseDto> Handle(GetUserCommissionPayoutsQuery request, CancellationToken cancellationToken)
|
||||
@@ -24,17 +21,11 @@ public class GetUserCommissionPayoutsQueryHandler : IRequestHandler<GetUserCommi
|
||||
.AsNoTracking()
|
||||
.AsQueryable();
|
||||
|
||||
// اگر UserId داده نشده، از CurrentUser بگیر (برای Customer API)
|
||||
// UserId > 0 → filter by that user
|
||||
// UserId == 0 or null → show ALL users (admin mode)
|
||||
// Customer endpoints resolve UserId from JWT before calling this handler
|
||||
long? userId = request.UserId;
|
||||
if (!userId.HasValue || userId.Value == 0)
|
||||
{
|
||||
if (long.TryParse(_currentUser.UserId, out var currentUserId))
|
||||
{
|
||||
userId = currentUserId;
|
||||
}
|
||||
}
|
||||
|
||||
// فیلترها
|
||||
if (userId.HasValue && userId.Value > 0)
|
||||
{
|
||||
query = query.Where(x => x.UserId == userId.Value);
|
||||
|
||||
+4
-13
@@ -4,16 +4,13 @@ public class GetUserWeeklyBalancesQueryHandler : IRequestHandler<GetUserWeeklyBa
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly IWeekDefinitionRepository _weekDefinitionRepository;
|
||||
private readonly ICurrentUserService _currentUser;
|
||||
|
||||
public GetUserWeeklyBalancesQueryHandler(
|
||||
IApplicationDbContext context,
|
||||
IWeekDefinitionRepository weekDefinitionRepository,
|
||||
ICurrentUserService currentUser)
|
||||
IWeekDefinitionRepository weekDefinitionRepository)
|
||||
{
|
||||
_context = context;
|
||||
_weekDefinitionRepository = weekDefinitionRepository;
|
||||
_currentUser = currentUser;
|
||||
}
|
||||
|
||||
public async Task<GetUserWeeklyBalancesResponseDto> Handle(GetUserWeeklyBalancesQuery request, CancellationToken cancellationToken)
|
||||
@@ -24,17 +21,11 @@ public class GetUserWeeklyBalancesQueryHandler : IRequestHandler<GetUserWeeklyBa
|
||||
.AsNoTracking()
|
||||
.AsQueryable();
|
||||
|
||||
// اگر UserId داده نشده، از CurrentUser بگیر (برای Customer API)
|
||||
// UserId > 0 → filter by that user
|
||||
// UserId == 0 or null → show ALL users (admin mode)
|
||||
// Customer endpoints resolve UserId from JWT before calling this handler
|
||||
long? userId = request.UserId;
|
||||
if (!userId.HasValue || userId.Value == 0)
|
||||
{
|
||||
if (long.TryParse(_currentUser.UserId, out var currentUserId))
|
||||
{
|
||||
userId = currentUserId;
|
||||
}
|
||||
}
|
||||
|
||||
// فیلترها
|
||||
if (userId.HasValue && userId.Value > 0)
|
||||
{
|
||||
query = query.Where(x => x.UserId == userId.Value);
|
||||
|
||||
+1
-1
@@ -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; }
|
||||
}
|
||||
|
||||
+15
-12
@@ -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)
|
||||
|
||||
+4
-9
@@ -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
|
||||
|
||||
@@ -2,21 +2,19 @@ namespace CMSMicroservice.Application.UserCQ.Queries.GetUser;
|
||||
public class GetUserQueryHandler : IRequestHandler<GetUserQuery, GetUserResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly ICurrentUserService _currentUser;
|
||||
|
||||
public GetUserQueryHandler(IApplicationDbContext context, ICurrentUserService currentUser)
|
||||
public GetUserQueryHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
_currentUser = currentUser;
|
||||
}
|
||||
|
||||
public async Task<GetUserResponseDto> Handle(GetUserQuery request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
// If Id is 0 or not provided, get the current authenticated user's ID
|
||||
var userId = request.Id == 0
|
||||
? (long.TryParse(_currentUser.UserId, out var currentUserId) ? currentUserId : 0)
|
||||
: request.Id;
|
||||
// UserId must be provided - customer endpoints resolve from JWT before calling
|
||||
var userId = request.Id;
|
||||
if (userId == 0)
|
||||
throw new ArgumentException("UserId is required");
|
||||
|
||||
var response = await _context.Users
|
||||
.AsNoTracking()
|
||||
|
||||
+5
-12
@@ -7,29 +7,22 @@ namespace CMSMicroservice.Application.UserOrderCQ.Queries.GetCustomerOrder;
|
||||
public class GetCustomerOrderQueryHandler : IRequestHandler<GetCustomerOrderQuery, GetCustomerOrderResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly ICurrentUserService _currentUser;
|
||||
|
||||
public GetCustomerOrderQueryHandler(
|
||||
IApplicationDbContext context,
|
||||
ICurrentUserService currentUser)
|
||||
IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
_currentUser = currentUser;
|
||||
}
|
||||
|
||||
public async Task<GetCustomerOrderResponseDto> Handle(GetCustomerOrderQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
// Resolve UserId from JWT if not specified
|
||||
var userId = request.UserId == 0
|
||||
? (long.TryParse(_currentUser.UserId, out var currentUserId) ? currentUserId : 0)
|
||||
: request.UserId;
|
||||
|
||||
if (userId == 0)
|
||||
throw new UnauthorizedAccessException("User ID not found");
|
||||
// UserId > 0 → filter by that user (customer security)
|
||||
// UserId == 0 → no user filter (admin can view any order by ID)
|
||||
var userId = request.UserId;
|
||||
|
||||
var order = await _context.UserOrders
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.OrderId && x.UserId == userId)
|
||||
.Where(x => x.Id == request.OrderId && (userId == 0 || x.UserId == userId))
|
||||
.Include(x => x.Package)
|
||||
.Include(x => x.Transaction)
|
||||
.Include(x => x.UserAddress)
|
||||
|
||||
+5
-12
@@ -8,29 +8,22 @@ namespace CMSMicroservice.Application.UserOrderCQ.Queries.GetCustomerOrders;
|
||||
public class GetCustomerOrdersQueryHandler : IRequestHandler<GetCustomerOrdersQuery, GetCustomerOrdersResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly ICurrentUserService _currentUser;
|
||||
|
||||
public GetCustomerOrdersQueryHandler(
|
||||
IApplicationDbContext context,
|
||||
ICurrentUserService currentUser)
|
||||
IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
_currentUser = currentUser;
|
||||
}
|
||||
|
||||
public async Task<GetCustomerOrdersResponseDto> Handle(GetCustomerOrdersQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
// Resolve UserId from JWT if not specified
|
||||
var userId = request.UserId == 0
|
||||
? (long.TryParse(_currentUser.UserId, out var currentUserId) ? currentUserId : 0)
|
||||
: request.UserId;
|
||||
|
||||
if (userId == 0)
|
||||
throw new UnauthorizedAccessException("User ID not found");
|
||||
// UserId > 0 → filter by that user
|
||||
// UserId == 0 → show ALL users (admin mode)
|
||||
var userId = request.UserId;
|
||||
|
||||
var query = _context.UserOrders
|
||||
.AsNoTracking()
|
||||
.Where(x => x.UserId == userId)
|
||||
.Where(x => userId == 0 || x.UserId == userId)
|
||||
.Include(x => x.Package)
|
||||
.Include(x => x.Transaction)
|
||||
.Include(x => x.UserAddress)
|
||||
|
||||
+5
-7
@@ -2,21 +2,19 @@ namespace CMSMicroservice.Application.UserWalletCQ.Queries.GetUserWallet;
|
||||
public class GetUserWalletQueryHandler : IRequestHandler<GetUserWalletQuery, GetUserWalletResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly ICurrentUserService _currentUser;
|
||||
|
||||
public GetUserWalletQueryHandler(IApplicationDbContext context, ICurrentUserService currentUser)
|
||||
public GetUserWalletQueryHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
_currentUser = currentUser;
|
||||
}
|
||||
|
||||
public async Task<GetUserWalletResponseDto> Handle(GetUserWalletQuery request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
// If Id is 0 or not provided, get the current authenticated user's ID
|
||||
var userId = request.Id == 0
|
||||
? (long.TryParse(_currentUser.UserId, out var currentUserId) ? currentUserId : 0)
|
||||
: request.Id;
|
||||
// UserId must be provided - customer endpoints resolve from JWT before calling
|
||||
var userId = request.Id;
|
||||
if (userId == 0)
|
||||
throw new ArgumentException("UserId is required");
|
||||
|
||||
var response = await _context.UserWallets
|
||||
.AsNoTracking()
|
||||
|
||||
Reference in New Issue
Block a user