From 7bb68b25677cccbb19ae6523eee8bbc9e5a78f96 Mon Sep 17 00:00:00 2001 From: peter Date: Tue, 20 Jan 2026 21:17:11 +0100 Subject: [PATCH] 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 --- .../Services/CompanyVerifierService.cs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/TrueCV.Infrastructure/Services/CompanyVerifierService.cs b/src/TrueCV.Infrastructure/Services/CompanyVerifierService.cs index 782f05b..c72234a 100644 --- a/src/TrueCV.Infrastructure/Services/CompanyVerifierService.cs +++ b/src/TrueCV.Infrastructure/Services/CompanyVerifierService.cs @@ -51,12 +51,24 @@ public sealed class CompanyVerifierService : ICompanyVerifierService _logger.LogDebug("Verifying company: {CompanyName}", companyName); var flags = new List(); - // 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); if (cachedMatch is not null) { - _logger.LogDebug("Found cached company match for: {CompanyName}", companyName); - return CreateResultFromCache(cachedMatch, companyName, startDate, endDate, jobTitle, flags); + // Check if cached company existed at the claimed start date + 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