Skip to content

Commit

Permalink
chore(cb2-12087): added new logging (#75)
Browse files Browse the repository at this point in the history
* chore(cb2-12087): added temporary logging

* feat(cb2-12097): add elvis operator to stop error on no test types

* feat(CB2-12097): change to logging

---------

Co-authored-by: kristian carr <kristian.carr@bjss.com>
Co-authored-by: Daniel Searle <daniel.searle1@dvsa.gov.uk>
  • Loading branch information
3 people authored May 23, 2024
1 parent 3a029a9 commit d57c7fc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
7 changes: 6 additions & 1 deletion src/functions/certGenInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,23 @@ const certGenInit: Handler = async (

// Convert the received event into a readable array of filtered test results
const expandedRecords: any[] = StreamService.getTestResultStream(event);
console.log(`Number of Retrieved records: ${expandedRecords.length}`);
const certGenFilteredRecords: any[] =
Utils.filterCertificateGenerationRecords(expandedRecords);

console.log(`Number of Filtered Retrieved Records: ${certGenFilteredRecords.length}`);

// Instantiate the Simple Queue Service
const sqService: SQService = new SQService(new SQSClient());
const sendMessagePromises: Array<
Promise<SendMessageCommandOutput | ServiceException>
> = [];

certGenFilteredRecords.forEach((record: any) => {
const stringifiedRecord = JSON.stringify(record);
console.log(stringifiedRecord);
sendMessagePromises.push(
sqService.sendCertGenMessage(JSON.stringify(record))
sqService.sendCertGenMessage(stringifiedRecord)
);
});

Expand Down
1 change: 1 addition & 0 deletions src/services/SQService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class SQService {
* @param messageBody
*/
public sendCertGenMessage(messageBody: string) {
console.log(`Message Body to be sent: ${messageBody}`);
return this.sendMessage(messageBody, this.config.queueName[0]);
}

Expand Down
32 changes: 19 additions & 13 deletions src/services/StreamService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// import { DynamoDB } from "aws-sdk";
import { DynamoDBRecord } from "aws-lambda";
import { unmarshall } from "@aws-sdk/util-dynamodb";
import { DynamoDBRecord } from "aws-lambda";

/**
* Service class for interpreting and formatting
Expand All @@ -26,6 +26,7 @@ class StreamService {
* @param event
*/
public static getTestResultStream(event: any) {
console.log(event);
// Create from a test result with multiple test types, multiple test result with one test type each
const records: any[] = event.Records.filter((record: DynamoDBRecord) => {
// Retrieve "INSERT" events
Expand Down Expand Up @@ -72,21 +73,26 @@ class StreamService {
const splittedRecords: any[] = [];
const templateRecord: any = Object.assign({}, record);
Object.assign(templateRecord, {});
console.log("before for each");
if (record.testTypes instanceof Array) {
record.testTypes?.forEach((testType: any, i: number, array: any[]) => {
console.log("in for each");
const clonedRecord: any = Object.assign({}, templateRecord); // Create record from template
Object.assign(clonedRecord, { testTypes: testType }); // Assign it the test type
Object.assign(clonedRecord, {
// Assign certificate order number
order: {
current: i + 1,
total: array.length,
},
});

record.testTypes.forEach((testType: any, i: number, array: any[]) => {
const clonedRecord: any = Object.assign({}, templateRecord); // Create record from template
Object.assign(clonedRecord, { testTypes: testType }); // Assign it the test type
Object.assign(clonedRecord, {
// Assign certificate order number
order: {
current: i + 1,
total: array.length,
},
});
splittedRecords.push(clonedRecord);
});
}

splittedRecords.push(clonedRecord);
});

console.log("after for each");
return splittedRecords;
})
.reduce((acc: any[], val: any) => acc.concat(val), []); // Flatten the array
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/certGenInit.unitTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Configuration } from "../../src/utils/Configuration";
import { SQMockClient } from "../models/SQMockClient";
import event from "../resources/stream-event.json";
import { mockClient } from "aws-sdk-client-mock";
import {marshall, unmarshall} from "@aws-sdk/util-dynamodb";

const record = {
testerStaffId: "1",
Expand Down Expand Up @@ -132,6 +133,17 @@ describe("cert-gen-init", () => {
expect(processedEvent).toEqual(expectedResult);
});

it("should result in an empty array when the test type is an object", () => {
process.env.PROCESS_MODIFY_EVENTS = "true";
const eventWithTestTypeObject = unmarshall({...event}.Records[0].dynamodb.NewImage);
eventWithTestTypeObject.testTypes = {};
event.Records[0].eventName = "MODIFY";
const mainEvent = {...event};
mainEvent.Records[0].dynamodb.NewImage = marshall(eventWithTestTypeObject) as any;
processedEvent = StreamService.getTestResultStream(mainEvent);
expect(processedEvent).toEqual([]);
});

it("should throw an error if PROCESS_MODIFY_EVENTS is not true or false", () => {
process.env.PROCESS_MODIFY_EVENTS = "";
event.Records[0].eventName = "MODIFY";
Expand Down

0 comments on commit d57c7fc

Please sign in to comment.