Skip to content

Commit

Permalink
Merge pull request #32 from dvsa/feature/CB2-14232
Browse files Browse the repository at this point in the history
feat(CB2-14232): update doc-gen-upload to remove snowball SQS pattern
  • Loading branch information
m-coslett authored Oct 21, 2024
2 parents 64c28d0 + 9cb2029 commit 412da36
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 42 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ reports
*.map

# npm audit results
npm-audit.html
npm-audit.html

# idea specific hidden files
.idea/**
*.iml
77 changes: 36 additions & 41 deletions src/handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Handler, SQSBatchResponse, SQSEvent } from 'aws-lambda';
import {
Handler, SQSBatchItemFailure, SQSBatchResponse, SQSEvent,
} from 'aws-lambda';
import { TextDecoder } from 'util';
import { DocumentModel } from './models/documentModel';
import { getDocumentFromRequest } from './models/documentModel.factory';
Expand All @@ -13,50 +15,43 @@ export const handler: Handler = async (event: SQSEvent): Promise<SQSBatchRespons
logger.error('ERROR: event is not defined.');
throw new Error('Event is empty');
}

const altPromiseArray = event.Records.map((sqsRecord) => {
const request = JSON.parse(sqsRecord.body) as Request;

const document: DocumentModel = getDocumentFromRequest(request);
return generateAndUpload(document, request);
});

const results = await Promise.allSettled(altPromiseArray);
const ids = results
.map((result, index) => (result.status === 'fulfilled' ? null : event.Records[index].messageId))
.filter((item) => item !== null);
return {
batchItemFailures: ids.map((id) => ({ itemIdentifier: id })),
};
const batchItemFailures: SQSBatchItemFailure[] = [];
// eslint-disable-next-line no-restricted-syntax
for (const sqsRecord of event.Records) {
try {
const request = JSON.parse(sqsRecord.body) as Request;
const document: DocumentModel = getDocumentFromRequest(request);
// eslint-disable-next-line no-await-in-loop
await generateAndUpload(document, request);
} catch (error) {
console.error(error);
batchItemFailures.push({ itemIdentifier: sqsRecord.messageId });
}
}
return { batchItemFailures };
};

const generateAndUpload = async (document: DocumentModel, request: Request) => {
try {
logger.info(`Starting lambda to lambda invoke (data): ${JSON.stringify(document)}`);
const response = await invokePdfGenLambda(document, request.documentName);
logger.info('Finished lambda to lambda invoke, checking response');
logger.info(`Starting lambda to lambda invoke (data): ${JSON.stringify(document)}`);
const response = await invokePdfGenLambda(document, request.documentName);
logger.info('Finished lambda to lambda invoke, checking response');

if (response.StatusCode !== 200) {
logger.error(`Error invoking doc gen (lambda call failed with ${response.StatusCode}`);
throw new Error(`Error invoking doc gen (lambda call failed with ${response.StatusCode}`);
}
const responseString: string = new TextDecoder().decode(response.Payload);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const responseJson: any = JSON.parse(responseString);
if (responseJson.statusCode !== 200) {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
throw new Error(`Error returned from doc gen (${responseJson.statusCode}): ${responseJson.body}`);
}
if (response.StatusCode !== 200) {
throw new Error(`Error invoking doc gen (lambda call failed with ${response.StatusCode}`);
}
const responseString: string = new TextDecoder().decode(response.Payload);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const responseJson: any = JSON.parse(responseString);

Check warning on line 44 in src/handler.ts

View workflow job for this annotation

GitHub Actions / scanner

Unexpected any. Specify a different type

Check warning on line 44 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 44 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 44 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 44 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 44 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 44 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 44 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type

Check warning on line 44 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unexpected any. Specify a different type
if (responseJson.statusCode !== 200) {

Check warning on line 45 in src/handler.ts

View workflow job for this annotation

GitHub Actions / scanner

Unsafe member access .statusCode on an `any` value

Check warning on line 45 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .statusCode on an `any` value

Check warning on line 45 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .statusCode on an `any` value

Check warning on line 45 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .statusCode on an `any` value

Check warning on line 45 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .statusCode on an `any` value

Check warning on line 45 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .statusCode on an `any` value

Check warning on line 45 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .statusCode on an `any` value

Check warning on line 45 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .statusCode on an `any` value

Check warning on line 45 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .statusCode on an `any` value
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
throw new Error(`Error returned from doc gen (${responseJson.statusCode}): ${responseJson.body}`);

Check warning on line 47 in src/handler.ts

View workflow job for this annotation

GitHub Actions / scanner

Unsafe member access .statusCode on an `any` value

Check warning on line 47 in src/handler.ts

View workflow job for this annotation

GitHub Actions / scanner

Unsafe member access .body on an `any` value

Check warning on line 47 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .statusCode on an `any` value

Check warning on line 47 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .body on an `any` value

Check warning on line 47 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .statusCode on an `any` value

Check warning on line 47 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .body on an `any` value

Check warning on line 47 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .statusCode on an `any` value

Check warning on line 47 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .body on an `any` value

Check warning on line 47 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .statusCode on an `any` value

Check warning on line 47 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .body on an `any` value

Check warning on line 47 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .statusCode on an `any` value

Check warning on line 47 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .body on an `any` value

Check warning on line 47 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .statusCode on an `any` value

Check warning on line 47 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .body on an `any` value

Check warning on line 47 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .statusCode on an `any` value

Check warning on line 47 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .body on an `any` value

Check warning on line 47 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .statusCode on an `any` value

Check warning on line 47 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .body on an `any` value
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const responseBuffer: Buffer = Buffer.from(responseJson.body, 'base64');
document.setFileSize(responseBuffer.byteLength);
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const responseBuffer: Buffer = Buffer.from(responseJson.body, 'base64');

Check warning on line 51 in src/handler.ts

View workflow job for this annotation

GitHub Actions / scanner

Unsafe member access .body on an `any` value

Check warning on line 51 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .body on an `any` value

Check warning on line 51 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .body on an `any` value

Check warning on line 51 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .body on an `any` value

Check warning on line 51 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .body on an `any` value

Check warning on line 51 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .body on an `any` value

Check warning on line 51 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .body on an `any` value

Check warning on line 51 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .body on an `any` value

Check warning on line 51 in src/handler.ts

View workflow job for this annotation

GitHub Actions / Build / build

Unsafe member access .body on an `any` value
document.setFileSize(responseBuffer.byteLength);

logger.info(`Starting s3 upload for file: ${process.env.BRANCH}/${document.filename}`);
await uploadPdfToS3(responseBuffer, document.metaData, document.filename);
logger.info('Finished s3 upload');
} catch (error) {
logger.error(error.message);
throw error;
}
logger.info(`Starting s3 upload for file: ${process.env.BRANCH}/${document.filename}`);
await uploadPdfToS3(responseBuffer, document.metaData, document.filename);
logger.info('Finished s3 upload');
};
8 changes: 8 additions & 0 deletions tests/unit/handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { InvokeCommandOutput, LambdaClient } from '@aws-sdk/client-lambda';
import { PutObjectCommandOutput, S3 } from '@aws-sdk/client-s3';
import { SQSBatchResponse } from 'aws-lambda';
import { DocumentName } from '../../src/enums/documentName.enum';
import { ReasonForIssue } from '../../src/enums/reasonForIssue.enum';
import * as Handler from '../../src/handler';
Expand Down Expand Up @@ -38,6 +39,13 @@ describe('handler tests', () => {
}
});

it('should return failed records', async () => {
const sqsEvent = pass;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const batchFails : SQSBatchResponse = await Handler.handler(sqsEvent, undefined, () => true);
expect(batchFails.batchItemFailures[0].itemIdentifier).toEqual(sqsEvent.Records[0].messageId);
});

it('should throw an error if document type not supported', async () => {
const sqsEvent = pass;
sqsEvent.Records[0].body = JSON.stringify({
Expand Down

0 comments on commit 412da36

Please sign in to comment.