@@ -47,8 +47,7 @@ function updateTransactionsAll() {
4747 let firstTransactionDate = '1970-01-01' ;
4848 var today = new Date ( ) ;
4949 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 ) ;
5251 let { LMCategories, plaidAccountNames, assetAccountNames, plaidAccounts, assetAccounts} = loadCategoriesAndAccounts ( ) ;
5352 let { parsedTransactions_2d, months, days} = parseTransactions ( transactions , LMCategories , plaidAccountNames , assetAccountNames ) ;
5453 transactionsAllSheet = createTransactionsAllSheet ( ) ;
@@ -390,6 +389,10 @@ function loadTransactions(startDate, endDate) {
390389 try {
391390 if ( result != false ) {
392391 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+ }
393396 return result . transactions ;
394397 } else {
395398 if ( LMdebug ) { Logger . log ( 'loadTransactions empty for %s' , url ) ; }
@@ -407,6 +410,56 @@ function loadTransactions(startDate, endDate) {
407410 }
408411}
409412
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+
410463function coalesce ( object , period , parsedTransaction , name ) {
411464 if ( object . hasOwnProperty ( period ) ) {
412465 if ( object [ period ] . hasOwnProperty ( name ) ) {
0 commit comments