Skip to content

Commit 766c990

Browse files
Validate path parameter only
1 parent e372fc7 commit 766c990

File tree

2 files changed

+32
-51
lines changed

2 files changed

+32
-51
lines changed

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,9 @@ describe('patchLetters API Handler', () => {
6464
});
6565
});
6666

67-
it('returns 404 Not Found as path is unknown', async () => {
68-
const event = makeApiGwEvent({
69-
path: '/unknown',
70-
body: requestBody,
71-
pathParameters: {id: "id1"},
72-
headers: {'app-supplier-id': 'supplier1'}});
73-
const context = mockDeep<Context>();
74-
const callback = jest.fn();
75-
const result = await patchLetters(event, context, callback);
76-
77-
expect(result).toEqual({
78-
statusCode: 404,
79-
body: 'Not Found: The requested resource does not exist',
80-
});
81-
});
82-
8367
it('returns 404 Not Found as path parameter is not found', async () => {
8468
const event = makeApiGwEvent({
85-
path: '/letters',
69+
path: '/letters/',
8670
body: requestBody,
8771
headers: {'app-supplier-id': 'supplier1'}});
8872
const context = mockDeep<Context>();

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

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,50 +16,47 @@ export const patchLetters: APIGatewayProxyHandler = async (event) => {
1616
};
1717
}
1818

19-
const pathParameters = event.pathParameters || {};
20-
const letterId = pathParameters["id"];
19+
const letterId = event.pathParameters?.id;
2120

22-
if (event.path.includes('/letters/') && letterId) {
21+
if (!letterId) {
22+
return {
23+
statusCode: 404,
24+
body: "Not Found: The requested resource does not exist"
25+
};
26+
}
2327

24-
if (!event.body)
25-
{
26-
return {
27-
statusCode: 400,
28-
body: "Bad Request: Missing request body"
29-
}
28+
if (!event.body)
29+
{
30+
return {
31+
statusCode: 400,
32+
body: "Bad Request: Missing request body"
3033
}
34+
}
3135

32-
const patchLetterRequest: LetterApiDocument = JSON.parse(event.body);
36+
const patchLetterRequest: LetterApiDocument = JSON.parse(event.body);
3337

34-
try {
38+
try {
3539

36-
// TODO CCM-11188: Is it worth retrieving the letter first to check if the status is different?
40+
// TODO CCM-11188: Is it worth retrieving the letter first to check if the status is different?
3741

38-
const result = await patchLetterStatus(patchLetterRequest.data, letterId, supplierId, letterRepo);
42+
const result = await patchLetterStatus(patchLetterRequest.data, letterId, supplierId, letterRepo);
3943

44+
return {
45+
statusCode: 200,
46+
body: JSON.stringify(result, null, 2)
47+
};
48+
} catch (error) {
49+
if (error instanceof ValidationError) {
4050
return {
41-
statusCode: 200,
42-
body: JSON.stringify(result, null, 2)
51+
statusCode: 400,
52+
body: error.message
53+
};
54+
} else if (error instanceof NotFoundError) {
55+
return {
56+
statusCode: 404,
57+
body: error.message
4358
};
44-
} catch (error) {
45-
if (error instanceof ValidationError) {
46-
return {
47-
statusCode: 400,
48-
body: error.message
49-
};
50-
} else if (error instanceof NotFoundError) {
51-
return {
52-
statusCode: 404,
53-
body: error.message
54-
};
55-
}
56-
throw error;
5759
}
60+
throw error;
5861
}
59-
60-
// TODO CCM-11188: Is this reachable with the API GW?
61-
return {
62-
statusCode: 404,
63-
body: 'Not Found: The requested resource does not exist',
64-
};
6562
};

0 commit comments

Comments
 (0)