2026-07-26 20:45:35 +00:00
|
|
|
using Heimdall.Domain.Enums;
|
|
|
|
|
using Heimdall.Domain.Primitives;
|
|
|
|
|
|
|
|
|
|
namespace Heimdall.Domain.Entities;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Per-user notification preferences including channel configuration,
|
2026-08-12 20:05:12 +02:00
|
|
|
/// language, digest schedule, and subscribed event types.
|
2026-07-26 20:45:35 +00:00
|
|
|
/// </summary>
|
|
|
|
|
public class UserNotificationConfig : Entity<Guid>, IAuditableEntity
|
|
|
|
|
{
|
|
|
|
|
public Guid UserId { get; private set; }
|
|
|
|
|
public bool EmailEnabled { get; private set; }
|
|
|
|
|
public string? EmailAddress { get; private set; }
|
2026-07-27 04:56:16 +00:00
|
|
|
public bool NtfyEnabled { get; private set; }
|
|
|
|
|
public string? NtfyUrl { get; private set; }
|
2026-08-20 18:14:46 +02:00
|
|
|
public string? NtfyTopic { get; private set; }
|
2026-07-26 20:45:35 +00:00
|
|
|
public string Language { get; private set; } = "fr";
|
|
|
|
|
public bool DailyDigest { get; private set; }
|
|
|
|
|
public bool WeeklyDigest { get; private set; }
|
|
|
|
|
public string? SubscribedEventsJson { get; private set; } // JSON array of EventType values
|
|
|
|
|
public DateTime CreatedAt { get; set; }
|
|
|
|
|
public DateTime UpdatedAt { get; set; }
|
|
|
|
|
|
|
|
|
|
private UserNotificationConfig() { } // EF Core
|
|
|
|
|
|
|
|
|
|
public UserNotificationConfig(Guid userId)
|
|
|
|
|
: base(Guid.NewGuid())
|
|
|
|
|
{
|
|
|
|
|
UserId = userId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateEmail(string? emailAddress, bool enabled)
|
|
|
|
|
{
|
|
|
|
|
EmailAddress = emailAddress;
|
|
|
|
|
EmailEnabled = enabled;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-20 18:14:46 +02:00
|
|
|
public void UpdateNtfy(string? url, bool enabled, string? topic = null)
|
2026-07-26 20:45:35 +00:00
|
|
|
{
|
2026-07-27 04:56:16 +00:00
|
|
|
NtfyUrl = url;
|
|
|
|
|
NtfyEnabled = enabled;
|
2026-08-20 18:14:46 +02:00
|
|
|
if (topic is not null)
|
|
|
|
|
NtfyTopic = topic;
|
2026-07-26 20:45:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateDigests(bool daily, bool weekly)
|
|
|
|
|
{
|
|
|
|
|
DailyDigest = daily;
|
|
|
|
|
WeeklyDigest = weekly;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetSubscribedEvents(string? subscribedEventsJson)
|
|
|
|
|
{
|
|
|
|
|
SubscribedEventsJson = subscribedEventsJson;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-20 15:10:37 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Parses the stored JSON into a list of <see cref="EventType"/> values.
|
|
|
|
|
/// Returns null if no filter is configured (meaning: receive all events).
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IReadOnlyList<EventType>? GetSubscribedEvents()
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(SubscribedEventsJson))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var names = System.Text.Json.JsonSerializer.Deserialize<List<string>>(SubscribedEventsJson);
|
|
|
|
|
if (names is null || names.Count == 0)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var result = new List<EventType>();
|
|
|
|
|
foreach (var name in names)
|
|
|
|
|
{
|
|
|
|
|
if (Enum.TryParse<EventType>(name, ignoreCase: true, out var et))
|
|
|
|
|
result.Add(et);
|
|
|
|
|
}
|
|
|
|
|
return result.Count > 0 ? result : null;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks if a given EventType is subscribed (or if no filter is set, always returns true).
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsEventTypeSubscribed(EventType? eventType)
|
|
|
|
|
{
|
|
|
|
|
if (eventType is null) return true; // System events always pass
|
|
|
|
|
var subscribed = GetSubscribedEvents();
|
|
|
|
|
return subscribed is null || subscribed.Contains(eventType.Value);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-26 20:45:35 +00:00
|
|
|
public void SetLanguage(string language)
|
|
|
|
|
{
|
|
|
|
|
Language = language;
|
|
|
|
|
}
|
|
|
|
|
}
|