Skip to content

Commit 99749c3

Browse files
committed
CCM-11602: MAX LIMIT envar and added into test context
1 parent 3b893c6 commit 99749c3

File tree

5 files changed

+44
-21
lines changed

5 files changed

+44
-21
lines changed

infrastructure/terraform/components/api/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ No requirements.
1212
| <a name="input_aws_account_id"></a> [aws\_account\_id](#input\_aws\_account\_id) | The AWS Account ID (numeric) | `string` | n/a | yes |
1313
| <a name="input_ca_pem_filename"></a> [ca\_pem\_filename](#input\_ca\_pem\_filename) | Filename for the CA truststore file within the s3 bucket | `string` | `null` | no |
1414
| <a name="input_component"></a> [component](#input\_component) | The variable encapsulating the name of this component | `string` | `"supapi"` | no |
15-
| <a name="input_default_get_limit"></a> [default\_get\_limit](#input\_default\_get\_limit) | Default limit to apply to GET requests that support pagination | `number` | `2500` | no |
1615
| <a name="input_default_tags"></a> [default\_tags](#input\_default\_tags) | A map of default tags to apply to all taggable resources within the component | `map(string)` | `{}` | no |
1716
| <a name="input_enable_backups"></a> [enable\_backups](#input\_enable\_backups) | Enable backups | `bool` | `false` | no |
1817
| <a name="input_environment"></a> [environment](#input\_environment) | The name of the tfscaffold environment | `string` | n/a | yes |
@@ -23,6 +22,7 @@ No requirements.
2322
| <a name="input_log_level"></a> [log\_level](#input\_log\_level) | The log level to be used in lambda functions within the component. Any log with a lower severity than the configured value will not be logged: https://docs.python.org/3/library/logging.html#levels | `string` | `"INFO"` | no |
2423
| <a name="input_log_retention_in_days"></a> [log\_retention\_in\_days](#input\_log\_retention\_in\_days) | The retention period in days for the Cloudwatch Logs events to be retained, default of 0 is indefinite | `number` | `0` | no |
2524
| <a name="input_manually_configure_mtls_truststore"></a> [manually\_configure\_mtls\_truststore](#input\_manually\_configure\_mtls\_truststore) | Manually manage the truststore used for API Gateway mTLS (e.g. for prod environment) | `bool` | `false` | no |
25+
| <a name="input_max_get_limit"></a> [max\_get\_limit](#input\_max\_get\_limit) | Default limit to apply to GET requests that support pagination | `number` | `2500` | no |
2626
| <a name="input_parent_acct_environment"></a> [parent\_acct\_environment](#input\_parent\_acct\_environment) | Name of the environment responsible for the acct resources used, affects things like DNS zone. Useful for named dev environments | `string` | `"main"` | no |
2727
| <a name="input_project"></a> [project](#input\_project) | The name of the tfscaffold project | `string` | n/a | yes |
2828
| <a name="input_region"></a> [region](#input\_region) | The AWS Region | `string` | n/a | yes |

infrastructure/terraform/components/api/module_lambda_get_letters.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module "get_letters" {
3838
lambda_env_vars = {
3939
LETTERS_TABLE_NAME = aws_dynamodb_table.letters.name,
4040
LETTER_TTL_HOURS = var.letter_table_ttl_hours,
41-
DEFAULT_LIMIT = var.default_get_limit,
41+
MAX_LIMIT = var.max_get_limit,
4242
}
4343
}
4444

infrastructure/terraform/components/api/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ variable "letter_table_ttl_hours" {
111111
default = 24
112112
}
113113

114-
variable "default_get_limit" {
114+
variable "max_get_limit" {
115115
type = number
116116
description = "Default limit to apply to GET requests that support pagination"
117117
default = 2500

lambdas/api-handler/src/handlers/__tests__/get-letters.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,27 @@ import type { Context } from 'aws-lambda';
33
import { mockDeep } from 'jest-mock-extended';
44
import { makeApiGwEvent } from './utils/test-utils';
55
import * as letterService from '../../services/letter-operations';
6+
import { getEnvars } from '../get-letters';
67

78
jest.mock('../../services/letter-operations');
89

910
describe('API Lambda handler', () => {
1011

12+
const originalEnv = process.env;
13+
1114
beforeEach(() => {
1215
jest.resetAllMocks();
13-
process.env.DEFAULT_LIMIT = '100';
16+
jest.resetModules();
17+
process.env = { ...originalEnv };
18+
process.env.MAX_LIMIT = '2500';
19+
});
20+
21+
afterEach(() => {
22+
process.env = originalEnv;
23+
});
24+
25+
it('uses process.env.MAX_LIMIT for max limit set', async () => {
26+
expect(getEnvars().maxLimit).toBe(2500);
1427
});
1528

1629
it('returns 200 OK with basic paginated resources', async () => {

lambdas/api-handler/src/handlers/get-letters.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ import pino from 'pino';
66

77
const letterRepo = createLetterRepository();
88
const log = pino();
9-
const DEFAULT_LIMIT = process.env.DEFAULT_LIMIT || '2500';
9+
10+
export const getEnvars = (): { maxLimit: number } => ({
11+
maxLimit: parseInt(process.env.MAX_LIMIT!)
12+
});
1013

1114
export const getLetters: APIGatewayProxyHandler = async (event) => {
15+
16+
const { maxLimit } = getEnvars();
17+
1218
if (event.path === "/letters") {
1319
const supplierId = event.headers ? event.headers["NHSD-Supplier-ID"] : undefined;
1420

@@ -42,25 +48,29 @@ export const getLetters: APIGatewayProxyHandler = async (event) => {
4248
};
4349
}
4450

45-
let limit = event.queryStringParameters?.limit || DEFAULT_LIMIT;
46-
47-
let limitNumber = Number(limit);
48-
49-
if (isNaN(limitNumber)) {
50-
log.info({
51-
description: "limit parameter is not a number",
52-
limit,
53-
});
54-
return {
55-
statusCode: 400,
56-
body: "Invalid Request: limit parameter must be a positive number not greater than 2500",
57-
};
51+
let limitNumber;
52+
53+
if (event.queryStringParameters?.limit) {
54+
let limitParam = event.queryStringParameters?.limit;
55+
limitNumber = Number(limitParam);
56+
if (isNaN(limitNumber)) {
57+
log.info({
58+
description: "limit parameter is not a number",
59+
limitParam,
60+
});
61+
return {
62+
statusCode: 400,
63+
body: "Invalid Request: limit parameter must be a positive number not greater than 2500",
64+
};
65+
}
66+
} else {
67+
limitNumber = maxLimit;
5868
}
5969

60-
if (limitNumber < 0 || limitNumber > 2500) {
70+
if (limitNumber < 0 || limitNumber > maxLimit) {
6171
log.info({
6272
description: "Limit value is invalid",
63-
limit,
73+
limitNumber,
6474
});
6575
return {
6676
statusCode: 400,
@@ -80,7 +90,7 @@ export const getLetters: APIGatewayProxyHandler = async (event) => {
8090
log.info({
8191
description: 'Pending letters successfully fetched',
8292
supplierId,
83-
limit,
93+
limitNumber,
8494
status,
8595
lettersCount: letters.length
8696
});

0 commit comments

Comments
 (0)