user deletion/blocking

This commit is contained in:
Artyom Ashirov
2024-11-15 02:26:13 +03:00
parent b45f7daa6f
commit 4251f1a0bd
6 changed files with 278 additions and 111 deletions

View File

@@ -215,7 +215,7 @@ export default class AdminLocationHandler {
const keyboard = {
inline_keyboard: locations.map(loc => [{
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}`
}])
};
@@ -239,7 +239,7 @@ export default class AdminLocationHandler {
async handleConfirmDelete(callbackQuery) {
const chatId = callbackQuery.message.chat.id;
const [country, city, district] = callbackQuery.data
.replace('confirm_delete_', '')
.replace('confirm_delete_location_', '')
.split('_');
try {

View File

@@ -265,7 +265,7 @@ ${purchases.map(p => ` • ${p.product_name} x${p.quantity} - $${p.total_price}
const chatId = callbackQuery.message.chat.id;
try {
await User.delete(userId);
await User.updateUserStatus(userId, 1);
const 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(
`✅ 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) {
if (!this.isAdmin(callbackQuery.from.id)) return;

View File

@@ -1,28 +1,52 @@
import db from '../config/database.js';
import User from '../models/User.js';
import config from "../config/config.js";
export default class UserHandler {
constructor(bot) {
this.bot = bot;
}
constructor(bot) {
this.bot = bot;
}
async showProfile(msg) {
const chatId = msg.chat.id;
const userId = msg.from.id;
async canUseBot(msg) {
const userId = msg.from.id;
const user = await User.getById(userId);
try {
const userStats = await User.getUserStats(userId);
const keyboard = {
inline_keyboard: [
[{text: "Contact support", url: config.SUPPORT_LINK}]
]
};
if (!userStats) {
await this.bot.sendMessage(chatId, 'Profile not found. Please use /start to create one.');
return;
}
switch (user?.status) {
case 0:
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
? `${userStats.country}, ${userStats.city}, ${userStats.district}`
: 'Not set';
async showProfile(msg) {
const chatId = msg.chat.id;
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*
📱 Telegram ID: \`${userId}\`
@@ -37,58 +61,58 @@ export default class UserHandler {
📅 Member since: ${new Date(userStats.created_at).toLocaleDateString()}
`;
const keyboard = {
inline_keyboard: [
[{ text: '📍 Set Location', callback_data: 'set_location' }],
[{ text: '❌ Delete Account', callback_data: 'delete_account' }]
]
};
const keyboard = {
inline_keyboard: [
[{text: '📍 Set Location', callback_data: 'set_location'}],
[{text: '❌ Delete Account', callback_data: 'delete_account'}]
]
};
await this.bot.sendMessage(chatId, text, {
parse_mode: 'Markdown',
reply_markup: keyboard
});
} catch (error) {
console.error('Error in showProfile:', error);
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, text, {
parse_mode: 'Markdown',
reply_markup: keyboard
});
} catch (error) {
console.error('Error in showProfile:', error);
await this.bot.sendMessage(chatId, 'Error loading profile. Please try again.');
}
};
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);
}
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) {
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);
}
}