Users location edition added
This commit is contained in:
160
src/handlers/adminUserLocationHandler.js
Normal file
160
src/handlers/adminUserLocationHandler.js
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
import db from '../config/database.js';
|
||||||
|
import Validators from '../utils/validators.js';
|
||||||
|
|
||||||
|
export default class AdminUserLocationHandler {
|
||||||
|
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}` }
|
||||||
|
]]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
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}` }]
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
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.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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]
|
||||||
|
);
|
||||||
|
|
||||||
|
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.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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]
|
||||||
|
);
|
||||||
|
|
||||||
|
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.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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');
|
||||||
|
|
||||||
|
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.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
16
src/index.js
16
src/index.js
@@ -10,6 +10,7 @@ import AdminLocationHandler from './handlers/adminLocationHandler.js';
|
|||||||
import AdminProductHandler from './handlers/adminProductHandler.js';
|
import AdminProductHandler from './handlers/adminProductHandler.js';
|
||||||
import ErrorHandler from './utils/errorHandler.js';
|
import ErrorHandler from './utils/errorHandler.js';
|
||||||
import User from './models/User.js';
|
import User from './models/User.js';
|
||||||
|
import AdminUserLocationHandler from "./handlers/adminUserLocationHandler.js";
|
||||||
|
|
||||||
// Debug logging function
|
// Debug logging function
|
||||||
const logDebug = (action, functionName) => {
|
const logDebug = (action, functionName) => {
|
||||||
@@ -36,6 +37,7 @@ const userLocationHandler = new UserLocationHandler(bot);
|
|||||||
const adminHandler = new AdminHandler(bot);
|
const adminHandler = new AdminHandler(bot);
|
||||||
const adminUserHandler = new AdminUserHandler(bot);
|
const adminUserHandler = new AdminUserHandler(bot);
|
||||||
const adminLocationHandler = new AdminLocationHandler(bot);
|
const adminLocationHandler = new AdminLocationHandler(bot);
|
||||||
|
const adminUserLocationHandler = new AdminUserLocationHandler(bot);
|
||||||
const adminProductHandler = new AdminProductHandler(bot);
|
const adminProductHandler = new AdminProductHandler(bot);
|
||||||
|
|
||||||
// Start command - Create user profile
|
// Start command - Create user profile
|
||||||
@@ -262,6 +264,20 @@ bot.on('callback_query', async (callbackQuery) => {
|
|||||||
logDebug(action, 'handleEditUserBalance');
|
logDebug(action, 'handleEditUserBalance');
|
||||||
await adminUserHandler.handleEditUserBalance(callbackQuery);
|
await adminUserHandler.handleEditUserBalance(callbackQuery);
|
||||||
}
|
}
|
||||||
|
// Admin users location management
|
||||||
|
else if (action.startsWith('edit_user_location_')) {
|
||||||
|
logDebug(action, 'handleEditUserLocation');
|
||||||
|
await adminUserLocationHandler.handleEditUserLocation(callbackQuery);
|
||||||
|
} else if (action.startsWith('edit_user_country_')) {
|
||||||
|
logDebug(action, 'handleEditUserCountry');
|
||||||
|
await adminUserLocationHandler.handleEditUserCountry(callbackQuery);
|
||||||
|
} else if (action.startsWith('edit_user_city_')) {
|
||||||
|
logDebug(action, 'handleEditUserCity');
|
||||||
|
await adminUserLocationHandler.handleEditUserCity(callbackQuery);
|
||||||
|
} else if (action.startsWith('edit_user_district_')) {
|
||||||
|
logDebug(action, 'handleEditUserDistrict');
|
||||||
|
await adminUserLocationHandler.handleEditUserDistrict(callbackQuery)
|
||||||
|
}
|
||||||
await bot.answerCallbackQuery(callbackQuery.id);
|
await bot.answerCallbackQuery(callbackQuery.id);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
await ErrorHandler.handleError(bot, msg.chat.id, error, 'callback query');
|
await ErrorHandler.handleError(bot, msg.chat.id, error, 'callback query');
|
||||||
|
|||||||
Reference in New Issue
Block a user