Skip to content

Commit 089eec2

Browse files
committed
CCM-11189 Lambda to POST management information
1 parent ab25c06 commit 089eec2

File tree

20 files changed

+554
-7
lines changed

20 files changed

+554
-7
lines changed

.tool-versions

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ gitleaks 8.24.0
33
jq 1.6
44
nodejs 22.15.0
55
pre-commit 3.6.0
6-
python 3.12.11
6+
python 3.13.2
77
terraform 1.10.1
88
terraform-docs 0.19.0
99
trivy 0.61.0

infrastructure/terraform/components/api/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ No requirements.
3939
| <a name="module_kms"></a> [kms](#module\_kms) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.20/terraform-kms.zip | n/a |
4040
| <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 |
4141
| <a name="module_patch_letter"></a> [patch\_letter](#module\_patch\_letter) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.20/terraform-lambda.zip | n/a |
42+
| <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 |
4243
| <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 |
4344
| <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 |
4445
## Outputs

infrastructure/terraform/components/api/iam_role_api_gateway_execution_role.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ data "aws_iam_policy_document" "api_gateway_execution_policy" {
5151
module.authorizer_lambda.function_arn,
5252
module.get_letters.function_arn,
5353
module.patch_letter.function_arn,
54-
module.get_letter_data.function_arn
54+
module.get_letter_data.function_arn,
55+
module.post_mi.function_arn
5556
]
5657
}
5758
}

infrastructure/terraform/components/api/locals.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ locals {
1111
GET_LETTERS_LAMBDA_ARN = module.get_letters.function_arn
1212
GET_LETTER_DATA_LAMBDA_ARN = module.get_letter_data.function_arn
1313
PATCH_LETTER_LAMBDA_ARN = module.patch_letter.function_arn
14+
POST_MI_LAMBDA_ARN = module.post_mi.function_arn
1415
})
1516

1617
destination_arn = "arn:aws:logs:${var.region}:${var.shared_infra_account_id}:destination:nhs-main-obs-firehose-logs"
1718

1819
common_lambda_env_vars = {
1920
LETTERS_TABLE_NAME = aws_dynamodb_table.letters.name,
21+
MI_TABLE_NAME = aws_dynamodb_table.mi.name,
2022
LETTER_TTL_HOURS = 24,
2123
SUPPLIER_ID_HEADER = "nhsd-supplier-id",
2224
APIM_CORRELATION_HEADER = "nhsd-correlation-id",
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
module "post_mi" {
2+
source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-lambda.zip"
3+
4+
function_name = "post_mi"
5+
description = "Add management information"
6+
7+
aws_account_id = var.aws_account_id
8+
component = var.component
9+
environment = var.environment
10+
project = var.project
11+
region = var.region
12+
group = var.group
13+
14+
log_retention_in_days = var.log_retention_in_days
15+
kms_key_arn = module.kms.key_arn
16+
17+
iam_policy_document = {
18+
body = data.aws_iam_policy_document.post_mi_lambda.json
19+
}
20+
21+
function_s3_bucket = local.acct.s3_buckets["lambda_function_artefacts"]["id"]
22+
function_code_base_path = local.aws_lambda_functions_dir_path
23+
function_code_dir = "api-handler/dist"
24+
function_include_common = true
25+
handler_function_name = "postMi"
26+
runtime = "nodejs22.x"
27+
memory = 128
28+
timeout = 5
29+
log_level = var.log_level
30+
31+
force_lambda_code_deploy = var.force_lambda_code_deploy
32+
enable_lambda_insights = false
33+
34+
send_to_firehose = true
35+
log_destination_arn = local.destination_arn
36+
log_subscription_role_arn = local.acct.log_subscription_role_arn
37+
38+
lambda_env_vars = merge(local.common_lambda_env_vars, {})
39+
}
40+
41+
data "aws_iam_policy_document" "post_mi_lambda" {
42+
statement {
43+
sid = "KMSPermissions"
44+
effect = "Allow"
45+
46+
actions = [
47+
"kms:Decrypt",
48+
"kms:GenerateDataKey",
49+
]
50+
51+
resources = [
52+
module.kms.key_arn, ## Requires shared kms module
53+
]
54+
}
55+
56+
statement {
57+
sid = "AllowDynamoDBAccess"
58+
effect = "Allow"
59+
60+
actions = [
61+
"dynamodb:PutItem",
62+
]
63+
64+
resources = [
65+
aws_dynamodb_table.mi.arn,
66+
]
67+
}
68+
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,45 @@
165165
}
166166
}
167167
]
168+
},
169+
"/mi": {
170+
"post": {
171+
"description": "Provide management information.",
172+
"operationId": "postMi",
173+
"requestBody": {
174+
"required": true
175+
},
176+
"responses": {
177+
"201": {
178+
"description": "Resource created"
179+
},
180+
"400": {
181+
"description": "Bad request, invalid input data"
182+
},
183+
"500": {
184+
"description": "Server error"
185+
}
186+
},
187+
"security": [
188+
{
189+
"LambdaAuthorizer": []
190+
}
191+
],
192+
"x-amazon-apigateway-integration": {
193+
"contentHandling": "CONVERT_TO_TEXT",
194+
"credentials": "${APIG_EXECUTION_ROLE_ARN}",
195+
"httpMethod": "POST",
196+
"passthroughBehavior": "WHEN_NO_TEMPLATES",
197+
"responses": {
198+
".*": {
199+
"statusCode": "200"
200+
}
201+
},
202+
"timeoutInMillis": 29000,
203+
"type": "AWS_PROXY",
204+
"uri": "arn:aws:apigateway:${AWS_REGION}:lambda:path/2015-03-31/functions/${POST_MI_LAMBDA_ARN}/invocations"
205+
}
206+
}
168207
}
169208
}
170209
}

internal/datastore/src/__test__/mi-repository.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { Logger } from "pino";
2-
import { LetterRepository } from "../letter-repository";
32
import { setupDynamoDBContainer, createTables, DBContext, deleteTables } from "./db";
43
import { createTestLogger, LogStream } from "./logs";
54
import { MIRepository } from "../mi-repository";
65

6+
// Database tests can take longer, especially with setup and teardown
7+
jest.setTimeout(30000);
8+
9+
710
describe('MiRepository', () => {
811
let db: DBContext;
912
let miRepository: MIRepository;
@@ -46,6 +49,7 @@ describe('MiRepository', () => {
4649
groupId:'group1',
4750
lineItem: 'item1',
4851
quantity: 12,
52+
timestamp: new Date().toISOString(),
4953
stockRemaining: 0
5054
};
5155

internal/datastore/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './types';
2+
export * from './mi-repository';
23
export * from './letter-repository';
34
export * from './types';

internal/datastore/src/mi-repository.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export class MIRepository {
1717
}
1818

1919
async putMI(mi: Omit<MI, 'id' | 'createdAt' | 'updatedAt'>): Promise<MI> {
20+
2021
const now = new Date().toISOString();
2122
const miDb = {
2223
...mi,

internal/datastore/src/types.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ export type LetterBase = z.infer<typeof LetterSchemaBase>;
4848

4949
export const MISchemaBase = z.object({
5050
id: z.string(),
51-
specificationId: z.string(),
52-
groupId: z.string(),
5351
lineItem: z.string(),
52+
timestamp: z.string(),
5453
quantity: z.number(),
55-
stockRemaining: z.number()
54+
specificationId: z.string().optional(),
55+
groupId: z.string().optional(),
56+
stockRemaining: z.number().optional()
5657
});
5758

5859
export const MISchema = MISchemaBase.extend({

0 commit comments

Comments
 (0)