diff --git a/src/functions/reportGen.ts b/src/functions/reportGen.ts index 2dbab8a..a3fde51 100644 --- a/src/functions/reportGen.ts +++ b/src/functions/reportGen.ts @@ -1,5 +1,5 @@ import { Callback, Context, Handler } from "aws-lambda"; -import { AWSError, DynamoDB, Lambda } from "aws-sdk"; +import { AWSError, Lambda } from "aws-sdk"; import { ManagedUpload } from "aws-sdk/clients/s3"; import { ERRORS } from "../assets/enum"; import { ActivitiesService } from "../services/ActivitiesService"; @@ -7,6 +7,7 @@ import { LambdaService } from "../services/LambdaService"; import { ReportGenerationService } from "../services/ReportGenerationService"; import { SendATFReport } from "../services/SendATFReport"; import { TestResultsService } from "../services/TestResultsService"; +import { processRecord } from "../utils/sqsProcess"; /** * λ function to process a DynamoDB stream of test results into a queue for certificate generation. @@ -27,18 +28,19 @@ const reportGen: Handler = async (event: any, context?: Context, callback?: Call event.Records.forEach((record: any) => { const visit: any = processRecord(record); - console.log(JSON.stringify(visit)); - const atfReportPromise = reportService - .generateATFReport(visit) - .then((generationServiceResponse) => { - return sendATFReport.sendATFReport(generationServiceResponse, visit); - }) - .catch((error: any) => { - console.log(error); - throw error; - }); + if (visit) { + const atfReportPromise = reportService + .generateATFReport(visit) + .then((generationServiceResponse) => { + return sendATFReport.sendATFReport(generationServiceResponse, visit); + }) + .catch((error: any) => { + console.log(error); + throw error; + }); - atfReportPromises.push(atfReportPromise); + atfReportPromises.push(atfReportPromise); + } }); return Promise.all(atfReportPromises).catch((error: AWSError) => { @@ -47,19 +49,4 @@ const reportGen: Handler = async (event: any, context?: Context, callback?: Call }); }; -export const processRecord = (record: any) => { - console.log(record.body); - console.log(JSON.parse(record.body)); - console.log(JSON.parse(record.body).Message); - const recordBody = JSON.parse(JSON.parse(record.body).Message) ?? ""; - if ( - recordBody.eventName === "MODIFY" - && recordBody.dynamodb - && recordBody.dynamodb.NewImage - ) { - return DynamoDB.Converter.unmarshall(recordBody.dynamodb.NewImage); - } - return undefined; -}; - export { reportGen }; diff --git a/src/utils/sqsProcess.ts b/src/utils/sqsProcess.ts new file mode 100644 index 0000000..1f652ac --- /dev/null +++ b/src/utils/sqsProcess.ts @@ -0,0 +1,13 @@ +import DynamoDB from "aws-sdk/clients/dynamodb"; + +export const processRecord = (record: any) => { + const recordBody = JSON.parse(record.body).Message; + if ( + recordBody.eventName === "MODIFY" + && recordBody.dynamodb + && recordBody.dynamodb.NewImage + ) { + return DynamoDB.Converter.unmarshall(recordBody.dynamodb.NewImage); + } + return undefined; +}; diff --git a/tests/unit/reportGenFunction.unitTest.ts b/tests/unit/reportGenFunction.unitTest.ts index 648f757..b3137f0 100644 --- a/tests/unit/reportGenFunction.unitTest.ts +++ b/tests/unit/reportGenFunction.unitTest.ts @@ -1,3 +1,5 @@ +const mockProcessRecord = jest.fn(); + import { reportGen } from "../../src/functions/reportGen"; import { ReportGenerationService } from "../../src/services/ReportGenerationService"; import { SendATFReport } from "../../src/services/SendATFReport"; @@ -5,8 +7,9 @@ import { SendATFReport } from "../../src/services/SendATFReport"; jest.mock("../../src/services/ReportGenerationService"); jest.mock("../../src/services/SendATFReport"); - -const mockPayload = "{\n \"Type\" : \"Notification\",\n \"MessageId\" : \"-c9d5---\",\n \"TopicArn\" : \"tf-visit\",\n \"Message\" : \"{\\\"eventID\\\":\\\"f9e63bf29bd6adf174e308201a97259f\\\",\\\"eventName\\\":\\\"MODIFY\\\",\\\"eventVersion\\\":\\\"1.1\\\",\\\"eventSource\\\":\\\"aws:dynamodb\\\",\\\"awsRegion\\\":\\\"eu-west-1\\\",\\\"dynamodb\\\":{\\\"ApproximateCreationDateTime\\\":1711549645,\\\"Keys\\\":{\\\"id\\\":{\\\"S\\\":\\\"6e4bd304-446e-4678-8289-dasdasjkl\\\"}},\\\"NewImage\\\":{\\\"testerStaffId\\\":{\\\"S\\\":\\\"132\\\"},\\\"testStationPNumber\\\":{\\\"S\\\":\\\"87-1369564\\\"},\\\"testerEmail\\\":{\\\"S\\\":\\\"tester@dvsa.gov.uk1111\\\"},\\\"testStationType\\\":{\\\"S\\\":\\\"gvts\\\"},\\\"testStationEmail\\\":{\\\"S\\\":\\\"teststationname@dvsa.gov.uk\\\"},\\\"startTime\\\":{\\\"S\\\":\\\"2022-01-01T10:00:40.561Z\\\"},\\\"endTime\\\":{\\\"S\\\":\\\"2022-01-01T10:00:40.561Z\\\"},\\\"id\\\":{\\\"S\\\":\\\"6e4bd304-446e-4678-8289-dasdasjkl\\\"},\\\"testStationName\\\":{\\\"S\\\":\\\"Rowe, Wunsch and Wisoky\\\"},\\\"activityType\\\":{\\\"S\\\":\\\"visit\\\"},\\\"activityDay\\\":{\\\"S\\\":\\\"2022-01-01\\\"},\\\"testerName\\\":{\\\"S\\\":\\\"namey mcname\\\"}},\\\"OldImage\\\":{\\\"testerStaffId\\\":{\\\"S\\\":\\\"132\\\"},\\\"testStationPNumber\\\":{\\\"S\\\":\\\"87-1369564\\\"},\\\"testerEmail\\\":{\\\"S\\\":\\\"tester@dvsa.gov.uk1111\\\"},\\\"testStationType\\\":{\\\"S\\\":\\\"gvts\\\"},\\\"testStationEmail\\\":{\\\"S\\\":\\\"teststationname@dvsa.gov.uk\\\"},\\\"startTime\\\":{\\\"S\\\":\\\"2022-01-01T10:00:40.561Z\\\"},\\\"endTime\\\":{\\\"S\\\":\\\"2022-01-01T10:00:40.561Z\\\"},\\\"id\\\":{\\\"S\\\":\\\"6e4bd304-446e-4678-8289-dasdasjkl\\\"},\\\"testStationName\\\":{\\\"S\\\":\\\"Rowe, Wunsch and Wisoky\\\"},\\\"activityType\\\":{\\\"S\\\":\\\"visit\\\"},\\\"activityDay\\\":{\\\"S\\\":\\\"2022-01-01\\\"},\\\"testerName\\\":{\\\"S\\\":\\\"231232132\\\"}},\\\"SequenceNumber\\\":\\\"1234\\\",\\\"SizeBytes\\\":704,\\\"StreamViewType\\\":\\\"NEW_AND_OLD_IMAGES\\\"},\\\"eventSourceARN\\\":\\\"arn:aws::eu--1::/cvs---//:32:37.491\\\"}\",\n \"Timestamp\" : \"2024-03-27T14:27:25.926Z\",\n \"SignatureVersion\" : \"1\",\n \"Signature\" : \"+/+/+3//+//2f3y0TI+/+//---\"\n}"; +jest.mock("../../src/utils/sqsProcess.ts", () => ({ + processRecord: mockProcessRecord +})); describe("Retro Gen Function", () => { beforeAll(() => jest.setTimeout(60000)); @@ -77,9 +80,10 @@ describe("Retro Gen Function", () => { it("Should throw an error (generateATFReport fails)", async () => { ReportGenerationService.prototype.generateATFReport = jest.fn().mockRejectedValue(new Error("Oh no!")); + mockProcessRecord.mockReturnValueOnce("All good"); expect.assertions(1); try { - await reportGen({ Records: [{ body: mockPayload }] }, ctx as any, () => { + await reportGen({ Records: [{ body: "test" }] }, ctx as any, () => { return; }); } catch (e) { @@ -89,9 +93,10 @@ describe("Retro Gen Function", () => { it("Should throw an error (bucket upload fails)", async () => { ReportGenerationService.prototype.generateATFReport = jest.fn().mockResolvedValue("Looking good"); SendATFReport.prototype.sendATFReport = jest.fn().mockRejectedValue(new Error("Oh dear")); + mockProcessRecord.mockReturnValueOnce("All good"); expect.assertions(1); try { - await reportGen({ Records: [{ body: mockPayload }] }, ctx as any, () => { + await reportGen({ Records: [{ body: "test" }] }, ctx as any, () => { return; }); } catch (e) {