Merge pull request 'feature/admin-section' (#12) from feature/admin-section into main

Reviewed-on: #12
This commit was merged in pull request #12.
This commit is contained in:
NW
2024-11-14 19:32:30 +00:00
7 changed files with 651 additions and 393 deletions

Binary file not shown.

View File

@@ -90,6 +90,7 @@ const initDb = async () => {
CREATE TABLE IF NOT EXISTS users ( CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
telegram_id TEXT UNIQUE NOT NULL, telegram_id TEXT UNIQUE NOT NULL,
username TEXT,
country TEXT, country TEXT,
city TEXT, city TEXT,
district TEXT, district TEXT,

View File

@@ -3,22 +3,17 @@ import config from '../config/config.js';
import db from '../config/database.js'; import db from '../config/database.js';
export default class AdminUserHandler { export default class AdminUserHandler {
constructor(bot) { constructor(bot) {
this.bot = bot; this.bot = bot;
}
isAdmin(userId) {
return config.ADMIN_IDS.includes(userId.toString());
}
async handleUserList(msg) {
if (!this.isAdmin(msg.from.id)) {
await this.bot.sendMessage(msg.chat.id, 'Unauthorized access.');
return;
} }
try { isAdmin(userId) {
const users = await db.allAsync(` return config.ADMIN_IDS.includes(userId.toString());
}
async calculateStatistics() {
try {
const users = await db.allAsync(`
SELECT SELECT
u.*, u.*,
COUNT(DISTINCT p.id) as total_purchases, COUNT(DISTINCT p.id) as total_purchases,
@@ -30,53 +25,120 @@ export default class AdminUserHandler {
LEFT JOIN transactions t ON u.id = t.user_id LEFT JOIN transactions t ON u.id = t.user_id
GROUP BY u.id GROUP BY u.id
ORDER BY u.created_at DESC ORDER BY u.created_at DESC
`); ` );
if (users.length === 0) {
await this.bot.sendMessage(msg.chat.id, 'No users registered yet.');
return;
}
// Calculate general statistics // Calculate general statistics
const totalUsers = users.length; const totalUsers = users.length;
const activeUsers = users.filter(u => u.total_purchases > 0).length; const activeUsers = users.filter(u => u.total_purchases > 0).length;
const totalBalance = users.reduce((sum, u) => sum + (u.total_balance || 0), 0); const totalBalance = users.reduce((sum, u) => sum + (u.total_balance || 0), 0);
const totalPurchases = users.reduce((sum, u) => sum + (u.total_purchases || 0), 0); const totalPurchases = users.reduce((sum, u) => sum + (u.total_purchases || 0), 0);
// Create statistics message // Create statistics message
let message = `📊 System Statistics\n\n`; let message = `📊 System Statistics\n\n`;
message += `👥 Total Users: ${totalUsers}\n`; message += `👥 Total Users: ${totalUsers}\n`;
message += `✅ Active Users: ${activeUsers}\n`; message += `✅ Active Users: ${activeUsers}\n`;
message += `💰 Total Balance: $${totalBalance.toFixed(2)}\n`; message += `💰 Total Balance: $${totalBalance.toFixed(2)}\n`;
message += `🛍 Total Purchases: ${totalPurchases}\n\n`; message += `🛍 Total Purchases: ${totalPurchases}`;
message += `Select a user from the list below:`;
// Create inline keyboard with user list return message;
const keyboard = { } catch (error) {
inline_keyboard: users.map(user => [{ return null
text: `ID: ${user.telegram_id} | Balance: $${user.total_balance || 0}`, }
callback_data: `view_user_${user.telegram_id}`
}])
};
await this.bot.sendMessage(msg.chat.id, message, {
parse_mode: 'HTML',
reply_markup: keyboard
});
} catch (error) {
console.error('Error in handleUserList:', error);
await this.bot.sendMessage(msg.chat.id, 'Error loading user list. Please try again.');
} }
}
async handleViewUser(callbackQuery) { async viewUserPage(page) {
if (!this.isAdmin(callbackQuery.from.id)) return; const limit = 10;
const offset = (page || 0) * limit;
const userId = callbackQuery.data.replace('view_user_', ''); const previousPage = page > 0 ? page - 1 : 0;
const chatId = callbackQuery.message.chat.id; const nextPage = page + 1;
try { try {
const userStats = await db.getAsync(` const users = await db.allAsync(`
SELECT
u.*,
COUNT(DISTINCT p.id) as total_purchases,
COUNT(DISTINCT cw.id) as total_wallets,
COALESCE(SUM(t.amount), 0) as total_balance
FROM users u
LEFT JOIN purchases p ON u.id = p.user_id
LEFT JOIN crypto_wallets cw ON u.id = cw.user_id
LEFT JOIN transactions t ON u.id = t.user_id
GROUP BY u.id
ORDER BY u.created_at DESC
LIMIT ?
OFFSET ?
`, [limit, offset]);
if ((users.length === 0) && (page == 0)) {
return {text: 'No users registered yet.'};
}
if ((users.length === 0) && (page > 0)) {
return await this.viewUserPage(page - 1);
}
const statistics = await this.calculateStatistics()
const message = `${statistics}\n\nSelect a user from the list below:`;
// Create inline keyboard with user list
const keyboard = {
inline_keyboard: users.map(user => [{
text: `ID: ${user.telegram_id} | Nickname: ${user.username ? "@" + user.username : "None"} | Balance: $${user.total_balance || 0}`,
callback_data: `view_user_${user.telegram_id}`
}])
};
keyboard.inline_keyboard.push([
{text: `«`, callback_data: `list_users_${previousPage}`},
{text: `»`, callback_data: `list_users_${nextPage}`},
])
return {text: message, markup: keyboard}
} catch (error) {
console.error('Error in handleUserList:', error);
return {text: 'Error loading user list. Please try again.'}
}
}
async handleUserList(msg) {
if (!this.isAdmin(msg.from.id)) {
await this.bot.sendMessage(msg.chat.id, 'Unauthorized access.');
return;
}
const {text, markup} = await this.viewUserPage(0);
await this.bot.sendMessage(msg.chat.id, text, {reply_markup: markup})
}
async handleUserListPage(callbackQuery) {
if (!this.isAdmin(callbackQuery.from.id)) {
return;
}
const chatId = callbackQuery.message.chat.id;
const page = parseInt(callbackQuery.data.replace('list_users_', ''));
try {
const {text, markup} = await this.viewUserPage(page);
await this.bot.editMessageText(text, {
chat_id: chatId,
message_id: callbackQuery.message.message_id,
reply_markup: markup,
parse_mode: 'HTML'
});
} catch (e) {
return;
}
}
async handleViewUser(callbackQuery) {
if (!this.isAdmin(callbackQuery.from.id)) return;
const userId = callbackQuery.data.replace('view_user_', '');
const chatId = callbackQuery.message.chat.id;
try {
const userStats = await db.getAsync(`
SELECT SELECT
u.*, u.*,
COUNT(DISTINCT p.id) as purchase_count, COUNT(DISTINCT p.id) as purchase_count,
@@ -90,14 +152,14 @@ export default class AdminUserHandler {
WHERE u.telegram_id = ? WHERE u.telegram_id = ?
GROUP BY u.id GROUP BY u.id
`, [userId]); `, [userId]);
if (!userStats) {
await this.bot.sendMessage(chatId, 'User not found.');
return;
}
// Get recent transactions if (!userStats) {
const transactions = await db.allAsync(` await this.bot.sendMessage(chatId, 'User not found.');
return;
}
// Get recent transactions
const transactions = await db.allAsync(`
SELECT t.amount, t.created_at, t.wallet_type, t.tx_hash SELECT t.amount, t.created_at, t.wallet_type, t.tx_hash
FROM transactions t FROM transactions t
JOIN users u ON t.user_id = u.id JOIN users u ON t.user_id = u.id
@@ -106,8 +168,8 @@ export default class AdminUserHandler {
LIMIT 5 LIMIT 5
`, [userId]); `, [userId]);
// Get recent purchases // Get recent purchases
const purchases = await db.allAsync(` const purchases = await db.allAsync(`
SELECT p.quantity, p.total_price, p.purchase_date, SELECT p.quantity, p.total_price, p.purchase_date,
pr.name as product_name pr.name as product_name
FROM purchases p FROM purchases p
@@ -118,7 +180,7 @@ export default class AdminUserHandler {
LIMIT 5 LIMIT 5
`, [userId]); `, [userId]);
const message = ` const message = `
👤 User Profile: 👤 User Profile:
ID: ${userId} ID: ${userId}
@@ -139,133 +201,136 @@ ${purchases.map(p => ` • ${p.product_name} x${p.quantity} - $${p.total_price}
📅 Registered: ${new Date(userStats.created_at).toLocaleString()} 📅 Registered: ${new Date(userStats.created_at).toLocaleString()}
`; `;
const keyboard = { const keyboard = {
inline_keyboard: [ inline_keyboard: [
[ [
{ text: '💰 Edit Balance', callback_data: `edit_user_balance_${userId}` }, {text: '💰 Edit Balance', callback_data: `edit_user_balance_${userId}`},
{ text: '📍 Edit Location', callback_data: `edit_user_location_${userId}` } {text: '📍 Edit Location', callback_data: `edit_user_location_${userId}`}
], ],
[ [
{ text: '🚫 Block User', callback_data: `block_user_${userId}` }, {text: '🚫 Block User', callback_data: `block_user_${userId}`},
{ text: '❌ Delete User', callback_data: `delete_user_${userId}` } {text: '❌ Delete User', callback_data: `delete_user_${userId}`}
], ],
[{ text: '« Back to User List', callback_data: 'admin_users' }] [{text: '« Back to User List', callback_data: 'admin_users'}]
] ]
}; };
await this.bot.editMessageText(message, { await this.bot.editMessageText(message, {
chat_id: chatId, chat_id: chatId,
message_id: callbackQuery.message.message_id, message_id: callbackQuery.message.message_id,
reply_markup: keyboard, reply_markup: keyboard,
parse_mode: 'HTML' parse_mode: 'HTML'
}); });
} catch (error) { } catch (error) {
console.error('Error in handleViewUser:', error); console.error('Error in handleViewUser:', error);
await this.bot.sendMessage(chatId, 'Error loading user details. Please try again.'); await this.bot.sendMessage(chatId, 'Error loading user details. Please try again.');
}
}
async handleDeleteUser(callbackQuery) {
if (!this.isAdmin(callbackQuery.from.id)) return;
const userId = callbackQuery.data.replace('delete_user_', '');
const chatId = callbackQuery.message.chat.id;
try {
const keyboard = {
inline_keyboard: [
[
{ text: '✅ Confirm Delete', callback_data: `confirm_delete_user_${userId}` },
{ text: '❌ Cancel', callback_data: `view_user_${userId}` }
]
]
};
await this.bot.editMessageText(
`⚠️ Are you sure you want to delete user ${userId}?\n\nThis action will:\n- Delete all user data\n- Remove all wallets\n- Erase purchase history\n\nThis action cannot be undone!`,
{
chat_id: chatId,
message_id: callbackQuery.message.message_id,
reply_markup: keyboard,
parse_mode: 'HTML'
} }
);
} catch (error) {
console.error('Error in handleDeleteUser:', error);
await this.bot.sendMessage(chatId, 'Error processing delete request. Please try again.');
} }
}
async handleConfirmDelete(callbackQuery) { async handleDeleteUser(callbackQuery) {
if (!this.isAdmin(callbackQuery.from.id)) return; if (!this.isAdmin(callbackQuery.from.id)) return;
const userId = callbackQuery.data.replace('confirm_delete_user_', ''); const userId = callbackQuery.data.replace('delete_user_', '');
const chatId = callbackQuery.message.chat.id; const chatId = callbackQuery.message.chat.id;
try { try {
await User.delete(userId); const keyboard = {
inline_keyboard: [
[
{text: '✅ Confirm Delete', callback_data: `confirm_delete_user_${userId}`},
{text: '❌ Cancel', callback_data: `view_user_${userId}`}
]
]
};
const keyboard = { await this.bot.editMessageText(
inline_keyboard: [ `⚠️ Are you sure you want to delete user ${userId}?\n\nThis action will:\n- Delete all user data\n- Remove all wallets\n- Erase purchase history\n\nThis action cannot be undone!`,
[{ text: '« Back to User List', callback_data: 'admin_users' }] {
] chat_id: chatId,
}; message_id: callbackQuery.message.message_id,
reply_markup: keyboard,
await this.bot.editMessageText( parse_mode: 'HTML'
`✅ User ${userId} has been successfully deleted.`, }
{ );
chat_id: chatId, } catch (error) {
message_id: callbackQuery.message.message_id, console.error('Error in handleDeleteUser:', error);
reply_markup: keyboard await this.bot.sendMessage(chatId, 'Error processing delete request. Please try again.');
} }
);
} catch (error) {
console.error('Error in handleConfirmDelete:', error);
await this.bot.sendMessage(chatId, 'Error deleting user. Please try again.');
} }
}
async handleEditUserBalance(callbackQuery) { async handleConfirmDelete(callbackQuery) {
if (!this.isAdmin(callbackQuery.from.id)) return; if (!this.isAdmin(callbackQuery.from.id)) return;
const userId = callbackQuery.data.replace('edit_user_balance_', ''); const userId = callbackQuery.data.replace('confirm_delete_user_', '');
const chatId = callbackQuery.message.chat.id; const chatId = callbackQuery.message.chat.id;
try { try {
const user = await User.getById(userId); await User.delete(userId);
if (!user) {
await this.bot.sendMessage(chatId, 'User not found.');
return;
}
const wallets = await db.allAsync(` const keyboard = {
inline_keyboard: [
[{text: '« Back to User List', callback_data: 'admin_users'}]
]
};
await this.bot.editMessageText(
`✅ User ${userId} has been successfully deleted.`,
{
chat_id: chatId,
message_id: callbackQuery.message.message_id,
reply_markup: keyboard
}
);
} catch (error) {
console.error('Error in handleConfirmDelete:', error);
await this.bot.sendMessage(chatId, 'Error deleting user. Please try again.');
}
}
async handleEditUserBalance(callbackQuery) {
if (!this.isAdmin(callbackQuery.from.id)) return;
const userId = callbackQuery.data.replace('edit_user_balance_', '');
const chatId = callbackQuery.message.chat.id;
try {
const user = await User.getById(userId);
if (!user) {
await this.bot.sendMessage(chatId, 'User not found.');
return;
}
const wallets = await db.allAsync(`
SELECT wallet_type, address SELECT wallet_type, address
FROM crypto_wallets FROM crypto_wallets
WHERE user_id = (SELECT id FROM users WHERE telegram_id = ?) WHERE user_id = (SELECT id FROM users WHERE telegram_id = ?)
ORDER BY wallet_type ORDER BY wallet_type
`, [userId]); `, [userId]);
const keyboard = { const keyboard = {
inline_keyboard: [ inline_keyboard: [
...wallets.map(wallet => [ ...wallets.map(wallet => [
{ text: `${wallet.wallet_type}: ${wallet.address}`, callback_data: `edit_wallet_${wallet.wallet_type}` } {
]), text: `${wallet.wallet_type}: ${wallet.address}`,
[{ text: '« Back', callback_data: `view_user_${userId}` }] callback_data: `edit_wallet_${wallet.wallet_type}`
] }
}; ]),
[{text: '« Back', callback_data: `view_user_${userId}`}]
]
};
await this.bot.editMessageText( await this.bot.editMessageText(
`Select wallet to edit for user ${userId}:`, `Select wallet to edit for user ${userId}:`,
{ {
chat_id: chatId, chat_id: chatId,
message_id: callbackQuery.message.message_id, message_id: callbackQuery.message.message_id,
reply_markup: keyboard reply_markup: keyboard
}
);
} catch (error) {
console.error('Error in handleEditUserBalance:', error);
await this.bot.sendMessage(chatId, 'Error loading user wallets. Please try again.');
} }
);
} catch (error) {
console.error('Error in handleEditUserBalance:', error);
await this.bot.sendMessage(chatId, 'Error loading user wallets. Please try again.');
} }
}
} }

View File

@@ -0,0 +1,172 @@
import db from '../config/database.js';
import config from "../config/config.js";
export default class AdminUserLocationHandler {
constructor(bot) {
this.bot = bot;
}
isAdmin(userId) {
return config.ADMIN_IDS.includes(userId.toString());
}
async handleEditUserLocation(callbackQuery) {
if (!this.isAdmin(callbackQuery.from.id)) return;
const userId = callbackQuery.data.replace('edit_user_location_', '');
const chatId = callbackQuery.message.chat.id;
const messageId = callbackQuery.message.message_id;
try {
const countries = await db.allAsync('SELECT DISTINCT country FROM locations ORDER BY country');
if (countries.length === 0) {
await this.bot.editMessageText(
'No locations available yet.',
{
chat_id: chatId,
message_id: messageId,
reply_markup: {
inline_keyboard: [[
{text: '« Back to User', callback_data: `view_user_${userId}`}
]]
}
}
);
return;
}
const keyboard = {
inline_keyboard: [
...countries.map(loc => [{
text: loc.country,
callback_data: `edit_user_country_${loc.country}_${userId}`
}]),
[{text: '« Back to User', callback_data: `view_user_${userId}`}]
]
};
await this.bot.editMessageText(
'🌍 Select user country:',
{
chat_id: chatId,
message_id: messageId,
reply_markup: keyboard
}
);
} catch (error) {
console.error('Error in handleSetLocation:', error);
await this.bot.sendMessage(chatId, 'Error loading countries. Please try again.');
}
}
async handleEditUserCountry(callbackQuery) {
if (!this.isAdmin(callbackQuery.from.id)) return;
const chatId = callbackQuery.message.chat.id;
const messageId = callbackQuery.message.message_id;
const [country, userId] = callbackQuery.data.replace('edit_user_country_', '').split("_");
try {
const cities = await db.allAsync(
'SELECT DISTINCT city FROM locations WHERE country = ? ORDER BY city',
[country]
);
const keyboard = {
inline_keyboard: [
...cities.map(loc => [{
text: loc.city,
callback_data: `edit_user_city_${country}_${loc.city}_${userId}`
}]),
[{text: '« Back to Countries', callback_data: `edit_user_location_${userId}`}]
]
};
await this.bot.editMessageText(
`🏙 Select city in ${country}:`,
{
chat_id: chatId,
message_id: messageId,
reply_markup: keyboard
}
);
} catch (error) {
console.error('Error in handleSetCountry:', error);
await this.bot.sendMessage(chatId, 'Error loading cities. Please try again.');
}
}
async handleEditUserCity(callbackQuery) {
if (!this.isAdmin(callbackQuery.from.id)) return;
const chatId = callbackQuery.message.chat.id;
const messageId = callbackQuery.message.message_id;
const [country, city, userId] = callbackQuery.data.replace('edit_user_city_', '').split('_');
try {
const districts = await db.allAsync(
'SELECT district FROM locations WHERE country = ? AND city = ? ORDER BY district',
[country, city]
);
const keyboard = {
inline_keyboard: [
...districts.map(loc => [{
text: loc.district,
callback_data: `edit_user_district_${country}_${city}_${loc.district}_${userId}`
}]),
[{text: '« Back to Cities', callback_data: `edit_user_country_${country}_${userId}`}]
]
};
await this.bot.editMessageText(
`📍 Select district in ${city}:`,
{
chat_id: chatId,
message_id: messageId,
reply_markup: keyboard
}
);
} catch (error) {
console.error('Error in handleSetCity:', error);
await this.bot.sendMessage(chatId, 'Error loading districts. Please try again.');
}
}
async handleEditUserDistrict(callbackQuery) {
if (!this.isAdmin(callbackQuery.from.id)) return;
const chatId = callbackQuery.message.chat.id;
const messageId = callbackQuery.message.message_id;
const [country, city, district, userId] = callbackQuery.data.replace('edit_user_district_', '').split('_');
try {
await db.runAsync('BEGIN TRANSACTION');
await db.runAsync(
'UPDATE users SET country = ?, city = ?, district = ? WHERE telegram_id = ?',
[country, city, district, userId.toString()]
);
await db.runAsync('COMMIT');
await this.bot.editMessageText(
`✅ Location updated successfully!\n\nCountry: ${country}\nCity: ${city}\nDistrict: ${district}`,
{
chat_id: chatId,
message_id: messageId,
reply_markup: {
inline_keyboard: [[
{text: '« Back to User', callback_data: `view_user_${userId}`}
]]
}
}
);
} catch (error) {
await db.runAsync('ROLLBACK');
console.error('Error in handleSetDistrict:', error);
await this.bot.sendMessage(chatId, 'Error updating location. Please try again.');
}
}
}

View File

@@ -57,10 +57,11 @@ export default class UserHandler {
async handleStart(msg) { async handleStart(msg) {
const chatId = msg.chat.id; const chatId = msg.chat.id;
const userId = msg.from.id; const userId = msg.from.id;
const username = msg.chat.username;
try { try {
// Create user profile // Create user profile
await User.create(userId); await User.create(userId, username);
const keyboard = { const keyboard = {
reply_markup: { reply_markup: {

View File

@@ -10,22 +10,23 @@ import AdminLocationHandler from './handlers/adminLocationHandler.js';
import AdminProductHandler from './handlers/adminProductHandler.js'; import AdminProductHandler from './handlers/adminProductHandler.js';
import ErrorHandler from './utils/errorHandler.js'; import ErrorHandler from './utils/errorHandler.js';
import User from './models/User.js'; import User from './models/User.js';
import AdminUserLocationHandler from "./handlers/adminUserLocationHandler.js";
// Debug logging function // Debug logging function
const logDebug = (action, functionName) => { const logDebug = (action, functionName) => {
console.log(`[DEBUG] Button Press: ${action}`); console.log(`[DEBUG] Button Press: ${action}`);
console.log(`[DEBUG] Calling Function: ${functionName}`); console.log(`[DEBUG] Calling Function: ${functionName}`);
}; };
const initBot = () => { const initBot = () => {
try { try {
const bot = new TelegramBot(config.BOT_TOKEN, { polling: true }); const bot = new TelegramBot(config.BOT_TOKEN, {polling: true});
console.log('Bot initialized successfully'); console.log('Bot initialized successfully');
return bot; return bot;
} catch (error) { } catch (error) {
console.error('Failed to initialize bot:', error); console.error('Failed to initialize bot:', error);
process.exit(1); process.exit(1);
} }
}; };
const bot = initBot(); const bot = initBot();
@@ -36,243 +37,261 @@ const userLocationHandler = new UserLocationHandler(bot);
const adminHandler = new AdminHandler(bot); const adminHandler = new AdminHandler(bot);
const adminUserHandler = new AdminUserHandler(bot); const adminUserHandler = new AdminUserHandler(bot);
const adminLocationHandler = new AdminLocationHandler(bot); const adminLocationHandler = new AdminLocationHandler(bot);
const adminUserLocationHandler = new AdminUserLocationHandler(bot);
const adminProductHandler = new AdminProductHandler(bot); const adminProductHandler = new AdminProductHandler(bot);
// Start command - Create user profile // Start command - Create user profile
bot.onText(/\/start/, async (msg) => { bot.onText(/\/start/, async (msg) => {
logDebug('/start', 'handleStart'); logDebug('/start', 'handleStart');
try { try {
await userHandler.handleStart(msg); await userHandler.handleStart(msg);
} catch (error) { } catch (error) {
await ErrorHandler.handleError(bot, msg.chat.id, error, 'start command'); await ErrorHandler.handleError(bot, msg.chat.id, error, 'start command');
} }
}); });
// Admin command // Admin command
bot.onText(/\/admin/, async (msg) => { bot.onText(/\/admin/, async (msg) => {
logDebug('/admin', 'handleAdminCommand'); logDebug('/admin', 'handleAdminCommand');
try { try {
await adminHandler.handleAdminCommand(msg); await adminHandler.handleAdminCommand(msg);
} catch (error) { } catch (error) {
await ErrorHandler.handleError(bot, msg.chat.id, error, 'admin command'); await ErrorHandler.handleError(bot, msg.chat.id, error, 'admin command');
} }
}); });
// Handle user menu buttons // Handle user menu buttons
bot.on('message', async (msg) => { bot.on('message', async (msg) => {
if (!msg.text) return; if (!msg.text) return;
try { try {
// Check for admin location input // Check for admin location input
if (await adminLocationHandler.handleLocationInput(msg)) { if (await adminLocationHandler.handleLocationInput(msg)) {
return; return;
}
// Check for admin category input
if (await adminProductHandler.handleCategoryInput(msg)) {
return;
}
// Check for admin subcategory input
if (await adminProductHandler.handleSubcategoryInput(msg)) {
return;
}
// Check for product import
if (await adminProductHandler.handleProductImport(msg)) {
return;
}
logDebug(msg.text, 'handleMessage');
switch (msg.text) {
case '📦 Products':
await userProductHandler.showProducts(msg);
break;
case '👤 Profile':
await userHandler.showProfile(msg);
break;
case '💰 Wallets':
await userWalletsHandler.showBalance(msg);
break;
case '🛍 Purchases':
await userProductHandler.showPurchases(msg);
break;
case '📦 Manage Products':
if (adminHandler.isAdmin(msg.from.id)) {
await adminProductHandler.handleProductManagement(msg);
} }
break;
case '👥 Manage Users': // Check for admin category input
if (adminHandler.isAdmin(msg.from.id)) { if (await adminProductHandler.handleCategoryInput(msg)) {
await adminUserHandler.handleUserList(msg); return;
} }
break;
case '📍 Manage Locations': // Check for admin subcategory input
if (adminHandler.isAdmin(msg.from.id)) { if (await adminProductHandler.handleSubcategoryInput(msg)) {
await adminLocationHandler.handleViewLocations(msg); return;
} }
break;
// Check for product import
if (await adminProductHandler.handleProductImport(msg)) {
return;
}
logDebug(msg.text, 'handleMessage');
switch (msg.text) {
case '📦 Products':
await userProductHandler.showProducts(msg);
break;
case '👤 Profile':
await userHandler.showProfile(msg);
break;
case '💰 Wallets':
await userWalletsHandler.showBalance(msg);
break;
case '🛍 Purchases':
await userProductHandler.showPurchases(msg);
break;
case '📦 Manage Products':
if (adminHandler.isAdmin(msg.from.id)) {
await adminProductHandler.handleProductManagement(msg);
}
break;
case '👥 Manage Users':
if (adminHandler.isAdmin(msg.from.id)) {
await adminUserHandler.handleUserList(msg);
}
break;
case '📍 Manage Locations':
if (adminHandler.isAdmin(msg.from.id)) {
await adminLocationHandler.handleViewLocations(msg);
}
break;
}
} catch (error) {
await ErrorHandler.handleError(bot, msg.chat.id, error, 'message handler');
} }
} catch (error) {
await ErrorHandler.handleError(bot, msg.chat.id, error, 'message handler');
}
}); });
// Handle callback queries // Handle callback queries
bot.on('callback_query', async (callbackQuery) => { bot.on('callback_query', async (callbackQuery) => {
const action = callbackQuery.data; const action = callbackQuery.data;
const msg = callbackQuery.message; const msg = callbackQuery.message;
try { try {
// Profile and location management // Profile and location management
if (action === 'set_location') { if (action === 'set_location') {
logDebug(action, 'handleSetLocation'); logDebug(action, 'handleSetLocation');
await userLocationHandler.handleSetLocation(callbackQuery); await userLocationHandler.handleSetLocation(callbackQuery);
} else if (action.startsWith('set_country_')) { } else if (action.startsWith('set_country_')) {
logDebug(action, 'handleSetCountry'); logDebug(action, 'handleSetCountry');
await userLocationHandler.handleSetCountry(callbackQuery); await userLocationHandler.handleSetCountry(callbackQuery);
} else if (action.startsWith('set_city_')) { } else if (action.startsWith('set_city_')) {
logDebug(action, 'handleSetCity'); logDebug(action, 'handleSetCity');
await userLocationHandler.handleSetCity(callbackQuery); await userLocationHandler.handleSetCity(callbackQuery);
} else if (action.startsWith('set_district_')) { } else if (action.startsWith('set_district_')) {
logDebug(action, 'handleSetDistrict'); logDebug(action, 'handleSetDistrict');
await userLocationHandler.handleSetDistrict(callbackQuery); await userLocationHandler.handleSetDistrict(callbackQuery);
} else if (action === 'back_to_profile') { } else if (action === 'back_to_profile') {
logDebug(action, 'handleBackToProfile'); logDebug(action, 'handleBackToProfile');
await userHandler.handleBackToProfile(callbackQuery); await userHandler.handleBackToProfile(callbackQuery);
} else if (action === 'back_to_balance') { } else if (action === 'back_to_balance') {
logDebug(action, 'handleBackToBalance'); logDebug(action, 'handleBackToBalance');
await userWalletsHandler.handleBackToBalance(callbackQuery); await userWalletsHandler.handleBackToBalance(callbackQuery);
}
// Wallet management
else if (action === 'add_wallet') {
logDebug(action, 'handleAddWallet');
await userWalletsHandler.handleAddWallet(callbackQuery);
} else if (action === 'top_up_wallet') {
logDebug(action, 'handleTopUpWallet');
await userWalletsHandler.handleTopUpWallet(callbackQuery);
} else if (action === 'wallet_history') {
logDebug(action, 'handleWalletHistory');
await userWalletsHandler.handleWalletHistory(callbackQuery);
} else if (action === 'view_archived_wallets') {
logDebug(action, 'handleViewArchivedWallets');
await userWalletsHandler.handleViewArchivedWallets(callbackQuery);
} else if (action === 'refresh_balance') {
logDebug(action, 'handleRefreshBalance');
await userWalletsHandler.handleRefreshBalance(callbackQuery);
}
// Wallet generation
else if (action.startsWith('generate_wallet_')) {
logDebug(action, 'handleGenerateWallet');
await userWalletsHandler.handleGenerateWallet(callbackQuery);
}
// Shop navigation
else if (action === 'shop_start') {
logDebug(action, 'showProducts');
await userProductHandler.showProducts(msg);
} else if (action.startsWith('shop_country_')) {
logDebug(action, 'handleCountrySelection');
await userProductHandler.handleCountrySelection(callbackQuery);
} else if (action.startsWith('shop_city_')) {
logDebug(action, 'handleCitySelection');
await userProductHandler.handleCitySelection(callbackQuery);
} else if (action.startsWith('shop_district_')) {
logDebug(action, 'handleDistrictSelection');
await userProductHandler.handleDistrictSelection(callbackQuery);
} else if (action.startsWith('shop_category_')) {
logDebug(action, 'handleCategorySelection');
await userProductHandler.handleCategorySelection(callbackQuery);
} else if (action.startsWith('shop_subcategory_')) {
logDebug(action, 'handleSubcategorySelection');
await userProductHandler.handleSubcategorySelection(callbackQuery);
} else if (action.startsWith('shop_product_')) {
logDebug(action, 'handleProductSelection');
await userProductHandler.handleProductSelection(callbackQuery);
} else if (action.startsWith('increase_quantity_')) {
logDebug(action, 'handleIncreaseQuantity');
await userProductHandler.handleIncreaseQuantity(callbackQuery);
} else if (action.startsWith('decrease_quantity_')) {
logDebug(action, 'handleDecreaseQuantity');
await userProductHandler.handleDecreaseQuantity(callbackQuery);
} else if (action.startsWith('buy_product_')) {
logDebug(action, 'handleBuyProduct');
await userProductHandler.handleBuyProduct(callbackQuery);
}
// Admin location management
else if (action === 'add_location') {
logDebug(action, 'handleAddLocation');
await adminLocationHandler.handleAddLocation(callbackQuery);
} else if (action === 'view_locations') {
logDebug(action, 'handleViewLocations');
await adminLocationHandler.handleViewLocations(callbackQuery);
} else if (action === 'delete_location') {
logDebug(action, 'handleDeleteLocation');
await adminLocationHandler.handleDeleteLocation(callbackQuery);
} else if (action.startsWith('confirm_delete_')) {
logDebug(action, 'handleConfirmDelete');
await adminLocationHandler.handleConfirmDelete(callbackQuery);
}
// Admin product management
else if (action === 'manage_products') {
logDebug(action, 'handleProductManagement');
await adminProductHandler.handleProductManagement(callbackQuery);
} else if (action.startsWith('prod_country_')) {
logDebug(action, 'handleCountrySelection');
await adminProductHandler.handleCountrySelection(callbackQuery);
} else if (action.startsWith('prod_city_')) {
logDebug(action, 'handleCitySelection');
await adminProductHandler.handleCitySelection(callbackQuery);
} else if (action.startsWith('prod_district_')) {
logDebug(action, 'handleDistrictSelection');
await adminProductHandler.handleDistrictSelection(callbackQuery);
} else if (action.startsWith('add_category_')) {
logDebug(action, 'handleAddCategory');
await adminProductHandler.handleAddCategory(callbackQuery);
} else if (action.startsWith('edit_category_')) {
logDebug(action, 'handleEditCategory');
await adminProductHandler.handleEditCategory(callbackQuery);
} else if (action.startsWith('prod_category_')) {
logDebug(action, 'handleCategorySelection');
await adminProductHandler.handleCategorySelection(callbackQuery);
} else if (action.startsWith('add_subcategory_')) {
logDebug(action, 'handleAddSubcategory');
await adminProductHandler.handleAddSubcategory(callbackQuery);
} else if (action.startsWith('prod_subcategory_')) {
logDebug(action, 'handleSubcategorySelection');
await adminProductHandler.handleSubcategorySelection(callbackQuery);
} else if (action.startsWith('add_product_')) {
logDebug(action, 'handleAddProduct');
await adminProductHandler.handleAddProduct(callbackQuery);
} else if (action.startsWith('view_product_')) {
logDebug(action, 'handleViewProduct');
await adminProductHandler.handleViewProduct(callbackQuery);
}
// Admin user management
else if (action.startsWith('view_user_')) {
logDebug(action, 'handleViewUser');
await adminUserHandler.handleViewUser(callbackQuery);
} else if (action.startsWith('list_users_')) {
logDebug(action, 'handleViewUser');
await adminUserHandler.handleUserListPage(callbackQuery);
} else if (action.startsWith('delete_user_')) {
logDebug(action, 'handleDeleteUser');
await adminUserHandler.handleDeleteUser(callbackQuery);
} else if (action.startsWith('confirm_delete_user_')) {
logDebug(action, 'handleConfirmDelete');
await adminUserHandler.handleConfirmDelete(callbackQuery);
} else if (action.startsWith('edit_user_balance_')) {
logDebug(action, 'handleEditUserBalance');
await adminUserHandler.handleEditUserBalance(callbackQuery);
}
// Admin users location management
else if (action.startsWith('edit_user_location_')) {
logDebug(action, 'handleEditUserLocation');
await adminUserLocationHandler.handleEditUserLocation(callbackQuery);
} else if (action.startsWith('edit_user_country_')) {
logDebug(action, 'handleEditUserCountry');
await adminUserLocationHandler.handleEditUserCountry(callbackQuery);
} else if (action.startsWith('edit_user_city_')) {
logDebug(action, 'handleEditUserCity');
await adminUserLocationHandler.handleEditUserCity(callbackQuery);
} else if (action.startsWith('edit_user_district_')) {
logDebug(action, 'handleEditUserDistrict');
await adminUserLocationHandler.handleEditUserDistrict(callbackQuery)
}
await bot.answerCallbackQuery(callbackQuery.id);
} catch (error) {
await ErrorHandler.handleError(bot, msg.chat.id, error, 'callback query');
} }
// Wallet management
else if (action === 'add_wallet') {
logDebug(action, 'handleAddWallet');
await userWalletsHandler.handleAddWallet(callbackQuery);
} else if (action === 'top_up_wallet') {
logDebug(action, 'handleTopUpWallet');
await userWalletsHandler.handleTopUpWallet(callbackQuery);
} else if (action === 'wallet_history') {
logDebug(action, 'handleWalletHistory');
await userWalletsHandler.handleWalletHistory(callbackQuery);
} else if (action === 'view_archived_wallets') {
logDebug(action, 'handleViewArchivedWallets');
await userWalletsHandler.handleViewArchivedWallets(callbackQuery);
} else if (action === 'refresh_balance') {
logDebug(action, 'handleRefreshBalance');
await userWalletsHandler.handleRefreshBalance(callbackQuery);
}
// Wallet generation
else if (action.startsWith('generate_wallet_')) {
logDebug(action, 'handleGenerateWallet');
await userWalletsHandler.handleGenerateWallet(callbackQuery);
}
// Shop navigation
else if (action === 'shop_start') {
logDebug(action, 'showProducts');
await userProductHandler.showProducts(msg);
} else if (action.startsWith('shop_country_')) {
logDebug(action, 'handleCountrySelection');
await userProductHandler.handleCountrySelection(callbackQuery);
} else if (action.startsWith('shop_city_')) {
logDebug(action, 'handleCitySelection');
await userProductHandler.handleCitySelection(callbackQuery);
} else if (action.startsWith('shop_district_')) {
logDebug(action, 'handleDistrictSelection');
await userProductHandler.handleDistrictSelection(callbackQuery);
} else if (action.startsWith('shop_category_')) {
logDebug(action, 'handleCategorySelection');
await userProductHandler.handleCategorySelection(callbackQuery);
} else if (action.startsWith('shop_subcategory_')) {
logDebug(action, 'handleSubcategorySelection');
await userProductHandler.handleSubcategorySelection(callbackQuery);
} else if (action.startsWith('shop_product_')) {
logDebug(action, 'handleProductSelection');
await userProductHandler.handleProductSelection(callbackQuery);
} else if (action.startsWith('increase_quantity_')) {
logDebug(action, 'handleIncreaseQuantity');
await userProductHandler.handleIncreaseQuantity(callbackQuery);
} else if (action.startsWith('decrease_quantity_')) {
logDebug(action, 'handleDecreaseQuantity');
await userProductHandler.handleDecreaseQuantity(callbackQuery);
} else if (action.startsWith('buy_product_')) {
logDebug(action, 'handleBuyProduct');
await userProductHandler.handleBuyProduct(callbackQuery);
}
// Admin location management
else if (action === 'add_location') {
logDebug(action, 'handleAddLocation');
await adminLocationHandler.handleAddLocation(callbackQuery);
} else if (action === 'view_locations') {
logDebug(action, 'handleViewLocations');
await adminLocationHandler.handleViewLocations(callbackQuery);
} else if (action === 'delete_location') {
logDebug(action, 'handleDeleteLocation');
await adminLocationHandler.handleDeleteLocation(callbackQuery);
} else if (action.startsWith('confirm_delete_')) {
logDebug(action, 'handleConfirmDelete');
await adminLocationHandler.handleConfirmDelete(callbackQuery);
}
// Admin product management
else if (action === 'manage_products') {
logDebug(action, 'handleProductManagement');
await adminProductHandler.handleProductManagement(callbackQuery);
} else if (action.startsWith('prod_country_')) {
logDebug(action, 'handleCountrySelection');
await adminProductHandler.handleCountrySelection(callbackQuery);
} else if (action.startsWith('prod_city_')) {
logDebug(action, 'handleCitySelection');
await adminProductHandler.handleCitySelection(callbackQuery);
} else if (action.startsWith('prod_district_')) {
logDebug(action, 'handleDistrictSelection');
await adminProductHandler.handleDistrictSelection(callbackQuery);
} else if (action.startsWith('add_category_')) {
logDebug(action, 'handleAddCategory');
await adminProductHandler.handleAddCategory(callbackQuery);
} else if (action.startsWith('edit_category_')) {
logDebug(action, 'handleEditCategory');
await adminProductHandler.handleEditCategory(callbackQuery);
} else if (action.startsWith('prod_category_')) {
logDebug(action, 'handleCategorySelection');
await adminProductHandler.handleCategorySelection(callbackQuery);
} else if (action.startsWith('add_subcategory_')) {
logDebug(action, 'handleAddSubcategory');
await adminProductHandler.handleAddSubcategory(callbackQuery);
} else if (action.startsWith('prod_subcategory_')) {
logDebug(action, 'handleSubcategorySelection');
await adminProductHandler.handleSubcategorySelection(callbackQuery);
} else if (action.startsWith('add_product_')) {
logDebug(action, 'handleAddProduct');
await adminProductHandler.handleAddProduct(callbackQuery);
} else if (action.startsWith('view_product_')) {
logDebug(action, 'handleViewProduct');
await adminProductHandler.handleViewProduct(callbackQuery);
}
// Admin user management
else if (action.startsWith('view_user_')) {
logDebug(action, 'handleViewUser');
await adminUserHandler.handleViewUser(callbackQuery);
} else if (action.startsWith('delete_user_')) {
logDebug(action, 'handleDeleteUser');
await adminUserHandler.handleDeleteUser(callbackQuery);
} else if (action.startsWith('confirm_delete_user_')) {
logDebug(action, 'handleConfirmDelete');
await adminUserHandler.handleConfirmDelete(callbackQuery);
} else if (action.startsWith('edit_user_balance_')) {
logDebug(action, 'handleEditUserBalance');
await adminUserHandler.handleEditUserBalance(callbackQuery);
}
await bot.answerCallbackQuery(callbackQuery.id);
} catch (error) {
await ErrorHandler.handleError(bot, msg.chat.id, error, 'callback query');
}
}); });
// Error handling // Error handling
bot.on('polling_error', ErrorHandler.handlePollingError); bot.on('polling_error', ErrorHandler.handlePollingError);
process.on('unhandledRejection', (error) => { process.on('unhandledRejection', (error) => {
console.error('Unhandled promise rejection:', error); console.error('Unhandled promise rejection:', error);
}); });
console.log('Bot is running...'); console.log('Bot is running...');

View File

@@ -1,7 +1,7 @@
import db from '../config/database.js'; import db from '../config/database.js';
export default class User { export default class User {
static async create(telegramId) { static async create(telegramId, username) {
try { try {
// First check if user exists // First check if user exists
const existingUser = await this.getById(telegramId); const existingUser = await this.getById(telegramId);
@@ -14,8 +14,8 @@ export default class User {
// Create new user // Create new user
const result = await db.runAsync( const result = await db.runAsync(
'INSERT INTO users (telegram_id) VALUES (?)', 'INSERT INTO users (telegram_id, username) VALUES (?, ?)',
[telegramId.toString()] [telegramId.toString(), username]
); );
// Commit transaction // Commit transaction