Merge pull request 'feature/admin-section' (#20) from feature/admin-section into main

Reviewed-on: #20
This commit was merged in pull request #20.
This commit is contained in:
1323ed5
2024-11-15 12:34:09 +00:00
4 changed files with 813 additions and 569 deletions

BIN
corrupt-photo.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

File diff suppressed because it is too large Load Diff

View File

@@ -1,28 +1,52 @@
import db from '../config/database.js';
import User from '../models/User.js'; import User from '../models/User.js';
import config from "../config/config.js";
export default class UserHandler { export default class UserHandler {
constructor(bot) { constructor(bot) {
this.bot = bot; this.bot = bot;
} }
async showProfile(msg) { async canUseBot(msg) {
const chatId = msg.chat.id; const userId = msg.from.id;
const userId = msg.from.id; const user = await User.getById(userId);
try { const keyboard = {
const userStats = await User.getUserStats(userId); inline_keyboard: [
[{text: "Contact support", url: config.SUPPORT_LINK}]
]
};
if (!userStats) { switch (user?.status) {
await this.bot.sendMessage(chatId, 'Profile not found. Please use /start to create one.'); case 0:
return; return true;
} case 1:
await this.bot.sendMessage(userId, '⚠Your account has been deleted by administrator', {reply_markup: keyboard});
return false;
case 2:
await this.bot.sendMessage(userId, '⚠Your account has been blocked by administrator', {reply_markup: keyboard});
return false;
default:
return true
}
}
const locationText = userStats.country && userStats.city && userStats.district async showProfile(msg) {
? `${userStats.country}, ${userStats.city}, ${userStats.district}` const chatId = msg.chat.id;
: 'Not set'; const userId = msg.from.id;
const text = ` try {
const userStats = await User.getUserStats(userId);
if (!userStats) {
await this.bot.sendMessage(chatId, 'Profile not found. Please use /start to create one.');
return;
}
const locationText = userStats.country && userStats.city && userStats.district
? `${userStats.country}, ${userStats.city}, ${userStats.district}`
: 'Not set';
const text = `
👤 *Your Profile* 👤 *Your Profile*
📱 Telegram ID: \`${userId}\` 📱 Telegram ID: \`${userId}\`
@@ -37,58 +61,58 @@ export default class UserHandler {
📅 Member since: ${new Date(userStats.created_at).toLocaleDateString()} 📅 Member since: ${new Date(userStats.created_at).toLocaleDateString()}
`; `;
const keyboard = { const keyboard = {
inline_keyboard: [ inline_keyboard: [
[{ text: '📍 Set Location', callback_data: 'set_location' }], [{text: '📍 Set Location', callback_data: 'set_location'}],
[{ text: '❌ Delete Account', callback_data: 'delete_account' }] [{text: '❌ Delete Account', callback_data: 'delete_account'}]
] ]
}; };
await this.bot.sendMessage(chatId, text, { await this.bot.sendMessage(chatId, text, {
parse_mode: 'Markdown', parse_mode: 'Markdown',
reply_markup: keyboard reply_markup: keyboard
}); });
} catch (error) { } catch (error) {
console.error('Error in showProfile:', error); console.error('Error in showProfile:', error);
await this.bot.sendMessage(chatId, 'Error loading profile. Please try again.'); await this.bot.sendMessage(chatId, 'Error loading profile. Please try again.');
}
}
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, username);
const keyboard = {
reply_markup: {
keyboard: [
['📦 Products', '👤 Profile'],
['🛍 Purchases', '💰 Wallets']
],
resize_keyboard: true
} }
};
await this.bot.sendMessage(
chatId,
'Welcome to the shop! Choose an option:',
keyboard
);
} catch (error) {
console.error('Error in handleStart:', error);
await this.bot.sendMessage(chatId, 'Error creating user profile. Please try again.');
} }
}
async handleBackToProfile(callbackQuery) { async handleStart(msg) {
await this.showProfile({ const chatId = msg.chat.id;
chat: { id: callbackQuery.message.chat.id }, const userId = msg.from.id;
from: { id: callbackQuery.from.id } const username = msg.chat.username;
});
await this.bot.deleteMessage(callbackQuery.message.chat.id, callbackQuery.message.message_id); try {
} // Create user profile
await User.create(userId, username);
const keyboard = {
reply_markup: {
keyboard: [
['📦 Products', '👤 Profile'],
['🛍 Purchases', '💰 Wallets']
],
resize_keyboard: true
}
};
await this.bot.sendMessage(
chatId,
'Welcome to the shop! Choose an option:',
keyboard
);
} catch (error) {
console.error('Error in handleStart:', error);
await this.bot.sendMessage(chatId, 'Error creating user profile. Please try again.');
}
}
async handleBackToProfile(callbackQuery) {
await this.showProfile({
chat: {id: callbackQuery.message.chat.id},
from: {id: callbackQuery.from.id}
});
await this.bot.deleteMessage(callbackQuery.message.chat.id, callbackQuery.message.message_id);
}
} }

View File

@@ -69,7 +69,15 @@ bot.onText(/\/admin/, async (msg) => {
// Handle user menu buttons // Handle user menu buttons
bot.on('message', async (msg) => { bot.on('message', async (msg) => {
if (!msg.text) return; if (msg.text && msg.text.toLowerCase() === '/start') {
return;
}
const canUse = await userHandler.canUseBot(msg);
if (!canUse) {
return;
}
if (msg.text.toLowerCase() === '/start') { if (msg.text.toLowerCase() === '/start') {
return; return;
@@ -271,12 +279,21 @@ bot.on('callback_query', async (callbackQuery) => {
} else if (action.startsWith('prod_subcategory_')) { } else if (action.startsWith('prod_subcategory_')) {
logDebug(action, 'handleSubcategorySelection'); logDebug(action, 'handleSubcategorySelection');
await adminProductHandler.handleSubcategorySelection(callbackQuery); await adminProductHandler.handleSubcategorySelection(callbackQuery);
} else if (action.startsWith('list_products_')) {
logDebug(action, 'handleSubcategorySelection');
await adminProductHandler.handleProductListPage(callbackQuery);
} else if (action.startsWith('add_product_')) { } else if (action.startsWith('add_product_')) {
logDebug(action, 'handleAddProduct'); logDebug(action, 'handleAddProduct');
await adminProductHandler.handleAddProduct(callbackQuery); await adminProductHandler.handleAddProduct(callbackQuery);
} else if (action.startsWith('view_product_')) { } else if (action.startsWith('view_product_')) {
logDebug(action, 'handleViewProduct'); logDebug(action, 'handleViewProduct');
await adminProductHandler.handleViewProduct(callbackQuery); await adminProductHandler.handleViewProduct(callbackQuery);
} else if (action.startsWith('delete_product_')) {
logDebug(action, 'handleViewProduct');
await adminProductHandler.handleProductDelete(callbackQuery);
} else if (action.startsWith('confirm_delete_product_')) {
logDebug(action, 'handleConfirmDelete');
await adminProductHandler.handleConfirmDelete(callbackQuery);
} }
// Admin user management // Admin user management
else if (action.startsWith('view_user_')) { else if (action.startsWith('view_user_')) {