2023-07-17 11:48:52 +05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
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>
|
2023-07-25 13:36:09 +05:00
|
|
|
|
/// <param name="userRepository"></param>
|
2023-07-17 11:48:52 +05:00
|
|
|
|
/// <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)
|
|
|
|
|
{
|
|
|
|
|
var notificationCategory = await notificationCategoryRepository
|
2023-07-19 14:24:22 +05:00
|
|
|
|
.GetOrDefaultAsync(request.IdNotificationCategory, cancellationToken)
|
|
|
|
|
?? 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,
|
2023-07-25 13:36:09 +05:00
|
|
|
|
RegistrationDate = DateTime.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-07-19 14:24:22 +05:00
|
|
|
|
var notificationTransportService = GetNotificationTransportService(request.IdTransportType);
|
2023-07-17 11:48:52 +05:00
|
|
|
|
|
|
|
|
|
await notificationTransportService.SendAsync(notification, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Обновление уведомления
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idNotification"></param>
|
|
|
|
|
/// <param name="isRead"></param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task UpdateNotificationAsync(int idNotification,
|
|
|
|
|
bool isRead,
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var notification = await notificationRepository.GetOrDefaultAsync(idNotification,
|
|
|
|
|
cancellationToken)
|
|
|
|
|
?? throw new ArgumentInvalidException("Уведомление не найдено", nameof(idNotification));
|
|
|
|
|
|
|
|
|
|
if (isRead)
|
|
|
|
|
{
|
|
|
|
|
if (notification.SentDate == null)
|
|
|
|
|
throw new ArgumentInvalidException("Уведомление не может быть прочитано", nameof(isRead));
|
|
|
|
|
|
2023-07-25 15:40:54 +05:00
|
|
|
|
notification.ReadDate = DateTime.UtcNow;
|
2023-07-17 11:48:52 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await notificationRepository.UpdateAsync(notification,
|
|
|
|
|
cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Отправка уведомлений, которые не были отправлены
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idUser"></param>
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task ResendNotificationAsync(int idUser,
|
|
|
|
|
NotificationRequest request,
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
if (!request.IdTransportType.HasValue)
|
|
|
|
|
throw new ArgumentInvalidException("Id типа доставки уведомления должен иметь значение",
|
|
|
|
|
nameof(request.IdTransportType));
|
|
|
|
|
|
|
|
|
|
var result = await notificationRepository.GetNotificationsAsync(idUser,
|
|
|
|
|
request,
|
|
|
|
|
cancellationToken);
|
|
|
|
|
|
|
|
|
|
var notificationTransportService = GetNotificationTransportService(request.IdTransportType.Value);
|
|
|
|
|
|
|
|
|
|
await notificationTransportService.SendRangeAsync(result.Items,
|
|
|
|
|
cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private INotificationTransportService GetNotificationTransportService(int idTransportType)
|
|
|
|
|
{
|
|
|
|
|
var notificationTransportService = notificationTransportServices
|
|
|
|
|
.FirstOrDefault(s => s.IdTransportType == idTransportType)
|
|
|
|
|
?? throw new ArgumentInvalidException("Доставщик уведомлений не найден", nameof(idTransportType));
|
|
|
|
|
|
|
|
|
|
return notificationTransportService;
|
|
|
|
|
}
|
|
|
|
|
}
|