From 5b8abd8bbcd4bcf186c03ce325372657fbb3dc10 Mon Sep 17 00:00:00 2001 From: Keith Solomon Date: Fri, 7 Aug 2026 17:34:37 -0500 Subject: [PATCH] Revert vaultLoop to per-user SQL MAX(running_balance) --- includes/utilities.php | 37 ++++++++++--------------------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/includes/utilities.php b/includes/utilities.php index e44b2e8..c27820e 100644 --- a/includes/utilities.php +++ b/includes/utilities.php @@ -205,39 +205,22 @@ function fetchVaultRecords($user = null) { * @return int The total balance of the given user (or all users if no user is given). */ function vaultLoop ($name=null) { - if ($name !== null) { - // Per-user path: prefer live /money balance, fall back to MAX(running_balance). - $liveBalance = fetchLiveVaultBalance($name); - if ($liveBalance !== null) { - return $liveBalance; - } + $pdo = getDatabaseConnection(); - $pdo = getDatabaseConnection(); - $stmt = $pdo->prepare('SELECT MAX(running_balance) FROM vault WHERE user = :user'); - $stmt->bindValue(':user', $name); - $stmt->execute(); + 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(); } - // All-users path: sum the live /money value for each user. For any user - // whose /money call failed, fall back to MAX(running_balance) for that user. - $pdo = getDatabaseConnection(); - $sum = 0; - foreach (USER_KEYS as $user => $apiKey) { - $live = fetchLiveVaultBalance($user); - if ($live !== null) { - $sum += $live; - continue; - } + $stmt = $pdo->prepare('SELECT MAX(running_balance) FROM vault WHERE user = :user'); + $stmt->bindValue(':user', $name); + $stmt->execute(); - $stmt = $pdo->prepare('SELECT MAX(running_balance) FROM vault WHERE user = :user'); - $stmt->bindValue(':user', $user); - $stmt->execute(); - $sum += (int)$stmt->fetchColumn(); - } - - return $sum; + return (int)$stmt->fetchColumn(); } /**