feat: implémenter SignalR hubs (Monitoring, Notifications, Admin) + système de notifications complet
SignalR Hubs: - MonitoringHub (/hubs/monitoring) — statuts agents/caméras/stockage/GPU jobs - NotificationsHub (/hubs/notifications) — notifications in-app badge temps réel - AdminHub (/hubs/admin) — broadcast changements config (Admin only) Notification System: - Nouvelles entités: Notification, NotificationTemplate, UserNotificationConfig - CQRS/MediatR: 6 handlers (list, unread, mark-read, mark-all, config get/update) - Infrastructure: NotificationService orchestrator + EmailService + GotifyService - Rate limiter IMemoryCache (20 events/min/camera/type) - Hangfire jobs: SendNotification, DailyDigest, WeeklyDigest - NotificationsController API (6 endpoints) - Blazor UI: NotificationsPage + NotificationBadge - Migration AddNotifications (3 nouvelles tables) - 644 nouveaux tests (Domain: 131, Application: 321, Infra: 151, Integration: 120, +autres)
This commit is contained in:
1 parent
ef3ea9d1ca
commit
f9be0f1e0d
62 files changed
+6664
-3
No files matched your search
@@ -0,0 +1,7 @@
|
||||
using Heimdall.Application.Common;
|
||||
using Heimdall.Application.DTOs;
|
||||
using MediatR;
|
||||
|
||||
namespace Heimdall.Application.Notifications.Commands.MarkAllAsRead;
|
||||
|
||||
public record MarkAllAsReadCommand(Guid UserId) : IRequest<ServiceResult<MarkAsReadResponse>>;
|
||||
@@ -0,0 +1,27 @@
|
||||
using Heimdall.Application.Common;
|
||||
using Heimdall.Application.DTOs;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Heimdall.Application.Notifications.Commands.MarkAllAsRead;
|
||||
|
||||
public class MarkAllAsReadHandler : IRequestHandler<MarkAllAsReadCommand, ServiceResult<MarkAsReadResponse>>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public MarkAllAsReadHandler(IApplicationDbContext context) => _context = context;
|
||||
|
||||
public async Task<ServiceResult<MarkAsReadResponse>> Handle(MarkAllAsReadCommand request, CancellationToken ct)
|
||||
{
|
||||
var unread = await _context.Notifications
|
||||
.Where(n => n.UserId == request.UserId && !n.IsRead)
|
||||
.ToListAsync(ct);
|
||||
|
||||
foreach (var notification in unread)
|
||||
notification.MarkAsRead();
|
||||
|
||||
await _context.SaveChangesWithRetryAsync(ct: ct);
|
||||
|
||||
return ServiceResult<MarkAsReadResponse>.Success(new MarkAsReadResponse(true));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
using Heimdall.Application.Common;
|
||||
using Heimdall.Application.DTOs;
|
||||
using MediatR;
|
||||
|
||||
namespace Heimdall.Application.Notifications.Commands.MarkAsRead;
|
||||
|
||||
public record MarkAsReadCommand(Guid NotificationId, Guid UserId) : IRequest<ServiceResult<MarkAsReadResponse>>;
|
||||
@@ -0,0 +1,27 @@
|
||||
using Heimdall.Application.Common;
|
||||
using Heimdall.Application.DTOs;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Heimdall.Application.Notifications.Commands.MarkAsRead;
|
||||
|
||||
public class MarkAsReadHandler : IRequestHandler<MarkAsReadCommand, ServiceResult<MarkAsReadResponse>>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public MarkAsReadHandler(IApplicationDbContext context) => _context = context;
|
||||
|
||||
public async Task<ServiceResult<MarkAsReadResponse>> Handle(MarkAsReadCommand request, CancellationToken ct)
|
||||
{
|
||||
var notification = await _context.Notifications
|
||||
.FirstOrDefaultAsync(n => n.Id == request.NotificationId && n.UserId == request.UserId, ct);
|
||||
|
||||
if (notification is null)
|
||||
return ServiceResult<MarkAsReadResponse>.NotFound("Notification not found");
|
||||
|
||||
notification.MarkAsRead();
|
||||
await _context.SaveChangesWithRetryAsync(ct: ct);
|
||||
|
||||
return ServiceResult<MarkAsReadResponse>.Success(new MarkAsReadResponse(true));
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
using Heimdall.Application.Common;
|
||||
using Heimdall.Application.DTOs;
|
||||
using MediatR;
|
||||
|
||||
namespace Heimdall.Application.Notifications.Commands.UpdateConfig;
|
||||
|
||||
public record UpdateNotificationConfigCommand(
|
||||
Guid UserId,
|
||||
bool? EmailEnabled,
|
||||
string? EmailAddress,
|
||||
bool? GotifyEnabled,
|
||||
string? GotifyUrl,
|
||||
string? GotifyToken,
|
||||
string? Language,
|
||||
bool? DailyDigest,
|
||||
bool? WeeklyDigest,
|
||||
List<string>? SubscribedEvents) : IRequest<ServiceResult<UserNotificationConfigDto>>;
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
using AutoMapper;
|
||||
using Heimdall.Application.Common;
|
||||
using Heimdall.Application.DTOs;
|
||||
using Heimdall.Domain.Entities;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Heimdall.Application.Notifications.Commands.UpdateConfig;
|
||||
|
||||
public class UpdateNotificationConfigHandler : IRequestHandler<UpdateNotificationConfigCommand, ServiceResult<UserNotificationConfigDto>>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public UpdateNotificationConfigHandler(IApplicationDbContext context, IMapper mapper)
|
||||
{
|
||||
_context = context;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<ServiceResult<UserNotificationConfigDto>> Handle(UpdateNotificationConfigCommand request, CancellationToken ct)
|
||||
{
|
||||
var config = await _context.UserNotificationConfigs
|
||||
.FirstOrDefaultAsync(c => c.UserId == request.UserId, ct);
|
||||
|
||||
if (config is null)
|
||||
{
|
||||
config = new UserNotificationConfig(request.UserId);
|
||||
_context.UserNotificationConfigs.Add(config);
|
||||
}
|
||||
|
||||
if (request.EmailEnabled.HasValue || request.EmailAddress is not null)
|
||||
config.UpdateEmail(request.EmailAddress ?? config.EmailAddress, request.EmailEnabled ?? config.EmailEnabled);
|
||||
|
||||
if (request.GotifyEnabled.HasValue || request.GotifyUrl is not null || request.GotifyToken is not null)
|
||||
config.UpdateGotify(request.GotifyUrl ?? config.GotifyUrl, request.GotifyToken ?? config.GotifyToken, request.GotifyEnabled ?? config.GotifyEnabled);
|
||||
|
||||
if (request.DailyDigest.HasValue || request.WeeklyDigest.HasValue)
|
||||
config.UpdateDigests(request.DailyDigest ?? config.DailyDigest, request.WeeklyDigest ?? config.WeeklyDigest);
|
||||
|
||||
if (request.SubscribedEvents is not null)
|
||||
config.SetSubscribedEvents(System.Text.Json.JsonSerializer.Serialize(request.SubscribedEvents));
|
||||
|
||||
if (request.Language is not null)
|
||||
config.SetLanguage(request.Language);
|
||||
|
||||
await _context.SaveChangesWithRetryAsync(ct: ct);
|
||||
|
||||
return ServiceResult<UserNotificationConfigDto>.Success(_mapper.Map<UserNotificationConfigDto>(config));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using AutoMapper;
|
||||
using Heimdall.Application.DTOs;
|
||||
using Heimdall.Domain.Entities;
|
||||
|
||||
namespace Heimdall.Application.Notifications.Mapping;
|
||||
|
||||
public class NotificationMappingProfile : Profile
|
||||
{
|
||||
public NotificationMappingProfile()
|
||||
{
|
||||
CreateMap<Notification, NotificationDto>()
|
||||
.ForMember(d => d.Type, opt => opt.MapFrom(s => s.Type.ToString()))
|
||||
.ForMember(d => d.EventType, opt => opt.MapFrom(s => s.EventType.HasValue ? s.EventType.Value.ToString() : null));
|
||||
|
||||
CreateMap<NotificationTemplate, NotificationTemplateDto>()
|
||||
.ForMember(d => d.EventType, opt => opt.MapFrom(s => s.EventType.ToString()))
|
||||
.ForMember(d => d.Channel, opt => opt.MapFrom(s => s.Channel.ToString()));
|
||||
|
||||
CreateMap<UserNotificationConfig, UserNotificationConfigDto>()
|
||||
.ForCtorParam("SubscribedEvents", opt => opt.MapFrom(s =>
|
||||
s.SubscribedEventsJson != null
|
||||
? System.Text.Json.JsonSerializer.Deserialize<List<string>>(s.SubscribedEventsJson)
|
||||
: null))
|
||||
.ForMember(d => d.SubscribedEvents, opt => opt.MapFrom(s =>
|
||||
s.SubscribedEventsJson != null
|
||||
? System.Text.Json.JsonSerializer.Deserialize<List<string>>(s.SubscribedEventsJson)
|
||||
: null));
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
using AutoMapper;
|
||||
using Heimdall.Application.Common;
|
||||
using Heimdall.Application.DTOs;
|
||||
using Heimdall.Domain.Entities;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Heimdall.Application.Notifications.Queries.GetConfig;
|
||||
|
||||
public class GetNotificationConfigHandler : IRequestHandler<GetNotificationConfigQuery, ServiceResult<UserNotificationConfigDto>>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public GetNotificationConfigHandler(IApplicationDbContext context, IMapper mapper)
|
||||
{
|
||||
_context = context;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<ServiceResult<UserNotificationConfigDto>> Handle(GetNotificationConfigQuery request, CancellationToken ct)
|
||||
{
|
||||
var config = await _context.UserNotificationConfigs
|
||||
.FirstOrDefaultAsync(c => c.UserId == request.UserId, ct);
|
||||
|
||||
if (config is null)
|
||||
{
|
||||
config = new UserNotificationConfig(request.UserId);
|
||||
_context.UserNotificationConfigs.Add(config);
|
||||
await _context.SaveChangesWithRetryAsync(ct: ct);
|
||||
}
|
||||
|
||||
return ServiceResult<UserNotificationConfigDto>.Success(_mapper.Map<UserNotificationConfigDto>(config));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
using Heimdall.Application.Common;
|
||||
using Heimdall.Application.DTOs;
|
||||
using MediatR;
|
||||
|
||||
namespace Heimdall.Application.Notifications.Queries.GetConfig;
|
||||
|
||||
public record GetNotificationConfigQuery(Guid UserId) : IRequest<ServiceResult<UserNotificationConfigDto>>;
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
using AutoMapper;
|
||||
using AutoMapper.QueryableExtensions;
|
||||
using Heimdall.Application.Common;
|
||||
using Heimdall.Application.DTOs;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Heimdall.Application.Notifications.Queries.GetNotifications;
|
||||
|
||||
public class GetNotificationsHandler : IRequestHandler<GetNotificationsQuery, PagedResult<NotificationDto>>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public GetNotificationsHandler(IApplicationDbContext context, IMapper mapper)
|
||||
{
|
||||
_context = context;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<PagedResult<NotificationDto>> Handle(GetNotificationsQuery request, CancellationToken ct)
|
||||
{
|
||||
var query = _context.Notifications
|
||||
.Where(n => n.UserId == request.UserId);
|
||||
|
||||
if (request.IsRead.HasValue)
|
||||
query = query.Where(n => n.IsRead == request.IsRead.Value);
|
||||
|
||||
var totalCount = await query.CountAsync(ct);
|
||||
|
||||
var items = await query
|
||||
.OrderByDescending(n => n.CreatedAt)
|
||||
.Skip((request.Page - 1) * request.PageSize)
|
||||
.Take(request.PageSize)
|
||||
.ProjectTo<NotificationDto>(_mapper.ConfigurationProvider)
|
||||
.ToListAsync(ct);
|
||||
|
||||
return new PagedResult<NotificationDto>(items, totalCount, request.Page, request.PageSize);
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
using Heimdall.Application.Common;
|
||||
using Heimdall.Application.DTOs;
|
||||
using MediatR;
|
||||
|
||||
namespace Heimdall.Application.Notifications.Queries.GetNotifications;
|
||||
|
||||
public record GetNotificationsQuery(
|
||||
Guid UserId,
|
||||
bool? IsRead,
|
||||
int Page = 1,
|
||||
int PageSize = 20) : IRequest<PagedResult<NotificationDto>>;
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
using Heimdall.Application.Common;
|
||||
using Heimdall.Application.DTOs;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Heimdall.Application.Notifications.Queries.GetUnreadCount;
|
||||
|
||||
public class GetUnreadCountHandler : IRequestHandler<GetUnreadCountQuery, ServiceResult<UnreadCountDto>>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public GetUnreadCountHandler(IApplicationDbContext context) => _context = context;
|
||||
|
||||
public async Task<ServiceResult<UnreadCountDto>> Handle(GetUnreadCountQuery request, CancellationToken ct)
|
||||
{
|
||||
var count = await _context.Notifications
|
||||
.CountAsync(n => n.UserId == request.UserId && !n.IsRead, ct);
|
||||
|
||||
return ServiceResult<UnreadCountDto>.Success(new UnreadCountDto(count));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
using Heimdall.Application.Common;
|
||||
using Heimdall.Application.DTOs;
|
||||
using MediatR;
|
||||
|
||||
namespace Heimdall.Application.Notifications.Queries.GetUnreadCount;
|
||||
|
||||
public record GetUnreadCountQuery(Guid UserId) : IRequest<ServiceResult<UnreadCountDto>>;
|
||||
Reference in new issue
Block a user