fetchAndStoreLogPage: walk prev chain to fetch full vault history
This commit is contained in:
+14
-6
@@ -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'
|
||||||
);
|
);
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user