Skip to content

Commit 3d995c9

Browse files
committed
implement createGetMIHandler
1 parent 01f2e52 commit 3d995c9

2 files changed

Lines changed: 19 additions & 33 deletions

File tree

lambdas/api-handler/src/contracts/errors.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export enum ApiErrorDetail {
3232
InvalidRequestMissingBody = "The request is missing the body",
3333
InvalidRequestMissingLetterIdPathParameter = "The request is missing the letter id path parameter",
3434
InvalidRequestLetterIdsMismatch = "The letter ID in the request body does not match the letter ID path parameter",
35+
InvalidRequestMissingMiIdPathParameter = "The request is missing the mi id path parameter",
3536
InvalidRequestBody = "The request body is invalid",
3637
InvalidRequestLimitNotANumber = "The limit parameter is not a number",
3738
InvalidRequestLimitNotInRange = "The limit parameter must be a positive number not greater than %s",

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

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@ import { APIGatewayProxyHandler } from "aws-lambda";
22
import { Unit } from "aws-embedded-metrics";
33
import pino from "pino";
44
import { MetricEntry, MetricStatus, buildEMFObject } from "@internal/helpers";
5-
import { postMI as postMIOperation } from "../services/mi-operations";
5+
import { getMI as getMIOperation } from "../services/mi-operations";
66
import { ApiErrorDetail } from "../contracts/errors";
77
import ValidationError from "../errors/validation-error";
88
import { processError } from "../mappers/error-mapper";
9-
import { assertNotEmpty, validateIso8601Timestamp } from "../utils/validation";
9+
import { assertNotEmpty } from "../utils/validation";
1010
import { extractCommonIds } from "../utils/common-ids";
11-
import { PostMIRequest, PostMIRequestSchema } from "../contracts/mi";
12-
import { mapToMI } from "../mappers/mi-mapper";
1311
import { Deps } from "../config/deps";
1412

15-
export default function createGettMIHandler(
13+
export default function createGetMIHandler(
1614
deps: Deps,
1715
): APIGatewayProxyHandler {
1816
return async (event) => {
@@ -32,34 +30,21 @@ export default function createGettMIHandler(
3230

3331
const { supplierId } = commonIds.value;
3432
try {
35-
const body = assertNotEmpty(
36-
event.body,
37-
new ValidationError(ApiErrorDetail.InvalidRequestMissingBody),
33+
const miId = assertNotEmpty(
34+
event.pathParameters?.id,
35+
new ValidationError(
36+
ApiErrorDetail.InvalidRequestMissingMiIdPathParameter,
37+
),
3838
);
3939

40-
let postMIRequest: PostMIRequest;
41-
42-
try {
43-
postMIRequest = PostMIRequestSchema.parse(JSON.parse(body));
44-
} catch (error) {
45-
emitErrorMetric(supplierId, deps.logger);
46-
const typedError =
47-
error instanceof Error
48-
? new ValidationError(ApiErrorDetail.InvalidRequestBody, {
49-
cause: error,
50-
})
51-
: error;
52-
throw typedError;
53-
}
54-
validateIso8601Timestamp(postMIRequest.data.attributes.timestamp);
55-
56-
const result = await postMIOperation(
57-
mapToMI(postMIRequest, supplierId),
40+
const result = await getMIOperation(
41+
miId,
42+
supplierId,
5843
deps.miRepo,
5944
);
6045

6146
deps.logger.info({
62-
description: "Posted management information",
47+
description: "Retrieved management information",
6348
supplierId: commonIds.value.supplierId,
6449
correlationId: commonIds.value.correlationId,
6550
});
@@ -71,18 +56,18 @@ export default function createGettMIHandler(
7156
value: 1,
7257
unit: Unit.Count,
7358
};
74-
let emf = buildEMFObject("postMi", dimensions, metric);
59+
let emf = buildEMFObject("getMi", dimensions, metric);
7560
deps.logger.info(emf);
7661

7762
// metric displaying the type/number of lineItems posted per supplier
78-
dimensions.lineItem = postMIRequest.data.attributes.lineItem;
63+
dimensions.lineItem = result.data.attributes.lineItem;
7964
metric.key = "LineItem per supplier";
80-
metric.value = postMIRequest.data.attributes.quantity;
81-
emf = buildEMFObject("postMi", dimensions, metric);
65+
metric.value = result.data.attributes.quantity;
66+
emf = buildEMFObject("getMi", dimensions, metric);
8267
deps.logger.info(emf);
8368

8469
return {
85-
statusCode: 201,
70+
statusCode: 200,
8671
body: JSON.stringify(result, null, 2),
8772
};
8873
} catch (error) {
@@ -99,6 +84,6 @@ function emitErrorMetric(supplierId: string, logger: pino.Logger) {
9984
value: 1,
10085
unit: Unit.Count,
10186
};
102-
const emf = buildEMFObject("postMi", dimensions, metric);
87+
const emf = buildEMFObject("getMi", dimensions, metric);
10388
logger.info(emf);
10489
}

0 commit comments

Comments
 (0)