@@ -404,7 +404,7 @@ function showItemsForCallNumberRange(firstCN, lastCN, locationId) {
404
404
} else {
405
405
// Get the location name so we can write it in the error message.
406
406
const locName = getLocationsList ( ) . find ( el => ( el . id == locationId ) ) . name ;
407
- quit ( 'No results for given call number range and location' ,
407
+ quit ( 'No results for this combination of call number range and location' ,
408
408
`Searching FOLIO for the call number range ${ firstCN } – ${ lastCN } ` +
409
409
` (in either order) at location "${ locName } " produced no results.` +
410
410
' Please verify the call numbers (paying special attention to any' +
@@ -418,37 +418,18 @@ function showItemsForCallNumberRange(firstCN, lastCN, locationId) {
418
418
419
419
// Don't start downloading results if we won't be able to write them.
420
420
const numColumns = getEnabledFields ( ) . length ;
421
- const maxRecords = Math . trunc ( maxGoogleSheetCells / numColumns ) - 1 ;
422
- if ( expected . totalRecords > maxRecords ) {
421
+ const maxRows = Math . trunc ( maxGoogleSheetCells / numColumns ) - 1 ;
422
+ if ( expected . totalRecords > maxRows ) {
423
423
quit ( 'This query exceeds the maximum number of results that can be written' ,
424
- `The number of records this produced (${ maxRecords . toLocaleString ( ) } )` +
425
- ` times the number of currently-selected data fields (${ numColumns } )` +
426
- ` exceeds the number of spreadsheet cells that Google Sheets allow in` +
427
- ` a single empty spreadsheet.` ) ;
424
+ `The number of records this produced (${ maxRows . toLocaleString ( ) } )` +
425
+ ` times the number of currently-selected data fields (${ numColumns } )` +
426
+ ` exceeds the number of cells that a Google spreadsheet can contain.` ) ;
428
427
}
429
428
430
429
// Now get the records.
431
- let records = [ ] ;
432
- let results ;
433
430
note ( `Fetching ${ expected . totalRecords . toLocaleString ( ) } records from FOLIO …` , 30 ) ;
434
- for ( let offset = 0 ; offset < expected . totalRecords ; offset += 100 ) {
435
- endpoint = makeRangeQuery ( firstESO , lastESO , 100 , offset ) ;
436
- results = fetchJSON ( endpoint , tenantId , token ) ;
437
- if ( results . items ) {
438
- records . push . apply ( records , results . items ) ;
439
- } else {
440
- quit ( 'Failed to get complete set of records' ,
441
- `While downloading the item records for ${ firstCN } – ${ lastCN } ,` +
442
- ' Boffo unexpectedly received an empty batch from FOLIO. It may' +
443
- ' be due to a sudden network glitch or other temporary failure,' +
444
- ' or it may indicate a deeper problem. Please wait a short time' +
445
- ' then try the command again. If this situation repeats, please' +
446
- ' report it to the developers.' ) ;
447
- }
448
- if ( offset > 0 && ( offset % 5000 ) == 0 ) {
449
- note ( `Fetched ${ offset . toLocaleString ( ) } records so far and still going …` , 30 ) ;
450
- }
451
- }
431
+ const makeQuery = makeRangeQuery . bind ( null , firstESO , lastESO ) ;
432
+ const records = getRecordsForQuery ( makeQuery , expected . totalRecords , tenantId , token ) ;
452
433
453
434
// And we're done.
454
435
writeResultsSheet ( sortByShelvingOrder ( records ) ) ;
@@ -483,41 +464,22 @@ function getItemsForCN(cn, locationId) {
483
464
const { folioUrl, tenantId, token} = getStoredCredentials ( ) ;
484
465
const baseUrl = `${ folioUrl } /inventory/items` ;
485
466
486
- function makeQuery ( offset = 0 ) {
467
+ function makeQuery ( limit = 0 , offset = 0 ) {
487
468
// Search on the wildcarded call number at the given location.
488
469
// 100 is the max that the Folio API will return for this query.
489
- return baseUrl + `?limit=100 &offset=${ offset } &query=` +
470
+ return baseUrl + `?limit=${ limit } &offset=${ offset } &query=` +
490
471
encodeURI ( `effectiveLocationId==${ locationId } AND ` +
491
472
`effectiveCallNumberComponents.callNumber=="${ cn } "` ) ;
492
473
}
493
474
494
- let endpoint = makeQuery ( 0 ) ;
495
- let records = fetchJSON ( endpoint , tenantId , token ) ;
496
- log ( `FOLIO has ${ records . totalRecords } items for ${ cn } at location ${ locationId } ` ) ;
497
-
498
- if ( records . totalRecords > 0 && records . totalRecords <= 100 ) {
499
- return sortByShelvingOrder ( records . items ) ;
500
- } else if ( records . totalRecords > 100 ) {
501
- // The call above didn't get all of the records.
502
- log ( 'iterating to get all records' ) ;
503
- let itemRecords = records . items ;
504
- let results ;
505
- for ( let offset = 100 ; offset <= records . totalRecords ; offset += 100 ) {
506
- endpoint = makeQuery ( offset ) ;
507
- results = fetchJSON ( endpoint , tenantId , token ) ;
508
- if ( results . items ) {
509
- itemRecords . push . apply ( itemRecords , results . items ) ;
510
- } else {
511
- quit ( `Failed to get complete set of records for ${ givenCN } .` ,
512
- ' Boffo unexpectedly received an empty batch from FOLIO. It' +
513
- ' may be due to a sudden network glitch or other temporary' +
514
- ' failure, or it may indicate a deeper problem. Please wait' +
515
- ' a few seconds, then try again. If this situation repeats,' +
516
- ' please report it to the developers.' ) ;
517
- }
518
- }
519
- log ( `got ${ itemRecords . length } item records` ) ;
520
- return sortByShelvingOrder ( itemRecords ) ;
475
+ // Do preliminary query to get the number of records.
476
+ let endpoint = makeQuery ( ) ;
477
+ let results = fetchJSON ( endpoint , tenantId , token ) ;
478
+ log ( `FOLIO has ${ results . totalRecords } items for ${ cn } at location ${ locationId } ` ) ;
479
+
480
+ // Now get the records.
481
+ if ( results . totalRecords > 0 ) {
482
+ return getRecordsForQuery ( makeQuery , results . totalRecords , tenantId , token ) ;
521
483
} else {
522
484
// Get the location name so we can write it in the error message.
523
485
const locName = getLocationsList ( ) . find ( el => ( el . id == locationId ) ) . name ;
@@ -535,6 +497,37 @@ function getItemsForCN(cn, locationId) {
535
497
}
536
498
}
537
499
500
+ /**
501
+ * Takes a function object, a total numbef of records to get, and the
502
+ * tenant ID and token, then iterates to get all the Folio records, and
503
+ * finally returns the array of record objects.
504
+ *
505
+ * The first argument (makeQuery) must be a function object that takes
506
+ * two parameters: the "limit" value to a Folio call, and the "offset"
507
+ * value.
508
+ */
509
+ function getRecordsForQuery ( makeQuery , totalRecords , tenantId , token ) {
510
+ let records = [ ] ;
511
+ let results ;
512
+ for ( let offset = 0 ; offset <= totalRecords ; offset += 100 ) {
513
+ results = fetchJSON ( makeQuery ( 100 , offset ) , tenantId , token ) ;
514
+ if ( results . items ) {
515
+ records . push . apply ( records , results . items ) ;
516
+ } else {
517
+ quit ( `Failed to get complete set of records` ,
518
+ ' Boffo unexpectedly received an empty batch from FOLIO. It' +
519
+ ' may be due to a sudden network glitch or other temporary' +
520
+ ' failure, or it may indicate a deeper problem. Please wait' +
521
+ ' a few seconds, then try again. If this situation repeats,' +
522
+ ' please report it to the developers.' ) ;
523
+ }
524
+ if ( offset > 0 && ( offset % 5000 ) == 0 ) {
525
+ note ( `Fetched ${ offset . toLocaleString ( ) } records so far and still going …` , 30 ) ;
526
+ }
527
+ }
528
+ return records ;
529
+ }
530
+
538
531
/**
539
532
* Takes a list of items (assumed to be sorted by effectiveShelvingOrder) and
540
533
* returns the first value of effectiveShelvingOrder found.
0 commit comments