diff --git a/src/insert.ts b/src/insert.ts index 6e48036..99eb082 100644 --- a/src/insert.ts +++ b/src/insert.ts @@ -1,6 +1,6 @@ import 'source-map-support/register'; import { - Context, Callback, SQSEvent, SQSBatchResponse, + SQSEvent, SQSBatchResponse, } from 'aws-lambda'; import logger from './observability/logger'; import { eventHandler } from './eventHandler'; @@ -13,21 +13,13 @@ logger.debug( `\nRunning Service:\n '${SERVICE}'\n mode: ${NODE_ENV}\n stage: '${AWS_STAGE}'\n region: '${AWS_REGION}'\n\n`, ); -const handler = async (event: SQSEvent, _context: Context, callback: Callback): Promise => { +const handler = async (event: SQSEvent): Promise => { let batchItemFailures: SQSBatchResponse = { batchItemFailures: [] }; - try { - logger.debug(`Function triggered with '${JSON.stringify(event)}'.`); - batchItemFailures = await eventHandler(event); + logger.debug(`Function triggered with '${JSON.stringify(event)}'.`); + batchItemFailures = await eventHandler(event); - logger.info('Data processed successfully.'); - callback(null, 'Data processed successfully.'); - return batchItemFailures; - } catch (error) { - logger.info('Data processed unsuccessfully.'); - logger.error('', error); - callback(new Error('Data processed unsuccessfully.')); - return batchItemFailures; - } + logger.info(`Data processed successfully with ${batchItemFailures.batchItemFailures.length} failures`); + return batchItemFailures; }; export { handler }; diff --git a/tests/unit/insert.test.ts b/tests/unit/insert.test.ts index ba0f841..40715f2 100644 --- a/tests/unit/insert.test.ts +++ b/tests/unit/insert.test.ts @@ -39,24 +39,12 @@ describe('Application entry', () => { }); describe('Handler', () => { - it('GIVEN an event WHEN the eventHandler resolves THEN a callback result is returned', async () => { - mocked(eventHandler).mockReturnValue(Promise.resolve({ batchItemFailures: [] } as SQSBatchResponse)); - await handler(mockEvent, null, (error: string | Error, result: string) => { - expect(error).toBeNull(); - expect(result).toBe('Data processed successfully.'); - }); - expect(eventHandler).toHaveBeenCalled(); - expect(eventHandler).toHaveBeenCalledWith(mockEvent); - }); - - it('GIVEN an event WHEN the eventHandler throws an error THEN a call back error is returned and', async () => { - mocked(eventHandler).mockReturnValue(Promise.reject()); - await handler(mockEvent, null, (error: string | Error, result: string) => { - expect(error).toEqual(new Error('Data processed unsuccessfully.')); - expect(result).toBeUndefined(); - }); + it('GIVEN an event WHEN the eventHandler throws an error THEN return the sqsBatchResponse with failed records', async () => { + mocked(eventHandler).mockReturnValue(Promise.resolve({ batchItemFailures: [{ itemIdentifier: 'test' }] } as SQSBatchResponse)); + const sqsBatchResponse = await handler(mockEvent); expect(eventHandler).toHaveBeenCalled(); expect(eventHandler).toHaveBeenCalledWith(mockEvent); + expect(sqsBatchResponse.batchItemFailures.length).toBeGreaterThan(0); }); }); });