Merge pull request 'feature/admin-section' (#12) from feature/admin-section into main
Reviewed-on: https://git.softuniq.eu/Telegram-Market/telegram-shop/pulls/12
This commit is contained in:
commit
82132c7466
BIN
db/shop.db
BIN
db/shop.db
Binary file not shown.
@ -90,6 +90,7 @@ const initDb = async () => {
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
telegram_id TEXT UNIQUE NOT NULL,
|
||||
username TEXT,
|
||||
country TEXT,
|
||||
city TEXT,
|
||||
district TEXT,
|
||||
|
@ -11,12 +11,7 @@ export default class AdminUserHandler {
|
||||
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;
|
||||
}
|
||||
|
||||
async calculateStatistics() {
|
||||
try {
|
||||
const users = await db.allAsync(`
|
||||
SELECT
|
||||
@ -32,11 +27,6 @@ export default class AdminUserHandler {
|
||||
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
|
||||
const totalUsers = users.length;
|
||||
const activeUsers = users.filter(u => u.total_purchases > 0).length;
|
||||
@ -48,24 +38,96 @@ export default class AdminUserHandler {
|
||||
message += `👥 Total Users: ${totalUsers}\n`;
|
||||
message += `✅ Active Users: ${activeUsers}\n`;
|
||||
message += `💰 Total Balance: $${totalBalance.toFixed(2)}\n`;
|
||||
message += `🛍 Total Purchases: ${totalPurchases}\n\n`;
|
||||
message += `Select a user from the list below:`;
|
||||
message += `🛍 Total Purchases: ${totalPurchases}`;
|
||||
|
||||
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
|
||||
const keyboard = {
|
||||
inline_keyboard: users.map(user => [{
|
||||
text: `ID: ${user.telegram_id} | Balance: $${user.total_balance || 0}`,
|
||||
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
|
||||
});
|
||||
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);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -249,7 +311,10 @@ ${purchases.map(p => ` • ${p.product_name} x${p.quantity} - $${p.total_price}
|
||||
const keyboard = {
|
||||
inline_keyboard: [
|
||||
...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}`}]
|
||||
]
|
||||
|
172
src/handlers/adminUserLocationHandler.js
Normal file
172
src/handlers/adminUserLocationHandler.js
Normal 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.');
|
||||
}
|
||||
}
|
||||
}
|
@ -57,10 +57,11 @@ export default class UserHandler {
|
||||
async handleStart(msg) {
|
||||
const chatId = msg.chat.id;
|
||||
const userId = msg.from.id;
|
||||
const username = msg.chat.username;
|
||||
|
||||
try {
|
||||
// Create user profile
|
||||
await User.create(userId);
|
||||
await User.create(userId, username);
|
||||
|
||||
const keyboard = {
|
||||
reply_markup: {
|
||||
|
19
src/index.js
19
src/index.js
@ -10,6 +10,7 @@ import AdminLocationHandler from './handlers/adminLocationHandler.js';
|
||||
import AdminProductHandler from './handlers/adminProductHandler.js';
|
||||
import ErrorHandler from './utils/errorHandler.js';
|
||||
import User from './models/User.js';
|
||||
import AdminUserLocationHandler from "./handlers/adminUserLocationHandler.js";
|
||||
|
||||
// Debug logging function
|
||||
const logDebug = (action, functionName) => {
|
||||
@ -36,6 +37,7 @@ const userLocationHandler = new UserLocationHandler(bot);
|
||||
const adminHandler = new AdminHandler(bot);
|
||||
const adminUserHandler = new AdminUserHandler(bot);
|
||||
const adminLocationHandler = new AdminLocationHandler(bot);
|
||||
const adminUserLocationHandler = new AdminUserLocationHandler(bot);
|
||||
const adminProductHandler = new AdminProductHandler(bot);
|
||||
|
||||
// Start command - Create user profile
|
||||
@ -252,6 +254,9 @@ bot.on('callback_query', async (callbackQuery) => {
|
||||
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);
|
||||
@ -262,6 +267,20 @@ bot.on('callback_query', async (callbackQuery) => {
|
||||
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');
|
||||
|
@ -1,7 +1,7 @@
|
||||
import db from '../config/database.js';
|
||||
|
||||
export default class User {
|
||||
static async create(telegramId) {
|
||||
static async create(telegramId, username) {
|
||||
try {
|
||||
// First check if user exists
|
||||
const existingUser = await this.getById(telegramId);
|
||||
@ -14,8 +14,8 @@ export default class User {
|
||||
|
||||
// Create new user
|
||||
const result = await db.runAsync(
|
||||
'INSERT INTO users (telegram_id) VALUES (?)',
|
||||
[telegramId.toString()]
|
||||
'INSERT INTO users (telegram_id, username) VALUES (?, ?)',
|
||||
[telegramId.toString(), username]
|
||||
);
|
||||
|
||||
// Commit transaction
|
||||
|
Loading…
Reference in New Issue
Block a user