a0a0575a99
🚀 Create and publish a Docker image / Detect changes in backend and frontend (push) Successful in 10s
🚀 Create and publish a Docker image / Build & publish backend image (push) Successful in 1m17s
🚀 Create and publish a Docker image / Build & publish frontend image (push) Successful in 12s
🚀 Create and publish a Docker image / Update stack on Portainer (push) Successful in 6s
50 lines
2.0 KiB
C#
50 lines
2.0 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using UniVerse.Application.DTOs.Notifications;
|
|
using UniVerse.Application.Interfaces;
|
|
|
|
namespace UniVerse.Infrastructure.Notifications;
|
|
|
|
public class NotificationService : INotificationService
|
|
{
|
|
private readonly IEnumerable<INotificationProvider> _providers;
|
|
private readonly INotificationScheduler _scheduler;
|
|
private readonly ILogger<NotificationService> _logger;
|
|
|
|
public NotificationService(
|
|
IEnumerable<INotificationProvider> providers,
|
|
INotificationScheduler scheduler,
|
|
ILogger<NotificationService> logger)
|
|
{
|
|
_providers = providers;
|
|
_scheduler = scheduler;
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task SendAsync(NotificationMessage message, CancellationToken cancellationToken = default)
|
|
{
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(message.Channel);
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(message.Recipient);
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(message.Subject);
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(message.Body);
|
|
|
|
var provider = _providers.FirstOrDefault(p => string.Equals(p.Channel, message.Channel, StringComparison.OrdinalIgnoreCase))
|
|
?? throw new InvalidOperationException($"Notification provider for channel '{message.Channel}' is not registered.");
|
|
|
|
_logger.LogInformation("Dispatching notification through {Channel} to {Recipient}", message.Channel, message.Recipient);
|
|
await provider.SendAsync(message, cancellationToken);
|
|
}
|
|
|
|
public Task<ScheduledNotificationResponse> ScheduleAsync(ScheduleNotificationRequest request, CancellationToken cancellationToken = default)
|
|
{
|
|
var message = new NotificationMessage(
|
|
request.Channel,
|
|
request.Recipient,
|
|
request.Subject,
|
|
request.Body,
|
|
request.RecipientName,
|
|
request.Metadata);
|
|
|
|
return _scheduler.ScheduleAsync(message, request.SendAt, cancellationToken);
|
|
}
|
|
}
|