Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitleaksignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ debc75a97cfe551a69fd1e8694be483213322a9d:pact-contracts/pacts/letter-rendering/s
4fa1923947bbff2387218d698d766cbb7c121a0f:pact-contracts/pacts/letter-rendering/supplier-api-letter-request-prepared.json:generic-api-key:10
d005112adcfd286c3bef076214836dbb2fe8d0b5:.npmrc:npm-access-token:9
d005112adcfd286c3bef076214836dbb2fe8d0b5:.npmrc:github-pat:7
ff889d4c3f29da4468ecf1f05f467fe84d35b2a1:lambdas/supplier-mock/.aws-sam/build/SupplierMockFunction/index.js.map:ipv4:4
ff889d4c3f29da4468ecf1f05f467fe84d35b2a1:lambdas/supplier-mock/.aws-sam/build/SupplierMockFunction/index.js:ipv4:63
ff889d4c3f29da4468ecf1f05f467fe84d35b2a1:lambdas/supplier-mock/.aws-sam/build/SupplierMockFunction/index.js:ipv4:62
ff889d4c3f29da4468ecf1f05f467fe84d35b2a1:lambdas/supplier-mock/.aws-sam/build/SupplierMockFunction/index.js:ipv4:60
ff889d4c3f29da4468ecf1f05f467fe84d35b2a1:lambdas/supplier-mock/.aws-sam/build/SupplierMockFunction/index.js:ipv4:59
ff889d4c3f29da4468ecf1f05f467fe84d35b2a1:lambdas/supplier-mock/.aws-sam/build/SupplierMockFunction/index.js:ipv4:24
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<!-- vale off -->

# NHS Notify Supplier API

