admin access

This commit is contained in:
Artyom Ashirov 2024-11-14 20:29:57 +03:00
parent 2beaa324fa
commit 93290dee1c

View File

@ -1,160 +1,172 @@
import db from '../config/database.js';
import Validators from '../utils/validators.js';
import config from "../config/config.js";
export default class AdminUserLocationHandler {
constructor(bot) {
this.bot = bot;
}
constructor(bot) {
this.bot = bot;
}
async handleEditUserLocation(callbackQuery) {
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}` }
]]
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;
}
}
);
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}` }]
]
};
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
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.');
}
);
} catch (error) {
console.error('Error in handleSetLocation:', error);
await this.bot.sendMessage(chatId, 'Error loading countries. Please try again.');
}
}
async handleEditUserCountry(callbackQuery) {
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]
);
async handleEditUserCountry(callbackQuery) {
if (!this.isAdmin(callbackQuery.from.id)) return;
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}` }]
]
};
const chatId = callbackQuery.message.chat.id;
const messageId = callbackQuery.message.message_id;
const [country, userId] = callbackQuery.data.replace('edit_user_country_', '').split("_");
await this.bot.editMessageText(
`🏙 Select city in ${country}:`,
{
chat_id: chatId,
message_id: messageId,
reply_markup: keyboard
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.');
}
);
} catch (error) {
console.error('Error in handleSetCountry:', error);
await this.bot.sendMessage(chatId, 'Error loading cities. Please try again.');
}
}
async handleEditUserCity(callbackQuery) {
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]
);
async handleEditUserCity(callbackQuery) {
if (!this.isAdmin(callbackQuery.from.id)) return;
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}` }]
]
};
const chatId = callbackQuery.message.chat.id;
const messageId = callbackQuery.message.message_id;
const [country, city, userId] = callbackQuery.data.replace('edit_user_city_', '').split('_');
await this.bot.editMessageText(
`📍 Select district in ${city}:`,
{
chat_id: chatId,
message_id: messageId,
reply_markup: keyboard
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.');
}
);
} catch (error) {
console.error('Error in handleSetCity:', error);
await this.bot.sendMessage(chatId, 'Error loading districts. Please try again.');
}
}
async handleEditUserDistrict(callbackQuery) {
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');
async handleEditUserDistrict(callbackQuery) {
if (!this.isAdmin(callbackQuery.from.id)) return;
await db.runAsync(
'UPDATE users SET country = ?, city = ?, district = ? WHERE telegram_id = ?',
[country, city, district, userId.toString()]
);
const chatId = callbackQuery.message.chat.id;
const messageId = callbackQuery.message.message_id;
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(
`✅ 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}` }
]]
}
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.');
}
);
} catch (error) {
await db.runAsync('ROLLBACK');
console.error('Error in handleSetDistrict:', error);
await this.bot.sendMessage(chatId, 'Error updating location. Please try again.');
}
}
}