| | | 1 | | using System.Globalization; |
| | | 2 | | |
| | | 3 | | namespace RoleRadar.Domain.Tracking; |
| | | 4 | | |
| | | 5 | | public sealed class ApplicationEvent |
| | | 6 | | { |
| | | 7 | | public const int MaxEventSourceLength = 100; |
| | | 8 | | public const int MaxNotesLength = 500; |
| | | 9 | | |
| | 5224 | 10 | | public Guid Id { get; init; } |
| | 12742 | 11 | | public string OwnerId { get; init; } = string.Empty; |
| | 10052 | 12 | | public Guid ApplicationId { get; init; } |
| | 8044 | 13 | | public string EventDate { get; private set; } = string.Empty; |
| | 5376 | 14 | | public ApplicationEventType EventType { get; private set; } |
| | 5378 | 15 | | public string? EventSource { get; private set; } |
| | 5428 | 16 | | public string? Notes { get; private set; } |
| | 5164 | 17 | | public DateTimeOffset CreatedAt { get; init; } |
| | | 18 | | |
| | | 19 | | public static ApplicationEvent Create( |
| | | 20 | | string ownerId, |
| | | 21 | | Guid applicationId, |
| | | 22 | | string eventDate, |
| | | 23 | | ApplicationEventType eventType, |
| | | 24 | | string? eventSource = null, |
| | | 25 | | string? notes = null, |
| | | 26 | | Guid? id = null, |
| | | 27 | | DateTimeOffset? now = null) |
| | | 28 | | { |
| | 2602 | 29 | | ArgumentException.ThrowIfNullOrWhiteSpace(ownerId); |
| | 2602 | 30 | | if (applicationId == Guid.Empty) |
| | | 31 | | { |
| | 0 | 32 | | throw new ArgumentException("Application ID cannot be empty.", nameof(applicationId)); |
| | | 33 | | } |
| | | 34 | | |
| | 2602 | 35 | | ArgumentException.ThrowIfNullOrWhiteSpace(eventDate); |
| | 2600 | 36 | | if (!DateOnly.TryParseExact(eventDate.Trim(), "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, o |
| | | 37 | | { |
| | 4 | 38 | | throw new ArgumentException($"Event date must be in YYYY-MM-DD format: '{eventDate}'.", nameof(eventDate)); |
| | | 39 | | } |
| | | 40 | | |
| | 2596 | 41 | | string? validatedEventSource = null; |
| | 2596 | 42 | | if (eventSource is not null) |
| | | 43 | | { |
| | 242 | 44 | | if (eventSource.Length > MaxEventSourceLength) |
| | | 45 | | { |
| | 2 | 46 | | throw new ArgumentException($"Event source cannot exceed {MaxEventSourceLength} characters.", nameof(eve |
| | | 47 | | } |
| | 240 | 48 | | validatedEventSource = eventSource.Trim(); |
| | | 49 | | } |
| | | 50 | | |
| | 2594 | 51 | | string? validatedNotes = null; |
| | 2594 | 52 | | if (notes is not null) |
| | | 53 | | { |
| | 228 | 54 | | if (notes.Length > MaxNotesLength) |
| | | 55 | | { |
| | 2 | 56 | | throw new ArgumentException($"Notes cannot exceed {MaxNotesLength} characters.", nameof(notes)); |
| | | 57 | | } |
| | 226 | 58 | | validatedNotes = notes.Trim(); |
| | | 59 | | } |
| | | 60 | | |
| | 2592 | 61 | | var ts = now ?? DateTimeOffset.UtcNow; |
| | 2592 | 62 | | return new ApplicationEvent |
| | 2592 | 63 | | { |
| | 2592 | 64 | | Id = id ?? Guid.CreateVersion7(), |
| | 2592 | 65 | | OwnerId = ownerId.Trim(), |
| | 2592 | 66 | | ApplicationId = applicationId, |
| | 2592 | 67 | | EventDate = eventDate.Trim(), |
| | 2592 | 68 | | EventType = eventType, |
| | 2592 | 69 | | EventSource = validatedEventSource, |
| | 2592 | 70 | | Notes = validatedNotes, |
| | 2592 | 71 | | CreatedAt = ts |
| | 2592 | 72 | | }; |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | public static ApplicationEvent Rehydrate( |
| | | 76 | | Guid id, |
| | | 77 | | string ownerId, |
| | | 78 | | Guid applicationId, |
| | | 79 | | string eventDate, |
| | | 80 | | ApplicationEventType eventType, |
| | | 81 | | string? eventSource, |
| | | 82 | | string? notes, |
| | | 83 | | DateTimeOffset createdAt) |
| | | 84 | | { |
| | 130 | 85 | | return new ApplicationEvent |
| | 130 | 86 | | { |
| | 130 | 87 | | Id = id, |
| | 130 | 88 | | OwnerId = ownerId, |
| | 130 | 89 | | ApplicationId = applicationId, |
| | 130 | 90 | | EventDate = eventDate, |
| | 130 | 91 | | EventType = eventType, |
| | 130 | 92 | | EventSource = eventSource, |
| | 130 | 93 | | Notes = notes, |
| | 130 | 94 | | CreatedAt = createdAt |
| | 130 | 95 | | }; |
| | | 96 | | } |
| | | 97 | | } |