2026-07-22 19:54:55 +00:00
|
|
|
using Heimdall.Application.Services;
|
2026-10-22 19:43:18 +02:00
|
|
|
using Heimdall.Domain.Enums;
|
2026-07-22 19:54:55 +00:00
|
|
|
using MediatR;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.Application.Events.Notifications;
|
|
|
|
|
|
|
|
|
|
public class EventCreatedNotificationHandler : INotificationHandler<EventCreatedNotification>
|
|
|
|
|
{
|
|
|
|
|
private readonly IEventBroadcastService _broadcast;
|
2026-10-22 19:43:18 +02:00
|
|
|
private readonly ICameraAccessService _cameraAccess;
|
|
|
|
|
private readonly INotificationService _notifications;
|
2026-07-22 19:54:55 +00:00
|
|
|
|
2026-10-22 19:43:18 +02:00
|
|
|
public EventCreatedNotificationHandler(
|
|
|
|
|
IEventBroadcastService broadcast,
|
|
|
|
|
ICameraAccessService cameraAccess,
|
|
|
|
|
INotificationService notifications)
|
|
|
|
|
{
|
|
|
|
|
_broadcast = broadcast;
|
|
|
|
|
_cameraAccess = cameraAccess;
|
|
|
|
|
_notifications = notifications;
|
|
|
|
|
}
|
2026-07-22 19:54:55 +00:00
|
|
|
|
|
|
|
|
public async Task Handle(EventCreatedNotification notification, CancellationToken ct)
|
|
|
|
|
{
|
2026-07-27 18:59:12 +00:00
|
|
|
await _broadcast.NotifyEventCreated(notification.Event, ct).ConfigureAwait(false);
|
2026-10-22 19:43:18 +02:00
|
|
|
|
|
|
|
|
var cameraEvent = notification.Event;
|
2026-10-23 07:20:47 +02:00
|
|
|
if (cameraEvent.Type == EventType.ManualSnapshot) return;
|
2026-10-22 19:43:18 +02:00
|
|
|
var recipients = await _cameraAccess.GetUserIdsWithCameraAccessAsync(cameraEvent.CameraId, ct)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
var title = cameraEvent.Type switch
|
|
|
|
|
{
|
|
|
|
|
EventType.PersonDetected => "Personne détectée",
|
|
|
|
|
EventType.VehicleDetected => "Véhicule détecté",
|
|
|
|
|
EventType.UnknownPerson => "Personne inconnue détectée",
|
|
|
|
|
EventType.FaceRecognized => "Visage reconnu",
|
|
|
|
|
EventType.LicensePlateDetected => "Plaque détectée",
|
|
|
|
|
EventType.CameraOffline => "Caméra hors ligne",
|
|
|
|
|
EventType.CameraOnline => "Caméra en ligne",
|
|
|
|
|
_ => "Mouvement détecté"
|
|
|
|
|
};
|
|
|
|
|
var message = string.IsNullOrWhiteSpace(cameraEvent.Label)
|
|
|
|
|
? $"{title} sur la caméra {cameraEvent.CameraId}."
|
|
|
|
|
: $"{title} sur la caméra {cameraEvent.CameraId} : {cameraEvent.Label}.";
|
|
|
|
|
var type = cameraEvent.Type is EventType.UnknownPerson or EventType.CameraOffline
|
|
|
|
|
? NotificationType.Alert
|
|
|
|
|
: cameraEvent.Type == EventType.CameraOnline
|
|
|
|
|
? NotificationType.Info
|
|
|
|
|
: NotificationType.Warning;
|
|
|
|
|
|
|
|
|
|
foreach (var userId in recipients)
|
|
|
|
|
{
|
|
|
|
|
var result = await _notifications.CreateNotificationAsync(
|
|
|
|
|
userId, title, message, type, cameraEvent.Type, cameraEvent.CameraId,
|
|
|
|
|
cameraEvent.SiteId, cameraEvent.SnapshotUrl,
|
|
|
|
|
$"/events?cameraId={cameraEvent.CameraId}&eventId={cameraEvent.Id}",
|
|
|
|
|
sourceEventId: cameraEvent.Id, ct: ct).ConfigureAwait(false);
|
|
|
|
|
if (!result.IsSuccess)
|
|
|
|
|
throw new InvalidOperationException($"Could not persist event notification for user {userId}: {result.Error}");
|
|
|
|
|
}
|
2026-07-22 19:54:55 +00:00
|
|
|
}
|
|
|
|
|
}
|