-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get benefits for identity ID specified in the path
- Loading branch information
Showing
3 changed files
with
129 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,64 @@ | ||
import { Lazy } from '@modules/lazy'; | ||
import type { UserBenefitsResponse } from '@modules/product-benefits/schemas'; | ||
import { getUserBenefitsExcludingStaff } from '@modules/product-benefits/userBenefits'; | ||
import { getProductCatalogFromApi } from '@modules/product-catalog/api'; | ||
import { ProductCatalogHelper } from '@modules/product-catalog/productCatalog'; | ||
import type { Stage } from '@modules/stage'; | ||
import type { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda'; | ||
import { getTrialInformation } from './trials'; | ||
|
||
export const benefitsIdentityIdHandler = ( | ||
const stage = process.env.STAGE as Stage; | ||
const productCatalog = new Lazy( | ||
async () => await getProductCatalogFromApi(stage), | ||
'Get product catalog', | ||
); | ||
|
||
const getUserBenefitsResponse = async ( | ||
stage: Stage, | ||
productCatalogHelper: ProductCatalogHelper, | ||
identityId: string, | ||
): Promise<UserBenefitsResponse> => { | ||
const benefits = await getUserBenefitsExcludingStaff( | ||
stage, | ||
productCatalogHelper, | ||
identityId, | ||
); | ||
console.log(`Benefits for user ${identityId} are: `, benefits); | ||
const trials = getTrialInformation(benefits); | ||
console.log(`Trials for user ${identityId} are: `, trials); | ||
return { | ||
benefits, | ||
trials, | ||
}; | ||
}; | ||
|
||
export const benefitsIdentityIdHandler = async ( | ||
event: APIGatewayProxyEvent, | ||
): Promise<APIGatewayProxyResult> => { | ||
console.log(`Input is ${JSON.stringify(event)}`); | ||
|
||
return Promise.resolve({ | ||
body: JSON.stringify({ test: true }), | ||
const identityId = event.pathParameters?.identityId; | ||
if (!identityId) { | ||
return { | ||
statusCode: 400, | ||
body: JSON.stringify({ | ||
message: 'Identity ID missing from request path', | ||
}), | ||
}; | ||
} | ||
|
||
const userBenefitsResponse = await getUserBenefitsResponse( | ||
stage, | ||
new ProductCatalogHelper(await productCatalog.get()), | ||
identityId, | ||
); | ||
|
||
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, | ||
}); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { getUserBenefitsExcludingStaff } from '@modules/product-benefits/userBenefits'; | ||
import type { ProductCatalogHelper } from '@modules/product-catalog/productCatalog'; | ||
import type { Stage } from '@modules/stage'; | ||
import type { APIGatewayProxyEvent } from 'aws-lambda'; | ||
import { benefitsIdentityIdHandler } from '../src/benefitsIdentityId'; | ||
|
||
jest.mock('@modules/product-catalog/api', () => ({ | ||
getProductCatalogFromApi: () => ({}), | ||
})); | ||
const goodIdentityId = 'good-identity-id'; | ||
jest.mock('@modules/product-benefits/userBenefits', () => ({ | ||
getUserBenefitsExcludingStaff: jest.fn( | ||
( | ||
stage: Stage, | ||
productCatalogHelper: ProductCatalogHelper, | ||
identityId: string, | ||
) => { | ||
if (identityId === 'good-identity-id') { | ||
return ['adFree']; | ||
} else { | ||
return []; | ||
} | ||
}, | ||
), | ||
})); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('benefitsIdentityIdHandler', () => { | ||
it('returns a 400 when the identityId path part is missing', async () => { | ||
const requestEvent = { | ||
path: '/benefits/', | ||
httpMethod: 'GET', | ||
pathParameters: { identityId: undefined }, | ||
} as unknown as APIGatewayProxyEvent; | ||
|
||
const response = await benefitsIdentityIdHandler(requestEvent); | ||
|
||
expect(response.statusCode).toEqual(400); | ||
}); | ||
|
||
it('returns a 200 with benefits retrieved for the identityId', async () => { | ||
const requestEvent = { | ||
path: `/benefits/${goodIdentityId}`, | ||
httpMethod: 'GET', | ||
pathParameters: { identityId: goodIdentityId }, | ||
} as unknown as APIGatewayProxyEvent; | ||
|
||
const response = await benefitsIdentityIdHandler(requestEvent); | ||
|
||
expect(response.statusCode).toEqual(200); | ||
expect(getUserBenefitsExcludingStaff).toHaveBeenCalledWith( | ||
undefined, // stage isn't defined in tests and anything doesn't match undefined | ||
expect.anything(), | ||
goodIdentityId, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters