Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Prod Release into Master #34

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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
Loading