From a6efb7d63d1c0dbde2565e83ab34a23d5c864bfe Mon Sep 17 00:00:00 2001 From: Keith Solomon Date: Fri, 7 Aug 2026 19:10:38 -0500 Subject: [PATCH] fetchAndStoreLogPage: walk prev chain to fetch full vault history --- includes/utilities.php | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/includes/utilities.php b/includes/utilities.php index 78abca4..1a90ed3 100644 --- a/includes/utilities.php +++ b/includes/utilities.php @@ -254,18 +254,26 @@ function fetchAndStoreLogPage($pdo, $user, $url) { throw new ApiKeyMissingException("User does not have an API key configured."); } - $responseData = executeApiCall($url, USER_KEYS[$user]); - validateApiResponse($responseData); - $insertStmt = $pdo->prepare( 'INSERT INTO vault (id, user, timestamp, description, amount, running_balance) ' . 'VALUES (:id, :user, :timestamp, :description, :amount, :running_balance) ' - . 'ON CONFLICT(id) DO NOTHING' + . 'ON CONFLICT(id) DO UPDATE SET running_balance = EXCLUDED.running_balance, amount = EXCLUDED.amount' ); - processLogEntries($responseData['log'], $user, $insertStmt); + do { + $responseData = executeApiCall($url, USER_KEYS[$user]); + validateApiResponse($responseData); - return $responseData['_metadata']['links']['next'] ?? null; + processLogEntries($responseData['log'], $user, $insertStmt); + + // Follow the older-pages chain so the very first call (which gets the + // most recent page) also retrieves every older entry. After the prev + // chain is exhausted, fall back to the newer-pages chain in case the + // caller passed a `from=` URL and the result has a next link. + $url = $responseData['_metadata']['links']['prev'] + ?? $responseData['_metadata']['links']['next'] + ?? null; + } while ($url !== null); } /**