diff --git a/serverless/lib/CaseProcessor.ts b/serverless/lib/CaseProcessor.ts index c16c556..96f1da6 100644 --- a/serverless/lib/CaseProcessor.ts +++ b/serverless/lib/CaseProcessor.ts @@ -10,6 +10,10 @@ import axios from 'axios'; import { wrapper } from 'axios-cookiejar-support'; import * as cheerio from 'cheerio'; +// Version date used to determine whether a cached 'complete' CaseSummary is +// up-to-date or should be re-fetched to align with current schema/logic. +export const CASE_SUMMARY_VERSION_DATE = new Date('2025-10-06T00:00:00Z'); + // Type for raw portal JSON data - using `any` is acceptable here since we're dealing with // dynamic external API responses that we don't control // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -254,13 +258,23 @@ async function processCaseDataRecord( receiptHandle: string ): Promise { try { - // Check for existing data and skip if already complete + // Check for existing data and skip if already complete with current schema version. const zipCase = await StorageClient.getCase(caseNumber); if (zipCase && zipCase.fetchStatus.status === 'complete') { - // Always use the cached data for complete cases - await QueueClient.deleteMessage(receiptHandle, 'data'); - console.log(`Case ${caseNumber} already complete; using cached data and deleted queue item`); - return zipCase.fetchStatus; + const lastUpdated = zipCase.lastUpdated ? new Date(zipCase.lastUpdated) : new Date(0); + + if (lastUpdated.getTime() >= CASE_SUMMARY_VERSION_DATE.getTime()) { + // Cached summary is new enough for current version - use it + await QueueClient.deleteMessage(receiptHandle, 'data'); + console.log( + `Case ${caseNumber} already complete and up-to-date (lastUpdated=${zipCase.lastUpdated}); using cached data` + ); + return zipCase.fetchStatus; + } + + console.log( + `Case ${caseNumber} lastUpdated (${zipCase.lastUpdated}) is older than version date ${CASE_SUMMARY_VERSION_DATE.toISOString()}; re-fetching case summary` + ); } // Fetch case summary diff --git a/serverless/lib/CaseSearchProcessor.ts b/serverless/lib/CaseSearchProcessor.ts index 713f112..675bc35 100644 --- a/serverless/lib/CaseSearchProcessor.ts +++ b/serverless/lib/CaseSearchProcessor.ts @@ -9,6 +9,7 @@ import axios from 'axios'; import { wrapper } from 'axios-cookiejar-support'; import * as cheerio from 'cheerio'; import UserAgentClient from './UserAgentClient'; +import { CASE_SUMMARY_VERSION_DATE } from './CaseProcessor'; // Process API case search requests export async function processCaseSearchRequest(req: CaseSearchRequest): Promise { @@ -54,27 +55,30 @@ export async function processCaseSearchRequest(req: CaseSearchRequest): Promise< switch (status) { case 'complete': - if (caseSummary) { - // Truly complete - has both ID and summary - console.log(`Case ${caseNumber} is complete with summary, preserving`); + const lastUpdated = results[caseNumber].zipCase.lastUpdated; + if (caseSummary && lastUpdated && new Date(lastUpdated) >= CASE_SUMMARY_VERSION_DATE) { + // Truly complete - has both ID and an up-to-date summary + console.log(`Case ${caseNumber} is complete with up-to-date summary schema, preserving`); continue; } else if (caseId) { - // Has ID but missing summary - treat as 'found' and queue for data retrieval + // Has ID but missing summary or summary schema is outdated - treat as 'found' and queue for data retrieval console.log( - `Case ${caseNumber} has 'complete' status but missing summary, treating as 'found' and queueing for data retrieval` + `Case ${caseNumber} has 'complete' status but ${caseSummary ? 'summary is outdated' : 'missing summary'}; treating as 'found' and queueing for data retrieval` ); - // Update status to 'found' since we need to rebuild the summary + const nowString = new Date().toISOString(); + + // Update status to 'found', since we need to rebuild the summary await StorageClient.saveCase({ caseNumber, caseId, fetchStatus: { status: 'found' }, - lastUpdated: new Date().toISOString(), + lastUpdated: nowString, }); // Also update the results object that will be returned to frontend results[caseNumber].zipCase.fetchStatus = { status: 'found' }; - results[caseNumber].zipCase.lastUpdated = new Date().toISOString(); + results[caseNumber].zipCase.lastUpdated = nowString; try { await QueueClient.queueCaseForDataRetrieval(caseNumber, caseId, req.userId);