| | | 1 | | using System.Globalization; |
| | | 2 | | using System.Net; |
| | | 3 | | using RoleRadar.Application.Abstractions; |
| | | 4 | | using RoleRadar.Domain.Audit; |
| | | 5 | | using RoleRadar.Domain.Common; |
| | | 6 | | using RoleRadar.Domain.Tracking; |
| | | 7 | | |
| | | 8 | | namespace RoleRadar.Application.Tracking.Import.Pipeline; |
| | | 9 | | |
| | | 10 | | public sealed class AppendixRowsStep : ITrackerImportStep |
| | | 11 | | { |
| | 8 | 12 | | private static readonly char[] ReasonSeparators = [',', ';']; |
| | | 13 | | private const string ImportActor = "cli-import-tracker"; |
| | | 14 | | private readonly ICsvParser _csvParser; |
| | | 15 | | private readonly ITrackingTransactionContext _context; |
| | | 16 | | private readonly CriteriaSeedingStep _criteriaStep; |
| | | 17 | | |
| | 62 | 18 | | public AppendixRowsStep(ICsvParser csvParser, ITrackingTransactionContext context, CriteriaSeedingStep criteriaStep) |
| | | 19 | | { |
| | 62 | 20 | | _csvParser = csvParser ?? throw new ArgumentNullException(nameof(csvParser)); |
| | 62 | 21 | | _context = context ?? throw new ArgumentNullException(nameof(context)); |
| | 62 | 22 | | _criteriaStep = criteriaStep ?? throw new ArgumentNullException(nameof(criteriaStep)); |
| | 62 | 23 | | } |
| | | 24 | | |
| | | 25 | | public async Task<Result<bool>> ExecuteAsync(TrackerImportContext context, CancellationToken cancellationToken = def |
| | | 26 | | { |
| | 60 | 27 | | ArgumentNullException.ThrowIfNull(context); |
| | | 28 | | |
| | 60 | 29 | | var filePath = Path.Combine(context.BundleDirectory, "appendix-rows.csv"); |
| | 60 | 30 | | var recon = context.Summary.GetOrCreateFileReconciliation("appendix-rows.csv"); |
| | | 31 | | |
| | 60 | 32 | | if (!File.Exists(filePath)) |
| | | 33 | | { |
| | 0 | 34 | | recon.IsAbsent = true; |
| | 0 | 35 | | return Result<bool>.Success(true); |
| | | 36 | | } |
| | | 37 | | |
| | 60 | 38 | | recon.IsAbsent = false; |
| | | 39 | | |
| | 60 | 40 | | var content = await File.ReadAllTextAsync(filePath, cancellationToken); |
| | 60 | 41 | | if (string.IsNullOrWhiteSpace(content)) |
| | | 42 | | { |
| | 2 | 43 | | recon.RecordErrored(); |
| | 2 | 44 | | context.Summary.Errors.Add(new TrackerImportRowError( |
| | 2 | 45 | | "appendix-rows.csv", |
| | 2 | 46 | | 1, |
| | 2 | 47 | | ErrorCode.ValidationError.ToString(), |
| | 2 | 48 | | "File is present but empty.")); |
| | 2 | 49 | | return Result<bool>.Failure(ErrorCode.ValidationError, "appendix-rows.csv is present but empty."); |
| | | 50 | | } |
| | | 51 | | |
| | 58 | 52 | | using var headerReader = new StringReader(content); |
| | 58 | 53 | | var headers = _csvParser.ReadHeader(headerReader); |
| | 58 | 54 | | var headerValidation = CsvHeaderValidator.Validate("appendix-rows.csv", headers, CsvHeaderValidator.AppendixHead |
| | 58 | 55 | | if (!headerValidation.IsSuccess) |
| | | 56 | | { |
| | 2 | 57 | | recon.RecordErrored(); |
| | 2 | 58 | | context.Summary.Errors.Add(new TrackerImportRowError( |
| | 2 | 59 | | "appendix-rows.csv", |
| | 2 | 60 | | 1, |
| | 2 | 61 | | ErrorCode.ValidationError.ToString(), |
| | 2 | 62 | | headerValidation.Error!.Value.Message)); |
| | 2 | 63 | | return Result<bool>.Failure(headerValidation.Error!.Value); |
| | | 64 | | } |
| | | 65 | | |
| | 56 | 66 | | var records = new List<ICsvRecord>(); |
| | 56 | 67 | | using (var reader = new StringReader(content)) |
| | | 68 | | { |
| | 56 | 69 | | records.AddRange(_csvParser.Read(reader, hasHeader: true)); |
| | 56 | 70 | | } |
| | | 71 | | |
| | | 72 | | // Partition records: non-duplicates before duplicates so duplicate_of_id can resolve |
| | 56 | 73 | | var nonDuplicates = new List<ICsvRecord>(); |
| | 56 | 74 | | var duplicates = new List<ICsvRecord>(); |
| | | 75 | | |
| | 328 | 76 | | foreach (var row in records) |
| | | 77 | | { |
| | 108 | 78 | | var rawProposedDisp = (TrackerImportTextSanitizer.SanitizePlainText(row["proposed_disposition"]) ?? TrackerI |
| | 108 | 79 | | var isDuplicate = string.Equals(rawProposedDisp, "duplicate", StringComparison.OrdinalIgnoreCase); |
| | | 80 | | |
| | 108 | 81 | | if (isDuplicate) |
| | | 82 | | { |
| | 14 | 83 | | duplicates.Add(row); |
| | | 84 | | } |
| | | 85 | | else |
| | | 86 | | { |
| | 94 | 87 | | nonDuplicates.Add(row); |
| | | 88 | | } |
| | | 89 | | } |
| | | 90 | | |
| | 56 | 91 | | var defaultDate = context.DefaultDate ?? DateOnly.FromDateTime(DateTime.UtcNow); |
| | | 92 | | |
| | | 93 | | // Process non-duplicates first, then duplicates |
| | 328 | 94 | | foreach (var row in nonDuplicates.Concat(duplicates)) |
| | | 95 | | { |
| | 108 | 96 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 108 | 97 | | recon.RecordRead(); |
| | | 98 | | |
| | 4104 | 99 | | foreach (var def in SourceFieldCatalog.ByFile["appendix-rows.csv"]) |
| | | 100 | | { |
| | 1944 | 101 | | var val = TrackerImportTextSanitizer.SanitizePlainText(row[def.Field])?.Trim(); |
| | 1944 | 102 | | context.Summary.RecordFieldRead("appendix-rows.csv", def.Field, !string.IsNullOrWhiteSpace(val)); |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | try |
| | | 106 | | { |
| | 250 | 107 | | if (!row.Fields.Any(f => !string.IsNullOrWhiteSpace(f))) |
| | | 108 | | { |
| | 2 | 109 | | recon.RecordSkipped("empty_row"); |
| | 2 | 110 | | continue; |
| | | 111 | | } |
| | | 112 | | |
| | 106 | 113 | | var companyName = TrackerImportTextSanitizer.SanitizePlainText(row["company"])?.Trim(); |
| | 106 | 114 | | var title = TrackerImportTextSanitizer.SanitizePlainText(row["title"])?.Trim(); |
| | | 115 | | |
| | 106 | 116 | | if (string.IsNullOrWhiteSpace(companyName) || string.IsNullOrWhiteSpace(title)) |
| | | 117 | | { |
| | 2 | 118 | | context.Summary.Errors.Add(new TrackerImportRowError( |
| | 2 | 119 | | "appendix-rows.csv", |
| | 2 | 120 | | row.LineNumber, |
| | 2 | 121 | | ErrorCode.ValidationError.ToString(), |
| | 2 | 122 | | "Missing required company or title.")); |
| | 2 | 123 | | recon.RecordErrored(); |
| | 2 | 124 | | continue; |
| | | 125 | | } |
| | | 126 | | |
| | 104 | 127 | | var decision = TrackerImportTextSanitizer.SanitizePlainText(row["decision"])?.Trim(); |
| | 104 | 128 | | if (string.Equals(decision, "skip", StringComparison.OrdinalIgnoreCase)) |
| | | 129 | | { |
| | 2 | 130 | | context.Summary.PostingsUnchanged++; |
| | 2 | 131 | | recon.RecordSkipped("decision_skip"); |
| | 2 | 132 | | continue; |
| | | 133 | | } |
| | | 134 | | |
| | 102 | 135 | | if (!string.IsNullOrWhiteSpace(decision)) |
| | | 136 | | { |
| | 0 | 137 | | context.Summary.Errors.Add(new TrackerImportRowError( |
| | 0 | 138 | | "appendix-rows.csv", |
| | 0 | 139 | | row.LineNumber, |
| | 0 | 140 | | ErrorCode.ValidationError.ToString(), |
| | 0 | 141 | | $"Invalid decision value '{decision}'. Expected blank or 'skip'.")); |
| | 0 | 142 | | recon.RecordErrored(); |
| | 0 | 143 | | continue; |
| | | 144 | | } |
| | | 145 | | |
| | 102 | 146 | | var canonicalCompany = context.CompanyAliases.TryGetValue(companyName, out var alias) ? alias : companyN |
| | | 147 | | |
| | | 148 | | // Enforce domain limits |
| | 102 | 149 | | canonicalCompany = TrackerImportFieldLimiter.LimitCompanyName(canonicalCompany, context.Summary)!; |
| | 102 | 150 | | title = TrackerImportFieldLimiter.LimitTitle(title, context.Summary)!; |
| | | 151 | | |
| | | 152 | | // 2. Parse date |
| | 102 | 153 | | var addedRaw = TrackerImportTextSanitizer.SanitizePlainText(row["added_md"])?.Trim(); |
| | 102 | 154 | | var dateOutcome = DateInferenceRule.Resolve(addedRaw, defaultDate); |
| | 100 | 155 | | if (dateOutcome.Kind == DateResolutionKind.DefaultedBlank) context.Summary.DatesDefaulted++; |
| | 144 | 156 | | if (dateOutcome.Kind is DateResolutionKind.InferredRollback or DateResolutionKind.InferredSameYear) cont |
| | 106 | 157 | | if (dateOutcome.IsFlagged) context.Summary.DatesFlagged++; |
| | | 158 | | |
| | 100 | 159 | | var effectiveDate = dateOutcome.ResolvedDate.ToDateTime(TimeOnly.MinValue, DateTimeKind.Utc); |
| | | 160 | | |
| | 100 | 161 | | var autoCreatedCompany = false; |
| | 100 | 162 | | Company? newCompany = null; |
| | 100 | 163 | | Posting? updatedPosting = null; |
| | 100 | 164 | | Posting? createdPosting = null; |
| | 100 | 165 | | var isUnchanged = false; |
| | | 166 | | |
| | 100 | 167 | | var rawReqId = (TrackerImportTextSanitizer.SanitizePlainText(row["req"]) ?? TrackerImportTextSanitizer.S |
| | 100 | 168 | | var reqId = TrackerImportFieldLimiter.LimitRequisitionId(rawReqId, context.Summary); |
| | 100 | 169 | | var rawReasonPrimary = TrackerImportTextSanitizer.SanitizeFreeText(row["reason_primary"])?.Trim(); |
| | 100 | 170 | | var reasonPrimary = TrackerImportFieldLimiter.LimitDispositionReason(rawReasonPrimary, context.Summary); |
| | 100 | 171 | | var reasonAll = TrackerImportTextSanitizer.SanitizeFreeText(row["reason_all"])?.Trim(); |
| | 100 | 172 | | var rawProposedDisp = (TrackerImportTextSanitizer.SanitizePlainText(row["proposed_disposition"]) ?? Trac |
| | | 173 | | |
| | 100 | 174 | | var rawComp = TrackerImportTextSanitizer.SanitizePlainText(row["comp_detected"])?.Trim(); |
| | 100 | 175 | | var rawCompEstimate = TrackerImportTextSanitizer.SanitizePlainText(row["comp_is_estimate"])?.Trim(); |
| | 100 | 176 | | Compensation? compensation = null; |
| | 100 | 177 | | if (!string.IsNullOrWhiteSpace(rawComp)) |
| | | 178 | | { |
| | 6 | 179 | | if (string.Equals(rawCompEstimate, "yes", StringComparison.OrdinalIgnoreCase) || string.Equals(rawCo |
| | | 180 | | { |
| | 4 | 181 | | if (!rawComp.Contains("estimate", StringComparison.OrdinalIgnoreCase)) |
| | | 182 | | { |
| | 4 | 183 | | rawComp = $"{rawComp} (estimate)"; |
| | | 184 | | } |
| | | 185 | | } |
| | 6 | 186 | | var compText = TrackerImportFieldLimiter.LimitCompensationText(rawComp, context.Summary); |
| | 6 | 187 | | var (compMin, compMax) = PostingsJsonStep.TryParseCompensationRange(compText); |
| | 6 | 188 | | compensation = compText != null ? new Compensation(compMin, compMax, "USD", compText) : null; |
| | | 189 | | } |
| | | 190 | | |
| | 100 | 191 | | var remoteMentioned = TrackerImportTextSanitizer.SanitizePlainText(row["remote_mentioned"])?.Trim(); |
| | 100 | 192 | | var isRemoteMentioned = string.Equals(remoteMentioned, "yes", StringComparison.OrdinalIgnoreCase) || str |
| | 100 | 193 | | if (isRemoteMentioned || (!string.IsNullOrWhiteSpace(remoteMentioned) && !string.Equals(remoteMentioned, |
| | | 194 | | { |
| | 96 | 195 | | context.Summary.RecordUnmappedValue("remote_mentioned", "not stored"); |
| | | 196 | | } |
| | 100 | 197 | | var remoteType = RemoteType.Unknown; |
| | 100 | 198 | | string? cultureSummary = null; |
| | | 199 | | |
| | 100 | 200 | | var isDuplicateRow = string.Equals(rawProposedDisp, "duplicate", StringComparison.OrdinalIgnoreCase); |
| | | 201 | | |
| | | 202 | | PostingDisposition targetDisposition; |
| | 100 | 203 | | var normalizedDisp = rawProposedDisp?.ToLowerInvariant().Replace("-", "_").Replace(" ", "_"); |
| | | 204 | | |
| | 100 | 205 | | if (isDuplicateRow) |
| | | 206 | | { |
| | 14 | 207 | | targetDisposition = PostingDisposition.ScreenedOut; |
| | | 208 | | } |
| | 86 | 209 | | else if (normalizedDisp is "screened_out" or "screenedout") |
| | | 210 | | { |
| | 58 | 211 | | targetDisposition = PostingDisposition.ScreenedOut; |
| | | 212 | | } |
| | 28 | 213 | | else if (normalizedDisp is "closed") |
| | | 214 | | { |
| | 10 | 215 | | targetDisposition = PostingDisposition.Closed; |
| | | 216 | | } |
| | 18 | 217 | | else if (normalizedDisp is "unscored") |
| | | 218 | | { |
| | 8 | 219 | | targetDisposition = PostingDisposition.Unscored; |
| | | 220 | | } |
| | 10 | 221 | | else if (normalizedDisp is "dismissed") |
| | | 222 | | { |
| | 8 | 223 | | targetDisposition = PostingDisposition.ScreenedOut; |
| | | 224 | | } |
| | 2 | 225 | | else if (string.IsNullOrWhiteSpace(normalizedDisp)) |
| | | 226 | | { |
| | 0 | 227 | | targetDisposition = PostingDisposition.ScreenedOut; |
| | | 228 | | } |
| | | 229 | | else |
| | | 230 | | { |
| | 2 | 231 | | context.Summary.Errors.Add(new TrackerImportRowError( |
| | 2 | 232 | | "appendix-rows.csv", |
| | 2 | 233 | | row.LineNumber, |
| | 2 | 234 | | ErrorCode.ValidationError.ToString(), |
| | 2 | 235 | | $"Invalid proposed_disposition '{rawProposedDisp}'.")); |
| | 2 | 236 | | recon.RecordErrored(); |
| | 2 | 237 | | continue; |
| | | 238 | | } |
| | | 239 | | |
| | 98 | 240 | | var rawReasonText = TrackerImportTextSanitizer.SanitizeFreeText(row["reason_text"])?.Trim(); |
| | 98 | 241 | | var rawDetails = TrackerImportTextSanitizer.SanitizeFreeText(row["details_raw"])?.Trim(); |
| | | 242 | | |
| | 98 | 243 | | var baseReason = !string.IsNullOrWhiteSpace(rawReasonText) |
| | 98 | 244 | | ? rawReasonText |
| | 98 | 245 | | : rawReasonPrimary; |
| | | 246 | | |
| | 98 | 247 | | var combinedReason = baseReason ?? (isDuplicateRow ? "duplicate" : (normalizedDisp == "dismissed" ? "dis |
| | | 248 | | |
| | 98 | 249 | | if (!string.IsNullOrWhiteSpace(rawDetails)) |
| | | 250 | | { |
| | 98 | 251 | | combinedReason += " | details: " + rawDetails; |
| | | 252 | | } |
| | | 253 | | |
| | 98 | 254 | | if (!string.IsNullOrWhiteSpace(reasonAll)) |
| | | 255 | | { |
| | 98 | 256 | | var otherCodes = reasonAll |
| | 98 | 257 | | .Split(ReasonSeparators, StringSplitOptions.RemoveEmptyEntries) |
| | 102 | 258 | | .Select(s => s.Trim()) |
| | 102 | 259 | | .Where(s => !string.IsNullOrWhiteSpace(s) && !string.Equals(s, rawReasonPrimary, StringCompariso |
| | 98 | 260 | | .ToList(); |
| | | 261 | | |
| | 98 | 262 | | if (otherCodes.Count > 0) |
| | | 263 | | { |
| | 4 | 264 | | combinedReason += " | also: " + string.Join(", ", otherCodes); |
| | | 265 | | } |
| | | 266 | | } |
| | | 267 | | |
| | 98 | 268 | | var finalReason = TrackerImportFieldLimiter.LimitDispositionReason(combinedReason, context.Summary); |
| | | 269 | | |
| | 98 | 270 | | var closedDate = targetDisposition == PostingDisposition.Closed |
| | 98 | 271 | | ? dateOutcome.ResolvedDate.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture) |
| | 98 | 272 | | : null; |
| | | 273 | | |
| | 98 | 274 | | var rawSource = TrackerImportTextSanitizer.SanitizePlainText(row["source"])?.Trim(); |
| | 98 | 275 | | var sourceKind = TrackingEnumExtensions.TryParseSourceKind(rawSource, out var parsedSrc) ? parsedSrc : S |
| | | 276 | | |
| | 98 | 277 | | var rowResult = await _context.ExecuteInTransactionAsync(async (txCtx, ct) => |
| | 98 | 278 | | { |
| | 98 | 279 | | Company? company = null; |
| | 98 | 280 | | if (!context.CanonicalCompanies.TryGetValue(canonicalCompany, out company)) |
| | 98 | 281 | | { |
| | 16 | 282 | | var existing = await txCtx.Companies.GetByNormalizedNameAsync(context.OwnerId, canonicalCompany, |
| | 16 | 283 | | if (existing.IsSuccess && existing.Value is not null) |
| | 98 | 284 | | { |
| | 0 | 285 | | company = existing.Value; |
| | 98 | 286 | | } |
| | 98 | 287 | | else |
| | 98 | 288 | | { |
| | 16 | 289 | | company = Company.Create( |
| | 16 | 290 | | ownerId: context.OwnerId, |
| | 16 | 291 | | name: canonicalCompany, |
| | 16 | 292 | | status: CompanyStatus.Neutral, |
| | 16 | 293 | | description: string.Empty); |
| | 98 | 294 | | |
| | 16 | 295 | | var saveCompany = await txCtx.Companies.UpsertCompanyAsync(company, ct); |
| | 16 | 296 | | if (!saveCompany.IsSuccess) |
| | 98 | 297 | | { |
| | 0 | 298 | | return Result<bool>.Failure(saveCompany.Error ?? new ResultError(ErrorCode.ValidationErr |
| | 98 | 299 | | } |
| | 98 | 300 | | |
| | 16 | 301 | | var audit = AuditLogEntry.CreateForTracking( |
| | 16 | 302 | | ownerId: context.OwnerId, |
| | 16 | 303 | | actor: ImportActor, |
| | 16 | 304 | | operation: AuditOperation.ImportCompanyCreate, |
| | 16 | 305 | | entityType: "company", |
| | 16 | 306 | | entityId: company.Id, |
| | 16 | 307 | | resultingVersionToken: company.VersionToken, |
| | 16 | 308 | | occurredAtUtc: DateTime.UtcNow, |
| | 16 | 309 | | details: new AuditDetails( |
| | 16 | 310 | | ChangedFields: ["name", "status"], |
| | 16 | 311 | | After: new Dictionary<string, object?> |
| | 16 | 312 | | { |
| | 16 | 313 | | ["name"] = company.Name, |
| | 16 | 314 | | ["status"] = company.Status.ToString() |
| | 16 | 315 | | })); |
| | 16 | 316 | | await txCtx.AppendAuditEntryAsync(audit, ct); |
| | 16 | 317 | | autoCreatedCompany = true; |
| | 16 | 318 | | newCompany = company; |
| | 98 | 319 | | } |
| | 98 | 320 | | } |
| | 98 | 321 | | |
| | 98 | 322 | | // 3. Resolve or create criterion from reason_primary |
| | 98 | 323 | | Guid? criterionId = null; |
| | 98 | 324 | | var criterionReason = !string.IsNullOrWhiteSpace(rawReasonPrimary) |
| | 98 | 325 | | ? rawReasonPrimary |
| | 98 | 326 | | : (normalizedDisp == "dismissed" ? "dismissed" : (isDuplicateRow ? "duplicate" : null)); |
| | 98 | 327 | | |
| | 98 | 328 | | if (!string.IsNullOrWhiteSpace(criterionReason)) |
| | 98 | 329 | | { |
| | 98 | 330 | | var critResult = await _criteriaStep.GetOrCreateReasonCriterionAsync( |
| | 98 | 331 | | context.OwnerId, criterionReason, effectiveDate, context.Summary, ct); |
| | 98 | 332 | | if (critResult.IsSuccess && critResult.Value is not null) |
| | 98 | 333 | | { |
| | 98 | 334 | | criterionId = critResult.Value.Id; |
| | 98 | 335 | | } |
| | 98 | 336 | | } |
| | 98 | 337 | | |
| | 98 | 338 | | // Duplicate link resolution |
| | 98 | 339 | | Guid? duplicateOfId = null; |
| | 98 | 340 | | if (isDuplicateRow) |
| | 98 | 341 | | { |
| | 14 | 342 | | var lookupKey = $"{canonicalCompany}|{TitleNormalizer.Normalize(title)}"; |
| | 14 | 343 | | if (context.PostingsByCompanyAndTitle.TryGetValue(lookupKey, out var targetPosting)) |
| | 98 | 344 | | { |
| | 8 | 345 | | duplicateOfId = targetPosting.Id; |
| | 98 | 346 | | } |
| | 6 | 347 | | else if (company is not null) |
| | 98 | 348 | | { |
| | 6 | 349 | | var candRes = await txCtx.Postings.FindCandidatesByCompanyAndTitleAsync(context.OwnerId, com |
| | 6 | 350 | | if (candRes.IsSuccess && candRes.Value is { Count: > 0 } candidates) |
| | 98 | 351 | | { |
| | 2 | 352 | | duplicateOfId = candidates[0].Id; |
| | 98 | 353 | | } |
| | 98 | 354 | | } |
| | 98 | 355 | | } |
| | 98 | 356 | | |
| | 98 | 357 | | // Match against existing posting in context or repository |
| | 98 | 358 | | Posting? existingPosting = null; |
| | 98 | 359 | | if (!string.IsNullOrWhiteSpace(reqId)) |
| | 98 | 360 | | { |
| | 96 | 361 | | if (context.PostingsByCompanyAndReq.TryGetValue($"{canonicalCompany}|{reqId}", out var byReq)) |
| | 98 | 362 | | { |
| | 6 | 363 | | existingPosting = byReq; |
| | 98 | 364 | | } |
| | 90 | 365 | | else if (company is not null) |
| | 98 | 366 | | { |
| | 90 | 367 | | var dedupC = $"{company.Id:D}|{reqId.Trim()}"; |
| | 90 | 368 | | var byCRes = await txCtx.Postings.GetByDedupKeyCAsync(context.OwnerId, dedupC, ct); |
| | 90 | 369 | | if (byCRes.IsSuccess && byCRes.Value is not null) |
| | 98 | 370 | | { |
| | 12 | 371 | | existingPosting = byCRes.Value; |
| | 98 | 372 | | } |
| | 98 | 373 | | } |
| | 98 | 374 | | } |
| | 98 | 375 | | |
| | 98 | 376 | | // If not matched by req, match by company + title ONLY IF candidate has no requisitionId (or same r |
| | 98 | 377 | | if (existingPosting is null && !isDuplicateRow) |
| | 98 | 378 | | { |
| | 68 | 379 | | var titleKey = $"{canonicalCompany}|{TitleNormalizer.Normalize(title)}"; |
| | 68 | 380 | | if (context.PostingsByCompanyAndTitle.TryGetValue(titleKey, out var candByTitle)) |
| | 98 | 381 | | { |
| | 6 | 382 | | if (string.IsNullOrWhiteSpace(candByTitle.RequisitionId) || string.Equals(candByTitle.Requis |
| | 98 | 383 | | { |
| | 2 | 384 | | existingPosting = candByTitle; |
| | 98 | 385 | | } |
| | 98 | 386 | | } |
| | 62 | 387 | | else if (company is not null) |
| | 98 | 388 | | { |
| | 62 | 389 | | var candRes = await txCtx.Postings.FindCandidatesByCompanyAndTitleAsync(context.OwnerId, com |
| | 62 | 390 | | if (candRes.IsSuccess && candRes.Value is { Count: > 0 } candidates) |
| | 98 | 391 | | { |
| | 0 | 392 | | foreach (var c in candidates) |
| | 98 | 393 | | { |
| | 0 | 394 | | var byIdRes = await txCtx.Postings.GetByIdAsync(context.OwnerId, c.Id, ct); |
| | 0 | 395 | | if (byIdRes.IsSuccess && byIdRes.Value is not null) |
| | 98 | 396 | | { |
| | 0 | 397 | | if (string.IsNullOrWhiteSpace(byIdRes.Value.RequisitionId) || string.Equals(byId |
| | 98 | 398 | | { |
| | 0 | 399 | | existingPosting = byIdRes.Value; |
| | 0 | 400 | | break; |
| | 98 | 401 | | } |
| | 98 | 402 | | } |
| | 98 | 403 | | } |
| | 98 | 404 | | } |
| | 98 | 405 | | } |
| | 98 | 406 | | } |
| | 98 | 407 | | |
| | 98 | 408 | | // Resurface policy |
| | 98 | 409 | | var proposedResurface = TrackerImportTextSanitizer.SanitizePlainText(row["proposed_resurface"])?.Tri |
| | 98 | 410 | | ResurfacePolicy resurfacePolicy; |
| | 98 | 411 | | if (string.IsNullOrWhiteSpace(proposedResurface)) |
| | 98 | 412 | | { |
| | 18 | 413 | | resurfacePolicy = existingPosting?.ResurfacePolicy ?? ResurfacePolicy.Never; |
| | 98 | 414 | | } |
| | 98 | 415 | | else |
| | 98 | 416 | | { |
| | 80 | 417 | | var s = proposedResurface.Trim().ToLowerInvariant().Replace("-", "_").Replace(" ", "_"); |
| | 80 | 418 | | if (s is "if_reopened" or "ifreopened") |
| | 98 | 419 | | { |
| | 12 | 420 | | resurfacePolicy = ResurfacePolicy.IfReopened; |
| | 98 | 421 | | } |
| | 68 | 422 | | else if (s is "never") |
| | 98 | 423 | | { |
| | 68 | 424 | | resurfacePolicy = ResurfacePolicy.Never; |
| | 98 | 425 | | } |
| | 98 | 426 | | else |
| | 98 | 427 | | { |
| | 0 | 428 | | context.Summary.RecordUnmappedValue("proposed_resurface", proposedResurface); |
| | 0 | 429 | | resurfacePolicy = existingPosting?.ResurfacePolicy ?? ResurfacePolicy.Never; |
| | 98 | 430 | | } |
| | 98 | 431 | | } |
| | 98 | 432 | | |
| | 98 | 433 | | if (existingPosting is not null) |
| | 98 | 434 | | { |
| | 20 | 435 | | isUnchanged = true; |
| | 20 | 436 | | updatedPosting = existingPosting; |
| | 20 | 437 | | return Result<bool>.Success(true); |
| | 98 | 438 | | } |
| | 98 | 439 | | |
| | 98 | 440 | | // Create new posting directly with target disposition |
| | 78 | 441 | | var newPosting = Posting.Create( |
| | 78 | 442 | | ownerId: context.OwnerId, |
| | 78 | 443 | | companyId: company?.Id ?? Guid.NewGuid(), |
| | 78 | 444 | | title: title, |
| | 78 | 445 | | sourceKind: sourceKind, |
| | 78 | 446 | | originalUrl: null, |
| | 78 | 447 | | requisitionId: reqId, |
| | 78 | 448 | | remoteType: remoteType, |
| | 78 | 449 | | compensation: compensation, |
| | 78 | 450 | | cultureSummary: cultureSummary, |
| | 78 | 451 | | disposition: targetDisposition, |
| | 78 | 452 | | dispositionReason: finalReason, |
| | 78 | 453 | | dispositionCriterionId: criterionId, |
| | 78 | 454 | | resurfacePolicy: resurfacePolicy, |
| | 78 | 455 | | decidedAt: targetDisposition is PostingDisposition.ScreenedOut or PostingDisposition.Closed ? ef |
| | 78 | 456 | | duplicateOfId: duplicateOfId, |
| | 78 | 457 | | now: effectiveDate, |
| | 78 | 458 | | closedDate: closedDate); |
| | 98 | 459 | | |
| | 78 | 460 | | var saveNew = await txCtx.Postings.UpsertPostingAsync(newPosting, ct); |
| | 78 | 461 | | if (!saveNew.IsSuccess) |
| | 98 | 462 | | { |
| | 0 | 463 | | return Result<bool>.Failure(saveNew.Error ?? new ResultError(ErrorCode.ValidationError, "Failed |
| | 98 | 464 | | } |
| | 98 | 465 | | |
| | 78 | 466 | | var newChangedFields = new List<string> { "title", "company_id", "disposition" }; |
| | 78 | 467 | | var newAfterDetails = new Dictionary<string, object?> |
| | 78 | 468 | | { |
| | 78 | 469 | | ["title"] = newPosting.Title, |
| | 78 | 470 | | ["disposition"] = newPosting.Disposition.ToString() |
| | 78 | 471 | | }; |
| | 78 | 472 | | if (closedDate is not null) |
| | 98 | 473 | | { |
| | 8 | 474 | | newChangedFields.Add("closed_date"); |
| | 8 | 475 | | newAfterDetails["closed_date"] = closedDate; |
| | 98 | 476 | | } |
| | 98 | 477 | | |
| | 78 | 478 | | var auditNew = AuditLogEntry.CreateForTracking( |
| | 78 | 479 | | ownerId: context.OwnerId, |
| | 78 | 480 | | actor: ImportActor, |
| | 78 | 481 | | operation: AuditOperation.ImportPostingCreate, |
| | 78 | 482 | | entityType: "posting", |
| | 78 | 483 | | entityId: newPosting.Id, |
| | 78 | 484 | | resultingVersionToken: newPosting.VersionToken, |
| | 78 | 485 | | occurredAtUtc: DateTime.UtcNow, |
| | 78 | 486 | | details: new AuditDetails( |
| | 78 | 487 | | ChangedFields: newChangedFields, |
| | 78 | 488 | | After: newAfterDetails)); |
| | 78 | 489 | | await txCtx.AppendAuditEntryAsync(auditNew, ct); |
| | 78 | 490 | | createdPosting = newPosting; |
| | 78 | 491 | | return Result<bool>.Success(true); |
| | 196 | 492 | | }, cancellationToken); |
| | | 493 | | |
| | 98 | 494 | | if (!rowResult.IsSuccess) |
| | | 495 | | { |
| | 0 | 496 | | context.Summary.Errors.Add(new TrackerImportRowError( |
| | 0 | 497 | | "appendix-rows.csv", |
| | 0 | 498 | | row.LineNumber, |
| | 0 | 499 | | rowResult.Error?.Code.ToString() ?? ErrorCode.ValidationError.ToString(), |
| | 0 | 500 | | rowResult.Error?.Message ?? "Failed to save appendix row")); |
| | 0 | 501 | | recon.RecordErrored(); |
| | 0 | 502 | | continue; |
| | | 503 | | } |
| | | 504 | | |
| | 98 | 505 | | if (autoCreatedCompany && newCompany is not null) |
| | | 506 | | { |
| | 16 | 507 | | context.CanonicalCompanies[canonicalCompany] = newCompany; |
| | 16 | 508 | | context.Summary.CompaniesCreated++; |
| | 16 | 509 | | context.Summary.AutoCreatedCompanies.Add(canonicalCompany); |
| | | 510 | | } |
| | | 511 | | |
| | 98 | 512 | | var affectedPosting = updatedPosting ?? createdPosting; |
| | 98 | 513 | | if (affectedPosting is not null) |
| | | 514 | | { |
| | 98 | 515 | | context.IndexPosting(affectedPosting, canonicalCompany); |
| | | 516 | | } |
| | | 517 | | |
| | 98 | 518 | | if (isUnchanged && updatedPosting is not null) |
| | | 519 | | { |
| | 20 | 520 | | context.Summary.PostingsUnchanged++; |
| | 20 | 521 | | recon.RecordUnchanged("merged"); |
| | | 522 | | } |
| | 78 | 523 | | else if (updatedPosting is not null) |
| | | 524 | | { |
| | 0 | 525 | | context.Summary.PostingsUpdated++; |
| | 0 | 526 | | recon.RecordUpdated(); |
| | | 527 | | } |
| | 78 | 528 | | else if (createdPosting is not null) |
| | | 529 | | { |
| | 78 | 530 | | context.Summary.PostingsCreated++; |
| | 78 | 531 | | recon.RecordCreated(); |
| | | 532 | | } |
| | 98 | 533 | | } |
| | 2 | 534 | | catch (Exception ex) when (ex is not OperationCanceledException) |
| | | 535 | | { |
| | 2 | 536 | | context.Summary.Errors.Add(new TrackerImportRowError( |
| | 2 | 537 | | "appendix-rows.csv", |
| | 2 | 538 | | row.LineNumber, |
| | 2 | 539 | | ErrorCode.ValidationError.ToString(), |
| | 2 | 540 | | ex.Message)); |
| | 2 | 541 | | recon.RecordErrored(); |
| | 2 | 542 | | } |
| | 100 | 543 | | } |
| | | 544 | | |
| | 56 | 545 | | return Result<bool>.Success(true); |
| | 60 | 546 | | } |
| | | 547 | | } |