Update handleBuyProduct

This commit is contained in:
NW 2024-12-14 12:58:38 +00:00
parent eea5d9b9e7
commit 3843dcb094

View File

@ -496,13 +496,12 @@ Subcategory: ${product.subcategory_name}
const state = userStates.get(chatId);
try {
const user = await UserService.getUserByTelegramId(telegramId)
const user = await UserService.getUserByTelegramId(telegramId);
if (!user) {
throw new Error('User not found');
}
const product = await ProductService.getProductById(productId);
if (!product) {
throw new Error('Product not found');
}
@ -510,7 +509,24 @@ Subcategory: ${product.subcategory_name}
const quantity = state?.quantity || 1;
const totalPrice = product.price * quantity;
// Get user's crypto wallets with balances
// Проверка баланса пользователя
const userBalance = await UserService.getUserBalance(user.id);
if (userBalance < totalPrice) {
await bot.sendMessage(
chatId,
`❌ Insufficient balance. Your current balance is $${userBalance}. You need $${totalPrice} to complete this purchase.`,
{
reply_markup: {
inline_keyboard: [[
{ text: '💰 Top Up Balance', callback_data: 'top_up_wallet' }
]]
}
}
);
return;
}
// Получение криптокошельков пользователя
const cryptoWallets = await db.allAsync(`
SELECT wallet_type, address
FROM crypto_wallets
@ -525,7 +541,7 @@ Subcategory: ${product.subcategory_name}
{
reply_markup: {
inline_keyboard: [[
{text: ' Add Wallet', callback_data: 'add_wallet'}
{ text: ' Add Wallet', callback_data: 'add_wallet' }
]]
}
}
@ -536,7 +552,7 @@ Subcategory: ${product.subcategory_name}
const keyboard = {
inline_keyboard: [
[{ text: `Pay`, callback_data: `pay_with_main_${productId}_${quantity}` }],
[{text: '« Cancel', callback_data: `shop_product_${productId}`}]
[{ text: '« Cancel', callback_data: `shop_product_${productId}` }]
]
};