Bonus balance editing
This commit is contained in:
parent
d4eb5d46c3
commit
a760cb2d23
@ -5,6 +5,7 @@ import db from '../config/database.js';
|
|||||||
export default class AdminUserHandler {
|
export default class AdminUserHandler {
|
||||||
constructor(bot) {
|
constructor(bot) {
|
||||||
this.bot = bot;
|
this.bot = bot;
|
||||||
|
this.userStates = new Map();
|
||||||
}
|
}
|
||||||
|
|
||||||
isAdmin(userId) {
|
isAdmin(userId) {
|
||||||
@ -17,8 +18,7 @@ export default class AdminUserHandler {
|
|||||||
SELECT
|
SELECT
|
||||||
u.*,
|
u.*,
|
||||||
COUNT(DISTINCT p.id) as total_purchases,
|
COUNT(DISTINCT p.id) as total_purchases,
|
||||||
COUNT(DISTINCT cw.id) as total_wallets,
|
COUNT(DISTINCT cw.id) as total_wallets
|
||||||
COALESCE(SUM(t.amount), 0) as total_balance
|
|
||||||
FROM users u
|
FROM users u
|
||||||
LEFT JOIN purchases p ON u.id = p.user_id
|
LEFT JOIN purchases p ON u.id = p.user_id
|
||||||
LEFT JOIN crypto_wallets cw ON u.id = cw.user_id
|
LEFT JOIN crypto_wallets cw ON u.id = cw.user_id
|
||||||
@ -31,13 +31,15 @@ export default class AdminUserHandler {
|
|||||||
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 bonusBalance = users.reduce((sum, u) => sum + (u.bonus_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 += `💰 Bonus Balance: $${bonusBalance.toFixed(2)}\n`;
|
||||||
|
message += `💰 Total Balance: $${(totalBalance + bonusBalance).toFixed(2)}\n`;
|
||||||
message += `🛍 Total Purchases: ${totalPurchases}`;
|
message += `🛍 Total Purchases: ${totalPurchases}`;
|
||||||
|
|
||||||
return message;
|
return message;
|
||||||
@ -58,8 +60,7 @@ export default class AdminUserHandler {
|
|||||||
SELECT
|
SELECT
|
||||||
u.*,
|
u.*,
|
||||||
COUNT(DISTINCT p.id) as total_purchases,
|
COUNT(DISTINCT p.id) as total_purchases,
|
||||||
COUNT(DISTINCT cw.id) as total_wallets,
|
COUNT(DISTINCT cw.id) as total_wallets
|
||||||
COALESCE(SUM(t.amount), 0) as total_balance
|
|
||||||
FROM users u
|
FROM users u
|
||||||
LEFT JOIN purchases p ON u.id = p.user_id
|
LEFT JOIN purchases p ON u.id = p.user_id
|
||||||
LEFT JOIN crypto_wallets cw ON u.id = cw.user_id
|
LEFT JOIN crypto_wallets cw ON u.id = cw.user_id
|
||||||
@ -83,7 +84,7 @@ export default class AdminUserHandler {
|
|||||||
// Create inline keyboard with user list
|
// Create inline keyboard with user list
|
||||||
const keyboard = {
|
const keyboard = {
|
||||||
inline_keyboard: users.map(user => [{
|
inline_keyboard: users.map(user => [{
|
||||||
text: `ID: ${user.telegram_id} | Nickname: ${user.username ? "@" + user.username : "None"} | Balance: $${user.total_balance || 0}`,
|
text: `ID: ${user.telegram_id} | Nickname: ${user.username ? "@" + user.username : "None"} | Balance: $${(user.total_balance || 0) + (user.bonus_balance || 0)}`,
|
||||||
callback_data: `view_user_${user.telegram_id}`
|
callback_data: `view_user_${user.telegram_id}`
|
||||||
}])
|
}])
|
||||||
};
|
};
|
||||||
@ -153,6 +154,13 @@ export default class AdminUserHandler {
|
|||||||
GROUP BY u.id
|
GROUP BY u.id
|
||||||
`, [userId]);
|
`, [userId]);
|
||||||
|
|
||||||
|
const user = await User.getById(userId);
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
await this.bot.sendMessage(chatId, 'User not found.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!userStats) {
|
if (!userStats) {
|
||||||
await this.bot.sendMessage(chatId, 'User not found.');
|
await this.bot.sendMessage(chatId, 'User not found.');
|
||||||
return;
|
return;
|
||||||
@ -190,7 +198,8 @@ ID: ${userId}
|
|||||||
- Total Purchases: ${userStats.purchase_count}
|
- Total Purchases: ${userStats.purchase_count}
|
||||||
- Total Spent: $${userStats.total_spent || 0}
|
- Total Spent: $${userStats.total_spent || 0}
|
||||||
- Active Wallets: ${userStats.wallet_count}
|
- Active Wallets: ${userStats.wallet_count}
|
||||||
- Total Balance: $${userStats.total_balance || 0}
|
- Bonus Balance: $${user.bonus_balance || 0}
|
||||||
|
- Total Balance: $${(user.total_balance || 0) + (user.bonus_balance || 0)}
|
||||||
|
|
||||||
💰 Recent Transactions:
|
💰 Recent Transactions:
|
||||||
${transactions.map(t => ` • ${t.amount} ${t.wallet_type} (${t.tx_hash})`).join('\n')}
|
${transactions.map(t => ` • ${t.amount} ${t.wallet_type} (${t.tx_hash})`).join('\n')}
|
||||||
@ -211,7 +220,7 @@ ${purchases.map(p => ` • ${p.product_name} x${p.quantity} - $${p.total_price}
|
|||||||
{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: `list_users_0`}]
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -373,36 +382,45 @@ ${purchases.map(p => ` • ${p.product_name} x${p.quantity} - $${p.total_price}
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const wallets = await db.allAsync(`
|
|
||||||
SELECT wallet_type, address
|
|
||||||
FROM crypto_wallets
|
|
||||||
WHERE user_id = (SELECT id FROM users WHERE telegram_id = ?)
|
|
||||||
ORDER BY wallet_type
|
|
||||||
`, [userId]);
|
|
||||||
|
|
||||||
const keyboard = {
|
|
||||||
inline_keyboard: [
|
|
||||||
...wallets.map(wallet => [
|
|
||||||
{
|
|
||||||
text: `${wallet.wallet_type}: ${wallet.address}`,
|
|
||||||
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}:`,
|
`Enter new value for bonus balance. \n\n👥 User: ${userId}\n💰 Bonus Balance Now: $${user.bonus_balance.toFixed(2)}`,
|
||||||
{
|
{
|
||||||
chat_id: chatId,
|
chat_id: chatId,
|
||||||
message_id: callbackQuery.message.message_id,
|
message_id: callbackQuery.message.message_id,
|
||||||
reply_markup: keyboard
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
this.userStates.set(chatId, { action: "edit_bonus_balance", telegram_id: userId });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error in handleEditUserBalance:', error);
|
console.error('Error in handleEditUserBalance:', error);
|
||||||
await this.bot.sendMessage(chatId, 'Error loading user wallets. Please try again.');
|
await this.bot.sendMessage(chatId, 'Error loading user wallets. Please try again.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async handleBonusBalanceInput(msg) {
|
||||||
|
if (!this.isAdmin(msg.from.id)) return;
|
||||||
|
|
||||||
|
const chatId = msg.chat.id;
|
||||||
|
const state = this.userStates.get(chatId);
|
||||||
|
|
||||||
|
if (!state || state.action !== 'edit_bonus_balance') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const newValue = parseFloat(msg.text);
|
||||||
|
|
||||||
|
if (isNaN(newValue)) {
|
||||||
|
await this.bot.sendMessage(chatId, 'Invalid value. Try again');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await db.runAsync(`UPDATE users SET bonus_balance = ? WHERE telegram_id = ?`, [newValue, state.telegram_id])
|
||||||
|
await this.bot.sendMessage(chatId, '✅ Done')
|
||||||
|
} catch (e) {
|
||||||
|
await this.bot.sendMessage(chatId, 'Something went wrong');
|
||||||
|
}
|
||||||
|
|
||||||
|
this.userStates.delete(chatId);
|
||||||
|
}
|
||||||
}
|
}
|
@ -114,6 +114,11 @@ bot.on('message', async (msg) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check for bonus balance input
|
||||||
|
if (await adminUserHandler.handleBonusBalanceInput(msg)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
logDebug(msg.text, 'handleMessage');
|
logDebug(msg.text, 'handleMessage');
|
||||||
|
|
||||||
switch (msg.text) {
|
switch (msg.text) {
|
||||||
|
Loading…
Reference in New Issue
Block a user