using HrynCo.NotificationService.DAL.Abstract.Providers; 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.Create; internal sealed class CreateEmailChannelHandler : RequestHandler> { private readonly IEmailChannelRepository _channels; private readonly INotificationServiceRepository _services; public CreateEmailChannelHandler( IContextualSerilogLogger logger, IEmailChannelRepository channels, INotificationServiceRepository services) : base(logger) { _channels = channels; _services = services; } protected override async Task> DoOnHandle( CreateEmailChannelCommand request, CancellationToken cancellationToken) { if (request.ServiceId == Guid.Empty || await _services.GetAsync(request.ServiceId, cancellationToken) is null) return Failure("Select an existing service.", ServiceErrorCode.InvalidRequest); var channel = new EmailChannel { ServiceId = request.ServiceId, Priority = request.Priority, EmailChannelType = request.ChannelType, Settings = request.Settings, DailyLimit = request.DailyLimit, MonthlyLimit = request.MonthlyLimit, WarnThresholdPercent = request.WarnThresholdPercent, IsActive = request.IsActive }; await _channels.AddAsync(channel, cancellationToken); return Success(channel.Id); } }