| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using RoleRadar.Application.Abstractions; |
| | | 3 | | using RoleRadar.Domain.Audit; |
| | | 4 | | using RoleRadar.Domain.Common; |
| | | 5 | | using RoleRadar.Domain.Tracking; |
| | | 6 | | |
| | | 7 | | namespace RoleRadar.Application.Tracking.Import.Pipeline; |
| | | 8 | | |
| | 96 | 9 | | public sealed record CriteriaSeedingResult(int CreatedCount, int SkippedCount); |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Step 0 in the tracker import pipeline: seeds predefined compensation criteria |
| | | 13 | | /// (comp_preferred_floor, comp_high_priority_bar) idempotently with stated provenance, |
| | | 14 | | /// and dynamically registers reason_primary criteria with inferred provenance on first use. |
| | | 15 | | /// </summary> |
| | | 16 | | public sealed class CriteriaSeedingStep |
| | | 17 | | { |
| | | 18 | | private const string ImportActor = "cli-import-tracker"; |
| | | 19 | | private readonly ITrackingTransactionContext _context; |
| | | 20 | | private readonly ConcurrentDictionary<string, Criterion> _cache = new(StringComparer.OrdinalIgnoreCase); |
| | | 21 | | |
| | | 22 | | public CriteriaSeedingStep(ITrackingTransactionContext context) |
| | | 23 | | { |
| | | 24 | | _context = context ?? throw new ArgumentNullException(nameof(context)); |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Idempotently seeds comp_preferred_floor=200000 and comp_high_priority_bar=220000 |
| | | 29 | | /// for the given owner if they do not already exist. |
| | | 30 | | /// </summary> |
| | | 31 | | public async Task<Result<CriteriaSeedingResult>> SeedPredefinedCriteriaAsync( |
| | | 32 | | string ownerId, |
| | | 33 | | DateOnly defaultDate, |
| | | 34 | | CancellationToken cancellationToken = default) |
| | | 35 | | { |
| | | 36 | | ArgumentException.ThrowIfNullOrWhiteSpace(ownerId); |
| | | 37 | | |
| | | 38 | | var createdCount = 0; |
| | | 39 | | var skippedCount = 0; |
| | | 40 | | var effectiveDateTime = defaultDate.ToDateTime(TimeOnly.MinValue, DateTimeKind.Utc); |
| | | 41 | | |
| | | 42 | | var seeds = new (string Key, string Value)[] |
| | | 43 | | { |
| | | 44 | | ("comp_preferred_floor", "200000"), |
| | | 45 | | ("comp_high_priority_bar", "220000") |
| | | 46 | | }; |
| | | 47 | | |
| | | 48 | | foreach (var (key, value) in seeds) |
| | | 49 | | { |
| | | 50 | | var existing = await _context.Criteria.GetActiveByKeyAsync(ownerId, key, cancellationToken); |
| | | 51 | | if (existing.IsSuccess && existing.Value is not null) |
| | | 52 | | { |
| | | 53 | | _cache[key] = existing.Value; |
| | | 54 | | skippedCount++; |
| | | 55 | | continue; |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | var criterion = Criterion.Create( |
| | | 59 | | ownerId: ownerId, |
| | | 60 | | key: key, |
| | | 61 | | category: CriterionCategory.Preference, |
| | | 62 | | value: value, |
| | | 63 | | provenance: ProvenanceKind.Stated, |
| | | 64 | | effectiveFrom: effectiveDateTime); |
| | | 65 | | |
| | | 66 | | var upsertResult = await _context.Criteria.UpsertCriterionAsync(criterion, cancellationToken); |
| | | 67 | | if (!upsertResult.IsSuccess) |
| | | 68 | | { |
| | | 69 | | return Result<CriteriaSeedingResult>.Failure(upsertResult.Error ?? new ResultError(ErrorCode.ValidationE |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | var auditEntry = AuditLogEntry.CreateForTracking( |
| | | 73 | | ownerId: ownerId, |
| | | 74 | | actor: ImportActor, |
| | | 75 | | operation: AuditOperation.ImportCriterionRevision, |
| | | 76 | | entityType: "criterion", |
| | | 77 | | entityId: criterion.Id, |
| | | 78 | | resultingVersionToken: criterion.VersionToken, |
| | | 79 | | occurredAtUtc: DateTime.UtcNow, |
| | | 80 | | details: new AuditDetails( |
| | | 81 | | ChangedFields: ["key", "value", "category", "provenance"], |
| | | 82 | | After: new Dictionary<string, object?> |
| | | 83 | | { |
| | | 84 | | ["key"] = criterion.Key, |
| | | 85 | | ["value"] = criterion.Value, |
| | | 86 | | ["category"] = criterion.Category.ToString(), |
| | | 87 | | ["provenance"] = criterion.Provenance.ToString() |
| | | 88 | | })); |
| | | 89 | | |
| | | 90 | | await _context.AppendAuditEntryAsync(auditEntry, cancellationToken); |
| | | 91 | | _cache[key] = criterion; |
| | | 92 | | createdCount++; |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | return Result<CriteriaSeedingResult>.Success(new CriteriaSeedingResult(createdCount, skippedCount)); |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Retrieves an existing criterion by key or registers a new criterion on first use |
| | | 100 | | /// with inferred provenance and HardSkip category. |
| | | 101 | | /// </summary> |
| | | 102 | | public async Task<Result<Criterion>> GetOrCreateReasonCriterionAsync( |
| | | 103 | | string ownerId, |
| | | 104 | | string reasonPrimary, |
| | | 105 | | DateTimeOffset effectiveDate, |
| | | 106 | | TrackerImportSummary? summary = null, |
| | | 107 | | CancellationToken cancellationToken = default) |
| | | 108 | | { |
| | | 109 | | ArgumentException.ThrowIfNullOrWhiteSpace(ownerId); |
| | | 110 | | ArgumentException.ThrowIfNullOrWhiteSpace(reasonPrimary); |
| | | 111 | | |
| | | 112 | | var rawKey = reasonPrimary.Trim(); |
| | | 113 | | var key = TrackerImportFieldLimiter.Limit(rawKey, TrackerImportFieldLimiter.CriteriaKeyLimit, "criteria_key", su |
| | | 114 | | if (_cache.TryGetValue(key, out var cached)) |
| | | 115 | | { |
| | | 116 | | return Result<Criterion>.Success(cached); |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | var existing = await _context.Criteria.GetActiveByKeyAsync(ownerId, key, cancellationToken); |
| | | 120 | | if (existing.IsSuccess && existing.Value is not null) |
| | | 121 | | { |
| | | 122 | | _cache[key] = existing.Value; |
| | | 123 | | return Result<Criterion>.Success(existing.Value); |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | var criterion = Criterion.Create( |
| | | 127 | | ownerId: ownerId, |
| | | 128 | | key: key, |
| | | 129 | | category: CriterionCategory.HardSkip, |
| | | 130 | | value: key, |
| | | 131 | | provenance: ProvenanceKind.Inferred, |
| | | 132 | | effectiveFrom: effectiveDate); |
| | | 133 | | |
| | | 134 | | var upsertResult = await _context.Criteria.UpsertCriterionAsync(criterion, cancellationToken); |
| | | 135 | | if (!upsertResult.IsSuccess) |
| | | 136 | | { |
| | | 137 | | return Result<Criterion>.Failure(upsertResult.Error ?? new ResultError(ErrorCode.ValidationError, "Failed to |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | if (summary is not null) |
| | | 141 | | { |
| | | 142 | | summary.CriteriaCreated++; |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | var auditEntry = AuditLogEntry.CreateForTracking( |
| | | 146 | | ownerId: ownerId, |
| | | 147 | | actor: ImportActor, |
| | | 148 | | operation: AuditOperation.ImportCriterionRevision, |
| | | 149 | | entityType: "criterion", |
| | | 150 | | entityId: criterion.Id, |
| | | 151 | | resultingVersionToken: criterion.VersionToken, |
| | | 152 | | occurredAtUtc: DateTime.UtcNow, |
| | | 153 | | details: new AuditDetails( |
| | | 154 | | ChangedFields: ["key", "value", "category", "provenance"], |
| | | 155 | | After: new Dictionary<string, object?> |
| | | 156 | | { |
| | | 157 | | ["key"] = criterion.Key, |
| | | 158 | | ["value"] = criterion.Value, |
| | | 159 | | ["category"] = criterion.Category.ToString(), |
| | | 160 | | ["provenance"] = criterion.Provenance.ToString() |
| | | 161 | | })); |
| | | 162 | | |
| | | 163 | | await _context.AppendAuditEntryAsync(auditEntry, cancellationToken); |
| | | 164 | | _cache[key] = criterion; |
| | | 165 | | |
| | | 166 | | return Result<Criterion>.Success(criterion); |
| | | 167 | | } |
| | | 168 | | } |