Fix file upload stale reference and improve company matching

File upload fix:
- Buffer file data immediately on selection to prevent stale IBrowserFile references
- Add BufferedFile record to hold file data in memory
- Add loading indicator while files are being buffered
- Fixes "Cannot read properties of null (reading '_blazorFilesById')" error

Company matching improvement:
- Prefer companies that existed at the claimed employment start date
- Fixes matching wrong company when newer company has similar name
- Example: "UK MATTEL LTD" (2025) vs "MATTEL U.K. LIMITED" (1980)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-20 21:14:01 +01:00
parent 3c8ee0a529
commit 67a98405af
2 changed files with 88 additions and 27 deletions

View File

@@ -70,8 +70,8 @@ public sealed class CompanyVerifierService : ICompanyVerifierService
return CreateUnverifiedResult(companyName, startDate, endDate, jobTitle, "No matching company found in Companies House");
}
// Find best fuzzy match
var bestMatch = FindBestMatch(companyName, searchResponse.Items);
// Find best fuzzy match, preferring companies that existed at claimed start date
var bestMatch = FindBestMatch(companyName, searchResponse.Items, startDate);
if (bestMatch is null)
{
@@ -560,7 +560,8 @@ public sealed class CompanyVerifierService : ICompanyVerifierService
private static (CompaniesHouseSearchItem Item, int Score)? FindBestMatch(
string companyName,
List<CompaniesHouseSearchItem> items)
List<CompaniesHouseSearchItem> items,
DateOnly? claimedStartDate)
{
var normalizedSearch = companyName.ToUpperInvariant();
@@ -568,10 +569,32 @@ public sealed class CompanyVerifierService : ICompanyVerifierService
.Where(item => !string.IsNullOrWhiteSpace(item.Title))
.Select(item => (Item: item, Score: Fuzz.TokenSetRatio(normalizedSearch, item.Title.ToUpperInvariant())))
.Where(m => m.Score >= FuzzyMatchThreshold)
.OrderByDescending(m => m.Score)
.ToList();
return matches.Count > 0 ? matches[0] : null;
if (matches.Count == 0) return null;
// If we have a claimed start date, prefer companies that existed at that time
if (claimedStartDate.HasValue)
{
var existedAtStartDate = matches
.Where(m =>
{
var incDate = DateHelpers.ParseDate(m.Item.DateOfCreation);
// Company existed if it was incorporated before the claimed start date
return incDate == null || incDate <= claimedStartDate.Value;
})
.OrderByDescending(m => m.Score)
.ToList();
// If any matches existed at the start date, prefer those
if (existedAtStartDate.Count > 0)
{
return existedAtStartDate[0];
}
}
// Fall back to highest score if no temporal match
return matches.OrderByDescending(m => m.Score).First();
}
private async Task CacheCompanyAsync(CompaniesHouseSearchItem item, CompaniesHouseCompany? details)