@@ -141,31 +141,44 @@ export class AnalyticsService {
141141
142142 // Filter sessions by payment status and fulfillment status
143143 let filteredSessions = [ ]
144- for ( const s of sessions . data ) {
145- if ( s . payment_status === 'paid' || s . status === 'complete' || s . status === 'completed' ) {
146- let includeSession = true
144+ const fulfillmentMap = new Map ( )
147145
148- // Apply fulfillment filtering if KV is available
149- if ( options . kvNamespace ) {
150- const fulfillmentKey = `order_fulfillment:${ s . id } `
151- const fulfillmentData = await options . kvNamespace . get ( fulfillmentKey )
152- const fulfillmentStatus = fulfillmentData ? JSON . parse ( fulfillmentData ) : { fulfilled : false }
146+ const candidates = sessions . data . filter ( s =>
147+ s . payment_status === 'paid' || s . status === 'complete' || s . status === 'completed'
148+ )
153149
154- if ( showFulfilled ) {
155- if ( ! fulfillmentStatus . fulfilled ) {
156- includeSession = false
157- }
158- } else {
159- if ( fulfillmentStatus . fulfilled ) {
160- includeSession = false
161- }
162- }
150+ if ( options . kvNamespace && candidates . length > 0 ) {
151+ await Promise . all ( candidates . map ( async ( s ) => {
152+ const fulfillmentKey = `order_fulfillment:${ s . id } `
153+ const fulfillmentData = await options . kvNamespace . get ( fulfillmentKey )
154+ if ( fulfillmentData ) {
155+ fulfillmentMap . set ( s . id , fulfillmentData )
163156 }
157+ } ) )
158+ }
159+
160+ for ( const s of candidates ) {
161+ let includeSession = true
164162
165- if ( includeSession ) {
166- filteredSessions . push ( s )
163+ // Apply fulfillment filtering if KV is available
164+ if ( options . kvNamespace ) {
165+ const fulfillmentData = fulfillmentMap . get ( s . id )
166+ const fulfillmentStatus = fulfillmentData ? JSON . parse ( fulfillmentData ) : { fulfilled : false }
167+
168+ if ( showFulfilled ) {
169+ if ( ! fulfillmentStatus . fulfilled ) {
170+ includeSession = false
171+ }
172+ } else {
173+ if ( fulfillmentStatus . fulfilled ) {
174+ includeSession = false
175+ }
167176 }
168177 }
178+
179+ if ( includeSession ) {
180+ filteredSessions . push ( s )
181+ }
169182 }
170183
171184 // Oldest at top within the current page
@@ -199,9 +212,13 @@ export class AnalyticsService {
199212 }
200213
201214 // Check fulfillment status from KV
202- const fulfillmentKey = `order_fulfillment:${ s . id } `
203- const fulfillmentData = options . kvNamespace ? await options . kvNamespace . get ( fulfillmentKey ) : null
204- const fulfillmentStatus = fulfillmentData ? JSON . parse ( fulfillmentData ) : { fulfilled : false , fulfilledAt : null , fulfilledBy : null }
215+ let fulfillmentStatus = { fulfilled : false , fulfilledAt : null , fulfilledBy : null }
216+ if ( options . kvNamespace ) {
217+ const fulfillmentData = fulfillmentMap . get ( s . id )
218+ if ( fulfillmentData ) {
219+ fulfillmentStatus = JSON . parse ( fulfillmentData )
220+ }
221+ }
205222
206223 return {
207224 id : s . id ,
@@ -256,9 +273,10 @@ export class AnalyticsService {
256273 // Handle error case...
257274 let fulfillmentStatus = { fulfilled : false , fulfilledAt : null , fulfilledBy : null }
258275 if ( options . kvNamespace ) {
259- const fulfillmentKey = `order_fulfillment:${ s . id } `
260- const fulfillmentData = await options . kvNamespace . get ( fulfillmentKey )
261- fulfillmentStatus = fulfillmentData ? JSON . parse ( fulfillmentData ) : { fulfilled : false , fulfilledAt : null , fulfilledBy : null }
276+ const fulfillmentData = fulfillmentMap . get ( s . id )
277+ if ( fulfillmentData ) {
278+ fulfillmentStatus = JSON . parse ( fulfillmentData )
279+ }
262280 }
263281
264282 return {
0 commit comments