diff --git a/src/handler/mot-update-vrm.ts b/src/handler/mot-update-vrm.ts index 080e7f82..ffaf0468 100644 --- a/src/handler/mot-update-vrm.ts +++ b/src/handler/mot-update-vrm.ts @@ -1,8 +1,5 @@ import { TechRecordType } from '@dvsa/cvs-type-definitions/types/v3/tech-record/tech-record-verb'; import { SQSEvent, SQSRecord } from 'aws-lambda'; -import { ADRCertificateDetails } from '@dvsa/cvs-type-definitions/types/v3/tech-record/get/hgv/complete'; -import { ADRCertificateTypes } from '@dvsa/cvs-type-definitions/types/v3/tech-record/enums/adrCertificateTypes.enum'; -import { TechRecordType as TechRecordTypeByVehicle } from '@dvsa/cvs-type-definitions/types/v3/tech-record/tech-record-vehicle-type'; import { MotCherishedTransfer } from '../models/motCherishedTransfer'; import { SearchCriteria } from '../models/search'; import { SNSMessageBody } from '../models/updateVrm'; @@ -11,11 +8,6 @@ import { getBySystemNumberAndCreatedTimestamp, searchByCriteria, updateVehicle } import { publish } from '../services/sns'; import { StatusCode } from '../util/enum'; import logger from '../util/logger'; -import { DocumentName, SQSRequestBody } from '../models/sqsPayload'; -import { validateAdrCertificateDetails } from '../validators/adrCertificate'; -import { addHttpHeaders } from '../util/httpHeaders'; -import { formatTechRecord } from '../util/formatTechRecord'; -import { addToSqs } from '../services/sqs'; const SYSTEM_USER = 'CVS Automated Cherished Transfer'; @@ -24,14 +16,12 @@ export const handler = async (event: SQSEvent): Promise => { try { const recordsToSend: SNSMessageBody[] = []; - const processRecordWrapper = async (record: SQSRecord): Promise => { const result = await processRecord(record); recordsToSend.push(...result); }; await Promise.all(event.Records.map(processRecordWrapper)); - if (recordsToSend.length) { await publish(JSON.stringify(recordsToSend), process.env.VRM_TRANSFERRED_ARN ?? ''); } @@ -43,7 +33,7 @@ export const handler = async (event: SQSEvent): Promise => { } }; -const processRecord = async (cherishedTransfer: SQSRecord): Promise => { +const processRecord = async (cherishedTransfer: SQSRecord) => { const parsedRecord = JSON.parse(cherishedTransfer.body) as MotCherishedTransfer; const allRecords = await searchByCriteria(SearchCriteria.VIN, parsedRecord.vin); @@ -51,7 +41,6 @@ const processRecord = async (cherishedTransfer: SQSRecord): Promise techRecord_statusCode === StatusCode.CURRENT); if (!allCurrentRecords.length) { @@ -74,7 +63,10 @@ const processRecord = async (cherishedTransfer: SQSRecord): Promise>(completeTechRecord); - formattedTechRecord.techRecord_adrPassCertificateDetails = formattedTechRecord.techRecord_adrPassCertificateDetails - ? [...formattedTechRecord.techRecord_adrPassCertificateDetails, newAdrCertificate] - : [newAdrCertificate]; - - const adrCertSqsPayload: SQSRequestBody = { - techRecord: formattedTechRecord, - adrCertificate: newAdrCertificate, - documentName: DocumentName.ADR_PASS_CERTIFICATE, - recipientEmailAddress: '', - }; - - await addToSqs(adrCertSqsPayload, process.env.DOC_GEN_SQS_QUEUE ?? ''); - return recordsToUpdate.map((record) => ({ ...record, userEmail: SYSTEM_USER })); -}; - -const createAdrCertificate = (systemNumber: string): ADRCertificateDetails => { - const generatedTimestamp = new Date().toISOString(); - const certificateId = `adr_pass_${systemNumber}_${generatedTimestamp}`; - - return { - createdByName: SYSTEM_USER, - certificateType: 'PASS' as ADRCertificateTypes, - generatedTimestamp, - certificateId, - }; + return recordsToUpdate; }; diff --git a/tests/unit/handler/mot-update-vrm.unit.test.ts b/tests/unit/handler/mot-update-vrm.unit.test.ts index 3e8e449f..facd96fe 100644 --- a/tests/unit/handler/mot-update-vrm.unit.test.ts +++ b/tests/unit/handler/mot-update-vrm.unit.test.ts @@ -4,11 +4,11 @@ const mockGetBySystemNumberAndCreatedTimestamp = jest.fn(); const mockSearchByCriteria = jest.fn(); const mockUpdateVehicle = jest.fn(); const mockPublish = jest.fn(); -const mockSqs = 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', () => ({ @@ -21,10 +21,6 @@ jest.mock('../../../src/services/sns', () => ({ publish: mockPublish, })); -jest.mock('../../../src/services/sqs', () => ({ - addToSqs: mockSqs, -})); - describe('Test Mot Update Vrm Lambda Function', () => { beforeEach(() => { jest.resetAllMocks(); @@ -120,7 +116,6 @@ describe('Test Mot Update Vrm Lambda Function', () => { }); it('should log when there is a current record with a matching VIN and no matching VRM', async () => { - mockSqs.mockResolvedValueOnce(undefined); mockSearchByCriteria.mockReturnValue([{ primaryVrm: '1', vin: '2', @@ -145,4 +140,49 @@ describe('Test Mot Update Vrm Lambda Function', () => { 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); + mockGetBySystemNumberAndCreatedTimestamp.mockResolvedValue({}); + 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'); + }); });