11import { APIGatewayProxyHandler } from "aws-lambda" ;
2- import { MetricsLogger , metricScope } from "aws-embedded-metrics" ;
2+ import { Unit } from "aws-embedded-metrics" ;
3+ import pino from "pino" ;
34import postMIOperation from "../services/mi-operations" ;
45import { ApiErrorDetail } from "../contracts/errors" ;
56import ValidationError from "../errors/validation-error" ;
@@ -9,91 +10,94 @@ import { extractCommonIds } from "../utils/common-ids";
910import { PostMIRequest , PostMIRequestSchema } from "../contracts/mi" ;
1011import { mapToMI } from "../mappers/mi-mapper" ;
1112import { Deps } from "../config/deps" ;
12- import { MetricStatus , emitForSingleSupplier } from "../utils/metrics" ;
13+ import { MetricEntry , MetricStatus , buildEMFObject } from "../utils/metrics" ;
1314
1415export default function createPostMIHandler (
1516 deps : Deps ,
1617) : APIGatewayProxyHandler {
17- return metricScope ( ( metrics : MetricsLogger ) => {
18- return async ( event ) => {
19- const commonIds = extractCommonIds (
20- event . headers ,
21- event . requestContext ,
22- deps ,
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 ,
2330 ) ;
31+ }
2432
25- if ( ! commonIds . ok ) {
26- return processError (
27- commonIds . error ,
28- commonIds . correlationId ,
29- deps . logger ,
30- ) ;
31- }
33+ const { supplierId } = commonIds . value ;
34+ try {
35+ const body = assertNotEmpty (
36+ event . body ,
37+ new ValidationError ( ApiErrorDetail . InvalidRequestMissingBody ) ,
38+ ) ;
3239
33- const { supplierId } = commonIds . value ;
34- try {
35- const body = assertNotEmpty (
36- event . body ,
37- new ValidationError ( ApiErrorDetail . InvalidRequestMissingBody ) ,
38- ) ;
40+ let postMIRequest : PostMIRequest ;
3941
40- let postMIRequest : PostMIRequest ;
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 ) ;
4155
42- try {
43- postMIRequest = PostMIRequestSchema . parse ( JSON . parse ( body ) ) ;
44- } catch ( error ) {
45- emitErrorMetric ( metrics , supplierId ) ;
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 ) ;
56+ const result = await postMIOperation (
57+ mapToMI ( postMIRequest , supplierId ) ,
58+ deps . miRepo ,
59+ ) ;
5560
56- const result = await postMIOperation (
57- mapToMI ( postMIRequest , supplierId ) ,
58- deps . miRepo ,
59- ) ;
61+ deps . logger . info ( {
62+ description : "Posted management information" ,
63+ supplierId : commonIds . value . supplierId ,
64+ correlationId : commonIds . value . correlationId ,
65+ } ) ;
6066
61- deps . logger . info ( {
62- description : "Posted management information" ,
63- supplierId : commonIds . value . supplierId ,
64- correlationId : commonIds . value . correlationId ,
65- } ) ;
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 ) ;
6676
67- // metric with count 1 specifying the supplier
68- emitForSingleSupplier (
69- metrics ,
70- "postMi" ,
71- supplierId ,
72- 1 ,
73- MetricStatus . Success ,
74- ) ;
75- // metric displaying the supplier and the type/number of lineItems posted
76- emitForSingleSupplier (
77- metrics ,
78- "postMi" ,
79- supplierId ,
80- postMIRequest . data . attributes . quantity ,
81- MetricStatus . Success ,
82- { lineItem : postMIRequest . data . attributes . lineItem } ,
83- ) ;
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+ emf = buildEMFObject ( "postMi" , dimensions , metric ) ;
81+ deps . logger . info ( emf ) ;
8482
85- return {
86- statusCode : 201 ,
87- body : JSON . stringify ( result , null , 2 ) ,
88- } ;
89- } catch ( error ) {
90- emitErrorMetric ( metrics , supplierId ) ;
91- return processError ( error , commonIds . value . correlationId , deps . logger ) ;
92- }
93- } ;
94- } ) ;
83+ return {
84+ statusCode : 201 ,
85+ body : JSON . stringify ( result , null , 2 ) ,
86+ } ;
87+ } catch ( error ) {
88+ emitErrorMetric ( supplierId , deps . logger ) ;
89+ return processError ( error , commonIds . value . correlationId , deps . logger ) ;
90+ }
91+ } ;
9592}
9693
97- function emitErrorMetric ( metrics : MetricsLogger , supplierId : string ) {
98- emitForSingleSupplier ( metrics , "postMi" , supplierId , 1 , MetricStatus . Failure ) ;
94+ function emitErrorMetric ( supplierId : string , logger : pino . Logger ) {
95+ const dimensions : Record < string , string > = { supplier : supplierId } ;
96+ const metric : MetricEntry = {
97+ key : MetricStatus . Failure ,
98+ value : 1 ,
99+ unit : Unit . Count ,
100+ } ;
101+ const emf = buildEMFObject ( "postMi" , dimensions , metric ) ;
102+ logger . info ( emf ) ;
99103}
0 commit comments