Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cb2-11360): create new automated cherished transfer lambda #146

Merged
merged 17 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,995 changes: 1,565 additions & 430 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"polly-js": "^1.8.3"
},
"devDependencies": {
"@aws-sdk/client-s3": "^3.550.0",
"@aws-sdk/client-dynamodb": "^3.359.0",
"@aws-sdk/client-lambda": "^3.362.0",
"@aws-sdk/client-sns": "^3.485.0",
Expand Down
75 changes: 75 additions & 0 deletions src/handler/mot-update-vrm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* eslint-disable no-continue */
/* eslint-disable no-await-in-loop */
import { TechRecordType } from '@dvsa/cvs-type-definitions/types/v3/tech-record/tech-record-verb';
import { SQSEvent } from 'aws-lambda';
import { MotCherishedTransfer } from '../models/motCherishedTransfer';
import { SearchCriteria } from '../models/search';
import { SNSMessageBody } from '../models/updateVrm';
import { processCherishedTransfer } from '../processors/processCherishedTransfer';
import { searchByCriteria, updateVehicle } from '../services/database';
import { publish } from '../services/sns';
import { StatusCode } from '../util/enum';
import logger from '../util/logger';

export const handler = async (event: SQSEvent) => {
naathanbrown marked this conversation as resolved.
Show resolved Hide resolved
logger.info('mot-update-vrm lambda triggered');

const SYSTEM_USER = 'CVS Automated Cherished Transfer';

try {
const recordsToSend: SNSMessageBody[] = [];

// eslint-disable-next-line no-restricted-syntax
for (const cherishedTransfer of event.Records) {
const parsedRecord: MotCherishedTransfer = JSON.parse(cherishedTransfer.body) as MotCherishedTransfer;
const allRecords = await searchByCriteria(SearchCriteria.VIN, parsedRecord.vin);

if (!allRecords.length) {
logger.info(`No record found for VIN: ${parsedRecord.vin}`);
continue;
}

const allCurrentRecords = allRecords.filter((x) => x.techRecord_statusCode === StatusCode.CURRENT);

if (!allCurrentRecords.length) {
logger.info(`No current record found for VIN: ${parsedRecord.vin}`);
continue;
}

const matchingCurrentVrmRecords = allCurrentRecords.find((x) => x.primaryVrm === parsedRecord.vrm);

if (allCurrentRecords.length > 1) {
logger.info(`Duplicate current records found for VIN ${parsedRecord.vin}`);
} else if (matchingCurrentVrmRecords) {
logger.info(`No update needed for VRM ${parsedRecord.vrm} and VIN ${parsedRecord.vin}`);
} else {
const currentRecord = allCurrentRecords[0];
const { recordsToArchive, recordsToUpdate } = processCherishedTransfer(
{
msOid: SYSTEM_USER,
username: SYSTEM_USER,
email: '',
},
parsedRecord.vrm,
currentRecord as TechRecordType<'get'>,
);

await updateVehicle(recordsToArchive, recordsToUpdate);

logger.info(`Updated systemNumber ${currentRecord.systemNumber} with VRM ${parsedRecord.vrm}`);

recordsToUpdate.forEach((record) => recordsToSend.push({ ...record, userEmail: SYSTEM_USER }));
}
}

if (recordsToSend.length) {
await publish(JSON.stringify(recordsToSend), process.env.VRM_TRANSFERRED_ARN ?? '');
}

logger.info('All records processed in SQS event');
} catch (error) {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
logger.error(`an error occurred during processing mot update vrm ${error}`);
throw error;
}
};
4 changes: 4 additions & 0 deletions src/models/motCherishedTransfer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface MotCherishedTransfer {
vin: string;
vrm: string;
}
7 changes: 5 additions & 2 deletions src/services/sqs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { SQSClient, SendMessageCommand } from '@aws-sdk/client-sqs';
import {
SQSClient, SendMessageCommand,
} from '@aws-sdk/client-sqs';
import { SQSRequestBody } from '../models/sqsPayload';
import logger from '../util/logger';

Expand All @@ -14,7 +16,8 @@
};

