admin access
This commit is contained in:
@@ -1,160 +1,172 @@
|
|||||||
import db from '../config/database.js';
|
import db from '../config/database.js';
|
||||||
import Validators from '../utils/validators.js';
|
import config from "../config/config.js";
|
||||||
|
|
||||||
export default class AdminUserLocationHandler {
|
export default class AdminUserLocationHandler {
|
||||||
constructor(bot) {
|
constructor(bot) {
|
||||||
this.bot = bot;
|
this.bot = bot;
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleEditUserLocation(callbackQuery) {
|
isAdmin(userId) {
|
||||||
const userId = callbackQuery.data.replace('edit_user_location_', '');
|
return config.ADMIN_IDS.includes(userId.toString());
|
||||||
const chatId = callbackQuery.message.chat.id;
|
}
|
||||||
const messageId = callbackQuery.message.message_id;
|
|
||||||
|
async handleEditUserLocation(callbackQuery) {
|
||||||
try {
|
if (!this.isAdmin(callbackQuery.from.id)) return;
|
||||||
const countries = await db.allAsync('SELECT DISTINCT country FROM locations ORDER BY country');
|
|
||||||
|
const userId = callbackQuery.data.replace('edit_user_location_', '');
|
||||||
if (countries.length === 0) {
|
const chatId = callbackQuery.message.chat.id;
|
||||||
await this.bot.editMessageText(
|
const messageId = callbackQuery.message.message_id;
|
||||||
'No locations available yet.',
|
|
||||||
{
|
try {
|
||||||
chat_id: chatId,
|
const countries = await db.allAsync('SELECT DISTINCT country FROM locations ORDER BY country');
|
||||||
message_id: messageId,
|
|
||||||
reply_markup: {
|
if (countries.length === 0) {
|
||||||
inline_keyboard: [[
|
await this.bot.editMessageText(
|
||||||
{ text: '« Back to User', callback_data: `view_user_${userId}` }
|
'No locations available yet.',
|
||||||
]]
|
{
|
||||||
|
chat_id: chatId,
|
||||||
|
message_id: messageId,
|
||||||
|
reply_markup: {
|
||||||
|
inline_keyboard: [[
|
||||||
|
{text: '« Back to User', callback_data: `view_user_${userId}`}
|
||||||
|
]]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const keyboard = {
|
const keyboard = {
|
||||||
inline_keyboard: [
|
inline_keyboard: [
|
||||||
...countries.map(loc => [{
|
...countries.map(loc => [{
|
||||||
text: loc.country,
|
text: loc.country,
|
||||||
callback_data: `edit_user_country_${loc.country}_${userId}`
|
callback_data: `edit_user_country_${loc.country}_${userId}`
|
||||||
}]),
|
}]),
|
||||||
[{ text: '« Back to User', callback_data: `view_user_${userId}` }]
|
[{text: '« Back to User', callback_data: `view_user_${userId}`}]
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|
||||||
await this.bot.editMessageText(
|
await this.bot.editMessageText(
|
||||||
'🌍 Select user country:',
|
'🌍 Select user country:',
|
||||||
{
|
{
|
||||||
chat_id: chatId,
|
chat_id: chatId,
|
||||||
message_id: messageId,
|
message_id: messageId,
|
||||||
reply_markup: keyboard
|
reply_markup: keyboard
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error in handleSetLocation:', error);
|
||||||
|
await this.bot.sendMessage(chatId, 'Error loading countries. Please try again.');
|
||||||
}
|
}
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error in handleSetLocation:', error);
|
|
||||||
await this.bot.sendMessage(chatId, 'Error loading countries. Please try again.');
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
async handleEditUserCountry(callbackQuery) {
|
async handleEditUserCountry(callbackQuery) {
|
||||||
const chatId = callbackQuery.message.chat.id;
|
if (!this.isAdmin(callbackQuery.from.id)) return;
|
||||||
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 = {
|
const chatId = callbackQuery.message.chat.id;
|
||||||
inline_keyboard: [
|
const messageId = callbackQuery.message.message_id;
|
||||||
...cities.map(loc => [{
|
const [country, userId] = callbackQuery.data.replace('edit_user_country_', '').split("_");
|
||||||
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(
|
try {
|
||||||
`🏙 Select city in ${country}:`,
|
const cities = await db.allAsync(
|
||||||
{
|
'SELECT DISTINCT city FROM locations WHERE country = ? ORDER BY city',
|
||||||
chat_id: chatId,
|
[country]
|
||||||
message_id: messageId,
|
);
|
||||||
reply_markup: keyboard
|
|
||||||
|
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.');
|
||||||
}
|
}
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error in handleSetCountry:', error);
|
|
||||||
await this.bot.sendMessage(chatId, 'Error loading cities. Please try again.');
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
async handleEditUserCity(callbackQuery) {
|
async handleEditUserCity(callbackQuery) {
|
||||||
const chatId = callbackQuery.message.chat.id;
|
if (!this.isAdmin(callbackQuery.from.id)) return;
|
||||||
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 = {
|
const chatId = callbackQuery.message.chat.id;
|
||||||
inline_keyboard: [
|
const messageId = callbackQuery.message.message_id;
|
||||||
...districts.map(loc => [{
|
const [country, city, userId] = callbackQuery.data.replace('edit_user_city_', '').split('_');
|
||||||
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(
|
try {
|
||||||
`📍 Select district in ${city}:`,
|
const districts = await db.allAsync(
|
||||||
{
|
'SELECT district FROM locations WHERE country = ? AND city = ? ORDER BY district',
|
||||||
chat_id: chatId,
|
[country, city]
|
||||||
message_id: messageId,
|
);
|
||||||
reply_markup: keyboard
|
|
||||||
|
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.');
|
||||||
}
|
}
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error in handleSetCity:', error);
|
|
||||||
await this.bot.sendMessage(chatId, 'Error loading districts. Please try again.');
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
async handleEditUserDistrict(callbackQuery) {
|
async handleEditUserDistrict(callbackQuery) {
|
||||||
const chatId = callbackQuery.message.chat.id;
|
if (!this.isAdmin(callbackQuery.from.id)) return;
|
||||||
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(
|
const chatId = callbackQuery.message.chat.id;
|
||||||
'UPDATE users SET country = ?, city = ?, district = ? WHERE telegram_id = ?',
|
const messageId = callbackQuery.message.message_id;
|
||||||
[country, city, district, userId.toString()]
|
const [country, city, district, userId] = callbackQuery.data.replace('edit_user_district_', '').split('_');
|
||||||
);
|
|
||||||
|
|
||||||
await db.runAsync('COMMIT');
|
try {
|
||||||
|
await db.runAsync('BEGIN TRANSACTION');
|
||||||
|
|
||||||
await this.bot.editMessageText(
|
await db.runAsync(
|
||||||
`✅ Location updated successfully!\n\nCountry: ${country}\nCity: ${city}\nDistrict: ${district}`,
|
'UPDATE users SET country = ?, city = ?, district = ? WHERE telegram_id = ?',
|
||||||
{
|
[country, city, district, userId.toString()]
|
||||||
chat_id: chatId,
|
);
|
||||||
message_id: messageId,
|
|
||||||
reply_markup: {
|
await db.runAsync('COMMIT');
|
||||||
inline_keyboard: [[
|
|
||||||
{ text: '« Back to User', callback_data: `view_user_${userId}` }
|
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.');
|
||||||
}
|
}
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
await db.runAsync('ROLLBACK');
|
|
||||||
console.error('Error in handleSetDistrict:', error);
|
|
||||||
await this.bot.sendMessage(chatId, 'Error updating location. Please try again.');
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user