diff --git a/serverless/lib/CaseSearchProcessor.ts b/serverless/lib/CaseSearchProcessor.ts index 64e6c9d..c628617 100644 --- a/serverless/lib/CaseSearchProcessor.ts +++ b/serverless/lib/CaseSearchProcessor.ts @@ -1,4 +1,4 @@ -import { CaseSearchRequest, CaseSearchResponse, SearchResult, FetchStatus } from '../../shared/types'; +import { CaseSearchRequest, CaseSearchResponse, SearchResult, FetchStatus, ZipCase } from '../../shared/types'; import QueueClient from './QueueClient'; import SearchParser from './SearchParser'; import StorageClient from './StorageClient'; @@ -117,22 +117,24 @@ export async function processCaseSearchRequest(req: CaseSearchRequest): Promise< case 'processing': // We requeue 'queued' and 'processing' because they might be stuck. // When they get picked up from the queue, we'll see whether they became 'complete' in the mean time and exit early. + const zipCase = results[caseNumber].zipCase; + + zipCase.fetchStatus = { status: 'queued' }; + + await StorageClient.saveCase(zipCase); + casesToQueue.push(caseNumber); } } else { // Case doesn't exist yet - create it with queued status and add to queue - results[caseNumber] = { - zipCase: { - caseNumber, - fetchStatus: { status: 'queued' }, - }, - }; - - // Save the new case to storage - await StorageClient.saveCase({ + const zipCase: ZipCase = { caseNumber, fetchStatus: { status: 'queued' }, - }); + }; + + results[caseNumber] = { zipCase: zipCase }; + + await StorageClient.saveCase(zipCase); casesToQueue.push(caseNumber); } diff --git a/serverless/lib/__tests__/CaseSearchProcessor.test.ts b/serverless/lib/__tests__/CaseSearchProcessor.test.ts index ca86bf0..585a057 100644 --- a/serverless/lib/__tests__/CaseSearchProcessor.test.ts +++ b/serverless/lib/__tests__/CaseSearchProcessor.test.ts @@ -221,7 +221,13 @@ describe('CaseSearchProcessor', () => { // Should queue for search (processing cases get re-queued in case they're stuck) expect(mockQueueClient.queueCasesForSearch).toHaveBeenCalledWith(['22CR123456-789'], 'test-user-id', 'Test Agent'); expect(mockQueueClient.queueCaseForDataRetrieval).not.toHaveBeenCalled(); - expect(mockStorageClient.saveCase).not.toHaveBeenCalled(); + // Status should be saved to DynamoDB as 'queued' + expect(mockStorageClient.saveCase).toHaveBeenCalledWith({ + caseNumber: '22CR123456-789', + fetchStatus: { status: 'queued' }, + caseId: 'test-case-id', + lastUpdated: expect.any(String), + }); }); it('should handle cases with notFound status (should queue for search retry)', async () => { @@ -244,7 +250,15 @@ describe('CaseSearchProcessor', () => { // Should queue for search retry, in case the record is not actually in-queue expect(mockQueueClient.queueCaseForDataRetrieval).not.toHaveBeenCalled(); expect(mockQueueClient.queueCasesForSearch).toHaveBeenCalledWith(['22CR123456-789'], 'test-user-id', 'Test Agent'); - expect(mockStorageClient.saveCase).not.toHaveBeenCalled(); + // Status should be saved to DynamoDB as 'queued' + expect(mockStorageClient.saveCase).toHaveBeenCalledWith({ + caseNumber: '22CR123456-789', + fetchStatus: { status: 'queued' }, + caseId: undefined, + lastUpdated: expect.any(String), + }); + // Status should be updated to 'queued' in the response for the UI + expect(result.results['22CR123456-789'].zipCase.fetchStatus.status).toBe('queued'); }); it('should handle cases with failed status (should queue for search)', async () => { @@ -267,7 +281,15 @@ describe('CaseSearchProcessor', () => { // Should queue for search (failed status gets re-queued) expect(mockQueueClient.queueCasesForSearch).toHaveBeenCalledWith(['22CR123456-789'], 'test-user-id', 'Test Agent'); expect(mockQueueClient.queueCaseForDataRetrieval).not.toHaveBeenCalled(); - expect(mockStorageClient.saveCase).not.toHaveBeenCalled(); + // Status should be saved to DynamoDB as 'queued' + expect(mockStorageClient.saveCase).toHaveBeenCalledWith({ + caseNumber: '22CR123456-789', + fetchStatus: { status: 'queued' }, + caseId: undefined, + lastUpdated: expect.any(String), + }); + // Status should be updated to 'queued' in the response for the UI + expect(result.results['22CR123456-789'].zipCase.fetchStatus.status).toBe('queued'); }); it('should handle new cases (not in storage)', async () => {