try {
await sqsClient.send(new SendMessageCommand(params));
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
await sqsClient.send(new SendMessageCommand(params) as any);

Check warning on line 20 in src/services/sqs.ts

View workflow job for this annotation

GitHub Actions / tests

Unexpected any. Specify a different type
naathanbrown marked this conversation as resolved.
Show resolved Hide resolved
return undefined;
} catch (err: unknown) {
logger.error(err);
Expand Down
8 changes: 8 additions & 0 deletions template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ Resources:
Path: !GetAtt LocalQueue.Arn
BatchSize: 10

MotUpdateVrm:
Type: 'AWS::Serverless::Function'
Properties:
CodeUri: src/handler/
Handler: mot-update-vrm.handler
Runtime: nodejs18.x
Timeout: 20

RemoveInvalidPrimaryVrms:
Type: 'AWS::Serverless::Function'
Properties:
Expand Down
58 changes: 58 additions & 0 deletions tests/resources/mot-vrm-update-event-multiple.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"Records": [
{
"attributes": {
"ApproximateFirstReceiveTimestamp": "1691599233730",
"ApproximateReceiveCount": "1",
"SenderId": "AROAQC27VTVQFNB6YB3PQ:75df3f853a5436f0871d40b179d4ba5e",
"SentTimestamp": "1691599233712"
},
"awsRegion": "eu-west-1",
"eventSource": "aws:sqs",
"body": "",
"eventSourceARN": "arn:aws:sqs:eu-west-1:006106226016:sync-test-result-info-cb2-8663-queue",
"level": "info",
"md5OfBody": "94860332cba78c4d0d888c6556e6f57b",
"message": "payload recieved from queue:",
"messageAttributes": {},
"messageId": "5695c906-9796-4a79-839c-c887cfd3ea5b",
"receiptHandle": "AQEBmrbVJpOCy7CxMt94wxfmXe9aJQ3spunsOq5XfsRRgFxYFmG1e0WPsH9EJ863z5+7wNjlC+ZExU0cWrcUeXlgvPrgTDchhr9etZXmC8DeeHe4WU7WI59tZwKgA9BBcnAstwxjVChVSAi+FBaL7rZwd6X3Kn8+bA46UJjF8wmrDit9pXqnRUtSSL9fUn/7eEsO6VQ/2UKbhRXIwmF3sBRsiuF5RXuXKukOtrWlK5fepAQJVngHc2NV8tcxnHDyiLBc+k/rpRuF3NR4OhT56bJW33LPWvl6Sl4C92NLzS3L5lnvMLUvQFoEE6aP3/IYZD29HxgmQON0mzsvnnvuXOWNa9ZBmBKl+01Z7Xd3W4w9jfSucPRf4EjsCRoDXUkpb5WlJ0xGnnyK3ouoCjLc943OabFeqT1ELyxmhJnWo08Vtaw="
},
{
"attributes": {
"ApproximateFirstReceiveTimestamp": "1691599233730",
"ApproximateReceiveCount": "1",
"SenderId": "AROAQC27VTVQFNB6YB3PQ:75df3f853a5436f0871d40b179d4ba5e",
"SentTimestamp": "1691599233712"
},
"awsRegion": "eu-west-1",
"eventSource": "aws:sqs",
"body": "",
"eventSourceARN": "arn:aws:sqs:eu-west-1:006106226016:sync-test-result-info-cb2-8663-queue",
"level": "info",
"md5OfBody": "94860332cba78c4d0d888c6556e6f57b",
"message": "payload recieved from queue:",
"messageAttributes": {},
"messageId": "5695c906-9796-4a79-839c-c887cfd3ea5b",
"receiptHandle": "AQEBmrbVJpOCy7CxMt94wxfmXe9aJQ3spunsOq5XfsRRgFxYFmG1e0WPsH9EJ863z5+7wNjlC+ZExU0cWrcUeXlgvPrgTDchhr9etZXmC8DeeHe4WU7WI59tZwKgA9BBcnAstwxjVChVSAi+FBaL7rZwd6X3Kn8+bA46UJjF8wmrDit9pXqnRUtSSL9fUn/7eEsO6VQ/2UKbhRXIwmF3sBRsiuF5RXuXKukOtrWlK5fepAQJVngHc2NV8tcxnHDyiLBc+k/rpRuF3NR4OhT56bJW33LPWvl6Sl4C92NLzS3L5lnvMLUvQFoEE6aP3/IYZD29HxgmQON0mzsvnnvuXOWNa9ZBmBKl+01Z7Xd3W4w9jfSucPRf4EjsCRoDXUkpb5WlJ0xGnnyK3ouoCjLc943OabFeqT1ELyxmhJnWo08Vtaw="
},
{
"attributes": {
"ApproximateFirstReceiveTimestamp": "1691599233730",
"ApproximateReceiveCount": "1",
"SenderId": "AROAQC27VTVQFNB6YB3PQ:75df3f853a5436f0871d40b179d4ba5e",
"SentTimestamp": "1691599233712"
},
"awsRegion": "eu-west-1",
"eventSource": "aws:sqs",
"body": "",
"eventSourceARN": "arn:aws:sqs:eu-west-1:006106226016:sync-test-result-info-cb2-8663-queue",
"level": "info",
"md5OfBody": "94860332cba78c4d0d888c6556e6f57b",
"message": "payload recieved from queue:",
"messageAttributes": {},
"messageId": "5695c906-9796-4a79-839c-c887cfd3ea5b",
"receiptHandle": "AQEBmrbVJpOCy7CxMt94wxfmXe9aJQ3spunsOq5XfsRRgFxYFmG1e0WPsH9EJ863z5+7wNjlC+ZExU0cWrcUeXlgvPrgTDchhr9etZXmC8DeeHe4WU7WI59tZwKgA9BBcnAstwxjVChVSAi+FBaL7rZwd6X3Kn8+bA46UJjF8wmrDit9pXqnRUtSSL9fUn/7eEsO6VQ/2UKbhRXIwmF3sBRsiuF5RXuXKukOtrWlK5fepAQJVngHc2NV8tcxnHDyiLBc+k/rpRuF3NR4OhT56bJW33LPWvl6Sl4C92NLzS3L5lnvMLUvQFoEE6aP3/IYZD29HxgmQON0mzsvnnvuXOWNa9ZBmBKl+01Z7Xd3W4w9jfSucPRf4EjsCRoDXUkpb5WlJ0xGnnyK3ouoCjLc943OabFeqT1ELyxmhJnWo08Vtaw="
}
]
}
22 changes: 22 additions & 0 deletions tests/resources/mot-vrm-update-event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"Records": [
{
"attributes": {
"ApproximateFirstReceiveTimestamp": "1691599233730",
"ApproximateReceiveCount": "1",
"SenderId": "AROAQC27VTVQFNB6YB3PQ:75df3f853a5436f0871d40b179d4ba5e",
"SentTimestamp": "1691599233712"
},
"awsRegion": "eu-west-1",
"eventSource": "aws:sqs",
"body": "",
"eventSourceARN": "arn:aws:sqs:eu-west-1:006106226016:sync-test-result-info-cb2-8663-queue",
"level": "info",
"md5OfBody": "94860332cba78c4d0d888c6556e6f57b",
"message": "payload recieved from queue:",
"messageAttributes": {},
"messageId": "5695c906-9796-4a79-839c-c887cfd3ea5b",
"receiptHandle": "AQEBmrbVJpOCy7CxMt94wxfmXe9aJQ3spunsOq5XfsRRgFxYFmG1e0WPsH9EJ863z5+7wNjlC+ZExU0cWrcUeXlgvPrgTDchhr9etZXmC8DeeHe4WU7WI59tZwKgA9BBcnAstwxjVChVSAi+FBaL7rZwd6X3Kn8+bA46UJjF8wmrDit9pXqnRUtSSL9fUn/7eEsO6VQ/2UKbhRXIwmF3sBRsiuF5RXuXKukOtrWlK5fepAQJVngHc2NV8tcxnHDyiLBc+k/rpRuF3NR4OhT56bJW33LPWvl6Sl4C92NLzS3L5lnvMLUvQFoEE6aP3/IYZD29HxgmQON0mzsvnnvuXOWNa9ZBmBKl+01Z7Xd3W4w9jfSucPRf4EjsCRoDXUkpb5WlJ0xGnnyK3ouoCjLc943OabFeqT1ELyxmhJnWo08Vtaw="
}
]
}
184 changes: 184 additions & 0 deletions tests/unit/handler/mot-update-vrm.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/* eslint-disable import/first */
const mockSearchByCriteria = jest.fn();
const mockUpdateVehicle = jest.fn();
const mockPublish = jest.fn();

