users pagination

This commit is contained in:
Artyom Ashirov
2024-11-14 21:27:32 +03:00
parent 93290dee1c
commit d506d79367
2 changed files with 239 additions and 170 deletions

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,52 +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} | Nickname: ${user.username? "@" + user.username: "None"} | 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,13 +153,13 @@ export default class AdminUserHandler {
GROUP BY u.id GROUP BY u.id
`, [userId]); `, [userId]);
if (!userStats) { if (!userStats) {
await this.bot.sendMessage(chatId, 'User not found.'); await this.bot.sendMessage(chatId, 'User not found.');
return; return;
} }
// Get recent transactions // Get recent transactions
const transactions = await db.allAsync(` 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
@@ -105,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
@@ -117,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}
@@ -138,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) { const keyboard = {
await this.bot.sendMessage(chatId, 'User not found.'); inline_keyboard: [
return; [{text: '« Back to User List', callback_data: 'admin_users'}]
} ]
};
const wallets = await db.allAsync(` 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

@@ -254,6 +254,9 @@ bot.on('callback_query', async (callbackQuery) => {
else if (action.startsWith('view_user_')) { else if (action.startsWith('view_user_')) {
logDebug(action, 'handleViewUser'); logDebug(action, 'handleViewUser');
await adminUserHandler.handleViewUser(callbackQuery); await adminUserHandler.handleViewUser(callbackQuery);
} else if (action.startsWith('list_users_')) {
logDebug(action, 'handleViewUser');
await adminUserHandler.handleUserListPage(callbackQuery);
} else if (action.startsWith('delete_user_')) { } else if (action.startsWith('delete_user_')) {
logDebug(action, 'handleDeleteUser'); logDebug(action, 'handleDeleteUser');
await adminUserHandler.handleDeleteUser(callbackQuery); await adminUserHandler.handleDeleteUser(callbackQuery);