2023-07-17 11:48:52 +05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2024-03-01 12:06:42 +05:00
|
|
|
|
using System.Net.Mail;
|
2023-07-17 11:48:52 +05:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudApp.Exceptions;
|
|
|
|
|
using AsbCloudApp.Repositories;
|
|
|
|
|
using AsbCloudApp.Requests;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudApp.Services.Notifications;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сервис для работы с уведомлениями
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class NotificationService
|
|
|
|
|
{
|
|
|
|
|
private readonly ICrudRepository<NotificationCategoryDto> notificationCategoryRepository;
|
|
|
|
|
private readonly INotificationRepository notificationRepository;
|
|
|
|
|
private readonly IEnumerable<INotificationTransportService> notificationTransportServices;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сервис для работы с уведомлениями
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="notificationCategoryRepository"></param>
|
|
|
|
|
/// <param name="notificationRepository"></param>
|
|
|
|
|
/// <param name="notificationTransportServices"></param>
|
|
|
|
|
public NotificationService(ICrudRepository<NotificationCategoryDto> notificationCategoryRepository,
|
|
|
|
|
INotificationRepository notificationRepository,
|
|
|
|
|
IEnumerable<INotificationTransportService> notificationTransportServices)
|
|
|
|
|
{
|
|
|
|
|
this.notificationCategoryRepository = notificationCategoryRepository;
|
|
|
|
|
this.notificationRepository = notificationRepository;
|
|
|
|
|
this.notificationTransportServices = notificationTransportServices;
|
|
|
|
|
}
|
2023-07-19 14:24:22 +05:00
|
|
|
|
|
2023-07-17 11:48:52 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Отправка нового уведомления
|
|
|
|
|
/// </summary>
|
2023-07-19 14:24:22 +05:00
|
|
|
|
/// <param name="request"></param>
|
2023-07-17 11:48:52 +05:00
|
|
|
|
/// <param name="cancellationToken"></param>
|
2023-07-19 14:24:22 +05:00
|
|
|
|
public async Task NotifyAsync(NotifyRequest request,
|
2023-07-17 11:48:52 +05:00
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
2023-08-04 09:47:22 +05:00
|
|
|
|
var notificationCategory = await notificationCategoryRepository.GetOrDefaultAsync(request.IdNotificationCategory, cancellationToken)
|
2023-09-29 12:06:46 +05:00
|
|
|
|
?? throw new ArgumentInvalidException(nameof(request.IdNotificationCategory), "Категория уведомления не найдена");
|
2023-07-17 11:48:52 +05:00
|
|
|
|
|
2023-07-19 14:24:22 +05:00
|
|
|
|
var notification = new NotificationDto
|
2023-07-17 11:48:52 +05:00
|
|
|
|
{
|
2023-07-19 14:24:22 +05:00
|
|
|
|
IdUser = request.IdUser,
|
2024-03-20 16:29:25 +05:00
|
|
|
|
RegistrationDate = DateTimeOffset.UtcNow,
|
2023-07-26 15:41:51 +05:00
|
|
|
|
IdNotificationCategory = notificationCategory.Id,
|
2023-07-19 14:24:22 +05:00
|
|
|
|
Title = request.Title,
|
|
|
|
|
Message = request.Message,
|
2023-07-25 13:36:09 +05:00
|
|
|
|
IdTransportType = request.IdTransportType,
|
2023-07-17 11:48:52 +05:00
|
|
|
|
};
|
|
|
|
|
|
2023-07-25 13:36:09 +05:00
|
|
|
|
notification.Id = await notificationRepository.InsertAsync(notification, cancellationToken);
|
2023-07-17 11:48:52 +05:00
|
|
|
|
notification.NotificationCategory = notificationCategory;
|
2023-07-26 15:41:51 +05:00
|
|
|
|
|
2023-08-04 09:47:22 +05:00
|
|
|
|
var notificationTransportService = GetTransportService(request.IdTransportType);
|
2023-07-17 11:48:52 +05:00
|
|
|
|
|
2024-03-01 12:06:42 +05:00
|
|
|
|
//todo Добавить задачу в WorkToSendEmail
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await notificationTransportService.SendAsync(notification, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
catch (SmtpException ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
}
|
2024-01-10 14:17:21 +05:00
|
|
|
|
|
2024-03-20 16:29:25 +05:00
|
|
|
|
notification.SentDate = DateTimeOffset.UtcNow;
|
2024-01-10 14:17:21 +05:00
|
|
|
|
await notificationRepository.UpdateAsync(notification, cancellationToken);
|
2023-07-17 11:48:52 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Обновление уведомления
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idNotification"></param>
|
|
|
|
|
/// <param name="isRead"></param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
2023-08-04 09:47:22 +05:00
|
|
|
|
public async Task UpdateAsync(int idNotification,
|
2023-07-17 11:48:52 +05:00
|
|
|
|
bool isRead,
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
2023-08-04 09:47:22 +05:00
|
|
|
|
var notification = await notificationRepository.GetOrDefaultAsync(idNotification, cancellationToken)
|
2023-09-29 12:06:46 +05:00
|
|
|
|
?? throw new ArgumentInvalidException(nameof(idNotification), "Уведомление не найдено");
|
2023-07-17 11:48:52 +05:00
|
|
|
|
|
2023-08-02 15:10:00 +05:00
|
|
|
|
if(isRead && !notification.SentDate.HasValue)
|
2023-09-29 12:06:46 +05:00
|
|
|
|
throw new ArgumentInvalidException(nameof(isRead), "Уведомление не может быть прочитано");
|
2023-08-02 15:10:00 +05:00
|
|
|
|
|
2024-03-20 16:29:25 +05:00
|
|
|
|
notification.ReadDate = isRead ? DateTimeOffset.UtcNow : null;
|
2023-07-17 11:48:52 +05:00
|
|
|
|
|
|
|
|
|
await notificationRepository.UpdateAsync(notification,
|
|
|
|
|
cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Отправка уведомлений, которые не были отправлены
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idUser"></param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
2023-08-04 09:47:22 +05:00
|
|
|
|
public async Task RenotifyAsync(int idUser, CancellationToken cancellationToken)
|
2023-07-17 11:48:52 +05:00
|
|
|
|
{
|
2023-09-14 17:17:35 +05:00
|
|
|
|
const int idTransportType = 0;
|
|
|
|
|
|
2023-08-04 09:47:22 +05:00
|
|
|
|
var notifications = await notificationRepository.GetAllAsync(idUser, false,
|
2023-09-14 17:17:35 +05:00
|
|
|
|
idTransportType,
|
2023-07-17 11:48:52 +05:00
|
|
|
|
cancellationToken);
|
|
|
|
|
|
2023-09-14 17:17:35 +05:00
|
|
|
|
var notificationTransportService = GetTransportService(idTransportType);
|
2023-07-17 11:48:52 +05:00
|
|
|
|
|
2023-08-04 09:47:22 +05:00
|
|
|
|
await notificationTransportService.SendRangeAsync(notifications,
|
2023-07-17 11:48:52 +05:00
|
|
|
|
cancellationToken);
|
2024-01-10 14:17:21 +05:00
|
|
|
|
|
|
|
|
|
var tasks = notifications.Select(notification =>
|
|
|
|
|
{
|
2024-03-20 16:29:25 +05:00
|
|
|
|
notification.SentDate = DateTimeOffset.UtcNow;
|
2024-01-10 14:17:21 +05:00
|
|
|
|
return notificationRepository.UpdateAsync(notification, cancellationToken);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await Task.WhenAll(tasks);
|
2023-07-17 11:48:52 +05:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-04 09:47:22 +05:00
|
|
|
|
private INotificationTransportService GetTransportService(int idTransportType)
|
2023-07-17 11:48:52 +05:00
|
|
|
|
{
|
|
|
|
|
var notificationTransportService = notificationTransportServices
|
|
|
|
|
.FirstOrDefault(s => s.IdTransportType == idTransportType)
|
2023-09-29 12:06:46 +05:00
|
|
|
|
?? throw new ArgumentInvalidException(nameof(idTransportType), "Доставщик уведомлений не найден");
|
2023-07-17 11:48:52 +05:00
|
|
|
|
|
|
|
|
|
return notificationTransportService;
|
|
|
|
|
}
|
|
|
|
|
}
|