| | | 1 | | using System.Globalization; |
| | | 2 | | |
| | | 3 | | namespace RoleRadar.Domain.Tracking; |
| | | 4 | | |
| | | 5 | | public sealed class Application |
| | | 6 | | { |
| | | 7 | | public const int MaxResumePointerLength = 300; |
| | | 8 | | public const int MaxConfirmationCodeLength = 100; |
| | | 9 | | public const int MaxOutcomeReasonLength = 500; |
| | | 10 | | |
| | 11420 | 11 | | public Guid Id { get; init; } |
| | 11970 | 12 | | public string OwnerId { get; init; } = string.Empty; |
| | 6202 | 13 | | public Guid PostingId { get; init; } |
| | 7582 | 14 | | public string SubmittedDate { get; private set; } = string.Empty; |
| | 9842 | 15 | | public DesiredSalary DesiredSalary { get; private set; } = new(); |
| | 3750 | 16 | | public string? TargetStartDate { get; private set; } |
| | 3764 | 17 | | public ResumeVariant ResumeVariant { get; private set; } |
| | 3756 | 18 | | public string? ResumePointer { get; private set; } |
| | 3750 | 19 | | public Guid? ReferralContactId { get; private set; } |
| | 3756 | 20 | | public string? ConfirmationCode { get; private set; } |
| | 6858 | 21 | | public ApplicationOutcome Outcome { get; private set; } = ApplicationOutcome.Applied; |
| | 4250 | 22 | | public string? OutcomeReason { get; private set; } |
| | 4486 | 23 | | public Guid VersionToken { get; private set; } |
| | 8232 | 24 | | public DateTimeOffset CreatedAt { get; init; } |
| | 4208 | 25 | | public DateTimeOffset UpdatedAt { get; private set; } |
| | | 26 | | |
| | | 27 | | public static Application Create( |
| | | 28 | | string ownerId, |
| | | 29 | | Guid postingId, |
| | | 30 | | string submittedDate, |
| | | 31 | | ResumeVariant resumeVariant, |
| | | 32 | | DesiredSalary? desiredSalary = null, |
| | | 33 | | string? targetStartDate = null, |
| | | 34 | | string? resumePointer = null, |
| | | 35 | | Guid? referralContactId = null, |
| | | 36 | | string? confirmationCode = null, |
| | | 37 | | ApplicationOutcome outcome = ApplicationOutcome.Applied, |
| | | 38 | | string? outcomeReason = null, |
| | | 39 | | Guid? id = null, |
| | | 40 | | Guid? versionToken = null, |
| | | 41 | | DateTimeOffset? now = null) |
| | | 42 | | { |
| | 1294 | 43 | | ArgumentException.ThrowIfNullOrWhiteSpace(ownerId); |
| | 1294 | 44 | | if (postingId == Guid.Empty) |
| | | 45 | | { |
| | 0 | 46 | | throw new ArgumentException("Posting ID cannot be empty.", nameof(postingId)); |
| | | 47 | | } |
| | | 48 | | |
| | 1294 | 49 | | ArgumentException.ThrowIfNullOrWhiteSpace(submittedDate); |
| | 1290 | 50 | | if (!DateOnly.TryParseExact(submittedDate.Trim(), "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.Non |
| | | 51 | | { |
| | 8 | 52 | | throw new ArgumentException($"Submitted date must be in YYYY-MM-DD format: '{submittedDate}'.", nameof(submi |
| | | 53 | | } |
| | | 54 | | |
| | 1282 | 55 | | string? validatedTargetStartDate = null; |
| | 1282 | 56 | | if (!string.IsNullOrWhiteSpace(targetStartDate)) |
| | | 57 | | { |
| | 8 | 58 | | if (!DateOnly.TryParseExact(targetStartDate.Trim(), "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyl |
| | | 59 | | { |
| | 6 | 60 | | throw new ArgumentException($"Target start date must be in YYYY-MM-DD format: '{targetStartDate}'.", nam |
| | | 61 | | } |
| | 2 | 62 | | validatedTargetStartDate = targetStartDate.Trim(); |
| | | 63 | | } |
| | | 64 | | |
| | 1276 | 65 | | var salary = desiredSalary ?? new DesiredSalary(); |
| | 1276 | 66 | | if (salary.Min.HasValue && salary.Min.Value < 0) |
| | | 67 | | { |
| | 4 | 68 | | throw new ArgumentException("Desired salary minimum must be >= 0.", nameof(desiredSalary)); |
| | | 69 | | } |
| | | 70 | | |
| | 1272 | 71 | | if (salary.Min.HasValue && salary.Max.HasValue && salary.Max.Value < salary.Min.Value) |
| | | 72 | | { |
| | 2 | 73 | | throw new ArgumentException("Desired salary maximum must be greater than or equal to minimum.", nameof(desir |
| | | 74 | | } |
| | | 75 | | |
| | 1270 | 76 | | if (string.IsNullOrWhiteSpace(salary.Currency) || salary.Currency.Trim().Length != 3) |
| | | 77 | | { |
| | 4 | 78 | | throw new ArgumentException("Desired salary currency must be a 3-character currency code.", nameof(desiredSa |
| | | 79 | | } |
| | | 80 | | |
| | 1266 | 81 | | string? validatedResumePointer = null; |
| | 1266 | 82 | | if (resumePointer is not null) |
| | | 83 | | { |
| | 12 | 84 | | if (resumePointer.Length > MaxResumePointerLength) |
| | | 85 | | { |
| | 2 | 86 | | throw new ArgumentException($"Resume pointer cannot exceed {MaxResumePointerLength} characters.", nameof |
| | | 87 | | } |
| | | 88 | | |
| | 10 | 89 | | if (resumePointer.Contains('\r') || resumePointer.Contains('\n')) |
| | | 90 | | { |
| | 2 | 91 | | throw new ArgumentException("Resume pointer cannot contain newline characters or raw resume document con |
| | | 92 | | } |
| | | 93 | | |
| | 8 | 94 | | validatedResumePointer = resumePointer.Trim(); |
| | | 95 | | } |
| | | 96 | | |
| | 1262 | 97 | | string? validatedConfirmationCode = null; |
| | 1262 | 98 | | if (confirmationCode is not null) |
| | | 99 | | { |
| | 10 | 100 | | if (confirmationCode.Length > MaxConfirmationCodeLength) |
| | | 101 | | { |
| | 2 | 102 | | throw new ArgumentException($"Confirmation code cannot exceed {MaxConfirmationCodeLength} characters.", |
| | | 103 | | } |
| | 8 | 104 | | validatedConfirmationCode = confirmationCode.Trim(); |
| | | 105 | | } |
| | | 106 | | |
| | 1260 | 107 | | string? validatedOutcomeReason = null; |
| | 1260 | 108 | | if (outcomeReason is not null) |
| | | 109 | | { |
| | 16 | 110 | | if (outcomeReason.Length > MaxOutcomeReasonLength) |
| | | 111 | | { |
| | 0 | 112 | | throw new ArgumentException($"Outcome reason cannot exceed {MaxOutcomeReasonLength} characters.", nameof |
| | | 113 | | } |
| | 16 | 114 | | validatedOutcomeReason = outcomeReason.Trim(); |
| | | 115 | | } |
| | | 116 | | |
| | 1260 | 117 | | var ts = now ?? DateTimeOffset.UtcNow; |
| | 1260 | 118 | | return new Application |
| | 1260 | 119 | | { |
| | 1260 | 120 | | Id = id ?? Guid.CreateVersion7(), |
| | 1260 | 121 | | OwnerId = ownerId.Trim(), |
| | 1260 | 122 | | PostingId = postingId, |
| | 1260 | 123 | | SubmittedDate = submittedDate.Trim(), |
| | 1260 | 124 | | DesiredSalary = salary, |
| | 1260 | 125 | | TargetStartDate = validatedTargetStartDate, |
| | 1260 | 126 | | ResumeVariant = resumeVariant, |
| | 1260 | 127 | | ResumePointer = validatedResumePointer, |
| | 1260 | 128 | | ReferralContactId = referralContactId, |
| | 1260 | 129 | | ConfirmationCode = validatedConfirmationCode, |
| | 1260 | 130 | | Outcome = outcome, |
| | 1260 | 131 | | OutcomeReason = validatedOutcomeReason, |
| | 1260 | 132 | | VersionToken = versionToken ?? Guid.CreateVersion7(), |
| | 1260 | 133 | | CreatedAt = ts, |
| | 1260 | 134 | | UpdatedAt = ts |
| | 1260 | 135 | | }; |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | public static Application Rehydrate( |
| | | 139 | | Guid id, |
| | | 140 | | string ownerId, |
| | | 141 | | Guid postingId, |
| | | 142 | | string submittedDate, |
| | | 143 | | DesiredSalary desiredSalary, |
| | | 144 | | string? targetStartDate, |
| | | 145 | | ResumeVariant resumeVariant, |
| | | 146 | | string? resumePointer, |
| | | 147 | | Guid? referralContactId, |
| | | 148 | | string? confirmationCode, |
| | | 149 | | ApplicationOutcome outcome, |
| | | 150 | | string? outcomeReason, |
| | | 151 | | Guid versionToken, |
| | | 152 | | DateTimeOffset createdAt, |
| | | 153 | | DateTimeOffset updatedAt) |
| | | 154 | | { |
| | 240 | 155 | | return new Application |
| | 240 | 156 | | { |
| | 240 | 157 | | Id = id, |
| | 240 | 158 | | OwnerId = ownerId, |
| | 240 | 159 | | PostingId = postingId, |
| | 240 | 160 | | SubmittedDate = submittedDate, |
| | 240 | 161 | | DesiredSalary = desiredSalary, |
| | 240 | 162 | | TargetStartDate = targetStartDate, |
| | 240 | 163 | | ResumeVariant = resumeVariant, |
| | 240 | 164 | | ResumePointer = resumePointer, |
| | 240 | 165 | | ReferralContactId = referralContactId, |
| | 240 | 166 | | ConfirmationCode = confirmationCode, |
| | 240 | 167 | | Outcome = outcome, |
| | 240 | 168 | | OutcomeReason = outcomeReason, |
| | 240 | 169 | | VersionToken = versionToken, |
| | 240 | 170 | | CreatedAt = createdAt, |
| | 240 | 171 | | UpdatedAt = updatedAt |
| | 240 | 172 | | }; |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | public void ApplyEvent(ApplicationEventType eventType, string? notes, DateTimeOffset timestamp) |
| | | 176 | | { |
| | 1352 | 177 | | var trimmedNotes = string.IsNullOrWhiteSpace(notes) ? null : notes.Trim(); |
| | 1352 | 178 | | if (trimmedNotes is not null && trimmedNotes.Length > MaxOutcomeReasonLength) |
| | | 179 | | { |
| | 0 | 180 | | trimmedNotes = trimmedNotes[..MaxOutcomeReasonLength]; |
| | | 181 | | } |
| | | 182 | | |
| | | 183 | | switch (eventType) |
| | | 184 | | { |
| | | 185 | | case ApplicationEventType.Offer: |
| | | 186 | | case ApplicationEventType.Applied: |
| | | 187 | | case ApplicationEventType.FollowUp: |
| | | 188 | | case ApplicationEventType.Interview: |
| | | 189 | | case ApplicationEventType.Note: |
| | | 190 | | // Non-terminal events: outcome remains unchanged |
| | | 191 | | break; |
| | | 192 | | |
| | | 193 | | case ApplicationEventType.Rejected: |
| | 322 | 194 | | Outcome = ApplicationOutcome.Rejected; |
| | 322 | 195 | | OutcomeReason = trimmedNotes; |
| | 322 | 196 | | UpdatedAt = timestamp; |
| | 322 | 197 | | VersionToken = Guid.CreateVersion7(); |
| | 322 | 198 | | break; |
| | | 199 | | |
| | | 200 | | case ApplicationEventType.OfferAccepted: |
| | | 201 | | case ApplicationEventType.OfferDeclined: |
| | | 202 | | case ApplicationEventType.Withdrawn: |
| | 126 | 203 | | Outcome = ApplicationOutcome.Closed; |
| | 126 | 204 | | OutcomeReason = trimmedNotes; |
| | 126 | 205 | | UpdatedAt = timestamp; |
| | 126 | 206 | | VersionToken = Guid.CreateVersion7(); |
| | | 207 | | break; |
| | | 208 | | } |
| | 1030 | 209 | | } |
| | | 210 | | } |