@@ -7,62 +7,38 @@ import { Location } from '../models/location';
77import { Driver } from '../models/driver' ;
88
99// Helper: batch fetch locations, riders, drivers and return maps keyed by id
10- // This is written defensively so that if any batchGet fails in a particular
11- // environment (e.g., missing tables or limited IAM permissions in production),
12- // we degrade gracefully by logging the error and continuing with empty maps
13- // instead of failing the entire scan.
1410async function buildEntityMapsFromSets (
1511 locationIds : Set < string > ,
1612 riderIds : Set < string > ,
1713 driverIds : Set < string >
1814) {
15+ const [ locationsArr , ridersArr , driversArr ] = await Promise . all ( [
16+ locationIds . size
17+ ? Location . batchGet ( Array . from ( locationIds ) . map ( ( id ) => ( { id } ) ) )
18+ : Promise . resolve ( [ ] ) ,
19+ riderIds . size
20+ ? Rider . batchGet ( Array . from ( riderIds ) . map ( ( id ) => ( { id } ) ) )
21+ : Promise . resolve ( [ ] ) ,
22+ driverIds . size
23+ ? Driver . batchGet ( Array . from ( driverIds ) . map ( ( id ) => ( { id } ) ) )
24+ : Promise . resolve ( [ ] ) ,
25+ ] ) ;
26+
1927 const locationMap = new Map < string , any > ( ) ;
2028 const riderMap = new Map < string , any > ( ) ;
2129 const driverMap = new Map < string , any > ( ) ;
2230
23- // Fetch locations
24- if ( locationIds . size ) {
25- try {
26- const locationsArr = ( await Location . batchGet (
27- Array . from ( locationIds ) . map ( ( id ) => ( { id } ) )
28- ) ) as any [ ] ;
29- for ( const l of locationsArr ) {
30- const j = l && l . toJSON ? l . toJSON ( ) : l ;
31- if ( j && j . id ) locationMap . set ( j . id , j ) ;
32- }
33- } catch ( err ) {
34- console . error ( 'Error batch fetching locations in buildEntityMapsFromSets:' , err ) ;
35- }
31+ for ( const l of locationsArr as any [ ] ) {
32+ const j = l && l . toJSON ? l . toJSON ( ) : l ;
33+ if ( j && j . id ) locationMap . set ( j . id , j ) ;
3634 }
37-
38- // Fetch riders
39- if ( riderIds . size ) {
40- try {
41- const ridersArr = ( await Rider . batchGet (
42- Array . from ( riderIds ) . map ( ( id ) => ( { id } ) )
43- ) ) as any [ ] ;
44- for ( const r of ridersArr ) {
45- const j = r && r . toJSON ? r . toJSON ( ) : r ;
46- if ( j && j . id ) riderMap . set ( j . id , j ) ;
47- }
48- } catch ( err ) {
49- console . error ( 'Error batch fetching riders in buildEntityMapsFromSets:' , err ) ;
50- }
35+ for ( const r of ridersArr as any [ ] ) {
36+ const j = r && r . toJSON ? r . toJSON ( ) : r ;
37+ if ( j && j . id ) riderMap . set ( j . id , j ) ;
5138 }
52-
53- // Fetch drivers
54- if ( driverIds . size ) {
55- try {
56- const driversArr = ( await Driver . batchGet (
57- Array . from ( driverIds ) . map ( ( id ) => ( { id } ) )
58- ) ) as any [ ] ;
59- for ( const d of driversArr ) {
60- const j = d && d . toJSON ? d . toJSON ( ) : d ;
61- if ( j && j . id ) driverMap . set ( j . id , j ) ;
62- }
63- } catch ( err ) {
64- console . error ( 'Error batch fetching drivers in buildEntityMapsFromSets:' , err ) ;
65- }
39+ for ( const d of driversArr as any [ ] ) {
40+ const j = d && d . toJSON ? d . toJSON ( ) : d ;
41+ if ( j && j . id ) driverMap . set ( j . id , j ) ;
6642 }
6743
6844 return { locationMap, riderMap, driverMap } ;
0 commit comments