Files
telegram-shop/src/handlers/userHandlers/wallet/refreshHandler.js
NW 6ce8da257a fix: clean chat navigation — edit messages instead of sending new ones
All callback handlers now use editOrSendCallback() to edit the existing
message in-place instead of bot.sendMessage() which creates new messages
and clutters the chat. If edit fails (message too old), the old message
is deleted and a new one sent.

Added src/utils/messageUtils.js with:
- editOrSendCallback(callbackQuery, text, options) — edit or fallback
- editOrSend(chatId, messageId, text, options) — edit or fallback
- deleteAndSend(chatId, messageId, text, options) — delete then send

Fixed handlers:
- userProductHandler: handleBuyProduct errors, handlePay validation/stock errors
- userPurchaseHandler: viewPurchase errors, handleConfirmReceived errors, handlePurchaseListPage errors
- userLocationHandler: all error paths now edit in-place
- userDeletionHandler: both error paths now edit in-place
- wallet/balanceHandler: showBalance error (text command, acceptable)
- wallet/refreshHandler: user not found and refresh errors
- wallet/topUpHandler: wallet loading error
- wallet/createHandler: invalid wallet type error
- wallet/historyHandler: both transaction history error paths
- wallet/archiveHandler: archived wallets error
2026-06-24 20:45:39 +01:00

78 lines
3.5 KiB
JavaScript

import db from '../../../config/database.js';
import WalletUtils from '../../../utils/walletUtils.js';
import UserService from '../../../services/userService.js';
import bot from '../../../context/bot.js';
import logger from '../../../utils/logger.js';
import { editOrSendCallback } from '../../../utils/messageUtils.js';
export default class RefreshHandler {
static async handleRefreshBalance(callbackQuery) {
const chatId = callbackQuery.message.chat.id;
const messageId = callbackQuery.message.message_id;
try {
await bot.answerCallbackQuery(callbackQuery.id, { text: '🔄 Refreshing balances...' });
const user = await UserService.getUserByTelegramId(callbackQuery.from.id.toString());
if (!user) {
await editOrSendCallback(callbackQuery, 'Profile not found. Please use /start to create one.');
return;
}
const activeWallets = await db.allAsync(`
SELECT wallet_type, address FROM crypto_wallets
WHERE user_id = ? AND wallet_type NOT LIKE '%#_%' ESCAPE '#'
`, [user.id]);
const walletAddresses = {
btc: activeWallets.find(w => w.wallet_type === 'BTC')?.address || null,
ltc: activeWallets.find(w => w.wallet_type === 'LTC')?.address || null,
eth: activeWallets.find(w => w.wallet_type === 'ETH')?.address || null,
usdt: activeWallets.find(w => w.wallet_type === 'USDT')?.address || null,
usdc: activeWallets.find(w => w.wallet_type === 'USDC')?.address || null,
};
const walletUtilsInstance = new WalletUtils(
walletAddresses.btc, walletAddresses.ltc, walletAddresses.eth,
walletAddresses.usdt, walletAddresses.usdc,
user.id, Date.now() - 30 * 24 * 60 * 60 * 1000
);
const balances = await walletUtilsInstance.getAllBalancesExt();
const walletTypeMap = { BTC: 'btc', LTC: 'ltc', ETH: 'eth', USDT: 'usdt', USDC: 'usdc' };
for (const [type, balance] of Object.entries(balances)) {
const address = walletAddresses[walletTypeMap[type]];
if (!address) continue;
const currentBalance = await db.getAsync(
'SELECT balance FROM crypto_wallets WHERE user_id = ? AND address = ?',
[user.id, address]
);
if (currentBalance?.balance !== balance.amount) {
await db.runAsync(
'UPDATE crypto_wallets SET balance = ? WHERE user_id = ? AND address = ?',
[balance.amount, user.id, address]
);
}
}
await UserService.recalculateUserBalanceByTelegramId(callbackQuery.from.id);
const BalanceHandler = (await import('./balanceHandler.js')).default;
await BalanceHandler.showBalance({
chat: { id: chatId }, from: { id: callbackQuery.from.id }
});
await bot.deleteMessage(chatId, messageId);
} catch (error) {
logger.error({ err: error }, 'Error in handleRefreshBalance');
await bot.answerCallbackQuery(callbackQuery.id, { text: '❌ Error refreshing balances.' });
await editOrSendCallback(callbackQuery, '❌ Error refreshing balances. Please try again.', {
reply_markup: { inline_keyboard: [[{ text: '« Back', callback_data: 'back_to_balance' }]] }
});
}
}
}