2024-10-02 19:23:19 +02:00
|
|
|
|
#nullable enable
|
|
|
|
|
|
using Bit.Core.Context;
|
|
|
|
|
|
using Bit.Core.NotificationCenter.Authorization;
|
|
|
|
|
|
using Bit.Core.NotificationCenter.Commands.Interfaces;
|
|
|
|
|
|
using Bit.Core.NotificationCenter.Entities;
|
|
|
|
|
|
using Bit.Core.NotificationCenter.Repositories;
|
2024-10-22 11:19:07 +01:00
|
|
|
|
using Bit.Core.Services;
|
2024-10-02 19:23:19 +02:00
|
|
|
|
using Bit.Core.Utilities;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.NotificationCenter.Commands;
|
|
|
|
|
|
|
|
|
|
|
|
public class CreateNotificationCommand : ICreateNotificationCommand
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly ICurrentContext _currentContext;
|
|
|
|
|
|
private readonly IAuthorizationService _authorizationService;
|
|
|
|
|
|
private readonly INotificationRepository _notificationRepository;
|
2024-10-22 11:19:07 +01:00
|
|
|
|
private readonly IPushNotificationService _pushNotificationService;
|
2024-10-02 19:23:19 +02:00
|
|
|
|
|
|
|
|
|
|
public CreateNotificationCommand(ICurrentContext currentContext,
|
|
|
|
|
|
IAuthorizationService authorizationService,
|
2024-10-22 11:19:07 +01:00
|
|
|
|
INotificationRepository notificationRepository,
|
|
|
|
|
|
IPushNotificationService pushNotificationService)
|
2024-10-02 19:23:19 +02:00
|
|
|
|
{
|
|
|
|
|
|
_currentContext = currentContext;
|
|
|
|
|
|
_authorizationService = authorizationService;
|
|
|
|
|
|
_notificationRepository = notificationRepository;
|
2024-10-22 11:19:07 +01:00
|
|
|
|
_pushNotificationService = pushNotificationService;
|
2024-10-02 19:23:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<Notification> CreateAsync(Notification notification)
|
|
|
|
|
|
{
|
|
|
|
|
|
notification.CreationDate = notification.RevisionDate = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
|
|
await _authorizationService.AuthorizeOrThrowAsync(_currentContext.HttpContext.User, notification,
|
|
|
|
|
|
NotificationOperations.Create);
|
|
|
|
|
|
|
2024-10-22 11:19:07 +01:00
|
|
|
|
var newNotification = await _notificationRepository.CreateAsync(notification);
|
|
|
|
|
|
|
|
|
|
|
|
await _pushNotificationService.PushSyncNotificationAsync(newNotification);
|
|
|
|
|
|
|
|
|
|
|
|
return newNotification;
|
2024-10-02 19:23:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|