-
Notifications
You must be signed in to change notification settings - Fork 2
Added get status endpoint for single letter #193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
69 changes: 69 additions & 0 deletions
69
infrastructure/terraform/components/api/module_lambda_get_letter.tf
This file contains hidden or 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,69 @@ | ||
| module "get_letter" { | ||
| source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-lambda.zip" | ||
|
|
||
| function_name = "get_letter" | ||
| description = "Get letter status" | ||
|
|
||
| aws_account_id = var.aws_account_id | ||
| component = var.component | ||
| environment = var.environment | ||
| project = var.project | ||
| region = var.region | ||
| group = var.group | ||
|
|
||
| log_retention_in_days = var.log_retention_in_days | ||
| kms_key_arn = module.kms.key_arn | ||
|
|
||
| iam_policy_document = { | ||
| body = data.aws_iam_policy_document.get_letter_lambda.json | ||
| } | ||
|
|
||
| function_s3_bucket = local.acct.s3_buckets["lambda_function_artefacts"]["id"] | ||
| function_code_base_path = local.aws_lambda_functions_dir_path | ||
| function_code_dir = "api-handler/dist" | ||
| function_include_common = true | ||
| handler_function_name = "getLetter" | ||
| runtime = "nodejs22.x" | ||
| memory = 128 | ||
| timeout = 5 | ||
| log_level = var.log_level | ||
|
|
||
| force_lambda_code_deploy = var.force_lambda_code_deploy | ||
| enable_lambda_insights = false | ||
|
|
||
| send_to_firehose = true | ||
| log_destination_arn = local.destination_arn | ||
| log_subscription_role_arn = local.acct.log_subscription_role_arn | ||
|
|
||
| lambda_env_vars = merge(local.common_lambda_env_vars, {}) | ||
| } | ||
|
|
||
| data "aws_iam_policy_document" "get_letter_lambda" { | ||
| statement { | ||
| sid = "KMSPermissions" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "kms:Decrypt", | ||
| "kms:GenerateDataKey", | ||
| ] | ||
|
|
||
| resources = [ | ||
| module.kms.key_arn, ## Requires shared kms module | ||
| ] | ||
| } | ||
|
|
||
| statement { | ||
| sid = "AllowDynamoDBAccess" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "dynamodb:GetItem", | ||
| "dynamodb:Query" | ||
| ] | ||
|
|
||
| resources = [ | ||
| aws_dynamodb_table.letters.arn | ||
| ] | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
169 changes: 169 additions & 0 deletions
169
lambdas/api-handler/src/handlers/__tests__/get-letter.test.ts
This file contains hidden or 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,169 @@ | ||
| import { Context } from 'aws-lambda'; | ||
| import { mockDeep } from 'jest-mock-extended'; | ||
| import * as letterService from '../../services/letter-operations'; | ||
| import { makeApiGwEvent } from './utils/test-utils'; | ||
| import { ApiErrorDetail } from '../../contracts/errors'; | ||
| import { NotFoundError } from '../../errors'; | ||
| import { S3Client } from '@aws-sdk/client-s3'; | ||
| import pino from 'pino'; | ||
| import { LetterRepository } from '../../../../../internal/datastore/src'; | ||
| import { Deps } from '../../config/deps'; | ||
| import { EnvVars } from '../../config/env'; | ||
| import { createGetLetterHandler } from '../get-letter'; | ||
|
|
||
| jest.mock('../../services/letter-operations'); | ||
|
|
||
|
|
||
| describe('API Lambda handler', () => { | ||
|
|
||
| const mockedDeps: jest.Mocked<Deps> = { | ||
| s3Client: {} as unknown as S3Client, | ||
| letterRepo: {} as unknown as LetterRepository, | ||
| logger: { info: jest.fn(), error: jest.fn() } as unknown as pino.Logger, | ||
| env: { | ||
| SUPPLIER_ID_HEADER: 'nhsd-supplier-id', | ||
| APIM_CORRELATION_HEADER: 'nhsd-correlation-id', | ||
| LETTERS_TABLE_NAME: 'LETTERS_TABLE_NAME', | ||
| LETTER_TTL_HOURS: 12960, | ||
| DOWNLOAD_URL_TTL_SECONDS: 60, | ||
| MAX_LIMIT: 2500 | ||
| } as unknown as EnvVars | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| jest.resetModules(); | ||
| }); | ||
|
|
||
| it('returns 200 OK and the letter status', async () => { | ||
|
|
||
| const mockedGetLetterById = letterService.getLetterById as jest.Mock; | ||
| mockedGetLetterById.mockResolvedValue({ | ||
| id: 'id1', | ||
| specificationId: 'spec1', | ||
| groupId: 'group1', | ||
| status: 'PENDING' | ||
| }); | ||
|
|
||
| const event = makeApiGwEvent({path: '/letters/id1', | ||
| headers: {'nhsd-supplier-id': 'supplier1', 'nhsd-correlation-id': 'correlationId', 'x-request-id': 'requestId'}, | ||
| pathParameters: {id: 'id1'}}); | ||
|
|
||
| const getLetter = createGetLetterHandler(mockedDeps); | ||
| const result = await getLetter(event, mockDeep<Context>(), jest.fn()); | ||
|
|
||
| const expected = { | ||
| data: { | ||
| id: 'id1', | ||
| type: 'Letter', | ||
| attributes: { | ||
| status: 'PENDING', | ||
| specificationId: 'spec1', | ||
| groupId: 'group1' | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| expect(result).toEqual({ | ||
| statusCode: 200, | ||
| body: JSON.stringify(expected, null, 2), | ||
| }); | ||
| }); | ||
|
|
||
| it('includes the reason code and reason text if present', async () => { | ||
|
|
||
| const mockedGetLetterById = letterService.getLetterById as jest.Mock; | ||
| mockedGetLetterById.mockResolvedValue({ | ||
| id: 'id1', | ||
| specificationId: 'spec1', | ||
| groupId: 'group1', | ||
| status: 'FAILED', | ||
| reasonCode: 100, | ||
| reasonText: 'failed validation' | ||
| }); | ||
|
|
||
| const event = makeApiGwEvent({path: '/letters/id1', | ||
| headers: {'nhsd-supplier-id': 'supplier1', 'nhsd-correlation-id': 'correlationId', 'x-request-id': 'requestId'}, | ||
| pathParameters: {id: 'id1'}}); | ||
|
|
||
| const getLetter = createGetLetterHandler(mockedDeps); | ||
| const result = await getLetter(event, mockDeep<Context>(), jest.fn()); | ||
|
|
||
| const expected = { | ||
| data: { | ||
| id: 'id1', | ||
| type: 'Letter', | ||
| attributes: { | ||
| status: 'FAILED', | ||
| specificationId: 'spec1', | ||
| groupId: 'group1', | ||
| reasonCode: 100, | ||
| reasonText: 'failed validation' | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| expect(result).toEqual({ | ||
| statusCode: 200, | ||
| body: JSON.stringify(expected, null, 2), | ||
| }); | ||
| }); | ||
|
|
||
| it('returns 404 Not Found when letter matching id is not found', async () => { | ||
|
|
||
| const mockedGetLetterById = letterService.getLetterById as jest.Mock; | ||
| mockedGetLetterById.mockImplementation(() => { | ||
| throw new NotFoundError(ApiErrorDetail.NotFoundLetterId); | ||
| }); | ||
|
|
||
| const event = makeApiGwEvent({path: '/letters/id1', | ||
| headers: {'nhsd-supplier-id': 'supplier1', 'nhsd-correlation-id': 'correlationId', 'x-request-id': 'requestId'}, | ||
| pathParameters: {id: 'id1'}}); | ||
|
|
||
| const getLetter = createGetLetterHandler(mockedDeps); | ||
| const result = await getLetter(event, mockDeep<Context>(), jest.fn()); | ||
|
|
||
| expect(result).toEqual(expect.objectContaining({ | ||
| statusCode: 404, | ||
| })); | ||
| }); | ||
|
|
||
| it ('returns 500 when correlation id is missing from header', async() => { | ||
| const event = makeApiGwEvent({path: '/letters/id1', | ||
| headers: {'nhsd-supplier-id': 'supplier1', 'x-request-id': 'requestId'}, | ||
| pathParameters: {id: 'id1'}}); | ||
|
|
||
| const getLetter = createGetLetterHandler(mockedDeps); | ||
| const result = await getLetter(event, mockDeep<Context>(), jest.fn()); | ||
|
|
||
| expect(result).toEqual(expect.objectContaining({ | ||
| statusCode: 500, | ||
| })); | ||
| }); | ||
|
|
||
| it ('returns 500 when supplier id is missing from header', async() => { | ||
| const event = makeApiGwEvent({path: '/letters/id1', | ||
| headers: {'nhsd-correlation-id': 'correlationId', 'x-request-id': 'requestId'}, | ||
| pathParameters: {id: 'id1'}}); | ||
|
|
||
| const getLetter = createGetLetterHandler(mockedDeps); | ||
| const result = await getLetter(event, mockDeep<Context>(), jest.fn()); | ||
|
|
||
| expect(result).toEqual(expect.objectContaining({ | ||
| statusCode: 500, | ||
| })); | ||
| }); | ||
|
|
||
|
|
||
| it ('returns 400 when letter id is missing from path', async() => { | ||
| const event = makeApiGwEvent({path: '/letters/id1', | ||
| headers: {'nhsd-supplier-id': 'supplier1', 'nhsd-correlation-id': 'correlationId', 'x-request-id': 'requestId'}}); | ||
|
|
||
| const getLetter = createGetLetterHandler(mockedDeps); | ||
| const result = await getLetter(event, mockDeep<Context>(), jest.fn()); | ||
|
|
||
| expect(result).toEqual(expect.objectContaining({ | ||
| statusCode: 400, | ||
| })); | ||
| }); | ||
| }); |
This file contains hidden or 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,43 @@ | ||
| import { APIGatewayProxyHandler } from "aws-lambda"; | ||
| import { assertNotEmpty, validateCommonHeaders } from "../utils/validation"; | ||
| import { ValidationError } from "../errors"; | ||
| import { ApiErrorDetail } from "../contracts/errors"; | ||
| import { getLetterById } from "../services/letter-operations"; | ||
| import { mapErrorToResponse } from "../mappers/error-mapper"; | ||
| import { mapToGetLetterResponse } from "../mappers/letter-mapper"; | ||
| import { Deps } from "../config/deps"; | ||
|
|
||
|
|
||
| export function createGetLetterHandler(deps: Deps): APIGatewayProxyHandler { | ||
|
|
||
| return async (event) => { | ||
|
|
||
| const commonHeadersResult = validateCommonHeaders(event.headers, deps); | ||
|
|
||
| if (!commonHeadersResult.ok) { | ||
| return mapErrorToResponse(commonHeadersResult.error, commonHeadersResult.correlationId, deps.logger); | ||
| } | ||
|
|
||
| try { | ||
| const letterId = assertNotEmpty(event.pathParameters?.id, new ValidationError(ApiErrorDetail.InvalidRequestMissingLetterIdPathParameter)); | ||
|
|
||
| const letter = await getLetterById(commonHeadersResult.value.supplierId, letterId, deps.letterRepo); | ||
|
|
||
| const response = mapToGetLetterResponse(letter); | ||
|
|
||
| deps.logger.info({ | ||
| description: 'Letter successfully fetched by id', | ||
| supplierId: commonHeadersResult.value.supplierId, | ||
| letterId | ||
stevebux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| return { | ||
| statusCode: 200, | ||
| body: JSON.stringify(response, null, 2), | ||
| }; | ||
| } catch (error) | ||
| { | ||
| return mapErrorToResponse(error, commonHeadersResult.value.correlationId, deps.logger); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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,10 +1,12 @@ | ||
| import { createDependenciesContainer } from "./config/deps"; | ||
| import { createGetLetterHandler } from "./handlers/get-letter"; | ||
| import { createGetLetterDataHandler } from "./handlers/get-letter-data"; | ||
| import { createGetLettersHandler } from "./handlers/get-letters"; | ||
| import { createPatchLetterHandler } from "./handlers/patch-letter"; | ||
|
|
||
| const container = createDependenciesContainer(); | ||
|
|
||
| export const getLetter = createGetLetterHandler(container); | ||
| export const getLetterData = createGetLetterDataHandler(container); | ||
| export const getLetters = createGetLettersHandler(container); | ||
| export const patchLetter = createPatchLetterHandler(container); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.