diff --git a/src/handlers/userHandlers/userProductHandler.js b/src/handlers/userHandlers/userProductHandler.js index c068c28..0b940eb 100644 --- a/src/handlers/userHandlers/userProductHandler.js +++ b/src/handlers/userHandlers/userProductHandler.js @@ -539,6 +539,22 @@ export default class UserProductHandler { // Получение баланса пользователя const userBalance = await UserService.getUserBalance(user.id); + // Проверка баланса пользователя + if (userBalance <= 0) { + 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; + } + if (userBalance < totalPrice) { await bot.sendMessage( chatId, diff --git a/src/services/userService.js b/src/services/userService.js index 2541724..3e12c09 100644 --- a/src/services/userService.js +++ b/src/services/userService.js @@ -51,31 +51,23 @@ class UserService { static async createUser(userData) { try { const existingUser = await this.getUserByTelegramId(userData?.telegram_id); - + if (existingUser) { return existingUser.id; } - + const fields = Object.keys(userData); - const values = []; - - for (const field of fields) { - values.push(userData[field]); - } - - const marks = []; - for (let i = 0; i < fields.length; i++) { - marks.push("?"); - } - - const query = [ - `INSERT INTO users (${fields.join(', ')})`, - `VALUES (${marks.join(', ')})` - ].join(""); - + const marks = Array(fields.length).fill('?'); + + const query = ` + INSERT INTO users (${fields.join(', ')}) + VALUES (${marks.join(', ')}) + `; + await db.runAsync('BEGIN TRANSACTION'); - const result = await db.runAsync(query, [values]); + const result = await db.runAsync(query, Object.values(userData)); await db.runAsync('COMMIT'); + return result.lastID; } catch (error) { await db.runAsync('ROLLBACK');