generated from NHSDigital/nhs-notify-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
CCM-11603: Get letter data #188
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
40 commits
Select commit
Hold shift + click to select a range
e7ea643
Change env var name
francisco-videira-nhs 6f006a7
fix env var name
francisco-videira-nhs 6e5bf07
Merge remote-tracking branch 'origin/main' into feature/CCM-11188_2
francisco-videira-nhs c94892f
Refactor contracts and mappers
francisco-videira-nhs 094289f
Merge remote-tracking branch 'origin/main' into feature/CCM-11188_2
francisco-videira-nhs 989792f
Fix invalid error message
francisco-videira-nhs 681ee2b
Clean up updateLetterStatus
francisco-videira-nhs 8a8bcc4
Merge remote-tracking branch 'origin/main' into feature/CCM-11188_2
francisco-videira-nhs 3a5da13
patchLetters to patchLetter
francisco-videira-nhs e2ae5fb
Fix sonar issues
francisco-videira-nhs 520be45
Read headers in lower case
francisco-videira-nhs 813c409
wip
francisco-videira-nhs 0d5b6d6
wip
francisco-videira-nhs ee5c3ea
Merge remote-tracking branch 'origin/main' into feature/CCM-11603
francisco-videira-nhs 933524f
Add tf and tests
francisco-videira-nhs a39a042
Merge remote-tracking branch 'origin/main' into feature/CCM-11603
francisco-videira-nhs 40f7580
Fix tf
francisco-videira-nhs a38c8f8
Fix spec and return code
francisco-videira-nhs d670dbb
Fix spec
francisco-videira-nhs b879ed4
Fix spec
francisco-videira-nhs f4ed8d2
Fix tf
francisco-videira-nhs f2cf557
Fix export
francisco-videira-nhs 95bc137
Fix location header
francisco-videira-nhs d1e5da3
Fix tests and tf
francisco-videira-nhs 5bfa8dd
Add aws cli in devcontainer
francisco-videira-nhs 0c80eed
Merge remote-tracking branch 'origin/main' into feature/CCM-11603
francisco-videira-nhs d93c073
Improve dependency injection
francisco-videira-nhs 957c732
Fix sonar attempt
francisco-videira-nhs aa36690
Add tests for sonar coverage
francisco-videira-nhs 8fcb28f
Add url ttl as config
francisco-videira-nhs b7e6fc6
Add peer review suggestions
francisco-videira-nhs f8881cb
Merge remote-tracking branch 'origin/main' into feature/CCM-11603
francisco-videira-nhs bfdc5fd
Remove non null assertion from env
francisco-videira-nhs fbb28c0
Fix lambdas ddb permissions
francisco-videira-nhs a4c7f26
Add dependency container
francisco-videira-nhs d37e735
Bump shared lambda module
francisco-videira-nhs 7d9a3f2
Refactor header validation
francisco-videira-nhs 1d0a6a5
Merge remote-tracking branch 'origin/main' into feature/CCM-11603
francisco-videira-nhs b91e208
Last peer review actions
francisco-videira-nhs f2928fd
Merge remote-tracking branch 'origin/main' into feature/CCM-11603
francisco-videira-nhs 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
2 changes: 1 addition & 1 deletion
2
infrastructure/terraform/components/api/module_authorizer_lambda.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
78 changes: 78 additions & 0 deletions
78
infrastructure/terraform/components/api/module_lambda_get_letter_data.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,78 @@ | ||
| module "get_letter_data" { | ||
| source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-lambda.zip" | ||
|
|
||
| function_name = "get_letter_data" | ||
| description = "Get the letter data" | ||
|
|
||
| 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_data_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 = "getLetterData" | ||
| 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_data_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, | ||
| "${aws_dynamodb_table.letters.arn}/index/supplierStatus-index" | ||
| ] | ||
| } | ||
|
|
||
| statement { | ||
| sid = "S3GetObjectForPresign" | ||
| actions = [ | ||
| "s3:GetObject", | ||
| "s3:ListBucket"] # allows 404 response instead of 403 if object missing | ||
| resources = ["${module.s3bucket_test_letters.arn}/*"] | ||
| } | ||
| } | ||
2 changes: 1 addition & 1 deletion
2
infrastructure/terraform/components/api/module_lambda_get_letters.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
2 changes: 1 addition & 1 deletion
2
infrastructure/terraform/components/api/module_lambda_patch_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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
|
|
||
| import type { Deps } from '../deps'; | ||
|
|
||
| describe('createDependenciesContainer', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| jest.resetModules(); | ||
|
|
||
| // pino | ||
| jest.mock('pino', () => ({ | ||
| __esModule: true, | ||
| default: jest.fn(() => ({ | ||
| info: jest.fn(), | ||
| error: jest.fn(), | ||
| warn: jest.fn(), | ||
| debug: jest.fn(), | ||
| })), | ||
| })); | ||
|
|
||
| jest.mock('@aws-sdk/client-s3', () => ({ | ||
| S3Client: jest.fn(), | ||
| })); | ||
|
|
||
| // Repo client | ||
| jest.mock('../../../../../internal/datastore', () => ({ | ||
| LetterRepository: jest.fn(), | ||
| })); | ||
|
|
||
| // Env | ||
| jest.mock('../env', () => ({ | ||
| envVars: { | ||
| LETTERS_TABLE_NAME: 'LettersTable', | ||
| LETTER_TTL_HOURS: 12960, | ||
| SUPPLIER_ID_HEADER: 'nhsd-supplier-id', | ||
| APIM_CORRELATION_HEADER: 'nhsd-correlation-id', | ||
| DOWNLOAD_URL_TTL_SECONDS: 60 | ||
| }, | ||
| })); | ||
| }); | ||
|
|
||
| test('constructs deps and wires repository config correctly', async () => { | ||
| // get current mock instances | ||
| const { S3Client } = jest.requireMock('@aws-sdk/client-s3') as { S3Client: jest.Mock }; | ||
| const pinoMock = jest.requireMock('pino') as { default: jest.Mock }; | ||
| const { LetterRepository } = jest.requireMock('../../../../../internal/datastore') as { LetterRepository: jest.Mock }; | ||
|
|
||
| const { createDependenciesContainer } = require('../deps'); | ||
| const deps: Deps = createDependenciesContainer(); | ||
|
|
||
| expect(S3Client).toHaveBeenCalledTimes(1); | ||
| expect(pinoMock.default).toHaveBeenCalledTimes(1); | ||
|
|
||
| expect(LetterRepository).toHaveBeenCalledTimes(1); | ||
| const repoCtorArgs = (LetterRepository as jest.Mock).mock.calls[0]; | ||
| expect(repoCtorArgs[2]).toEqual({ | ||
| lettersTableName: 'LettersTable', | ||
| ttlHours: 12960 | ||
| }); | ||
|
|
||
| expect(deps.env).toEqual({ | ||
| LETTERS_TABLE_NAME: 'LettersTable', | ||
| LETTER_TTL_HOURS: 12960, | ||
| SUPPLIER_ID_HEADER: 'nhsd-supplier-id', | ||
| APIM_CORRELATION_HEADER: 'nhsd-correlation-id', | ||
| DOWNLOAD_URL_TTL_SECONDS: 60 | ||
| }); | ||
| }); | ||
| }); |
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,63 @@ | ||
| import { ZodError } from 'zod'; | ||
|
|
||
| describe('lambdaEnv', () => { | ||
| const OLD_ENV = process.env; | ||
|
|
||
| beforeEach(() => { | ||
| jest.resetModules(); // Clears cached modules | ||
| process.env = { ...OLD_ENV }; // Clone original env | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| process.env = OLD_ENV; // Restore | ||
| }); | ||
|
|
||
| it('should load all environment variables successfully', () => { | ||
| process.env.SUPPLIER_ID_HEADER = 'nhsd-supplier-id'; | ||
| process.env.APIM_CORRELATION_HEADER = 'nhsd-correlation-id'; | ||
| process.env.LETTERS_TABLE_NAME = 'letters-table'; | ||
| process.env.LETTER_TTL_HOURS = '12960'; | ||
| process.env.DOWNLOAD_URL_TTL_SECONDS = '60'; | ||
| process.env.MAX_LIMIT = '2500'; | ||
|
|
||
| const { envVars } = require('../env'); | ||
|
|
||
| expect(envVars).toEqual({ | ||
| SUPPLIER_ID_HEADER: 'nhsd-supplier-id', | ||
| APIM_CORRELATION_HEADER: 'nhsd-correlation-id', | ||
| LETTERS_TABLE_NAME: 'letters-table', | ||
| LETTER_TTL_HOURS: 12960, | ||
| DOWNLOAD_URL_TTL_SECONDS: 60, | ||
| MAX_LIMIT: 2500, | ||
| }); | ||
| }); | ||
|
|
||
| it('should throw if a required env var is missing', () => { | ||
| process.env.SUPPLIER_ID_HEADER = 'nhsd-supplier-id'; | ||
| process.env.APIM_CORRELATION_HEADER = 'nhsd-correlation-id'; | ||
| process.env.LETTERS_TABLE_NAME = undefined; // simulate missing var | ||
| process.env.LETTER_TTL_HOURS = '12960'; | ||
| process.env.DOWNLOAD_URL_TTL_SECONDS = '60'; | ||
|
|
||
| expect(() => require('../env')).toThrow(ZodError); | ||
| }); | ||
|
|
||
| it('should not throw if optional are not set', () => { | ||
| process.env.SUPPLIER_ID_HEADER = 'nhsd-supplier-id'; | ||
| process.env.APIM_CORRELATION_HEADER = 'nhsd-correlation-id'; | ||
| process.env.LETTERS_TABLE_NAME = 'letters-table'; | ||
| process.env.LETTER_TTL_HOURS = '12960'; | ||
| process.env.DOWNLOAD_URL_TTL_SECONDS = '60'; | ||
|
|
||
| const { envVars } = require('../env'); | ||
|
|
||
| expect(envVars).toEqual({ | ||
| SUPPLIER_ID_HEADER: 'nhsd-supplier-id', | ||
| APIM_CORRELATION_HEADER: 'nhsd-correlation-id', | ||
| LETTERS_TABLE_NAME: 'letters-table', | ||
| LETTER_TTL_HOURS: 12960, | ||
| DOWNLOAD_URL_TTL_SECONDS: 60, | ||
| MAX_LIMIT: undefined | ||
| }); | ||
| }); | ||
| }); |
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,36 @@ | ||
| import { S3Client } from "@aws-sdk/client-s3"; | ||
| import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; | ||
| import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb'; | ||
| import pino from 'pino'; | ||
| import { LetterRepository } from '../../../../internal/datastore'; | ||
| import { envVars, EnvVars } from "../config/env"; | ||
|
|
||
| export type Deps = { | ||
| s3Client: S3Client; | ||
| letterRepo: LetterRepository; | ||
| logger: pino.Logger, | ||
| env: EnvVars | ||
| }; | ||
|
|
||
| function createLetterRepository(log: pino.Logger, envVars: EnvVars): LetterRepository { | ||
| const ddbClient = new DynamoDBClient({}); | ||
| const docClient = DynamoDBDocumentClient.from(ddbClient); | ||
| const config = { | ||
| lettersTableName: envVars.LETTERS_TABLE_NAME, | ||
| ttlHours: envVars.LETTER_TTL_HOURS | ||
| }; | ||
|
|
||
| return new LetterRepository(docClient, log, config); | ||
| } | ||
|
|
||
| export function createDependenciesContainer(): Deps { | ||
|
|
||
| const log = pino(); | ||
|
|
||
| return { | ||
| s3Client: new S3Client(), | ||
| letterRepo: createLetterRepository(log, envVars), | ||
| logger: log, | ||
| env: envVars | ||
| }; | ||
| } |
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.