@@ -2,6 +2,7 @@ import type { SQSRecord } from "aws-lambda";
22import pMap from "p-map" ;
33import type {
44 ClientCallbackPayload ,
5+ ClientSubscriptionConfiguration ,
56 StatusPublishEvent ,
67} from "@nhs-notify-client-callbacks/models" ;
78import { validateStatusPublishEvent } from "services/validators/event-validator" ;
@@ -121,10 +122,12 @@ function processSingleEvent(
121122 } ;
122123}
123124
125+ type ClientConfigMap = Map < string , ClientSubscriptionConfiguration | undefined > ;
126+
124127async function signBatch (
125128 filteredEvents : UnsignedEvent [ ] ,
126129 applicationsMapService : ApplicationsMapService ,
127- configLoader : ConfigLoader ,
130+ configByClientId : ClientConfigMap ,
128131 stats : BatchStats ,
129132) : Promise < TransformedEvent [ ] > {
130133 const results = await pMap (
@@ -144,7 +147,7 @@ async function signBatch(
144147 return undefined ;
145148 }
146149
147- const clientConfig = await configLoader . loadClientConfig ( clientId ) ;
150+ const clientConfig = configByClientId . get ( clientId ) ;
148151 const apiKey = clientConfig ?. [ 0 ] ?. Targets ?. [ 0 ] ?. APIKey ?. HeaderValue ;
149152 if ( ! apiKey ) {
150153 stats . recordFiltered ( ) ;
@@ -184,28 +187,29 @@ function recordDeliveryInitiated(
184187 }
185188}
186189
187- async function filterBatch (
188- transformedEvents : UnsignedEvent [ ] ,
190+ async function loadClientConfigs (
191+ events : UnsignedEvent [ ] ,
189192 configLoader : ConfigLoader ,
190- observability : ObservabilityService ,
191- stats : BatchStats ,
192- ) : Promise < UnsignedEvent [ ] > {
193- observability . recordFilteringStarted ( { batchSize : transformedEvents . length } ) ;
194-
195- const uniqueClientIds = new Set (
196- transformedEvents . map ( ( e ) => e . data . clientId ) ,
197- ) ;
198-
199- const configEntries = await pMap (
193+ ) : Promise < ClientConfigMap > {
194+ const uniqueClientIds = new Set ( events . map ( ( e ) => e . data . clientId ) ) ;
195+ const entries = await pMap (
200196 uniqueClientIds ,
201197 async ( clientId ) => {
202198 const config = await configLoader . loadClientConfig ( clientId ) ;
203199 return [ clientId , config ] as const ;
204200 } ,
205201 { concurrency : BATCH_CONCURRENCY } ,
206202 ) ;
203+ return new Map ( entries ) ;
204+ }
207205
208- const configByClientId = new Map ( configEntries ) ;
206+ async function filterBatch (
207+ transformedEvents : UnsignedEvent [ ] ,
208+ configByClientId : ClientConfigMap ,
209+ observability : ObservabilityService ,
210+ stats : BatchStats ,
211+ ) : Promise < UnsignedEvent [ ] > {
212+ observability . recordFilteringStarted ( { batchSize : transformedEvents . length } ) ;
209213
210214 const filtered : UnsignedEvent [ ] = [ ] ;
211215
@@ -278,17 +282,22 @@ export async function processEvents(
278282 try {
279283 const transformedEvents = await transformBatch ( event , observability , stats ) ;
280284
281- const filteredEvents = await filterBatch (
285+ const configByClientId = await loadClientConfigs (
282286 transformedEvents ,
283287 configLoader ,
288+ ) ;
289+
290+ const filteredEvents = await filterBatch (
291+ transformedEvents ,
292+ configByClientId ,
284293 observability ,
285294 stats ,
286295 ) ;
287296
288297 const signedEvents = await signBatch (
289298 filteredEvents ,
290299 applicationsMapService ,
291- configLoader ,
300+ configByClientId ,
292301 stats ,
293302 ) ;
294303
0 commit comments