Skip to content

Commit

Permalink
dep-updates: Make pdf-generator lambda return an object so that direc…
Browse files Browse the repository at this point in the history
…t invocations can rely on a specific shape.
  • Loading branch information
Zachary Rogers committed Sep 9, 2024
1 parent 0757e06 commit b852a8e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const lambdaClientMock = jest
.spyOn(LambdaClient.prototype, 'send')
.mockImplementation(() => {
return {
Payload: Buffer.from(JSON.stringify({ key: documentKey })),
Payload: Buffer.from(JSON.stringify({ tempId: documentKey })),
};
});

Expand Down Expand Up @@ -46,4 +46,32 @@ describe('generatePdfFromHtmlInteractor', () => {
applicationContext.getPersistenceGateway().getDocument,
).toHaveBeenCalled();
});

it('should throw an error when the pdf generator lambda does not generate a file id', async () => {
applicationContext.environment.stage = 'prod';
applicationContext.environment.currentColor = 'blue';
jest.spyOn(LambdaClient.prototype, 'send').mockImplementation(() => {
return {
Payload: Buffer.from(JSON.stringify({})),
};
});

await expect(
generatePdfFromHtmlInteractor(applicationContext, {
contentHtml: '<p>this is content</p>',
displayHeaderFooter: false,
docketNumber: '102-24',
footerHtml: '<div>this is a footer</div>',
headerHtml: '<span>this is a header</span>',
overwriteFooter: false,
}),
).rejects.toThrow(
`Unable to generate pdf. Check pdf_generator_${applicationContext.environment.stage}_${applicationContext.environment.currentColor} lambda for errors`,
);

expect(lambdaClientMock).toHaveBeenCalled();
expect(
applicationContext.getPersistenceGateway().getDocument,
).not.toHaveBeenCalled();
});
});
18 changes: 16 additions & 2 deletions web-api/src/business/useCases/pdf/generatePdfFromHtmlInteractor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { InvokeCommand, LambdaClient } from '@aws-sdk/client-lambda';
import { PdfGenerationResult } from '@web-api/lambdas/pdfGeneration/pdf-generation';
import { ServerApplicationContext } from '@web-api/applicationContext';

export const generatePdfFromHtmlInteractor = async (
Expand Down Expand Up @@ -63,8 +64,21 @@ export const generatePdfFromHtmlInteractor = async (

const response = await client.send(command);
const textDecoder = new TextDecoder('utf-8');
const responseStr = textDecoder.decode(response.Payload);
const key = JSON.parse(responseStr);
let key: string;
try {
const responseStr = textDecoder.decode(response.Payload);
const pdfGenerationResult: PdfGenerationResult = JSON.parse(responseStr);
if (!pdfGenerationResult.tempId) {
throw new Error(
`Unable to generate pdf. Check pdf_generator_${stage}_${currentColor} lambda for errors`,
);
}
key = pdfGenerationResult.tempId;
} catch (e) {
throw new Error(
`Unable to generate pdf. Check pdf_generator_${stage}_${currentColor} lambda for errors`,
);
}

return await applicationContext.getPersistenceGateway().getDocument({
applicationContext,
Expand Down
6 changes: 5 additions & 1 deletion web-api/src/lambdas/pdfGeneration/pdf-generation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { createApplicationContext } from '../../applicationContext';

export type PdfGenerationResult = {
tempId: string;
};

export const handler = async event => {
const applicationContext = createApplicationContext({});

Expand All @@ -20,7 +24,7 @@ export const handler = async event => {
useTempBucket: true,
});

return tempId;
return { tempId };
};

export const changeOfAddressHandler = async event => {
Expand Down

0 comments on commit b852a8e

Please sign in to comment.