Skip to content

Commit

Permalink
feat(cb2-14236): update batch plate failure in handler for sync test …
Browse files Browse the repository at this point in the history
…result info (#162)
  • Loading branch information
cb-cs authored Nov 1, 2024
1 parent 0d6535e commit ec306fd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 41 deletions.
66 changes: 26 additions & 40 deletions src/handler/sync-test-result-info.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EUVehicleCategory } from '@dvsa/cvs-type-definitions/types/v3/tech-record/enums/euVehicleCategory.enum.js';
import { SQSBatchResponse, SQSEvent, SQSRecord } from 'aws-lambda';
import { SQSBatchResponse, SQSEvent } from 'aws-lambda';
import 'dotenv/config';
import { TestResult, TestType } from '../models/testResult';
import { TestResult } from '../models/testResult';
import { processRecord } from '../processors/processSQSRecord';
import { syncTestResultInfo } from '../processors/processSyncTestResultInfo';
import logger from '../util/logger';
Expand All @@ -13,49 +13,35 @@ export const handler = async (event: SQSEvent): Promise<SQSBatchResponse> => {
batchItemFailures: [],
};

const promisesArray: Promise<object | undefined>[] = [];
try {
event.Records.forEach((record: SQSRecord) => {
// eslint-disable-next-line no-restricted-syntax
for (const record of event.Records) {
try {
logger.debug('payload received from queue:', record);
const test = processRecord(record) as TestResult;
logger.debug('processed record:', test ?? 'no test');

let promiseUpdateStatus: Promise<object | undefined> | undefined;

if (test) {
test.testTypes.forEach((testType: TestType) => {
if (promiseUpdateStatus === undefined) {
promiseUpdateStatus = syncTestResultInfo(
test.systemNumber,
test.testStatus,
testType.testResult ?? '',
testType.testTypeId,
test.createdById,
test.createdByName,
test.euVehicleCategory as EUVehicleCategory || undefined,
);
promisesArray.push(promiseUpdateStatus);
}
});
}
});

const results = await Promise.allSettled(promisesArray);

results.forEach((r, i) => {
if (r.status === 'rejected' || !r.value) {
response.batchItemFailures.push({
// eslint-disable-next-line security/detect-object-injection
itemIdentifier: event.Records[i].messageId,
});
// eslint-disable-next-line no-restricted-syntax
for (const testType of test.testTypes) {
// eslint-disable-next-line no-await-in-loop
await syncTestResultInfo(
test.systemNumber,
test.testStatus,
testType.testResult ?? '',
testType.testTypeId,
test.createdById,
test.createdByName,
test.euVehicleCategory as EUVehicleCategory || undefined,
);
}
}
});

logger.info('resolved promises:', results);
return response;
} catch (error) {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
logger.error(`an error occurred in the promises ${error}`);
throw error;
} catch (error) {
logger.error(`an error occurred while processing record ${record.messageId}: ${error instanceof Error
? error.message
: JSON.stringify(error)}`);
response.batchItemFailures.push({ itemIdentifier: record.messageId });
}
}

return response;
};
2 changes: 1 addition & 1 deletion tests/unit/handler/sync-test-result-info.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('syncTestResultInfo handler', () => {
jest.resetModules();
});
describe('Error handling', () => {
it('should now throw error if promise is rejected, but report on that failure', async () => {
it('should now throw error if problem with syncTestResulInfo method', async () => {
mockProcessRecord.mockReturnValue(parsedRecord);
mockSyncTestResultInfo.mockImplementation(() => Promise.reject(new Error('test error')));
const failures = (await handler(queueEvent)).batchItemFailures;
Expand Down

0 comments on commit ec306fd

Please sign in to comment.