Skip to content

Commit

Permalink
feat(cb2-10603): clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
naathanbrown committed Mar 28, 2024
1 parent 79ac2c8 commit 6a983b1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 31 deletions.
41 changes: 14 additions & 27 deletions src/functions/reportGen.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
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";
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.
Expand All @@ -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) => {
Expand All @@ -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 };
13 changes: 13 additions & 0 deletions src/utils/sqsProcess.ts
Original file line number Diff line number Diff line change
@@ -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;
};
13 changes: 9 additions & 4 deletions tests/unit/reportGenFunction.unitTest.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
const mockProcessRecord = jest.fn();

import { reportGen } from "../../src/functions/reportGen";
import { ReportGenerationService } from "../../src/services/ReportGenerationService";
import { SendATFReport } from "../../src/services/SendATFReport";
// import mockConfig from "../util/mockConfig";

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));
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 6a983b1

Please sign in to comment.