Files
NTBug/backend/src/Helpdesk.Domain/Notifications/Notification.cs
T

100 lines
3.3 KiB
C#
Raw Normal View History

2026-11-18 21:58:33 +01:00
using Helpdesk.Domain.Issues;
using Helpdesk.Domain.Proposals;
namespace Helpdesk.Domain.Notifications;
public enum NotificationType { IssueCreated, IssueAssigned, IssueStatusChanged, IssueCommentAdded, IssueReopened, ProposalStatusChanged }
public enum DeliveryChannel { Email, Push }
2026-11-19 14:02:02 +01:00
public enum DeliveryStatus { Pending, Sent, Failed, Skipped }
2026-11-18 21:58:33 +01:00
public sealed class Notification : IHasCreatedAt
{
private Notification() { }
public Notification(Issue issue, int userId, NotificationType type, string title, string message)
: this(userId, type, title, message)
{
Issue = issue;
}
public Notification(Proposal proposal, int userId, string title)
: this(userId, NotificationType.ProposalStatusChanged, title, proposal.Title)
{
ProposalId = proposal.Id;
}
private Notification(int userId, NotificationType type, string title, string message)
{
UserId = userId;
Type = type;
Title = title;
Message = message;
}
public int Id { get; private set; }
public int? IssueId { get; private set; }
public int? ProposalId { get; private set; }
public Issue? Issue { get; private set; }
public int UserId { get; private set; }
public NotificationType Type { get; private set; }
public string Title { get; private set; } = null!;
public string Message { get; private set; } = null!;
public DateTime CreatedAt { get; set; }
public DateTime? ReadAt { get; private set; }
}
public sealed class NotificationDelivery(
Notification notification, DeliveryChannel channel, bool enabled, DateTime now)
{
2026-11-19 14:02:02 +01:00
private static readonly TimeSpan[] RetryDelays =
[TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(30), TimeSpan.FromHours(2)];
2026-11-18 21:58:33 +01:00
private NotificationDelivery() : this(null!, default, false, default) { }
public int Id { get; private set; }
public int NotificationId { get; private set; }
public Notification Notification { get; private set; } = notification;
public DeliveryChannel Channel { get; private set; } = channel;
public DeliveryStatus Status { get; private set; } = enabled ? DeliveryStatus.Pending : DeliveryStatus.Skipped;
2026-11-19 14:02:02 +01:00
public int AttemptCount { get; private set; }
2026-11-18 21:58:33 +01:00
public DateTime? NextAttemptAt { get; private set; } = enabled ? now : null;
2026-11-19 14:02:02 +01:00
public string? LastError { get; private set; }
public DateTime? SentAt { get; private set; }
public void RecordSent(DateTime now)
{
AttemptCount++;
Status = DeliveryStatus.Sent;
SentAt = now;
NextAttemptAt = null;
LastError = null;
}
public void RecordFailure(DateTime now, string reason, bool retryable)
{
AttemptCount++;
LastError = reason;
if (retryable && AttemptCount <= RetryDelays.Length)
{
NextAttemptAt = now + RetryDelays[AttemptCount - 1];
return;
}
Status = DeliveryStatus.Failed;
NextAttemptAt = null;
}
public void Skip()
{
Status = DeliveryStatus.Skipped;
NextAttemptAt = null;
LastError = null;
}
2026-11-18 21:58:33 +01:00
}
public sealed class PushDevice
{
public int Id { get; private set; }
public int UserId { get; private set; }
2026-11-19 14:02:02 +01:00
public string FcmToken { get; private set; } = null!;
2026-11-18 21:58:33 +01:00
}