dotnet-cqrs/Svrnty.CQRS.Events.Abstractions/Models/EventSubscription.cs

89 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using Svrnty.CQRS.Events.Abstractions.Subscriptions;
namespace Svrnty.CQRS.Events.Abstractions.Models;
/// <summary>
/// Represents a client's subscription to events for a specific correlation.
/// </summary>
public sealed class EventSubscription
{
/// <summary>
/// Unique identifier for this subscription.
/// </summary>
public required string SubscriptionId { get; init; }
/// <summary>
/// ID of the user/client that created this subscription.
/// </summary>
public required string SubscriberId { get; init; }
/// <summary>
/// Correlation ID this subscription is listening to.
/// </summary>
public required string CorrelationId { get; init; }
/// <summary>
/// Event type names the subscriber wants to receive (e.g., "UserInvitationSentEvent").
/// Empty set means all event types.
/// </summary>
public required HashSet<string> EventTypes { get; init; }
/// <summary>
/// Event types that will complete/close this subscription when received.
/// </summary>
public HashSet<string> TerminalEventTypes { get; init; } = new();
/// <summary>
/// How events should be delivered to the subscriber.
/// </summary>
public DeliveryMode DeliveryMode { get; init; } = DeliveryMode.Immediate;
/// <summary>
/// When this subscription was created.
/// </summary>
public DateTimeOffset CreatedAt { get; init; }
/// <summary>
/// Optional expiration time for this subscription.
/// </summary>
public DateTimeOffset? ExpiresAt { get; init; }
/// <summary>
/// When this subscription was completed (terminal event or cancellation).
/// </summary>
public DateTimeOffset? CompletedAt { get; set; }
/// <summary>
/// Last successfully delivered event sequence number (for catch-up).
/// </summary>
public long LastDeliveredSequence { get; set; }
/// <summary>
/// Current status of this subscription.
/// </summary>
public SubscriptionStatus Status { get; set; } = SubscriptionStatus.Active;
/// <summary>
/// Checks if this subscription is expired.
/// </summary>
public bool IsExpired => ExpiresAt.HasValue && DateTimeOffset.UtcNow > ExpiresAt.Value;
/// <summary>
/// Checks if this subscription should receive the specified event type.
/// </summary>
public bool ShouldReceive(string eventType)
{
return EventTypes.Count == 0 || EventTypes.Contains(eventType);
}
/// <summary>
/// Checks if the specified event type is a terminal event for this subscription.
/// </summary>
public bool IsTerminalEvent(string eventType)
{
return TerminalEventTypes.Contains(eventType);
}
}