Skip to content

Commit

Permalink
chore(cb2-11283): upgrade to aws sdk v3 (#19)
Browse files Browse the repository at this point in the history
* chore(cb2-11283): upgrade to aws sdk v3

* chore(cb2-11283: fix tests

* chore(cb2-11283): added logging for debugging

* chore(cb2-11283): remove console logs

* chore(cb2-11283): readd one of the logs

* chore(cb2-11283): add logging for debugging again

* chore(cb2-11283): final remove logging
  • Loading branch information
luluharvey authored May 1, 2024
1 parent 13ef672 commit 5556fd6
Show file tree
Hide file tree
Showing 7 changed files with 3,530 additions and 76 deletions.
2,741 changes: 2,736 additions & 5 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@
"tools-setup": "echo 'nothing to do for now'"
},
"dependencies": {
"@aws-sdk/client-eventbridge": "3.556.0",
"@aws-sdk/util-dynamodb": "3.556.0",
"aws-lambda": "1.0.6",
"aws-sdk": "2.1354.0",
"dateformat": "4.6.3",
"joi": "^14.3.1",
"luxon": "3.2.1",
Expand All @@ -58,6 +59,7 @@
"@types/supertest": "2.0.10",
"@typescript-eslint/eslint-plugin": "5.3.1",
"@typescript-eslint/parser": "5.3.1",
"aws-sdk-client-mock": "4.0.0",
"commitlint-plugin-function-rules": "1.1.20",
"concurrently": "6.3.0",
"cross-env": "7.0.3",
Expand Down
24 changes: 17 additions & 7 deletions src/eventbridge/Send.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { EventBridge } from 'aws-sdk';
import { EventEntry } from './EventEntry';
import { Entries } from './Entries';
import { SendResponse } from './SendResponse';
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-call */
// eslint-disable-next-line import/no-extraneous-dependencies
import {
EventBridgeClient,
PutEventsCommand,
} from '@aws-sdk/client-eventbridge';
import logger from '../observability/Logger';
import { MCRequest } from '../utils/MCRequest';
import { Entries } from './Entries';
import { EventEntry } from './EventEntry';
import { SendResponse } from './SendResponse';

const eventbridge = new EventBridge();
const sendMCProhibition = async (mcRequests: MCRequest[]): Promise<SendResponse> => {
const eventBridge = new EventBridgeClient();
const sendMCProhibition = async (
mcRequests: MCRequest[],
): Promise<SendResponse> => {
const sendResponse: SendResponse = {
SuccessCount: 0,
FailCount: 0,
Expand All @@ -25,8 +34,9 @@ const sendMCProhibition = async (mcRequests: MCRequest[]): Promise<SendResponse>
Entries: [],
};
params.Entries.push(entry);
const command = new PutEventsCommand(params);
// eslint-disable-next-line no-await-in-loop
await eventbridge.putEvents(params).promise();
await eventBridge.send(command);
sendResponse.SuccessCount++;
}
} catch (error) {
Expand Down
49 changes: 34 additions & 15 deletions src/utils/ExtractTestResults.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,51 @@
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-unsafe-argument */

import { DynamoDBRecord } from 'aws-lambda';
import { DynamoDB } from 'aws-sdk';
import { unmarshall } from '@aws-sdk/util-dynamodb';
import { DateTime } from 'luxon';
import { TestResult } from './TestResult';
import { MCRequest } from './MCRequest';
import { PROHIB_CLEARANCE_TEST_TYPE_IDS } from '../assets/Enums';
import logger from '../observability/Logger';
import { ValidationUtil } from './ValidationUtil';
import { HTTPError } from './HTTPError';
import { PROHIB_CLEARANCE_TEST_TYPE_IDS } from '../assets/Enums';
import { MCRequest } from './MCRequest';
import { TestResult } from './TestResult';
import { ValidationUtil } from './ValidationUtil';

/**
* This is used to extract the relevant fields from the test record that is
* required to be sent to MC in order to clear prohibitions
* @param record
*/
export const extractMCTestResults = (record: DynamoDBRecord): MCRequest[] => {
const testResultUnmarshall = DynamoDB.Converter.unmarshall(record.dynamodb.NewImage);
logger.info(`Processing testResultId: ${JSON.stringify(testResultUnmarshall.testResultId)}`);
export const extractMCTestResults = (record: any): MCRequest[] => {

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / scanner

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 22 in src/utils/ExtractTestResults.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type
const testResultUnmarshall = unmarshall(record.dynamodb.NewImage as { any });
logger.info(
`Processing testResultId: ${JSON.stringify(
testResultUnmarshall.testResultId,
)}`,
);
const mcRequest: MCRequest[] = testResultUnmarshall.testTypes
.filter((testType) => PROHIB_CLEARANCE_TEST_TYPE_IDS.IDS.includes(testType.testTypeId))
.filter((testType) => (testType.testResult === ('pass') || testType.testResult === ('prs')))
.filter(() => (testResultUnmarshall.vehicleType === 'hgv') || testResultUnmarshall.vehicleType === 'psv' || testResultUnmarshall.vehicleType === 'trl')
.filter((testType) =>
PROHIB_CLEARANCE_TEST_TYPE_IDS.IDS.includes(testType.testTypeId),
)
.filter(
(testType) =>
testType.testResult === 'pass' || testType.testResult === 'prs',
)
.filter(
() =>
testResultUnmarshall.vehicleType === 'hgv' ||
testResultUnmarshall.vehicleType === 'psv' ||
testResultUnmarshall.vehicleType === 'trl',
)
.filter(() => testResultUnmarshall.testStatus === 'submitted')
.map((testResult: TestResult) => ({
vehicleIdentifier: testResultUnmarshall.vehicleType === 'trl' ? testResultUnmarshall.trailerId : testResultUnmarshall.vrm,
vehicleIdentifier:
testResultUnmarshall.vehicleType === 'trl'
? testResultUnmarshall.trailerId
: testResultUnmarshall.vrm,
testDate: isoDateFormatter(testResult.testTypeEndTimestamp),
vin: testResultUnmarshall.vin,
testResult: calculateTestResult(testResult),
Expand All @@ -50,10 +67,12 @@ export const extractMCTestResults = (record: DynamoDBRecord): MCRequest[] => {
* This method is used to change the test result to be a single, uppercase character
* @param testResult
*/
export const calculateTestResult = (testResult: TestResult): string => (testResult.testResult.toLowerCase() === 'pass' ? 'S' : 'R');
export const calculateTestResult = (testResult: TestResult): string =>
testResult.testResult.toLowerCase() === 'pass' ? 'S' : 'R';

/**
* This method is used to change the format of an iso string to be formatted as yyyy/MM/dd
* @param date
*/
export const isoDateFormatter = (date: string): string => DateTime.fromISO(date).toFormat('dd/MM/yyyy');
export const isoDateFormatter = (date: string): string =>
DateTime.fromISO(date).toFormat('dd/MM/yyyy');
14 changes: 9 additions & 5 deletions tests/unit/ExtractTestResults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@

import { DynamoDBRecord } from 'aws-lambda';
import { extractMCTestResults } from '../../src/utils/ExtractTestResults';
import dynamoEventWCert from './data/dynamoEventWithCert.json';
import { MCRequest } from '../../src/utils/MCRequest';
import dynamoEventCancelled from './data/dynamoEventCancelled.json';
import dynamoEventMultipleTests from './data/dynamoEventMultipleTestTypes.json';
import dynamoEventMultipleTestTypesPassAndPrs from './data/dynamoEventMultipleTestTypesPassAndPrs.json';
import dynamoEventCancelled from './data/dynamoEventCancelled.json';
import { MCRequest } from '../../src/utils/MCRequest';
import dynamoEventWCert from './data/dynamoEventWithCert.json';
import dynamoEventWithoutVin from './data/dynamoEventWithoutVin.json';

describe('extractTestResults', () => {
let DYNAMO_DATA: DynamoDBRecord;
let MC_RESULT: MCRequest[];

beforeEach(() => {
DYNAMO_DATA = null;
});

it('when a test result has a status of cancelled then expect no mc requests are to be created', () => {
DYNAMO_DATA = dynamoEventCancelled as DynamoDBRecord;
MC_RESULT = extractMCTestResults(DYNAMO_DATA);
Expand All @@ -33,8 +38,7 @@ describe('extractTestResults', () => {
expect(MC_RESULT).toHaveLength(1);
});
it('when processing a test result when there is a field missing a validation error is returned', () => {
DYNAMO_DATA = dynamoEventWCert as DynamoDBRecord;
DYNAMO_DATA.dynamodb.NewImage.vin = null;
DYNAMO_DATA = dynamoEventWithoutVin as DynamoDBRecord;

try {
extractMCTestResults(DYNAMO_DATA);
Expand Down
74 changes: 31 additions & 43 deletions tests/unit/Send.test.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,40 @@
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable no-underscore-dangle */
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { EventBridge, Request } from 'aws-sdk';
import { mocked } from 'ts-jest/utils';
import { PutEventsResponse, PutEventsRequest, PutEventsResultEntry } from 'aws-sdk/clients/eventbridge';

import {
EventBridgeClient,
PutEventsCommand,
PutEventsCommandInput,
PutEventsCommandOutput,
PutEventsResultEntry,
} from '@aws-sdk/client-eventbridge';
import { mockClient } from 'aws-sdk-client-mock';
import { sendMCProhibition } from '../../src/eventbridge/Send';
import { SendResponse } from '../../src/eventbridge/SendResponse';
import { MCRequest } from '../../src/utils/MCRequest';

jest.mock('aws-sdk', () => {
const mEventBridgeInstance = {
putEvents: jest.fn(),
};
const mRequestInstance = {
promise: jest.fn(),
};
const mEventBridge = jest.fn(() => mEventBridgeInstance);
const mRequest = jest.fn(() => mRequestInstance);

return {
EventBridge: mEventBridge,
Request: mRequest,
};
});
const mEventBridgeInstance = mockClient(EventBridgeClient);

type PutEventsWithParams = (params: PutEventsRequest) => AWS.Request<PutEventsResponse, AWS.AWSError>;

const mEventBridgeInstance = new EventBridge();
const mResultInstance = new Request<PutEventsResponse, AWS.AWSError>(null, null);
// eslint-disable-next-line @typescript-eslint/unbound-method
mocked(mEventBridgeInstance.putEvents as PutEventsWithParams)
.mockImplementation(
(params: PutEventsRequest): AWS.Request<PutEventsResponse, AWS.AWSError> => {
const mPutEventsResponse: PutEventsResponse = {
FailedEntryCount: 0,
Entries: Array<PutEventsResultEntry>(params.Entries.length),
};
if (params.Entries[0].Detail === '{ "testResult": "{\\"vehicleIdentifier\\":\\"test\\",\\"testDate\\":\\"\\",\\"vin\\":\\"\\",\\"testResult\\":\\"\\",\\"hgvPsvTrailFlag\\":\\"\\"}" }') {
mResultInstance.promise = jest.fn()
.mockReturnValue(Promise.reject(new Error('Oh no!')));
} else {
mResultInstance.promise = jest.fn()
.mockReturnValue(Promise.resolve(mPutEventsResponse));
}
return mResultInstance;
},
);
mEventBridgeInstance.on(PutEventsCommand).callsFake(
(params: PutEventsCommandInput): PutEventsCommandOutput => {
const mPutEventsResponse: PutEventsCommandOutput = {
FailedEntryCount: 0,
Entries: Array<PutEventsResultEntry>(params.Entries.length),
$metadata: undefined,
};
if (
params.Entries[0].Detail ===
'{ "testResult": "{\\"vehicleIdentifier\\":\\"test\\",\\"testDate\\":\\"\\",\\"vin\\":\\"\\",\\"testResult\\":\\"\\",\\"hgvPsvTrailFlag\\":\\"\\"}" }'
) {
throw new Error('Oh no!');
} else {
return mPutEventsResponse;
}
},
);

describe('Events sent', () => {
it('When one event with multiple test types are returned', async () => {
Expand All @@ -71,9 +61,7 @@ describe('Events sent', () => {
SuccessCount: 2,
FailCount: 0,
};
await expect(sendMCProhibition(mcRequests))
.resolves
.toEqual(mSendResponse);
await expect(sendMCProhibition(mcRequests)).resolves.toEqual(mSendResponse);
});

it('When the mc requests are null an error is returned', async () => {
Expand Down
Loading

0 comments on commit 5556fd6

Please sign in to comment.