Merge pull request 'feature/admin-section' (#16) from feature/admin-section into main
Reviewed-on: https://git.softuniq.eu/Telegram-Market/telegram-shop/pulls/16
This commit is contained in:
commit
7c79e7fd94
@ -94,6 +94,7 @@ const initDb = async () => {
|
|||||||
country TEXT,
|
country TEXT,
|
||||||
city TEXT,
|
city TEXT,
|
||||||
district TEXT,
|
district TEXT,
|
||||||
|
status INTEGER DEFAULT 0,
|
||||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||||
)
|
)
|
||||||
`);
|
`);
|
||||||
|
@ -1,35 +1,33 @@
|
|||||||
import db from '../config/database.js';
|
|
||||||
import config from '../config/config.js';
|
import config from '../config/config.js';
|
||||||
|
|
||||||
export default class AdminHandler {
|
export default class AdminHandler {
|
||||||
constructor(bot) {
|
constructor(bot) {
|
||||||
this.bot = bot;
|
this.bot = bot;
|
||||||
}
|
|
||||||
|
|
||||||
isAdmin(userId) {
|
|
||||||
return config.ADMIN_IDS.includes(userId.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
async handleAdminCommand(msg) {
|
|
||||||
const chatId = msg.chat.id;
|
|
||||||
|
|
||||||
if (!this.isAdmin(msg.from.id)) {
|
|
||||||
await this.bot.sendMessage(chatId, 'Unauthorized access.');
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const keyboard = {
|
isAdmin(userId) {
|
||||||
reply_markup: {
|
return config.ADMIN_IDS.includes(userId.toString());
|
||||||
keyboard: [
|
}
|
||||||
['👥 Manage Users', '📦 Manage Products'],
|
|
||||||
['💰 Manage Wallets', '📍 Manage Locations'],
|
|
||||||
['💾 Database Backup']
|
|
||||||
],
|
|
||||||
resize_keyboard: true
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
await this.bot.sendMessage(chatId, 'Admin Panel:', keyboard);
|
async handleAdminCommand(msg) {
|
||||||
}
|
const chatId = msg.chat.id;
|
||||||
|
|
||||||
|
if (!this.isAdmin(msg.from.id)) {
|
||||||
|
await this.bot.sendMessage(chatId, 'Unauthorized access.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const keyboard = {
|
||||||
|
reply_markup: {
|
||||||
|
keyboard: [
|
||||||
|
['👥 Manage Users', '📦 Manage Products'],
|
||||||
|
['💰 Manage Wallets', '📍 Manage Locations'],
|
||||||
|
['💾 Database Backup']
|
||||||
|
],
|
||||||
|
resize_keyboard: true
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
await this.bot.sendMessage(chatId, 'Admin Panel:', keyboard);
|
||||||
|
}
|
||||||
}
|
}
|
@ -125,6 +125,8 @@ export default class AdminLocationHandler {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.userStates.delete(chatId);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const locations = await db.allAsync(`
|
const locations = await db.allAsync(`
|
||||||
SELECT l.*,
|
SELECT l.*,
|
||||||
@ -213,7 +215,7 @@ export default class AdminLocationHandler {
|
|||||||
const keyboard = {
|
const keyboard = {
|
||||||
inline_keyboard: locations.map(loc => [{
|
inline_keyboard: locations.map(loc => [{
|
||||||
text: `${loc.country} > ${loc.city} > ${loc.district} (P:${loc.product_count} C:${loc.category_count})`,
|
text: `${loc.country} > ${loc.city} > ${loc.district} (P:${loc.product_count} C:${loc.category_count})`,
|
||||||
callback_data: `confirm_delete_${loc.country}_${loc.city}_${loc.district}`
|
callback_data: `confirm_delete_location_${loc.country}_${loc.city}_${loc.district}`
|
||||||
}])
|
}])
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -237,7 +239,7 @@ export default class AdminLocationHandler {
|
|||||||
async handleConfirmDelete(callbackQuery) {
|
async handleConfirmDelete(callbackQuery) {
|
||||||
const chatId = callbackQuery.message.chat.id;
|
const chatId = callbackQuery.message.chat.id;
|
||||||
const [country, city, district] = callbackQuery.data
|
const [country, city, district] = callbackQuery.data
|
||||||
.replace('confirm_delete_', '')
|
.replace('confirm_delete_location_', '')
|
||||||
.split('_');
|
.split('_');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -278,4 +280,33 @@ export default class AdminLocationHandler {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async backToMenu(callbackQuery) {
|
||||||
|
if (!this.isAdmin(callbackQuery.from.id)) return;
|
||||||
|
|
||||||
|
const chatId = callbackQuery.message.chat.id;
|
||||||
|
const messageId = callbackQuery.message.message_id;
|
||||||
|
|
||||||
|
const keyboard = {
|
||||||
|
reply_markup: {
|
||||||
|
keyboard: [
|
||||||
|
['👥 Manage Users', '📦 Manage Products'],
|
||||||
|
['💰 Manage Wallets', '📍 Manage Locations'],
|
||||||
|
['💾 Database Backup']
|
||||||
|
],
|
||||||
|
resize_keyboard: true
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
await this.bot.editMessageText(
|
||||||
|
`You we're returned to the admin menu`,
|
||||||
|
{
|
||||||
|
chat_id: chatId,
|
||||||
|
message_id: messageId,
|
||||||
|
reply_markup: keyboard
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
this.userStates.delete(chatId);
|
||||||
|
}
|
||||||
}
|
}
|
@ -265,7 +265,7 @@ ${purchases.map(p => ` • ${p.product_name} x${p.quantity} - $${p.total_price}
|
|||||||
const chatId = callbackQuery.message.chat.id;
|
const chatId = callbackQuery.message.chat.id;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await User.delete(userId);
|
await User.updateUserStatus(userId, 1);
|
||||||
|
|
||||||
const keyboard = {
|
const keyboard = {
|
||||||
inline_keyboard: [
|
inline_keyboard: [
|
||||||
@ -273,6 +273,12 @@ ${purchases.map(p => ` • ${p.product_name} x${p.quantity} - $${p.total_price}
|
|||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
await this.bot.sendMessage(userId, '⚠️Your account has been deleted by administrator');
|
||||||
|
} catch (e) {
|
||||||
|
// ignore if we can't notify user
|
||||||
|
}
|
||||||
|
|
||||||
await this.bot.editMessageText(
|
await this.bot.editMessageText(
|
||||||
`✅ User ${userId} has been successfully deleted.`,
|
`✅ User ${userId} has been successfully deleted.`,
|
||||||
{
|
{
|
||||||
@ -287,6 +293,72 @@ ${purchases.map(p => ` • ${p.product_name} x${p.quantity} - $${p.total_price}
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async handleBlockUser(callbackQuery) {
|
||||||
|
if (!this.isAdmin(callbackQuery.from.id)) return;
|
||||||
|
|
||||||
|
const userId = callbackQuery.data.replace('block_user_', '');
|
||||||
|
const chatId = callbackQuery.message.chat.id;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const keyboard = {
|
||||||
|
inline_keyboard: [
|
||||||
|
[
|
||||||
|
{text: '✅ Confirm Block', callback_data: `confirm_block_user_${userId}`},
|
||||||
|
{text: '❌ Cancel', callback_data: `view_user_${userId}`}
|
||||||
|
]
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
await this.bot.editMessageText(
|
||||||
|
`⚠️ Are you sure you want to block user ${userId}?`,
|
||||||
|
{
|
||||||
|
chat_id: chatId,
|
||||||
|
message_id: callbackQuery.message.message_id,
|
||||||
|
reply_markup: keyboard,
|
||||||
|
parse_mode: 'HTML'
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error in handleBlockUser:', error);
|
||||||
|
await this.bot.sendMessage(chatId, 'Error processing block request. Please try again.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async handleConfirmBlock(callbackQuery) {
|
||||||
|
if (!this.isAdmin(callbackQuery.from.id)) return;
|
||||||
|
|
||||||
|
const userId = callbackQuery.data.replace('confirm_block_user_', '');
|
||||||
|
const chatId = callbackQuery.message.chat.id;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await User.updateUserStatus(userId, 2);
|
||||||
|
|
||||||
|
const keyboard = {
|
||||||
|
inline_keyboard: [
|
||||||
|
[{text: '« Back to User List', callback_data: 'admin_users'}]
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
await this.bot.sendMessage(userId, '⚠️Your account has been blocked by administrator');
|
||||||
|
} catch (e) {
|
||||||
|
// ignore if we can't notify user
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.bot.editMessageText(
|
||||||
|
`✅ User ${userId} has been successfully blocked.`,
|
||||||
|
{
|
||||||
|
chat_id: chatId,
|
||||||
|
message_id: callbackQuery.message.message_id,
|
||||||
|
reply_markup: keyboard
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error in handleConfirmBlock:', error);
|
||||||
|
await this.bot.sendMessage(chatId, 'Error blocking user. Please try again.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async handleEditUserBalance(callbackQuery) {
|
async handleEditUserBalance(callbackQuery) {
|
||||||
if (!this.isAdmin(callbackQuery.from.id)) return;
|
if (!this.isAdmin(callbackQuery.from.id)) return;
|
||||||
|
|
||||||
|
36
src/index.js
36
src/index.js
@ -43,6 +43,13 @@ 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');
|
||||||
|
|
||||||
|
const canUse = await userHandler.canUseBot(msg);
|
||||||
|
|
||||||
|
if (!canUse) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await userHandler.handleStart(msg);
|
await userHandler.handleStart(msg);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -64,6 +71,16 @@ bot.onText(/\/admin/, async (msg) => {
|
|||||||
bot.on('message', async (msg) => {
|
bot.on('message', async (msg) => {
|
||||||
if (!msg.text) return;
|
if (!msg.text) return;
|
||||||
|
|
||||||
|
if (msg.text.toLowerCase() === '/start') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const canUse = await userHandler.canUseBot(msg);
|
||||||
|
|
||||||
|
if (!canUse) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Check for admin location input
|
// Check for admin location input
|
||||||
if (await adminLocationHandler.handleLocationInput(msg)) {
|
if (await adminLocationHandler.handleLocationInput(msg)) {
|
||||||
@ -126,6 +143,13 @@ bot.on('callback_query', async (callbackQuery) => {
|
|||||||
const action = callbackQuery.data;
|
const action = callbackQuery.data;
|
||||||
const msg = callbackQuery.message;
|
const msg = callbackQuery.message;
|
||||||
|
|
||||||
|
const canUse = await userHandler.canUseBot(callbackQuery);
|
||||||
|
|
||||||
|
if (!canUse) {
|
||||||
|
await bot.answerCallbackQuery(callbackQuery.id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Profile and location management
|
// Profile and location management
|
||||||
if (action === 'set_location') {
|
if (action === 'set_location') {
|
||||||
@ -211,10 +235,14 @@ bot.on('callback_query', async (callbackQuery) => {
|
|||||||
} else if (action === 'delete_location') {
|
} else if (action === 'delete_location') {
|
||||||
logDebug(action, 'handleDeleteLocation');
|
logDebug(action, 'handleDeleteLocation');
|
||||||
await adminLocationHandler.handleDeleteLocation(callbackQuery);
|
await adminLocationHandler.handleDeleteLocation(callbackQuery);
|
||||||
} else if (action.startsWith('confirm_delete_')) {
|
} else if (action.startsWith('confirm_delete_location_')) {
|
||||||
logDebug(action, 'handleConfirmDelete');
|
logDebug(action, 'handleConfirmDelete');
|
||||||
await adminLocationHandler.handleConfirmDelete(callbackQuery);
|
await adminLocationHandler.handleConfirmDelete(callbackQuery);
|
||||||
|
} else if (action === 'admin_menu') {
|
||||||
|
logDebug(action, 'backToMenu');
|
||||||
|
await adminLocationHandler.backToMenu(callbackQuery);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Admin product management
|
// Admin product management
|
||||||
else if (action === 'manage_products') {
|
else if (action === 'manage_products') {
|
||||||
logDebug(action, 'handleProductManagement');
|
logDebug(action, 'handleProductManagement');
|
||||||
@ -260,9 +288,15 @@ bot.on('callback_query', async (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);
|
||||||
|
} else if (action.startsWith('block_user_')) {
|
||||||
|
logDebug(action, 'handleDeleteUser');
|
||||||
|
await adminUserHandler.handleBlockUser(callbackQuery);
|
||||||
} else if (action.startsWith('confirm_delete_user_')) {
|
} else if (action.startsWith('confirm_delete_user_')) {
|
||||||
logDebug(action, 'handleConfirmDelete');
|
logDebug(action, 'handleConfirmDelete');
|
||||||
await adminUserHandler.handleConfirmDelete(callbackQuery);
|
await adminUserHandler.handleConfirmDelete(callbackQuery);
|
||||||
|
} else if (action.startsWith('confirm_block_user_')) {
|
||||||
|
logDebug(action, 'handleConfirmBlock');
|
||||||
|
await adminUserHandler.handleConfirmBlock(callbackQuery);
|
||||||
} else if (action.startsWith('edit_user_balance_')) {
|
} else if (action.startsWith('edit_user_balance_')) {
|
||||||
logDebug(action, 'handleEditUserBalance');
|
logDebug(action, 'handleEditUserBalance');
|
||||||
await adminUserHandler.handleEditUserBalance(callbackQuery);
|
await adminUserHandler.handleEditUserBalance(callbackQuery);
|
||||||
|
@ -1,50 +1,50 @@
|
|||||||
import db from '../config/database.js';
|
import db from '../config/database.js';
|
||||||
|
|
||||||
export default class User {
|
export default class User {
|
||||||
static async create(telegramId, username) {
|
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);
|
||||||
if (existingUser) {
|
if (existingUser) {
|
||||||
return existingUser.id;
|
return existingUser.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Begin transaction
|
// Begin transaction
|
||||||
await db.runAsync('BEGIN TRANSACTION');
|
await db.runAsync('BEGIN TRANSACTION');
|
||||||
|
|
||||||
// Create new user
|
// Create new user
|
||||||
const result = await db.runAsync(
|
const result = await db.runAsync(
|
||||||
'INSERT INTO users (telegram_id, username) VALUES (?, ?)',
|
'INSERT INTO users (telegram_id, username) VALUES (?, ?)',
|
||||||
[telegramId.toString(), username]
|
[telegramId.toString(), username]
|
||||||
);
|
);
|
||||||
|
|
||||||
// Commit transaction
|
// Commit transaction
|
||||||
await db.runAsync('COMMIT');
|
await db.runAsync('COMMIT');
|
||||||
|
|
||||||
return result.lastID;
|
return result.lastID;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Rollback on error
|
// Rollback on error
|
||||||
await db.runAsync('ROLLBACK');
|
await db.runAsync('ROLLBACK');
|
||||||
console.error('Error creating user:', error);
|
console.error('Error creating user:', error);
|
||||||
throw error;
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
static async getById(telegramId) {
|
static async getById(telegramId) {
|
||||||
try {
|
try {
|
||||||
return await db.getAsync(
|
return await db.getAsync(
|
||||||
'SELECT * FROM users WHERE telegram_id = ?',
|
'SELECT * FROM users WHERE telegram_id = ?',
|
||||||
[telegramId.toString()]
|
[telegramId.toString()]
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error getting user:', error);
|
console.error('Error getting user:', error);
|
||||||
throw error;
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
static async getUserStats(telegramId) {
|
static async getUserStats(telegramId) {
|
||||||
try {
|
try {
|
||||||
return await db.getAsync(`
|
return await db.getAsync(`
|
||||||
SELECT
|
SELECT
|
||||||
u.*,
|
u.*,
|
||||||
COUNT(DISTINCT p.id) as purchase_count,
|
COUNT(DISTINCT p.id) as purchase_count,
|
||||||
@ -58,9 +58,49 @@ export default class User {
|
|||||||
WHERE u.telegram_id = ?
|
WHERE u.telegram_id = ?
|
||||||
GROUP BY u.id
|
GROUP BY u.id
|
||||||
`, [telegramId.toString()]);
|
`, [telegramId.toString()]);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error getting user stats:', error);
|
console.error('Error getting user stats:', error);
|
||||||
throw error;
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static async updateUserStatus(telegramId, status) {
|
||||||
|
// statuses
|
||||||
|
// 0 - active
|
||||||
|
// 1 - deleted
|
||||||
|
// 2 - blocked
|
||||||
|
|
||||||
|
try {
|
||||||
|
await db.runAsync('BEGIN TRANSACTION');
|
||||||
|
|
||||||
|
// Update user status
|
||||||
|
await db.runAsync('UPDATE users SET status = ? WHERE telegram_id = ?', [status, telegramId.toString()]);
|
||||||
|
|
||||||
|
// Commit transaction
|
||||||
|
await db.runAsync('COMMIT');
|
||||||
|
} catch (e) {
|
||||||
|
await db.runAsync("ROLLBACK");
|
||||||
|
console.error('Error deleting user:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static async delete(telegramId) {
|
||||||
|
try {
|
||||||
|
await db.runAsync('BEGIN TRANSACTION');
|
||||||
|
|
||||||
|
// Delete user and his data
|
||||||
|
await db.runAsync('DELETE FROM users WHERE telegram_id = ?', [telegramId.toString()]);
|
||||||
|
await db.runAsync('DELETE FROM transactions WHERE user_id = ?', [telegramId.toString()]);
|
||||||
|
await db.runAsync('DELETE FROM purchases WHERE user_id = ?', [telegramId.toString()]);
|
||||||
|
await db.runAsync('DELETE FROM crypto_wallets WHERE user_id = ?', [telegramId.toString()]);
|
||||||
|
|
||||||
|
// Commit transaction
|
||||||
|
await db.runAsync('COMMIT');
|
||||||
|
} catch (e) {
|
||||||
|
await db.runAsync("ROLLBACK");
|
||||||
|
console.error('Error deleting user:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user