Fix cache bypassing temporal company matching

- Cache lookup now validates company existed at claimed start date
- If cached company was incorporated after employment date, search for alternatives
- Fixes matching wrong company when cached data points to newer company

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-20 21:17:11 +01:00
parent 67a98405af
commit 7bb68b2567

View File

@@ -51,12 +51,24 @@ public sealed class CompanyVerifierService : ICompanyVerifierService
_logger.LogDebug("Verifying company: {CompanyName}", companyName); _logger.LogDebug("Verifying company: {CompanyName}", companyName);
var flags = new List<CompanyVerificationFlag>(); var flags = new List<CompanyVerificationFlag>();
// Try to find a cached match first // Try to find a cached match first (but only if it existed at claimed start date)
var cachedMatch = await FindCachedMatchAsync(companyName); var cachedMatch = await FindCachedMatchAsync(companyName);
if (cachedMatch is not null) if (cachedMatch is not null)
{ {
_logger.LogDebug("Found cached company match for: {CompanyName}", companyName); // Check if cached company existed at the claimed start date
return CreateResultFromCache(cachedMatch, companyName, startDate, endDate, jobTitle, flags); var cacheValid = !startDate.HasValue ||
cachedMatch.IncorporationDate == null ||
cachedMatch.IncorporationDate <= startDate.Value;
if (cacheValid)
{
_logger.LogDebug("Found cached company match for: {CompanyName}", companyName);
return CreateResultFromCache(cachedMatch, companyName, startDate, endDate, jobTitle, flags);
}
else
{
_logger.LogDebug("Cached company {CachedName} was incorporated after claimed start date, searching for alternatives", cachedMatch.CompanyName);
}
} }
// Search Companies House // Search Companies House