11import { APIGatewayProxyHandler } from "aws-lambda" ;
2+ import { MetricsLogger , metricScope } from "aws-embedded-metrics" ;
23import { assertNotEmpty } from "../utils/validation" ;
34import { extractCommonIds } from "../utils/common-ids" ;
45import ValidationError from "../errors/validation-error" ;
@@ -7,53 +8,72 @@ import { getLetterById } from "../services/letter-operations";
78import { processError } from "../mappers/error-mapper" ;
89import { mapToGetLetterResponse } from "../mappers/letter-mapper" ;
910import { Deps } from "../config/deps" ;
11+ import { MetricStatus , emitForSingleSupplier } from "../utils/metrics" ;
1012
13+ // Get letter data
1114export default function createGetLetterHandler (
1215 deps : Deps ,
1316) : APIGatewayProxyHandler {
14- return async ( event ) => {
15- const commonIds = extractCommonIds (
16- event . headers ,
17- event . requestContext ,
18- deps ,
19- ) ;
20-
21- if ( ! commonIds . ok ) {
22- return processError (
23- commonIds . error ,
24- commonIds . correlationId ,
25- deps . logger ,
17+ return metricScope ( ( metrics : MetricsLogger ) => {
18+ return async ( event ) => {
19+ const commonIds = extractCommonIds (
20+ event . headers ,
21+ event . requestContext ,
22+ deps ,
2623 ) ;
27- }
2824
29- try {
30- const letterId = assertNotEmpty (
31- event . pathParameters ?. id ,
32- new ValidationError (
33- ApiErrorDetail . InvalidRequestMissingLetterIdPathParameter ,
34- ) ,
35- ) ;
25+ if ( ! commonIds . ok ) {
26+ return processError (
27+ commonIds . error ,
28+ commonIds . correlationId ,
29+ deps . logger ,
30+ ) ;
31+ }
3632
37- const letter = await getLetterById (
38- commonIds . value . supplierId ,
39- letterId ,
40- deps . letterRepo ,
41- ) ;
33+ const { supplierId } = commonIds . value ;
34+ try {
35+ const letterId = assertNotEmpty (
36+ event . pathParameters ?. id ,
37+ new ValidationError (
38+ ApiErrorDetail . InvalidRequestMissingLetterIdPathParameter ,
39+ ) ,
40+ ) ;
41+
42+ const letter = await getLetterById (
43+ supplierId ,
44+ letterId ,
45+ deps . letterRepo ,
46+ ) ;
4247
43- const response = mapToGetLetterResponse ( letter ) ;
48+ const response = mapToGetLetterResponse ( letter ) ;
4449
45- deps . logger . info ( {
46- description : "Letter successfully fetched by id" ,
47- supplierId : commonIds . value . supplierId ,
48- letterId,
49- } ) ;
50+ deps . logger . info ( {
51+ description : "Letter successfully fetched by id" ,
52+ supplierId,
53+ letterId,
54+ } ) ;
5055
51- return {
52- statusCode : 200 ,
53- body : JSON . stringify ( response , null , 2 ) ,
54- } ;
55- } catch ( error ) {
56- return processError ( error , commonIds . value . correlationId , deps . logger ) ;
57- }
58- } ;
56+ emitForSingleSupplier (
57+ metrics ,
58+ "getLetter" ,
59+ supplierId ,
60+ 1 ,
61+ MetricStatus . Success ,
62+ ) ;
63+ return {
64+ statusCode : 200 ,
65+ body : JSON . stringify ( response , null , 2 ) ,
66+ } ;
67+ } catch ( error ) {
68+ emitForSingleSupplier (
69+ metrics ,
70+ "getLetter" ,
71+ supplierId ,
72+ 1 ,
73+ MetricStatus . Failure ,
74+ ) ;
75+ return processError ( error , commonIds . value . correlationId , deps . logger ) ;
76+ }
77+ } ;
78+ } ) ;
5979}
0 commit comments