Skip to content

Commit 940bbdf

Browse files
add supplier mock dummy lambda function
1 parent f713f1a commit 940bbdf

10 files changed

Lines changed: 226 additions & 0 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
module "get_letter" {
2+
source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.29/terraform-lambda.zip"
3+
4+
function_name = "supplier_mock"
5+
description = "Mock the behaviour of a supplier"
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.supplier_mock_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 = "supplier-mock/dist"
24+
function_include_common = true
25+
handler_function_name = "supplierMockHandler" // double check that the handler function name is correct
26+
runtime = "nodejs22.x"
27+
memory = 512
28+
timeout = 29
29+
log_level = var.log_level
30+
31+
force_lambda_code_deploy = var.force_lambda_code_deploy
32+
enable_lambda_insights = false
33+
34+
log_destination_arn = local.destination_arn
35+
log_subscription_role_arn = local.acct.log_subscription_role_arn
36+
37+
lambda_env_vars = merge(local.common_lambda_env_vars, {})
38+
}
39+
40+
// TODO: add lambda invoke permissions
41+
data "aws_iam_policy_document" "supplier_mock_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 = "AllowInvokeLambda"
58+
effect = "Allow"
59+
60+
actions = [
61+
"lambda:InvokeFunction",
62+
]
63+
64+
resources = [
65+
module.get_letters.function_arn,
66+
module.patch_letter.function_arn
67+
]
68+
}
69+
}

lambdas/supplier-mock/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
coverage
2+
node_modules
3+
dist
4+
.reports
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
export const baseJestConfig = {
2+
preset: "ts-jest",
3+
extensionsToTreatAsEsm: [".ts"],
4+
transform: {
5+
"^.+\\.ts$": [
6+
"ts-jest",
7+
{
8+
useESM: true,
9+
},
10+
],
11+
},
12+
transformIgnorePatterns: [
13+
"node_modules/(?!(@nhsdigital/nhs-notify-event-schemas-supplier-config)/)",
14+
],
15+
16+
// Automatically clear mock calls, instances, contexts and results before every test
17+
clearMocks: true,
18+
19+
// Indicates whether the coverage information should be collected while executing the test
20+
collectCoverage: true,
21+
22+
// The directory where Jest should output its coverage files
23+
coverageDirectory: "./.reports/unit/coverage",
24+
25+
// Indicates which provider should be used to instrument code for coverage
26+
coverageProvider: "babel",
27+
28+
coverageThreshold: {
29+
global: {
30+
branches: 100,
31+
functions: 100,
32+
lines: 100,
33+
statements: -10,
34+
},
35+
},
36+
37+
coveragePathIgnorePatterns: ["/__tests__/"],
38+
testPathIgnorePatterns: [".build"],
39+
testMatch: ["**/?(*.)+(spec|test).[jt]s?(x)"],
40+
41+
// Use this configuration option to add custom reporters to Jest
42+
reporters: [
43+
"default",
44+
[
45+
"jest-html-reporter",
46+
{
47+
pageTitle: "Test Report",
48+
outputPath: "./.reports/unit/test-report.html",
49+
includeFailureMsg: true,
50+
},
51+
],
52+
],
53+
54+
// The test environment that will be used for testing
55+
testEnvironment: "jsdom",
56+
};
57+
58+
const utilsJestConfig = {
59+
...baseJestConfig,
60+
61+
testEnvironment: "node",
62+
63+
coveragePathIgnorePatterns: [
64+
...(baseJestConfig.coveragePathIgnorePatterns ?? []),
65+
"zod-validators.ts",
66+
],
67+
};
68+
69+
export default utilsJestConfig;

lambdas/supplier-mock/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"dependencies": {
3+
"@internal/helpers": "^0.1.0",
4+
"aws-embedded-metrics": "^4.2.1",
5+
"aws-lambda": "^1.0.7",
6+
"pino": "^10.3.1",
7+
"zod": "^4.3.6"
8+
},
9+
"name": "nhs-notify-supplier-api-supplier-mock",
10+
"private": true,
11+
"scripts": {
12+
"lambda-build": "rm -rf dist && npx esbuild --bundle --minify --sourcemap --target=es2020 --platform=node --loader:.node=file --entry-names=[name] --outdir=dist src/index.ts",
13+
"lint": "eslint .",
14+
"lint:fix": "eslint . --fix",
15+
"test:unit": "jest",
16+
"typecheck": "tsc --noEmit"
17+
},
18+
"version": "0.0.1"
19+
}

lambdas/supplier-mock/src/deps.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Logger } from "pino";
2+
import { createLogger } from "@internal/helpers/src";
3+
import { EnvVars, envVars } from "./env";
4+
5+
export type Deps = {
6+
logger: Logger;
7+
env: EnvVars;
8+
};
9+
10+
export function createDependenciesContainer(): Deps {
11+
const log = createLogger({ logLevel: envVars.PINO_LOG_LEVEL });
12+
13+
return {
14+
logger: log,
15+
env: envVars,
16+
};
17+
}

lambdas/supplier-mock/src/env.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { z } from "zod";
2+
3+
const EnvVarsSchema = z.object({
4+
PINO_LOG_LEVEL: z.coerce.string().optional(),
5+
});
6+
7+
export type EnvVars = z.infer<typeof EnvVarsSchema>;
8+
9+
export const envVars: EnvVars = EnvVarsSchema.parse(process.env);

lambdas/supplier-mock/src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { createDependenciesContainer } from "./deps";
2+
import createHandler from "./supplier-mock";
3+
4+
const container = createDependenciesContainer();
5+
6+
// eslint-disable-next-line import-x/prefer-default-export
7+
export const handler = createHandler(container);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Deps } from "./deps";
2+
3+
export default function createHandler(deps: Deps) {
4+
return async function handler() {
5+
deps.logger.info("Hello from the supplier mock lambda!");
6+
};
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"esModuleInterop": true
4+
},
5+
"extends": "../../tsconfig.base.json",
6+
"include": [
7+
"src/**/*",
8+
"jest.config.ts"
9+
]
10+
}

package-lock.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)