Skip to content

Commit f05987d

Browse files
Naming and minor refactor
1 parent 4a08c77 commit f05987d

File tree

11 files changed

+44
-49
lines changed

11 files changed

+44
-49
lines changed

infrastructure/terraform/components/api/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ No requirements.
4242
| <a name="module_letter_status_updates_queue"></a> [letter\_status\_updates\_queue](#module\_letter\_status\_updates\_queue) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-sqs.zip | n/a |
4343
| <a name="module_logging_bucket"></a> [logging\_bucket](#module\_logging\_bucket) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.20/terraform-s3bucket.zip | n/a |
4444
| <a name="module_patch_letter"></a> [patch\_letter](#module\_patch\_letter) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-lambda.zip | n/a |
45-
| <a name="module_post_letters_receiver"></a> [post\_letters\_receiver](#module\_post\_letters\_receiver) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-lambda.zip | n/a |
45+
| <a name="module_post_letters"></a> [post\_letters](#module\_post\_letters) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-lambda.zip | n/a |
4646
| <a name="module_post_mi"></a> [post\_mi](#module\_post\_mi) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-lambda.zip | n/a |
4747
| <a name="module_s3bucket_test_letters"></a> [s3bucket\_test\_letters](#module\_s3bucket\_test\_letters) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.20/terraform-s3bucket.zip | n/a |
4848
| <a name="module_supplier_ssl"></a> [supplier\_ssl](#module\_supplier\_ssl) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.20/terraform-ssl.zip | n/a |

infrastructure/terraform/components/api/event_source_mapping_status_updates_to_handler.tf

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,5 @@ resource "aws_lambda_event_source_mapping" "status_updates_sqs_to_status_update_
88
depends_on = [
99
module.letter_status_updates_queue, # ensures queue exists
1010
module.letter_status_update # ensures update handler exists
11-
# aws_iam_role_policy.letter_status_update, # ensures permissions exist?
12-
# aws_iam_role_policy.post_letters_receiver # ensures permissions exist?
1311
]
1412
}

infrastructure/terraform/components/api/iam_role_api_gateway_execution_role.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ data "aws_iam_policy_document" "api_gateway_execution_policy" {
5353
module.get_letter_data.function_arn,
5454
module.get_letters.function_arn,
5555
module.patch_letter.function_arn,
56-
module.post_letters_receiver.function_arn,
56+
module.post_letters.function_arn,
5757
module.post_mi.function_arn
5858
]
5959
}

infrastructure/terraform/components/api/locals.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ locals {
1212
GET_LETTERS_LAMBDA_ARN = module.get_letters.function_arn
1313
GET_LETTER_DATA_LAMBDA_ARN = module.get_letter_data.function_arn
1414
PATCH_LETTER_LAMBDA_ARN = module.patch_letter.function_arn
15-
POST_LETTERS_RECEIVER_LAMBDA_ARN = module.post_letters_receiver.function_arn
15+
POST_LETTERS_LAMBDA_ARN = module.post_letters.function_arn
1616
POST_MI_LAMBDA_ARN = module.post_mi.function_arn
1717
})
1818

infrastructure/terraform/components/api/module_lambda_post_letters_receiver.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
module "post_letters_receiver" {
1+
module "post_letters" {
22
source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-lambda.zip"
33

4-
function_name = "post_letters_receiver"
4+
function_name = "post_letters"
55
description = "Receives and accepts collection of letters to update"
66

77
aws_account_id = var.aws_account_id
@@ -15,14 +15,14 @@ module "post_letters_receiver" {
1515
kms_key_arn = module.kms.key_arn
1616

1717
iam_policy_document = {
18-
body = data.aws_iam_policy_document.post_letters_receiver.json
18+
body = data.aws_iam_policy_document.post_letters.json
1919
}
2020

2121
function_s3_bucket = local.acct.s3_buckets["lambda_function_artefacts"]["id"]
2222
function_code_base_path = local.aws_lambda_functions_dir_path
2323
function_code_dir = "api-handler/dist"
2424
function_include_common = true
25-
handler_function_name = "postLettersReceiver"
25+
handler_function_name = "postLetters"
2626
runtime = "nodejs22.x"
2727
memory = 128
2828
timeout = 5
@@ -41,7 +41,7 @@ module "post_letters_receiver" {
4141
})
4242
}
4343

44-
data "aws_iam_policy_document" "post_letters_receiver" {
44+
data "aws_iam_policy_document" "post_letters" {
4545
statement {
4646
sid = "KMSPermissions"
4747
effect = "Allow"

infrastructure/terraform/components/api/resources/spec.tmpl.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
},
8787
"timeoutInMillis": 29000,
8888
"type": "AWS_PROXY",
89-
"uri": "arn:aws:apigateway:${AWS_REGION}:lambda:path/2015-03-31/functions/${POST_LETTERS_RECEIVER_LAMBDA_ARN}/invocations"
89+
"uri": "arn:aws:apigateway:${AWS_REGION}:lambda:path/2015-03-31/functions/${POST_LETTERS_LAMBDA_ARN}/invocations"
9090
}
9191
}
9292
},

lambdas/api-handler/src/handlers/__tests__/post-letters-receiver.test.ts renamed to lambdas/api-handler/src/handlers/__tests__/post-letters.test.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import pino from 'pino';
2424
import { LetterRepository } from '@internal/datastore/src';
2525
import { EnvVars } from '../../config/env';
2626
import { Deps } from "../../config/deps";
27-
import { createPostLettersReceiverHandler } from '../post-letters-receiver';
27+
import { createPostLettersHandler } from '../post-letters';
2828

2929
const updateLettersRequest : PostLettersRequest = {
3030
data: [
@@ -92,8 +92,8 @@ describe('postLetters API Handler', () => {
9292

9393
mockedBatchUpdateStatus.mockResolvedValue();
9494

95-
const postLettersReceiverHandler = createPostLettersReceiverHandler(mockedDeps);
96-
const result = await postLettersReceiverHandler(event, context, callback);
95+
const postLettersHandler = createPostLettersHandler(mockedDeps);
96+
const result = await postLettersHandler(event, context, callback);
9797

9898
expect(result).toEqual({
9999
statusCode: 202,
@@ -113,8 +113,8 @@ describe('postLetters API Handler', () => {
113113
const context = mockDeep<Context>();
114114
const callback = jest.fn();
115115

116-
const postLettersReceiverHandler = createPostLettersReceiverHandler(mockedDeps);
117-
const result = await postLettersReceiverHandler(event, context, callback);
116+
const postLettersHandler = createPostLettersHandler(mockedDeps);
117+
const result = await postLettersHandler(event, context, callback);
118118

119119
expect(mockedProcessError).toHaveBeenCalledWith(new Error('The supplier ID is missing from the request'), 'correlationId', mockedDeps.logger);
120120
expect(result).toEqual(expectedErrorResponse);
@@ -132,8 +132,8 @@ describe('postLetters API Handler', () => {
132132
const context = mockDeep<Context>();
133133
const callback = jest.fn();
134134

135-
const postLettersReceiverHandler = createPostLettersReceiverHandler(mockedDeps);
136-
const result = await postLettersReceiverHandler(event, context, callback);
135+
const postLettersHandler = createPostLettersHandler(mockedDeps);
136+
const result = await postLettersHandler(event, context, callback);
137137

138138
expect(mockedProcessError).toHaveBeenCalledWith(new Error("The request headers don't contain the APIM correlation id"), undefined, mockedDeps.logger);
139139
expect(result).toEqual(expectedErrorResponse);
@@ -151,8 +151,8 @@ describe('postLetters API Handler', () => {
151151
const context = mockDeep<Context>();
152152
const callback = jest.fn();
153153

154-
const postLettersReceiverHandler = createPostLettersReceiverHandler(mockedDeps);
155-
const result = await postLettersReceiverHandler(event, context, callback);
154+
const postLettersHandler = createPostLettersHandler(mockedDeps);
155+
const result = await postLettersHandler(event, context, callback);
156156

157157
expect(mockedProcessError).toHaveBeenCalledWith(new ValidationError(errors.ApiErrorDetail.InvalidRequestMissingBody), 'correlationId', mockedDeps.logger);
158158
expect(result).toEqual(expectedErrorResponse);
@@ -171,8 +171,8 @@ describe('postLetters API Handler', () => {
171171
const context = mockDeep<Context>();
172172
const callback = jest.fn();
173173

174-
const postLettersReceiverHandler = createPostLettersReceiverHandler(mockedDeps);
175-
const result = await postLettersReceiverHandler(event, context, callback);
174+
const postLettersHandler = createPostLettersHandler(mockedDeps);
175+
const result = await postLettersHandler(event, context, callback);
176176

177177
expect(mockedProcessError).toHaveBeenCalledWith(new ValidationError(errors.ApiErrorDetail.InvalidRequestBody), 'correlationId', mockedDeps.logger);
178178
expect(result).toEqual(expectedErrorResponse);
@@ -202,8 +202,8 @@ describe('postLetters API Handler', () => {
202202
const context = mockDeep<Context>();
203203
const callback = jest.fn();
204204

205-
const postLettersReceiverHandler = createPostLettersReceiverHandler(mockedDeps);
206-
const result = await postLettersReceiverHandler(event, context, callback);
205+
const postLettersHandler = createPostLettersHandler(mockedDeps);
206+
const result = await postLettersHandler(event, context, callback);
207207

208208
expect(mockedProcessError).toHaveBeenCalledWith(new ValidationError(errors.ApiErrorDetail.InvalidRequestLettersToUpdate, { args: [mockedDeps.env.MAX_LIMIT]}), 'correlationId', mockedDeps.logger);
209209
expect(result).toEqual(expectedErrorResponse);
@@ -222,8 +222,8 @@ describe('postLetters API Handler', () => {
222222
const context = mockDeep<Context>();
223223
const callback = jest.fn();
224224

225-
const postLettersReceiverHandler = createPostLettersReceiverHandler(mockedDeps);
226-
const result = await postLettersReceiverHandler(event, context, callback);
225+
const postLettersHandler = createPostLettersHandler(mockedDeps);
226+
const result = await postLettersHandler(event, context, callback);
227227

228228
expect(mockedProcessError).toHaveBeenCalledWith(new ValidationError(errors.ApiErrorDetail.InvalidRequestBody), 'correlationId', mockedDeps.logger);
229229
expect(result).toEqual(expectedErrorResponse);
@@ -247,8 +247,8 @@ describe('postLetters API Handler', () => {
247247
throw error;
248248
});
249249

250-
const postLettersReceiverHandler = createPostLettersReceiverHandler(mockedDeps);
251-
const result = await postLettersReceiverHandler(event, context, callback);
250+
const postLettersHandler = createPostLettersHandler(mockedDeps);
251+
const result = await postLettersHandler(event, context, callback);
252252

253253
expect(mockedProcessError).toHaveBeenCalledWith(error, 'correlationId', mockedDeps.logger);
254254
expect(result).toEqual(expectedErrorResponse);

lambdas/api-handler/src/handlers/__tests__/utils/test-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { APIGatewayProxyEvent, SQSEvent } from 'aws-lambda';
1+
import type { APIGatewayProxyEvent } from 'aws-lambda';
22

33
export function makeApiGwEvent(
44
overrides: Partial<APIGatewayProxyEvent> = {}

lambdas/api-handler/src/handlers/letter-status-update.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,20 @@ import { Deps } from '../config/deps';
55
export function createLetterStatusUpdateHandler(deps: Deps): SQSHandler {
66

77
return async ( event: SQSEvent ) => {
8-
for (const message of event.Records) {
9-
await processMessageAsync(message, deps);
10-
}
11-
}
12-
}
13-
14-
async function processMessageAsync(message: SQSRecord, deps: Deps): Promise<any> {
8+
const tasks = event.Records.map( async (message) => {
9+
try {
10+
const letterToUpdate: LetterDto = LetterDtoSchema.parse(JSON.parse(message.body));
11+
await deps.letterRepo.updateLetterStatus(letterToUpdate);
12+
} catch (error) {
13+
deps.logger.error({
14+
err: error,
15+
messageId: message.messageId,
16+
correlationId: message.messageAttributes['CorrelationId'].stringValue,
17+
messageBody: message.body
18+
}, 'Error processing letter status update');
19+
}
20+
});
1521

16-
try {
17-
const letterToUpdate: LetterDto = LetterDtoSchema.parse(JSON.parse(message.body));
18-
await deps.letterRepo.updateLetterStatus(letterToUpdate);
19-
} catch (error) {
20-
deps.logger.error({
21-
err: error,
22-
messageId: message.messageId,
23-
correlationId: message.messageAttributes['CorrelationId'].stringValue,
24-
messageBody: message.body
25-
}, 'Error processing letter status update');
22+
await Promise.all(tasks);
2623
}
2724
}

lambdas/api-handler/src/handlers/post-letters-receiver.ts renamed to lambdas/api-handler/src/handlers/post-letters.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { processError } from '../mappers/error-mapper';
77
import { assertNotEmpty, requireEnvVar, validateCommonHeaders } from '../utils/validation';
88
import type { Deps } from "../config/deps";
99

10-
export function createPostLettersReceiverHandler(deps: Deps): APIGatewayProxyHandler {
10+
export function createPostLettersHandler(deps: Deps): APIGatewayProxyHandler {
1111

1212
return async (event) => {
1313

0 commit comments

Comments
 (0)