From 6534233863cd8532cab8428c114fe1b36d47f63a Mon Sep 17 00:00:00 2001 From: Tom Wey Date: Tue, 17 Dec 2024 15:23:58 +0000 Subject: [PATCH] Use router module in user-benefits API handler --- handlers/user-benefits/src/index.ts | 78 +++++++++++------------ handlers/user-benefits/test/index.test.ts | 36 ++++------- 2 files changed, 49 insertions(+), 65 deletions(-) diff --git a/handlers/user-benefits/src/index.ts b/handlers/user-benefits/src/index.ts index e9baa5882..0cb6153b5 100644 --- a/handlers/user-benefits/src/index.ts +++ b/handlers/user-benefits/src/index.ts @@ -1,4 +1,3 @@ -import { ValidationError } from '@modules/errors'; import { buildAuthenticate } from '@modules/identity/apiGateway'; import type { IdentityUserDetails } from '@modules/identity/identity'; import { Lazy } from '@modules/lazy'; @@ -6,8 +5,13 @@ import type { UserBenefitsResponse } from '@modules/product-benefits/schemas'; import { getUserBenefits } from '@modules/product-benefits/userBenefits'; import { getProductCatalogFromApi } from '@modules/product-catalog/api'; import { ProductCatalogHelper } from '@modules/product-catalog/productCatalog'; +import { Router } from '@modules/routing/router'; import type { Stage } from '@modules/stage'; -import type { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda'; +import type { + APIGatewayProxyEvent, + APIGatewayProxyResult, + Handler, +} from 'aws-lambda'; import { getTrialInformation } from './trials'; const stage = process.env.STAGE as Stage; @@ -36,47 +40,41 @@ const getUserBenefitsResponse = async ( }; }; -export const handler = async ( +export const userBenefitsHandler = async ( event: APIGatewayProxyEvent, ): Promise => { - console.log(`Input is ${JSON.stringify(event)}`); - if (!(event.path === '/benefits/me' && event.httpMethod === 'GET')) { - return { - body: 'Not Found', - statusCode: 404, - }; + const maybeAuthenticatedEvent = await authenticate(event); + + if (maybeAuthenticatedEvent.type === 'failure') { + return maybeAuthenticatedEvent.response; } - try { - const maybeAuthenticatedEvent = await authenticate(event); - if (maybeAuthenticatedEvent.type === 'failure') { - return maybeAuthenticatedEvent.response; - } + const userBenefitsResponse = await getUserBenefitsResponse( + stage, + new ProductCatalogHelper(await productCatalog.get()), + maybeAuthenticatedEvent.userDetails, + ); + return { + body: JSON.stringify(userBenefitsResponse), + // https://www.fastly.com/documentation/guides/concepts/edge-state/cache/cache-freshness/#preventing-content-from-being-cached + headers: { + 'Cache-Control': 'private, no-store', + }, + statusCode: 200, + }; +}; + +const router = new Router([ + { + httpMethod: 'GET', + path: '/benefits/me', + handler: userBenefitsHandler, + }, +]); - const userBenefitsResponse = await getUserBenefitsResponse( - stage, - new ProductCatalogHelper(await productCatalog.get()), - maybeAuthenticatedEvent.userDetails, - ); - return { - body: JSON.stringify(userBenefitsResponse), - // https://www.fastly.com/documentation/guides/concepts/edge-state/cache/cache-freshness/#preventing-content-from-being-cached - headers: { - 'Cache-Control': 'private, no-store', - }, - statusCode: 200, - }; - } catch (error) { - console.log('Caught exception with message: ', error); - if (error instanceof ValidationError) { - return { - body: error.message, - statusCode: 400, - }; - } - return { - body: 'Internal server error', - statusCode: 500, - }; - } +export const handler: Handler = async ( + event: APIGatewayProxyEvent, +): Promise => { + console.log(`Input is ${JSON.stringify(event)}`); + return router.routeRequest(event); }; diff --git a/handlers/user-benefits/test/index.test.ts b/handlers/user-benefits/test/index.test.ts index 5572e8a8a..fc9b3b398 100644 --- a/handlers/user-benefits/test/index.test.ts +++ b/handlers/user-benefits/test/index.test.ts @@ -1,6 +1,6 @@ import type { UserBenefitsResponse } from '@modules/product-benefits/schemas'; import type { APIGatewayProxyEvent } from 'aws-lambda'; -import { handler } from '../src/index'; +import { userBenefitsHandler } from '../src/index'; jest.mock('@modules/identity/apiGateway', () => ({ buildAuthenticate: () => (event: APIGatewayProxyEvent) => { @@ -27,34 +27,20 @@ jest.mock('@modules/product-benefits/userBenefits', () => ({ getUserBenefits: () => ['adFree'], })); -describe('handler', () => { - it('returns a 404 for an unrecognized path or HTTP method', async () => { +describe('userBenefitsHandler', () => { + it('returns a 200 with user benefits', async () => { const requestEvent = { - path: '/bad/path', + path: '/benefits/me', httpMethod: 'GET', - headers: {}, + headers: { + Authorization: 'Bearer good-token', + }, } as unknown as APIGatewayProxyEvent; - const response = await handler(requestEvent); + const response = await userBenefitsHandler(requestEvent); - expect(response.statusCode).toEqual(404); - }); - - describe('/benefits/me', () => { - it('returns a 200 with user benefits', async () => { - const requestEvent = { - path: '/benefits/me', - httpMethod: 'GET', - headers: { - Authorization: 'Bearer good-token', - }, - } as unknown as APIGatewayProxyEvent; - - const response = await handler(requestEvent); - - expect(response.statusCode).toEqual(200); - const parsedBody = JSON.parse(response.body) as UserBenefitsResponse; - expect(parsedBody.benefits).toEqual(['adFree']); - }); + expect(response.statusCode).toEqual(200); + const parsedBody = JSON.parse(response.body) as UserBenefitsResponse; + expect(parsedBody.benefits).toEqual(['adFree']); }); });