Skip to content

Commit

Permalink
feat(cb2-14233): fix insert handler
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mullen committed Oct 24, 2024
1 parent f053a09 commit de6f011
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 30 deletions.
20 changes: 6 additions & 14 deletions src/insert.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<SQSBatchResponse> => {
const handler = async (event: SQSEvent): Promise<SQSBatchResponse> => {
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 };
20 changes: 4 additions & 16 deletions tests/unit/insert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

0 comments on commit de6f011

Please sign in to comment.