@@ -47,8 +47,7 @@ function updateTransactionsAll() {
47
47
let firstTransactionDate = '1970-01-01' ;
48
48
var today = new Date ( ) ;
49
49
today = Utilities . formatDate ( today , LMSpreadsheetTimezone , "yyyy-MM-dd" ) ;
50
- let transactions = loadTransactions ( firstTransactionDate , today ) ;
51
- if ( transactions == false ) { throw new Error ( "problem loading transactions" ) ; }
50
+ let transactions = loadTransactionsPaginated ( firstTransactionDate , today ) ;
52
51
let { LMCategories, plaidAccountNames, assetAccountNames, plaidAccounts, assetAccounts} = loadCategoriesAndAccounts ( ) ;
53
52
let { parsedTransactions_2d, months, days} = parseTransactions ( transactions , LMCategories , plaidAccountNames , assetAccountNames ) ;
54
53
transactionsAllSheet = createTransactionsAllSheet ( ) ;
@@ -390,6 +389,10 @@ function loadTransactions(startDate, endDate) {
390
389
try {
391
390
if ( result != false ) {
392
391
if ( result . transactions . length > 0 ) {
392
+ if ( result . has_more ) {
393
+ if ( LMdebug ) { Logger . log ( 'loadTransactions returned more for %s' , url ) ; }
394
+ throw new Error ( 'loadTransactions returned more for ' + url ) ;
395
+ }
393
396
return result . transactions ;
394
397
} else {
395
398
if ( LMdebug ) { Logger . log ( 'loadTransactions empty for %s' , url ) ; }
@@ -407,6 +410,56 @@ function loadTransactions(startDate, endDate) {
407
410
}
408
411
}
409
412
413
+ function colateTransactions ( transactions , new_transactions ) {
414
+ for ( const transaction of new_transactions ) {
415
+ const insertionIndex = transactions . findIndex ( t => t . date > transaction . date ) ;
416
+
417
+ if ( insertionIndex === - 1 ) {
418
+ transactions . push ( transaction ) ;
419
+ } else {
420
+ transactions . splice ( insertionIndex , 0 , transaction ) ;
421
+ }
422
+ }
423
+ }
424
+
425
+ function loadTransactionsWithOffset ( startDate , endDate , offset = 0 ) {
426
+ let url = 'https://dev.lunchmoney.app/v1/transactions?&is_group=false&debit_as_negative=true&pending=true&limit=1000&start_date=' + startDate + '&end_date=' + endDate + '&offset=' + offset ;
427
+ let result = apiRequest ( url ) ;
428
+ try {
429
+ if ( result != false ) {
430
+ if ( result . transactions . length > 0 ) {
431
+ return {
432
+ transactions : result . transactions ,
433
+ has_more : result . has_more
434
+ } ;
435
+ } else {
436
+ if ( LMdebug ) { Logger . log ( 'loadTransactions empty for %s' , url ) ; }
437
+ throw new Error ( 'loadTransactions empty for ' + url ) ;
438
+ }
439
+ } else {
440
+ throw new Error ( 'loadTransactions failed for ' + url ) ;
441
+ }
442
+ } catch ( err ) {
443
+ if ( LMdebug ) { Logger . log ( 'result %s' , result ) ; }
444
+ if ( LMdebug ) { Logger . log ( 'loadTransactions failed with error %s' , err . message ) ; }
445
+ throw new Error ( 'loadTransactions failed with error ' + err . message + ' for ' + url ) ;
446
+ }
447
+ }
448
+
449
+ function loadTransactionsPaginated ( startDate , endDate ) {
450
+ let offset = 0 ;
451
+ let result = loadTransactionsWithOffset ( startDate , endDate , offset ) ;
452
+ let transactions = result ? result . transactions : [ ] ;
453
+ let has_more = result ? result . has_more : false ;
454
+ while ( has_more ) {
455
+ offset += 1000 ;
456
+ result = loadTransactionsWithOffset ( startDate , endDate , offset ) ;
457
+ colateTransactions ( transactions , result . transactions ) ;
458
+ has_more = result . has_more ;
459
+ }
460
+ return transactions ;
461
+ }
462
+
410
463
function coalesce ( object , period , parsedTransaction , name ) {
411
464
if ( object . hasOwnProperty ( period ) ) {
412
465
if ( object [ period ] . hasOwnProperty ( name ) ) {
0 commit comments