fetchAndStoreLogPage: walk prev chain to fetch full vault history

This commit is contained in:
Keith Solomon
2026-08-07 19:10:38 -05:00
parent a19058c6e9
commit a6efb7d63d
+13 -5
View File
@@ -254,18 +254,26 @@ function fetchAndStoreLogPage($pdo, $user, $url) {
throw new ApiKeyMissingException("User does not have an API key configured."); throw new ApiKeyMissingException("User does not have an API key configured.");
} }
$responseData = executeApiCall($url, USER_KEYS[$user]);
validateApiResponse($responseData);
$insertStmt = $pdo->prepare( $insertStmt = $pdo->prepare(
'INSERT INTO vault (id, user, timestamp, description, amount, running_balance) ' 'INSERT INTO vault (id, user, timestamp, description, amount, running_balance) '
. 'VALUES (: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'
); );
do {
$responseData = executeApiCall($url, USER_KEYS[$user]);
validateApiResponse($responseData);
processLogEntries($responseData['log'], $user, $insertStmt); processLogEntries($responseData['log'], $user, $insertStmt);
return $responseData['_metadata']['links']['next'] ?? null; // 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);
} }
/** /**