[![1. CI/CD pull request](https://github.com/NHSDigital/nhs-notify-supplier-api/actions/workflows/cicd-1-pull-request.yaml/badge.svg)](https://github.com/NHSDigital/nhs-notify-supplier-api/actions/workflows/cicd-1-pull-request.yaml)
Expand Down Expand Up @@ -75,6 +77,7 @@ New developers of the NHS Notify Supplier API should understand the below.

#### Prerequisites and Configuration

- add this line to your `.npmrc` file `//npm.pkg.github.com/:_authToken=<githubSshToken>` (make sure to replace the `githubSshToken` with the actual token value)
- Utilised the devcontainer, for pre reqs and configuration.
- You should open in a devcontainer or a Github workspaces.
- By default it will run `make config` when the container is first setup
Expand Down Expand Up @@ -151,3 +154,5 @@ Import the files into postman
Select a target environment in postman
Run the collection
The collections must be kept in sync manually

<!-- vale on -->
1 change: 1 addition & 0 deletions infrastructure/terraform/components/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ No requirements.
| <a name="module_sqs_letter_updates"></a> [sqs\_letter\_updates](#module\_sqs\_letter\_updates) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/3.0.6/terraform-sqs.zip | n/a |
| <a name="module_sqs_supplier_allocator"></a> [sqs\_supplier\_allocator](#module\_sqs\_supplier\_allocator) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/3.0.6/terraform-sqs.zip | n/a |
| <a name="module_supplier_allocator"></a> [supplier\_allocator](#module\_supplier\_allocator) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.29/terraform-lambda.zip | n/a |
| <a name="module_supplier_mock"></a> [supplier\_mock](#module\_supplier\_mock) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.29/terraform-lambda.zip | n/a |
| <a name="module_supplier_ssl"></a> [supplier\_ssl](#module\_supplier\_ssl) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.26/terraform-ssl.zip | n/a |
| <a name="module_update_letter_queue"></a> [update\_letter\_queue](#module\_update\_letter\_queue) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.29/terraform-lambda.zip | n/a |
| <a name="module_upsert_letter"></a> [upsert\_letter](#module\_upsert\_letter) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.29/terraform-lambda.zip | n/a |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
module "supplier_mock" {
source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.29/terraform-lambda.zip"

function_name = "supplier_mock"
description = "Mock the behaviour of a supplier"

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.supplier_mock_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 = "supplier-mock/dist"
function_include_common = true
handler_function_name = "handler"
runtime = "nodejs22.x"
memory = 512
timeout = 29
log_level = var.log_level

force_lambda_code_deploy = var.force_lambda_code_deploy
enable_lambda_insights = false

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, {
ENVIRONMENT = var.environment
})
}

data "aws_iam_policy_document" "supplier_mock_lambda" {
statement {
sid = "KMSPermissions"
effect = "Allow"

actions = [
"kms:Decrypt",
"kms:GenerateDataKey",
]

resources = [
module.kms.key_arn, ## Requires shared kms module
]
}

statement {
sid = "AllowInvokeLambda"
effect = "Allow"

actions = [
"lambda:InvokeFunction",
]

resources = [
module.get_letters.function_arn,
module.patch_letter.function_arn
]
}
}
5 changes: 5 additions & 0 deletions lambdas/supplier-mock/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
coverage
node_modules
dist
.reports
.aws-sam
5 changes: 5 additions & 0 deletions lambdas/supplier-mock/buildAndRun.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

npm run lambda-build
sam build
sam local invoke SupplierMockFunction --event event.json
3 changes: 3 additions & 0 deletions lambdas/supplier-mock/event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "Vlasios"
}
69 changes: 69 additions & 0 deletions lambdas/supplier-mock/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
export const baseJestConfig = {
preset: "ts-jest",
extensionsToTreatAsEsm: [".ts"],
transform: {
"^.+\\.ts$": [
"ts-jest",
{
useESM: true,
},
],
},
transformIgnorePatterns: [
"node_modules/(?!(@nhsdigital/nhs-notify-event-schemas-supplier-config)/)",
],

// Automatically clear mock calls, instances, contexts and results before every test
clearMocks: true,

// Indicates whether the coverage information should be collected while executing the test
collectCoverage: true,

// The directory where Jest should output its coverage files
coverageDirectory: "./.reports/unit/coverage",

// Indicates which provider should be used to instrument code for coverage
coverageProvider: "babel",

coverageThreshold: {
global: {
branches: 100,
functions: 100,
lines: 100,
statements: -10,
},
},

coveragePathIgnorePatterns: ["/__tests__/"],
testPathIgnorePatterns: [".build"],
testMatch: ["**/?(*.)+(spec|test).[jt]s?(x)"],

// Use this configuration option to add custom reporters to Jest
reporters: [
"default",
[
"jest-html-reporter",
{
pageTitle: "Test Report",
outputPath: "./.reports/unit/test-report.html",
includeFailureMsg: true,
},
],
],

// The test environment that will be used for testing
testEnvironment: "jsdom",
};

const utilsJestConfig = {
...baseJestConfig,

testEnvironment: "node",

coveragePathIgnorePatterns: [
...(baseJestConfig.coveragePathIgnorePatterns ?? []),
"zod-validators.ts",
],
};

export default utilsJestConfig;
21 changes: 21 additions & 0 deletions lambdas/supplier-mock/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"dependencies": {
"@aws-sdk/client-api-gateway": "^3.1030.0",
"@aws-sdk/client-lambda": "^3.1030.0",
"@internal/helpers": "^0.1.0",
"aws-embedded-metrics": "^4.2.1",
"aws-lambda": "^1.0.7",
"pino": "^10.3.1",
"zod": "^4.3.6"
},
"name": "nhs-notify-supplier-api-supplier-mock",
"private": true,
"scripts": {
"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",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"test:unit": "jest",
"typecheck": "tsc --noEmit"
},
"version": "0.0.1"
}
5 changes: 5 additions & 0 deletions lambdas/supplier-mock/src/__tests__/supplier-mock.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe("Supplier Mock Lambda", () => {
test("should return a successful response", async () => {
expect(true).toBe(true);
});
});
55 changes: 55 additions & 0 deletions lambdas/supplier-mock/src/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Logger } from "pino";
import { createLogger } from "@internal/helpers/src";
import { LambdaClient } from "@aws-sdk/client-lambda";
import {
APIGatewayClient,
paginateGetRestApis,
} from "@aws-sdk/client-api-gateway";
import { EnvVars, envVars } from "./env";

export type Deps = {
logger: Logger;
env: EnvVars;
lambdaClient: LambdaClient;
apiClient: APIGatewayClient;
baseUrl: string;
};

export async function createDependenciesContainer(): Promise<Deps> {
const log = createLogger({ logLevel: envVars.PINO_LOG_LEVEL });
const lambdaClient = new LambdaClient();
const apiClient = new APIGatewayClient();
const baseUrl = await getRestApiGatewayBaseUrl(envVars, apiClient);

return {
logger: log,
env: envVars,
lambdaClient,
apiClient,
baseUrl,
};
}

async function getRestApiGatewayBaseUrl(
environment: EnvVars,
apiClient: APIGatewayClient,
): Promise<string> {
console.log(
"VLASIS - about to retrieve API Gateway base URL using API client",
);
// const apiName = `nhs-${environment.ENVIRONMENT}-supapi`;
const apiName = `nhs-pr535-supapi`;
const api = await getApi(apiName, apiClient);
// return `https://${api.id}.execute-api.${environment.AWS_REGION}.amazonaws.com/main`;
return `https://${api.id}.execute-api.eu-west-2.amazonaws.com/main`;
}

async function getApi(apiName: string, client: APIGatewayClient) {
for await (const page of paginateGetRestApis({ client }, {})) {
const filtered = page.items?.filter((api) => api.name === apiName);
if (filtered?.length === 1) {
return filtered[0];
}
}
throw new Error(`API with name "${apiName}" not found.`);
}
11 changes: 11 additions & 0 deletions lambdas/supplier-mock/src/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { z } from "zod";

const EnvVarsSchema = z.object({
PINO_LOG_LEVEL: z.coerce.string().optional(),
ENVIRONMENT: z.string().optional(),
AWS_REGION: z.string().optional(),
});

export type EnvVars = z.infer<typeof EnvVarsSchema>;

export const envVars: EnvVars = EnvVarsSchema.parse(process.env);
7 changes: 7 additions & 0 deletions lambdas/supplier-mock/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createDependenciesContainer } from "./deps";
import createHandler from "./supplier-mock";

const container = createDependenciesContainer();

// eslint-disable-next-line import-x/prefer-default-export
export const handler = createHandler(container);
51 changes: 51 additions & 0 deletions lambdas/supplier-mock/src/supplier-mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Deps } from "./deps";
import { RequestHeaders } from "../../../tests/constants/request-headers";

export default function createHandler(deps: Deps) {
return async function handler() {
deps.logger.info("Hello from the supplier mock lambda!");
// const envName = deps.env.ENVIRONMENT;
const envName = "pr535";
console.log(`Environment: ${envName}`);
// const input: ListFunctionsRequest = {
// MaxItems: 1000,
// };
// const command = new ListFunctionsCommand(input);
// deps.logger.info("VLASIS - Invoking ListFunctionsCommand");
// const response = await deps.lambdaClient.send(command);
// const functions: FunctionConfiguration[] = response.Functions ?? [];
// console.log(
// "list of functions in my environment:",
// functions
// .map((fn) => fn.FunctionName)
// .filter((fnName) => fnName?.includes(envName))
// .join("\n"),
// );

// const getLettersLambdaResponse = await deps.lambdaClient.send(
// new InvokeCommand({
// FunctionName: `nhs-${envName}-supapi-getletters`,
// InvocationType: "RequestResponse",
// Payload: Buffer.from(JSON.stringify({ test: "VLASIS data" })),
// }),
// );

console.log(
"VLASIS - about to make a request to the get letters endpoint of the supplier API",
);
console.log(`Base URL from deps: ${deps.baseUrl}`);
const headers: RequestHeaders = {
"NHSD-Supplier-ID": "TestSupplier1",
"NHSD-Correlation-ID": "12345",
"X-Request-ID": "requestId1",
};

const getLettersResponse = await fetch(`${deps.baseUrl}/letters`, {
method: "GET",
headers,
});
console.log(
`Response from get letters lambda: ${getLettersResponse.status} - ${getLettersResponse.statusText}`,
);
};
}
13 changes: 13 additions & 0 deletions lambdas/supplier-mock/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31

Resources:
SupplierMockFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: supplier_mock
Runtime: nodejs22.x
Handler: index.handler
CodeUri: dist
MemorySize: 512
Timeout: 29
10 changes: 10 additions & 0 deletions lambdas/supplier-mock/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"esModuleInterop": true
},
"extends": "../../tsconfig.base.json",
"include": [
"src/**/*",
"jest.config.ts"
]
}
Loading
Loading