2023-12-21 12:31:25 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudApp.Services.Notifications;
|
|
|
|
|
using AsbCloudInfrastructure.Background;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2022-02-28 14:44:26 +05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2023-07-19 14:24:22 +05:00
|
|
|
|
namespace AsbCloudInfrastructure.Services.Email
|
2022-02-28 14:44:26 +05:00
|
|
|
|
{
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2023-07-19 14:24:22 +05:00
|
|
|
|
public class EmailNotificationTransportService : INotificationTransportService
|
2022-02-28 14:44:26 +05:00
|
|
|
|
{
|
2023-12-21 14:48:08 +05:00
|
|
|
|
private readonly IConfiguration configuration;
|
2022-12-02 14:48:23 +05:00
|
|
|
|
private readonly BackgroundWorker backgroundWorker;
|
2022-02-28 14:44:26 +05:00
|
|
|
|
|
2023-12-21 12:31:25 +05:00
|
|
|
|
public EmailNotificationTransportService(BackgroundWorker backgroundWorker,
|
2023-07-19 14:24:22 +05:00
|
|
|
|
IConfiguration configuration)
|
2022-02-28 14:44:26 +05:00
|
|
|
|
{
|
2023-12-21 12:31:25 +05:00
|
|
|
|
this.configuration = configuration;
|
2022-02-28 14:44:26 +05:00
|
|
|
|
this.backgroundWorker = backgroundWorker;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-14 17:17:35 +05:00
|
|
|
|
public int IdTransportType => 1;
|
2023-12-21 12:31:25 +05:00
|
|
|
|
|
2023-07-19 14:24:22 +05:00
|
|
|
|
public Task SendAsync(NotificationDto notification, CancellationToken cancellationToken)
|
2022-02-28 14:44:26 +05:00
|
|
|
|
{
|
2023-12-21 14:48:08 +05:00
|
|
|
|
var work = new WorkToSendEmail(notification, configuration);
|
|
|
|
|
if (!backgroundWorker.Works.Any(w => w.Id == work.Id))
|
2022-02-28 14:44:26 +05:00
|
|
|
|
{
|
2023-11-03 17:02:44 +05:00
|
|
|
|
backgroundWorker.Enqueue(work);
|
2022-04-11 18:00:34 +05:00
|
|
|
|
}
|
2023-07-19 14:24:22 +05:00
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task SendRangeAsync(IEnumerable<NotificationDto> notifications, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var tasks = notifications
|
|
|
|
|
.Select(notification => SendAsync(notification, cancellationToken));
|
|
|
|
|
|
|
|
|
|
return Task.WhenAll(tasks);
|
2022-02-28 14:44:26 +05:00
|
|
|
|
}
|
2023-12-21 12:31:25 +05:00
|
|
|
|
|
2023-12-21 14:48:08 +05:00
|
|
|
|
|
2022-02-28 14:44:26 +05:00
|
|
|
|
}
|
|
|
|
|
}
|