Skip to content

Commit 8ffd5a2

Browse files
Move supplier id header to config
1 parent 4a10a01 commit 8ffd5a2

File tree

8 files changed

+44
-10
lines changed

8 files changed

+44
-10
lines changed

infrastructure/terraform/components/api/locals.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ locals {
1414
})
1515

1616
destination_arn = "arn:aws:logs:${var.region}:${var.shared_infra_account_id}:destination:nhs-main-obs-firehose-logs"
17+
18+
common_db_access_lambda_env_vars = {
19+
LETTERS_TABLE_NAME = aws_dynamodb_table.letters.name,
20+
LETTER_TTL_HOURS = 24,
21+
SUPPLIER_ID_HEADER = "nhsd-supplier-id"
22+
}
1723
}

infrastructure/terraform/components/api/module_lambda_get_letters.tf

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ module "get_letters" {
3535
log_destination_arn = local.destination_arn
3636
log_subscription_role_arn = local.acct.log_subscription_role_arn
3737

38-
lambda_env_vars = {
39-
LETTERS_TABLE_NAME = aws_dynamodb_table.letters.name,
40-
LETTER_TTL_HOURS = 24
41-
}
38+
lambda_env_vars = merge(locals.common_db_access_lambda_env_vars, {})
4239
}
4340

4441
data "aws_iam_policy_document" "get_letters_lambda" {

infrastructure/terraform/components/api/module_lambda_patch_letters.tf

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ module "patch_letters" {
3535
log_destination_arn = local.destination_arn
3636
log_subscription_role_arn = local.acct.log_subscription_role_arn
3737

38-
lambda_env_vars = {
39-
LETTERS_TABLE_NAME = aws_dynamodb_table.letters.name,
40-
LETTER_TTL_HOURS = 24
41-
}
38+
lambda_env_vars = merge(locals.common_db_access_lambda_env_vars, {})
4239
}
4340

4441
data "aws_iam_policy_document" "patch_letters_lambda" {
@@ -56,7 +53,7 @@ data "aws_iam_policy_document" "patch_letters_lambda" {
5653
]
5754
}
5855

59-
statement {
56+
statement {
6057
sid = "AllowDynamoDBAccess"
6158
effect = "Allow"
6259

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
interface LambdaConfig {
2+
SUPPLIER_ID_HEADER: string;
3+
}
4+
5+
export const lambdaConfig: LambdaConfig = {
6+
SUPPLIER_ID_HEADER: getEnv("SUPPLIER_ID_HEADER")!,
7+
};
8+
9+
function getEnv(name: string, required = true): string | undefined {
10+
const value = process.env[name];
11+
if (!value && required) {
12+
throw new Error(`Missing required env var: ${name}`);
13+
}
14+
return value;
15+
}

lambdas/api-handler/src/handlers/__tests__/get-letters.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import * as letterService from '../../services/letter-operations';
66

77
jest.mock('../../services/letter-operations');
88

9+
jest.mock("../../config/lambda-config", () => ({
10+
lambdaConfig: {
11+
SUPPLIER_ID_HEADER: "nhsd-supplier-id"
12+
}
13+
}));
14+
915
describe('API Lambda handler', () => {
1016

1117
beforeEach(() => {

lambdas/api-handler/src/handlers/__tests__/hello-world.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import { helloWorld } from '../../index';
22
import type { Context } from 'aws-lambda';
33
import { mockDeep } from 'jest-mock-extended';
44

5+
jest.mock("../../config/lambda-config", () => ({
6+
lambdaConfig: {
7+
SUPPLIER_ID_HEADER: "nhsd-supplier-id"
8+
}
9+
}));
10+
511
describe('API Lambda handler', () => {
612
it('returns 200 OK with "Hello World" for the root path', async () => {
713
const event = { path: '/' };

lambdas/api-handler/src/handlers/__tests__/patch-letters.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import { mapErrorToResponse } from '../../mappers/error-mapper';
99
jest.mock('../../services/letter-operations');
1010
jest.mock('../../mappers/error-mapper');
1111

12+
jest.mock("../../config/lambda-config", () => ({
13+
lambdaConfig: {
14+
SUPPLIER_ID_HEADER: "nhsd-supplier-id"
15+
}
16+
}));
17+
1218
const mockedMapErrorToResponse = jest.mocked(mapErrorToResponse);
1319
const expectedErrorResponse: APIGatewayProxyResult = {
1420
statusCode: 400,

lambdas/api-handler/src/handlers/patch-letters.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import { LetterApiDocument, LetterApiDocumentSchema } from '../contracts/letter-
55
import * as errors from '../contracts/errors';
66
import { ValidationError } from '../errors';
77
import { mapErrorToResponse } from '../mappers/error-mapper';
8+
import { lambdaConfig } from "../config/lambda-config";
89

910
const letterRepo = createLetterRepository();
1011
export const patchLetters: APIGatewayProxyHandler = async (event) => {
1112

1213
try {
13-
const supplierId = assertNotEmpty(event.headers['nhsd-supplier-id'], errors.ApiErrorDetail.InvalidRequestMissingSupplierId);
14+
const supplierId = assertNotEmpty(event.headers[lambdaConfig.SUPPLIER_ID_HEADER], errors.ApiErrorDetail.InvalidRequestMissingSupplierId);
1415
const letterId = assertNotEmpty( event.pathParameters?.id, errors.ApiErrorDetail.InvalidRequestMissingLetterIdPathParameter);
1516
const body = assertNotEmpty(event.body, errors.ApiErrorDetail.InvalidRequestMissingBody);
1617

0 commit comments

Comments
 (0)