|
| 1 | +import { APIGatewayProxyHandler } from "aws-lambda"; |
| 2 | +import { Unit } from "aws-embedded-metrics"; |
| 3 | +import pino from "pino"; |
| 4 | +import { MetricEntry, MetricStatus, buildEMFObject } from "@internal/helpers"; |
| 5 | +import postMIOperation from "../services/mi-operations"; |
| 6 | +import { ApiErrorDetail } from "../contracts/errors"; |
| 7 | +import ValidationError from "../errors/validation-error"; |
| 8 | +import { processError } from "../mappers/error-mapper"; |
| 9 | +import { assertNotEmpty, validateIso8601Timestamp } from "../utils/validation"; |
| 10 | +import { extractCommonIds } from "../utils/common-ids"; |
| 11 | +import { PostMIRequest, PostMIRequestSchema } from "../contracts/mi"; |
| 12 | +import { mapToMI } from "../mappers/mi-mapper"; |
| 13 | +import { Deps } from "../config/deps"; |
| 14 | + |
| 15 | +export default function createPostMIHandler( |
| 16 | + deps: Deps, |
| 17 | +): APIGatewayProxyHandler { |
| 18 | + return async (event) => { |
| 19 | + const commonIds = extractCommonIds( |
| 20 | + event.headers, |
| 21 | + event.requestContext, |
| 22 | + deps, |
| 23 | + ); |
| 24 | + |
| 25 | + if (!commonIds.ok) { |
| 26 | + return processError( |
| 27 | + commonIds.error, |
| 28 | + commonIds.correlationId, |
| 29 | + deps.logger, |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + const { supplierId } = commonIds.value; |
| 34 | + try { |
| 35 | + const body = assertNotEmpty( |
| 36 | + event.body, |
| 37 | + new ValidationError(ApiErrorDetail.InvalidRequestMissingBody), |
| 38 | + ); |
| 39 | + |
| 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), |
| 58 | + deps.miRepo, |
| 59 | + ); |
| 60 | + |
| 61 | + deps.logger.info({ |
| 62 | + description: "Posted management information", |
| 63 | + supplierId: commonIds.value.supplierId, |
| 64 | + correlationId: commonIds.value.correlationId, |
| 65 | + }); |
| 66 | + |
| 67 | + // metric with count 1 specifying the supplier |
| 68 | + const dimensions: Record<string, string> = { supplier: supplierId }; |
| 69 | + const metric: MetricEntry = { |
| 70 | + key: MetricStatus.Success, |
| 71 | + value: 1, |
| 72 | + unit: Unit.Count, |
| 73 | + }; |
| 74 | + let emf = buildEMFObject("postMi", dimensions, metric); |
| 75 | + deps.logger.info(emf); |
| 76 | + |
| 77 | + // metric displaying the type/number of lineItems posted per supplier |
| 78 | + dimensions.lineItem = postMIRequest.data.attributes.lineItem; |
| 79 | + metric.key = "LineItem per supplier"; |
| 80 | + metric.value = postMIRequest.data.attributes.quantity; |
| 81 | + emf = buildEMFObject("postMi", dimensions, metric); |
| 82 | + deps.logger.info(emf); |
| 83 | + |
| 84 | + return { |
| 85 | + statusCode: 201, |
| 86 | + body: JSON.stringify(result, null, 2), |
| 87 | + }; |
| 88 | + } catch (error) { |
| 89 | + emitErrorMetric(supplierId, deps.logger); |
| 90 | + return processError(error, commonIds.value.correlationId, deps.logger); |
| 91 | + } |
| 92 | + }; |
| 93 | +} |
| 94 | + |
| 95 | +function emitErrorMetric(supplierId: string, logger: pino.Logger) { |
| 96 | + const dimensions: Record<string, string> = { supplier: supplierId }; |
| 97 | + const metric: MetricEntry = { |
| 98 | + key: MetricStatus.Failure, |
| 99 | + value: 1, |
| 100 | + unit: Unit.Count, |
| 101 | + }; |
| 102 | + const emf = buildEMFObject("postMi", dimensions, metric); |
| 103 | + logger.info(emf); |
| 104 | +} |
0 commit comments