fetchAndStoreLogPage: walk prev chain to fetch full vault history
This commit is contained in:
+13
-5
@@ -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'
|
||||
);
|
||||
|
||||
do {
|
||||
$responseData = executeApiCall($url, USER_KEYS[$user]);
|
||||
validateApiResponse($responseData);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user