| | | 1 | | using System.Net; |
| | | 2 | | using RoleRadar.Application.Abstractions; |
| | | 3 | | using RoleRadar.Domain.Common; |
| | | 4 | | |
| | | 5 | | namespace RoleRadar.Application.Tracking.Import.Pipeline; |
| | | 6 | | |
| | | 7 | | public sealed class CompanyAliasesStep : ITrackerImportStep |
| | | 8 | | { |
| | | 9 | | private readonly ICsvParser _csvParser; |
| | | 10 | | |
| | 46 | 11 | | public CompanyAliasesStep(ICsvParser csvParser) |
| | | 12 | | { |
| | 46 | 13 | | _csvParser = csvParser ?? throw new ArgumentNullException(nameof(csvParser)); |
| | 46 | 14 | | } |
| | | 15 | | |
| | | 16 | | public Task<Result<bool>> ExecuteAsync(TrackerImportContext context, CancellationToken cancellationToken = default) |
| | | 17 | | { |
| | 46 | 18 | | ArgumentNullException.ThrowIfNull(context); |
| | | 19 | | |
| | 46 | 20 | | var aliasesPath = Path.Combine(context.BundleDirectory, "company-aliases.csv"); |
| | 46 | 21 | | var recon = context.Summary.GetOrCreateFileReconciliation("company-aliases.csv"); |
| | | 22 | | |
| | 46 | 23 | | if (!File.Exists(aliasesPath)) |
| | | 24 | | { |
| | 4 | 25 | | recon.IsAbsent = true; |
| | 4 | 26 | | return Task.FromResult(Result<bool>.Success(true)); |
| | | 27 | | } |
| | | 28 | | |
| | 42 | 29 | | recon.IsAbsent = false; |
| | | 30 | | |
| | 42 | 31 | | var content = File.ReadAllText(aliasesPath); |
| | 42 | 32 | | if (string.IsNullOrWhiteSpace(content)) |
| | | 33 | | { |
| | 2 | 34 | | recon.RecordErrored(); |
| | 2 | 35 | | context.Summary.Errors.Add(new TrackerImportRowError( |
| | 2 | 36 | | "company-aliases.csv", |
| | 2 | 37 | | 1, |
| | 2 | 38 | | ErrorCode.ValidationError.ToString(), |
| | 2 | 39 | | "File is present but empty.")); |
| | 2 | 40 | | return Task.FromResult(Result<bool>.Failure(ErrorCode.ValidationError, "company-aliases.csv is present but e |
| | | 41 | | } |
| | | 42 | | |
| | 40 | 43 | | using var headerReader = new StringReader(content); |
| | 40 | 44 | | var headers = _csvParser.ReadHeader(headerReader); |
| | 40 | 45 | | var headerValidation = CsvHeaderValidator.Validate("company-aliases.csv", headers, CsvHeaderValidator.CompanyAli |
| | 40 | 46 | | if (!headerValidation.IsSuccess) |
| | | 47 | | { |
| | 0 | 48 | | recon.RecordErrored(); |
| | 0 | 49 | | context.Summary.Errors.Add(new TrackerImportRowError( |
| | 0 | 50 | | "company-aliases.csv", |
| | 0 | 51 | | 1, |
| | 0 | 52 | | ErrorCode.ValidationError.ToString(), |
| | 0 | 53 | | headerValidation.Error!.Value.Message)); |
| | 0 | 54 | | return Task.FromResult(Result<bool>.Failure(headerValidation.Error!.Value)); |
| | | 55 | | } |
| | | 56 | | |
| | 40 | 57 | | using var reader = new StringReader(content); |
| | 148 | 58 | | foreach (var row in _csvParser.Read(reader, hasHeader: true)) |
| | | 59 | | { |
| | 34 | 60 | | recon.RecordRead(); |
| | | 61 | | |
| | 68 | 62 | | if (!row.Fields.Any(f => !string.IsNullOrWhiteSpace(f))) |
| | | 63 | | { |
| | 0 | 64 | | recon.RecordSkipped("empty_row"); |
| | 0 | 65 | | continue; |
| | | 66 | | } |
| | | 67 | | |
| | 34 | 68 | | context.Summary.RecordFieldRead("company-aliases.csv", "alias", !string.IsNullOrWhiteSpace(row["alias"])); |
| | 34 | 69 | | context.Summary.RecordFieldRead("company-aliases.csv", "canonical", !string.IsNullOrWhiteSpace(row["canonica |
| | | 70 | | |
| | 34 | 71 | | var alias = TrackerImportTextSanitizer.SanitizePlainText(row["alias"])?.Trim(); |
| | 34 | 72 | | var canonical = TrackerImportTextSanitizer.SanitizePlainText(row["canonical"])?.Trim(); |
| | | 73 | | |
| | 34 | 74 | | if (!string.IsNullOrWhiteSpace(alias) && !string.IsNullOrWhiteSpace(canonical)) |
| | | 75 | | { |
| | 34 | 76 | | context.CompanyAliases[alias] = canonical; |
| | 34 | 77 | | recon.RecordCreated(); |
| | | 78 | | } |
| | | 79 | | else |
| | | 80 | | { |
| | 0 | 81 | | recon.RecordSkipped("missing_required_fields"); |
| | | 82 | | } |
| | | 83 | | } |
| | | 84 | | |
| | 40 | 85 | | return Task.FromResult(Result<bool>.Success(true)); |
| | 40 | 86 | | } |
| | | 87 | | } |