import { handler } from '../../../src/handler/mot-update-vrm';
import { StatusCode } from '../../../src/util/enum';
import logger from '../../../src/util/logger';
import updateEventMultiple from '../../resources/mot-vrm-update-event-multiple.json';
import updateEvent from '../../resources/mot-vrm-update-event.json';

jest.mock('../../../src/services/database.ts', () => ({
searchByCriteria: mockSearchByCriteria,
updateVehicle: mockUpdateVehicle,
}));

jest.mock('../../../src/services/sns', () => ({
publish: mockPublish,
}));

describe('Test Mot Update Vrm Lambda Function', () => {
beforeEach(() => {
jest.resetAllMocks();
jest.resetModules();
});

it('should log when no records are found', async () => {
mockSearchByCriteria.mockReturnValue([]);

const loggerSpy = jest.spyOn(logger, 'info');

updateEvent.Records[0].body = JSON.stringify({
vin: '1',
vrm: '3',
});

await handler(updateEvent);

expect(loggerSpy).toHaveBeenCalledWith('No record found for VIN: 1');
expect(mockPublish).not.toHaveBeenCalled();
});

it('should log when no current records are found', async () => {
mockSearchByCriteria.mockReturnValue([
{
primaryVrm: '1',
vin: '2',
techRecord_statusCode: StatusCode.ARCHIVED,
systemNumber: '15',
},
]);

const loggerSpy = jest.spyOn(logger, 'info');

updateEvent.Records[0].body = JSON.stringify({
vin: '1',
vrm: '3',
});

await handler(updateEvent);

expect(loggerSpy).toHaveBeenCalledWith('No current record found for VIN: 1');
expect(mockPublish).not.toHaveBeenCalled();
});

it('should log when a current record with a duplicate VIN is found', async () => {
mockSearchByCriteria.mockReturnValue([{
primaryVrm: '1',
vin: '2',
techRecord_statusCode: StatusCode.CURRENT,
systemNumber: '15',
},
{
primaryVrm: '1',
vin: '2',
techRecord_statusCode: StatusCode.CURRENT,
systemNumber: '16',
}]);

const loggerSpy = jest.spyOn(logger, 'info');

updateEvent.Records[0].body = JSON.stringify({
vin: '1',
vrm: '3',
});

await handler(updateEvent);

expect(loggerSpy).toHaveBeenCalledWith('Duplicate current records found for VIN 1');
expect(mockPublish).not.toHaveBeenCalled();
});

it('should log when a current record with a duplicate VIN and VRM is found', async () => {
mockSearchByCriteria.mockReturnValue([{
primaryVrm: '1',
vin: '2',
techRecord_statusCode: StatusCode.CURRENT,
systemNumber: '15',
},
]);

const loggerSpy = jest.spyOn(logger, 'info');

updateEvent.Records[0].body = JSON.stringify({
vin: '2',
vrm: '1',
});

await handler(updateEvent);

expect(loggerSpy).toHaveBeenCalledWith('No update needed for VRM 1 and VIN 2');
expect(mockPublish).not.toHaveBeenCalled();
});

it('should log when there is a current record with a matching VIN and no matching VRM', async () => {
mockSearchByCriteria.mockReturnValue([{
primaryVrm: '1',
vin: '2',
techRecord_statusCode: StatusCode.CURRENT,
systemNumber: '15',
},
]);

mockUpdateVehicle.mockResolvedValue(true);

const loggerSpy = jest.spyOn(logger, 'info');

updateEvent.Records[0].body = JSON.stringify({
vin: '2',
vrm: '3',
});

await handler(updateEvent);

expect(loggerSpy).toHaveBeenCalledWith('Updated systemNumber 15 with VRM 3');
expect(mockUpdateVehicle).toHaveBeenCalled();
expect(mockPublish).toHaveBeenCalled();
});

it('should run three events, pass one, fail one, pass the third one', async () => {
mockSearchByCriteria.mockReturnValueOnce([{
primaryVrm: '1',
vin: '2',
techRecord_statusCode: StatusCode.CURRENT,
systemNumber: '15',
},
]).mockReturnValueOnce([]).mockReturnValue([
{
primaryVrm: '10',
vin: '5',
techRecord_statusCode: StatusCode.CURRENT,
systemNumber: '16',
},
]);

mockUpdateVehicle.mockResolvedValue(true);

const loggerSpy = jest.spyOn(logger, 'info');

updateEventMultiple.Records[0].body = JSON.stringify({
vin: '2',
vrm: '3',
});

updateEventMultiple.Records[1].body = JSON.stringify({
vin: '3',
vrm: '4',
});

updateEventMultiple.Records[2].body = JSON.stringify({
vin: '5',
vrm: '6',
});

await handler(updateEventMultiple);

expect(loggerSpy).toHaveBeenCalledWith('Updated systemNumber 15 with VRM 3');
expect(loggerSpy).toHaveBeenCalledWith('No record found for VIN: 3');
expect(loggerSpy).toHaveBeenCalledWith('Updated systemNumber 16 with VRM 6');
expect(mockUpdateVehicle).toHaveBeenCalledTimes(2);
expect(mockPublish).toHaveBeenCalled();
expect(loggerSpy).toHaveBeenLastCalledWith('All records processed in SQS event');
});
});
Loading
Loading