33 APIGatewayProxyHandler ,
44} from "aws-lambda" ;
55import { Logger } from "pino" ;
6+ import { MetricsLogger , metricScope } from "aws-embedded-metrics" ;
67import { getLettersForSupplier } from "../services/letter-operations" ;
78import { extractCommonIds } from "../utils/common-ids" ;
89import { requireEnvVar } from "../utils/validation" ;
@@ -11,7 +12,9 @@ import { processError } from "../mappers/error-mapper";
1112import ValidationError from "../errors/validation-error" ;
1213import { mapToGetLettersResponse } from "../mappers/letter-mapper" ;
1314import type { Deps } from "../config/deps" ;
15+ import { MetricStatus , emitForSingleSupplier } from "../utils/metrics" ;
1416
17+ // List letters Handlers
1518// The endpoint should only return pending letters for now
1619const status = "PENDING" ;
1720
@@ -82,53 +85,70 @@ function getLimitOrDefault(
8285export default function createGetLettersHandler (
8386 deps : Deps ,
8487) : APIGatewayProxyHandler {
85- return async ( event ) => {
86- const commonIds = extractCommonIds (
87- event . headers ,
88- event . requestContext ,
89- deps ,
90- ) ;
91-
92- if ( ! commonIds . ok ) {
93- return processError (
94- commonIds . error ,
95- commonIds . correlationId ,
96- deps . logger ,
88+ return metricScope ( ( metrics : MetricsLogger ) => {
89+ return async ( event ) => {
90+ const commonIds = extractCommonIds (
91+ event . headers ,
92+ event . requestContext ,
93+ deps ,
9794 ) ;
98- }
9995
100- try {
101- const maxLimit = requireEnvVar ( deps . env , "MAX_LIMIT" ) ;
96+ if ( ! commonIds . ok ) {
97+ return processError (
98+ commonIds . error ,
99+ commonIds . correlationId ,
100+ deps . logger ,
101+ ) ;
102+ }
102103
103- const limitNumber = getLimitOrDefault (
104- event . queryStringParameters ,
105- maxLimit ,
106- deps . logger ,
107- ) ;
104+ const { supplierId } = commonIds . value ;
105+ try {
106+ const maxLimit = requireEnvVar ( deps . env , "MAX_LIMIT" ) ;
108107
109- const letters = await getLettersForSupplier (
110- commonIds . value . supplierId ,
111- status ,
112- limitNumber ,
113- deps . letterRepo ,
114- ) ;
108+ const limitNumber = getLimitOrDefault (
109+ event . queryStringParameters ,
110+ maxLimit ,
111+ deps . logger ,
112+ ) ;
113+
114+ const letters = await getLettersForSupplier (
115+ supplierId ,
116+ status ,
117+ limitNumber ,
118+ deps . letterRepo ,
119+ ) ;
115120
116- const response = mapToGetLettersResponse ( letters ) ;
121+ const response = mapToGetLettersResponse ( letters ) ;
117122
118- deps . logger . info ( {
119- description : "Pending letters successfully fetched" ,
120- supplierId : commonIds . value . supplierId ,
121- limitNumber,
122- status,
123- lettersCount : letters . length ,
124- } ) ;
123+ deps . logger . info ( {
124+ description : "Pending letters successfully fetched" ,
125+ supplierId,
126+ limitNumber,
127+ status,
128+ lettersCount : letters . length ,
129+ } ) ;
125130
126- return {
127- statusCode : 200 ,
128- body : JSON . stringify ( response , null , 2 ) ,
129- } ;
130- } catch ( error ) {
131- return processError ( error , commonIds . value . correlationId , deps . logger ) ;
132- }
133- } ;
131+ emitForSingleSupplier (
132+ metrics ,
133+ "getLetters" ,
134+ supplierId ,
135+ letters . length ,
136+ MetricStatus . Success ,
137+ ) ;
138+ return {
139+ statusCode : 200 ,
140+ body : JSON . stringify ( response , null , 2 ) ,
141+ } ;
142+ } catch ( error ) {
143+ emitForSingleSupplier (
144+ metrics ,
145+ "getLetters" ,
146+ supplierId ,
147+ 1 ,
148+ MetricStatus . Failure ,
149+ ) ;
150+ return processError ( error , commonIds . value . correlationId , deps . logger ) ;
151+ }
152+ } ;
153+ } ) ;
134154}
0 commit comments