using HrynCo.NotificationService.DAL.Abstract; using HrynCo.NotificationService.DAL.Abstract.Repositories; using HrynCo.NotificationService.Services.Core; using HrynCo.NotificationService.Services.Logging; using static HrynCo.NotificationService.Services.Core.ServiceResultHelper; namespace HrynCo.NotificationService.Services.EmailChannels.GetUsageSummary; internal sealed class GetChannelUsageSummaryHandler : RequestHandler>> { private readonly IEmailChannelRepository _channelsRepository; public GetChannelUsageSummaryHandler( IContextualSerilogLogger logger, IUnitOfWork unitOfWork, IEmailChannelRepository channelsRepository) : base(logger, unitOfWork) { _channelsRepository = channelsRepository; } protected override async Task>> DoOnHandle( GetChannelUsageSummaryQuery request, CancellationToken cancellationToken) { var today = DateOnly.FromDateTime(DateTime.UtcNow); var rows = await _channelsRepository.GetAllWithUsageSummaryAsync(today, cancellationToken); var entries = rows .Select(r => new ChannelUsageEntry( ChannelId: r.Channel.Id, ServiceName: r.Channel.ServiceName, ChannelType: r.Channel.EmailChannelType.ToString(), IsActive: r.Channel.IsActive, Priority: r.Channel.Priority, DailyLimit: r.Channel.DailyLimit, MonthlyLimit: r.Channel.MonthlyLimit, DailySent: r.DailySent, MonthlySent: r.MonthlySent)) .ToList(); return Success>(entries); } }