vaultLoop: sum per-user live /money balances for the all-users path
This commit is contained in:
+27
-20
@@ -205,32 +205,39 @@ function fetchVaultRecords($user = null) {
|
|||||||
* @return int The total balance of the given user (or all users if no user is given).
|
* @return int The total balance of the given user (or all users if no user is given).
|
||||||
*/
|
*/
|
||||||
function vaultLoop ($name=null) {
|
function vaultLoop ($name=null) {
|
||||||
$liveBalance = $name === null
|
if ($name !== null) {
|
||||||
? null
|
// Per-user path: prefer live /money balance, fall back to MAX(running_balance).
|
||||||
: fetchLiveVaultBalance($name);
|
$liveBalance = fetchLiveVaultBalance($name);
|
||||||
|
if ($liveBalance !== null) {
|
||||||
|
return $liveBalance;
|
||||||
|
}
|
||||||
|
|
||||||
if ($liveBalance !== null) {
|
$pdo = getDatabaseConnection();
|
||||||
return $liveBalance;
|
$stmt = $pdo->prepare('SELECT MAX(running_balance) FROM vault WHERE user = :user');
|
||||||
}
|
$stmt->bindValue(':user', $name);
|
||||||
|
$stmt->execute();
|
||||||
// Fallback: derive from stored entries. Used when the live /money call
|
|
||||||
// fails or is not configured for the user.
|
|
||||||
$pdo = getDatabaseConnection();
|
|
||||||
|
|
||||||
if ($name === null) {
|
|
||||||
$stmt = $pdo->query(
|
|
||||||
'SELECT COALESCE(SUM(max_balance), 0) FROM '
|
|
||||||
. '(SELECT MAX(running_balance) AS max_balance FROM vault GROUP BY user)'
|
|
||||||
);
|
|
||||||
|
|
||||||
return (int)$stmt->fetchColumn();
|
return (int)$stmt->fetchColumn();
|
||||||
}
|
}
|
||||||
|
|
||||||
$stmt = $pdo->prepare('SELECT MAX(running_balance) FROM vault WHERE user = :user');
|
// All-users path: sum the live /money value for each user. For any user
|
||||||
$stmt->bindValue(':user', $name);
|
// whose /money call failed, fall back to MAX(running_balance) for that user.
|
||||||
$stmt->execute();
|
$pdo = getDatabaseConnection();
|
||||||
|
$sum = 0;
|
||||||
|
foreach (USER_KEYS as $user => $apiKey) {
|
||||||
|
$live = fetchLiveVaultBalance($user);
|
||||||
|
if ($live !== null) {
|
||||||
|
$sum += $live;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
return (int)$stmt->fetchColumn();
|
$stmt = $pdo->prepare('SELECT MAX(running_balance) FROM vault WHERE user = :user');
|
||||||
|
$stmt->bindValue(':user', $user);
|
||||||
|
$stmt->execute();
|
||||||
|
$sum += (int)$stmt->fetchColumn();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user