@@ -160,21 +160,28 @@ async function emitMetrics(
160160) {
161161 metrics . setNamespace ( process . env . AWS_LAMBDA_FUNCTION_NAME || `upsertLetter` ) ;
162162 // emit success metrics
163- for ( const [ supplier , count ] of Object . entries ( successMetrics ) ) {
163+ for ( const [ supplier , count ] of successMetrics ) {
164164 metrics . putDimensions ( {
165165 Supplier : supplier ,
166166 } ) ;
167167 metrics . putMetric ( "MessagesProcessed" , count , Unit . Count ) ;
168168 }
169169 // emit failure metrics
170- for ( const [ supplier , count ] of Object . entries ( failMetrics ) ) {
170+ for ( const [ supplier , count ] of failMetrics ) {
171171 metrics . putDimensions ( {
172172 Supplier : supplier ,
173173 } ) ;
174174 metrics . putMetric ( "MessageFailed" , count , Unit . Count ) ;
175175 }
176176}
177177
178+ function getSupplierId ( snsEvent : any ) : string {
179+ if ( snsEvent && snsEvent . data && snsEvent . data . supplierId ) {
180+ return snsEvent . data . supplierId ;
181+ }
182+ return "unknown" ;
183+ }
184+
178185export default function createUpsertLetterHandler ( deps : Deps ) : SQSHandler {
179186 return metricScope ( ( metrics ) => {
180187 return async ( event : SQSEvent ) => {
@@ -187,14 +194,11 @@ export default function createUpsertLetterHandler(deps: Deps): SQSHandler {
187194 try {
188195 const message : string = parseSNSNotification ( record ) ;
189196 const snsEvent = JSON . parse ( message ) ;
190- supplier = snsEvent . data . supplierId || "unknown" ;
197+ supplier = getSupplierId ( snsEvent ) ;
191198 const letterEvent : unknown = removeEventBridgeWrapper ( snsEvent ) ;
192199 const type = getType ( letterEvent ) ;
193200
194201 const operation = getOperationFromType ( type ) ;
195- metrics . putDimensions ( {
196- supplier : snsEvent . data . supplierId || "unknown" ,
197- } ) ;
198202
199203 await runUpsert ( operation , letterEvent , deps ) ;
200204
@@ -216,8 +220,8 @@ export default function createUpsertLetterHandler(deps: Deps): SQSHandler {
216220 } ) ;
217221
218222 await Promise . all ( tasks ) ;
219- await emitMetrics ( metrics , perSupplierSuccess , perSupplierFailure ) ;
220223
224+ await emitMetrics ( metrics , perSupplierSuccess , perSupplierFailure ) ;
221225 return { batchItemFailures } ;
222226 } ;
223227 } ) ;
0 commit comments