Files
CMS/src/CMSMicroservice.Application/CommissionCQ/Queries/GetAllWeeklyPools/GetAllWeeklyPoolsQueryHandler.cs
T

88 lines
3.1 KiB
C#

namespace CMSMicroservice.Application.CommissionCQ.Queries.GetAllWeeklyPools;
public class GetAllWeeklyPoolsQueryHandler : IRequestHandler<GetAllWeeklyPoolsQuery, GetAllWeeklyPoolsResponseDto>
{
private readonly IApplicationDbContext _context;
private readonly IWeekDefinitionRepository _weekDefinitionRepository;
public GetAllWeeklyPoolsQueryHandler(
IApplicationDbContext context,
IWeekDefinitionRepository weekDefinitionRepository)
{
_context = context;
_weekDefinitionRepository = weekDefinitionRepository;
}
public async Task<GetAllWeeklyPoolsResponseDto> Handle(GetAllWeeklyPoolsQuery request, CancellationToken cancellationToken)
{
var query = _context.WeeklyCommissionPools
.Include(i=> i.WeekDefinition)
.AsNoTracking();
// Apply filters
if (request.FromWeekOrder!=null)
{
query = query.Where(x => x.WeekDefinition.WeekOrder>=request.FromWeekOrder );
}
if (request.ToWeekOrder!=null)
{
query = query.Where(x =>x.WeekDefinition.WeekOrder<= request.ToWeekOrder);
}
if (request.OnlyCalculated.HasValue && request.OnlyCalculated.Value)
{
query = query.Where(x => x.IsCalculated);
}
// Order by week number descending (newest first)
query = query.OrderByDescending(x => x.WeekDefinitionId);
// Count total
var totalCount = await query.CountAsync(cancellationToken);
// Paginate
var pools = await query
.Skip((request.PageIndex - 1) * request.PageSize)
.Take(request.PageSize)
.Select(x => new WeeklyCommissionPoolDto
{
Id = x.Id,
WeekDefinitionId = x.WeekDefinitionId,
WeekDisplayName = x.WeekDefinition.PersianWeekNumber,
TotalPoolAmount = x.TotalPoolAmount,
TotalBalances = x.TotalBalances,
ValuePerBalance = x.ValuePerBalance,
IsCalculated = x.IsCalculated,
CalculatedAt = x.CalculatedAt,
Created = x.Created
})
.ToListAsync(cancellationToken);
// Populate WeekDisplayName from cache
// foreach (var pool in pools)
// {
// // WeeklyCommissionPoolDto is a record, need to create new instance with display name
// // Since records are immutable, we can't modify them directly
// }
// // Create new list with WeekDisplayName populated
// var poolsWithDisplayName = pools.Select(p => p with
// {
// WeekDisplayName = _weekDefinitionRepository.GetDisplayNameByGregorianWeekNumber(p.WeekNumber)
// }).ToList();
return new GetAllWeeklyPoolsResponseDto
{
MetaData = new MetaDataDto
{
TotalCount = totalCount,
PageSize = request.PageSize,
CurrentPage = request.PageIndex,
TotalPages = (int)Math.Ceiling(totalCount / (double)request.PageSize)
},
Models = pools
};
}
}