users pagination
This commit is contained in:
@@ -11,12 +11,7 @@ export default class AdminUserHandler {
|
|||||||
return config.ADMIN_IDS.includes(userId.toString());
|
return config.ADMIN_IDS.includes(userId.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleUserList(msg) {
|
async calculateStatistics() {
|
||||||
if (!this.isAdmin(msg.from.id)) {
|
|
||||||
await this.bot.sendMessage(msg.chat.id, 'Unauthorized access.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const users = await db.allAsync(`
|
const users = await db.allAsync(`
|
||||||
SELECT
|
SELECT
|
||||||
@@ -31,10 +26,6 @@ export default class AdminUserHandler {
|
|||||||
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;
|
||||||
@@ -47,8 +38,47 @@ export default class AdminUserHandler {
|
|||||||
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:`;
|
|
||||||
|
return message;
|
||||||
|
} catch (error) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async viewUserPage(page) {
|
||||||
|
const limit = 10;
|
||||||
|
const offset = (page || 0) * limit;
|
||||||
|
|
||||||
|
const previousPage = page > 0 ? page - 1 : 0;
|
||||||
|
const nextPage = page + 1;
|
||||||
|
|
||||||
|
try {
|
||||||
|
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
|
// Create inline keyboard with user list
|
||||||
const keyboard = {
|
const keyboard = {
|
||||||
@@ -58,13 +88,46 @@ export default class AdminUserHandler {
|
|||||||
}])
|
}])
|
||||||
};
|
};
|
||||||
|
|
||||||
await this.bot.sendMessage(msg.chat.id, message, {
|
keyboard.inline_keyboard.push([
|
||||||
parse_mode: 'HTML',
|
{text: `«`, callback_data: `list_users_${previousPage}`},
|
||||||
reply_markup: keyboard
|
{text: `»`, callback_data: `list_users_${nextPage}`},
|
||||||
});
|
])
|
||||||
|
|
||||||
|
return {text: message, markup: keyboard}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error in handleUserList:', error);
|
console.error('Error in handleUserList:', error);
|
||||||
await this.bot.sendMessage(msg.chat.id, 'Error loading user list. Please try again.');
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -248,7 +311,10 @@ ${purchases.map(p => ` • ${p.product_name} x${p.quantity} - $${p.total_price}
|
|||||||
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}`,
|
||||||
|
callback_data: `edit_wallet_${wallet.wallet_type}`
|
||||||
|
}
|
||||||
]),
|
]),
|
||||||
[{text: '« Back', callback_data: `view_user_${userId}`}]
|
[{text: '« Back', callback_data: `view_user_${userId}`}]
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user